5 #include "mathed/support.h"
11 #include "symbol_def.h"
15 using std::lower_bound;
19 extern LyXFont WhichFont(short type, int size);
21 char const * math_font_name[] = {
32 char const * latex_mathspace[] = {
33 "!", ",", ":", ";", "quad", "qquad"
37 * Internal struct of a drawing: code n x1 y1 ... xn yn, where code is:
38 * 0 = end, 1 = line, 2 = polyline, 3 = square line, 4= square polyline
43 float parenthHigh[] = {
44 2.0, 13.0, 0.9840, 0.0014, 0.7143, 0.0323, 0.4603, 0.0772, 0.2540,
45 0.1278, 0.1746, 0.1966, 0.0952, 0.3300, 0.0950, 0.5000, 0.0952, 0.6700,
46 0.1746, 0.8034, 0.2540, 0.8722, 0.4603, 0.9228, 0.7143, 0.9677, 0.9840,
51 static float parenth[] = {
53 0.9930, 0.0071, 0.7324, 0.0578, 0.5141, 0.1126, 0.3380, 0.1714,
54 0.2183, 0.2333, 0.0634, 0.3621, 0.0141, 0.5000, 0.0563, 0.6369,
55 0.2113, 0.7647, 0.3310, 0.8276, 0.5070, 0.8864, 0.7254, 0.9412,
61 static float brace[] = {
63 0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243, 0.5819, 0.0527,
64 0.4859, 0.0892, 0.4463, 0.1278, 0.4463, 0.3732, 0.4011, 0.4199,
65 0.2712, 0.4615, 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
66 0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268, 0.4463, 0.8722,
67 0.4859, 0.9108, 0.5819, 0.9473, 0.7458, 0.9757, 0.9379, 0.9980,
72 static float arrow[] = {
74 0.015, 0.7500, 0.2, 0.6, 0.35, 0.35, 0.5, 0.05,
75 0.65, 0.35, 0.8, 0.6, 0.95, 0.7500,
76 3, 0.5, 0.15, 0.5, 0.95,
80 static float Arrow[] = {
82 0.015, 0.7500, 0.2, 0.6, 0.35, 0.35, 0.5, 0.05,
83 0.65, 0.35, 0.8, 0.6, 0.95, 0.7500,
84 3, 0.35, 0.5, 0.35, 0.95,
85 3, 0.65, 0.5, 0.65, 0.95,
89 static float udarrow[] = {
91 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
93 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
94 1, 0.5, 0.2, 0.5, 0.8,
98 static float Udarrow[] = {
100 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
102 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
103 1, 0.35, 0.2, 0.35, 0.8,
104 1, 0.65, 0.2, 0.65, 0.8,
108 static float brack[] = {
110 0.95, 0.05, 0.05, 0.05, 0.05, 0.95, 0.95, 0.95,
114 static float corner[] = {
116 0.95, 0.05, 0.05, 0.05, 0.05, 0.95,
120 static float angle[] = {
122 1, 0, 0.05, 0.5, 1, 1,
126 static float slash[] = {
127 1, 0.95, 0.05, 0.05, 0.95,
131 static float hline[] = {
132 1, 0.05, 0.5, 0.95, 0.5,
137 static float hline2[] = {
138 1, 0.1, 0.5, 0.3, 0.5,
139 1, 0.7, 0.5, 0.9, 0.5,
143 static float hline3[] = {
145 1, 0.475, 0, 0.525, 0,
151 static float dline3[] = {
152 1, 0.1, 0.1, 0.15, 0.15,
153 1, 0.475, 0.475, 0.525, 0.525,
154 1, 0.85, 0.85, 0.9, 0.9,
158 static float hlinesmall[] = {
159 1, 0.4, 0.5, 0.6, 0.5,
163 static float vert[] = {
164 1, 0.5, 0.05, 0.5, 0.95,
168 static float Vert[] = {
169 1, 0.3, 0.05, 0.3, 0.95,
170 1, 0.7, 0.05, 0.7, 0.95,
174 static float tilde[] = {
176 0.05, 0.8, 0.25, 0.2, 0.75, 0.8, 0.95, 0.2,
182 math_deco_struct math_deco_table[] = {
185 { LM_widehat, &angle[0], 3 },
186 { LM_widetilde, &tilde[0], 0 },
187 { LM_underline, &hline[0], 0 },
188 { LM_overline, &hline[0], 0 },
189 { LM_underbrace, &brace[0], 1 },
190 { LM_overbrace, &brace[0], 3 },
191 { LM_overleftarrow, &arrow[0], 1 },
192 { LM_overightarrow, &arrow[0], 3 },
195 { '(', &parenth[0], 0 },
196 { ')', &parenth[0], 2 },
197 { '{', &brace[0], 0 },
198 { '}', &brace[0], 2 },
199 { '[', &brack[0], 0 },
200 { ']', &brack[0], 2 },
201 { '|', &vert[0], 0 },
202 { '/', &slash[0], 0 },
203 { LM_Vert, &Vert[0], 0 },
204 { LM_backslash, &slash[0], 1 },
205 { LM_langle, &angle[0], 0 },
206 { LM_lceil, &corner[0], 0 },
207 { LM_lfloor, &corner[0], 1 },
208 { LM_rangle, &angle[0], 2 },
209 { LM_rceil, &corner[0], 3 },
210 { LM_rfloor, &corner[0], 2 },
211 { LM_downarrow, &arrow[0], 2 },
212 { LM_Downarrow, &Arrow[0], 2 },
213 { LM_uparrow, &arrow[0], 0 },
214 { LM_Uparrow, &Arrow[0], 0 },
215 { LM_updownarrow, &udarrow[0], 0 },
216 { LM_Updownarrow, &Udarrow[0], 0 },
219 { LM_ddot, &hline2[0], 0 },
220 { LM_hat, &angle[0], 3 },
221 { LM_grave, &slash[0], 1 },
222 { LM_acute, &slash[0], 0 },
223 { LM_tilde, &tilde[0], 0 },
224 { LM_bar, &hline[0], 0 },
225 { LM_dot, &hlinesmall[0], 0 },
226 { LM_check, &angle[0], 1 },
227 { LM_breve, &parenth[0], 1 },
228 { LM_vec, &arrow[0], 3 },
229 { LM_not, &slash[0], 0 },
232 { LM_ldots, &hline3[0], 0 },
233 { LM_cdots, &hline3[0], 0 },
234 { LM_vdots, &hline3[0], 1 },
235 { LM_ddots, &dline3[0], 0 }
239 struct math_deco_compare {
240 /// for use by sort and lower_bound
242 int operator()(math_deco_struct const & a,
243 math_deco_struct const & b) const {
244 return a.code < b.code;
250 int const math_deco_table_size = sizeof(math_deco_table) /sizeof(math_deco_struct);
252 class init_deco_table {
256 sort(math_deco_table,
257 math_deco_table + math_deco_table_size,
258 math_deco_compare());
259 init_deco_table::init = true;
266 bool init_deco_table::init = false;
267 static init_deco_table idt;
270 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
272 LyXFont font = WhichFont(type, size);
273 des = lyxfont::descent(c, font);
274 asc = lyxfont::ascent(c, font);
279 int mathed_char_width(short type, int size, byte c)
281 if (MathIsBinary(type)) {
284 return mathed_string_width(type, size, s);
287 return lyxfont::width(c, WhichFont(type, size));
291 int mathed_string_height(short type, int size, string const & s,
292 int & asc, int & des)
294 LyXFont font = WhichFont(type, size);
296 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
297 des = max(des, lyxfont::descent(*it, font));
298 asc = max(asc, lyxfont::ascent(*it, font));
304 int mathed_string_width(short type, int size, string const & s)
307 if (MathIsBinary(type))
308 for (string::const_iterator it = s.begin();
309 it != s.end(); ++it) {
317 LyXFont const f = WhichFont(type, size);
318 return lyxfont::width(st, f);
322 LyXFont mathed_get_font(short type, int size)
324 LyXFont f = WhichFont(type, size);
325 if (type == LM_TC_TEX) {
326 f.setLatex(LyXFont::ON);
332 void mathed_draw_deco(Painter & pain, int x, int y, int w, int h, int code)
343 math_deco_struct mds;
345 mds = search_deco(code);
347 catch (deco_not_found) {
348 // Should this ever happen?
349 lyxerr << "Deco was not found. Programming error?" << endl;
354 float * d = mds.data;
356 if (h > 70 && (mds.code == int('(')
357 || mds.code == int(')')))
360 math_deco_struct const * mds = search_deco(code);
362 // Should this ever happen?
363 lyxerr << "Deco was not found. Programming error?" << endl;
369 float * d = mds->data;
371 if (h > 70 && (mds->code == int('(')
372 || mds->code == int(')')))
379 int n = (w < h) ? w: h;
382 if (r > 0 && r < 3) y += h;
391 xx = d[i++]; yy = d[i++];
392 x2 = d[i++]; y2 = d[i++];
394 sqmt.transf(xx, yy, xx, yy);
396 mt.transf(xx, yy, xx, yy);
397 mt.transf(x2, y2, x2, y2);
398 pain.line(x + int(xx), y + int(yy),
399 x + int(x2), y + int(y2),
408 for (int j = 0; j < n; ++j) {
409 xx = d[i++]; yy = d[i++];
410 // lyxerr << " " << xx << " " << yy << " ";
412 sqmt.transf(xx, yy, xx, yy);
414 mt.transf(xx, yy, xx, yy);
417 // lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
419 pain.lines(xp, yp, n, LColor::mathline);
426 #define USE_EXCEPTIONS 0
428 struct deco_not_found {};
431 math_deco_struct const & search_deco(int code)
433 math_deco_struct * res =
434 lower_bound(math_deco_table,
435 math_deco_table + math_deco_table_size,
436 code, math_deco_compare());
437 if (res != math_deco_table + math_deco_table_size &&
440 throw deco_not_found();
446 math_deco_struct const * search_deco(int code)
448 math_deco_struct search_elem = { code, 0, 0 };
450 math_deco_struct * res =
451 lower_bound(math_deco_table,
452 math_deco_table + math_deco_table_size,
453 search_elem, math_deco_compare());
454 if (res != math_deco_table + math_deco_table_size &&
462 bool MathIsInset(short x)
464 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
468 bool MathIsFont(short x)
470 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
474 bool MathIsAlphaFont(short x)
476 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
480 bool MathIsActive(short x)
482 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
486 bool MathIsUp(short x)
488 return x == LM_TC_UP;
492 bool MathIsDown(short x)
494 return x == LM_TC_DOWN;
498 bool MathIsScript(short x)
500 return x == LM_TC_DOWN || x == LM_TC_UP;
504 bool MathIsBOPS(short x)
506 return MathedLookupBOP(x) > LMB_NONE;
510 bool MathIsBinary(short x)
512 return x == LM_TC_BOP || x == LM_TC_BOPS;
516 bool MathIsSymbol(short x) {
517 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
521 bool is_eqn_type(short int type)
523 return type >= LM_OT_MIN && type < LM_OT_MATRIX;
527 bool is_matrix_type(short int type)
529 return type == LM_OT_MATRIX;
533 bool is_multiline(short int type)
535 return type >= LM_OT_MPAR && type < LM_OT_MATRIX;
539 bool is_ams(short int type)
541 return type > LM_OT_MPARN && type < LM_OT_MATRIX;
545 bool is_singlely_numbered(short int type)
547 return type == LM_OT_PARN || type == LM_OT_MULTLINEN;
551 bool is_multi_numbered(short int type)
553 return type == LM_OT_MPARN || type == LM_OT_ALIGNN
554 || type == LM_OT_ALIGNATN;
558 bool is_numbered(short int type)
560 return is_singlely_numbered(type) || is_multi_numbered(type);
564 bool is_multicolumn(short int type)
566 return type == LM_OT_ALIGN || type == LM_OT_ALIGNN
567 || type == LM_OT_ALIGNAT || type == LM_OT_ALIGNATN;