5 #include "mathed/support.h"
11 #include "symbol_def.h"
13 #include "math_utils.h"
16 using std::lower_bound;
20 extern LyXFont WhichFont(short type, int size);
22 char const * math_font_name[] = {
33 char const * latex_mathspace[] = {
34 "!", ",", ":", ";", "quad", "qquad"
41 * Internal struct of a drawing: code n x1 y1 ... xn yn, where code is:
42 * 0 = end, 1 = line, 2 = polyline, 3 = square line, 4= square polyline
46 float const parenthHigh[] = {
48 0.9840, 0.0014, 0.7143, 0.0323, 0.4603, 0.0772,
49 0.2540, 0.1278, 0.1746, 0.1966, 0.0952, 0.3300,
50 0.0950, 0.5000, 0.0952, 0.6700, 0.1746, 0.8034,
51 0.2540, 0.8722, 0.4603, 0.9228, 0.7143, 0.9677,
57 float const parenth[] = {
59 0.9930, 0.0071, 0.7324, 0.0578, 0.5141, 0.1126,
60 0.3380, 0.1714, 0.2183, 0.2333, 0.0634, 0.3621,
61 0.0141, 0.5000, 0.0563, 0.6369, 0.2113, 0.7647,
62 0.3310, 0.8276, 0.5070, 0.8864, 0.7254, 0.9412,
68 float const brace[] = {
70 0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243,
71 0.5819, 0.0527, 0.4859, 0.0892, 0.4463, 0.1278,
72 0.4463, 0.3732, 0.4011, 0.4199, 0.2712, 0.4615,
73 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
74 0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268,
75 0.4463, 0.8722, 0.4859, 0.9108, 0.5819, 0.9473,
76 0.7458, 0.9757, 0.9379, 0.9980, 0.9492, 0.9980,
81 // Is this correct? (Lgb)
82 float const arrow[] = {
84 0.0150, 0.7500, 0.2000, 0.6000, 0.3500, 0.3500,
85 0.5000, 0.0500, 0.6500, 0.3500, 0.8000, 0.6000,
87 3, 0.5000, 0.1500, 0.5000, 0.9500,
92 // Is this correct? (Lgb)
93 float const Arrow[] = {
95 0.0150, 0.7500, 0.2000, 0.6000, 0.3500, 0.3500,
96 0.5000, 0.0500, 0.6500, 0.3500, 0.8000, 0.6000,
98 3, 0.3500, 0.5000, 0.3500, 0.9500,
99 3, 0.6500, 0.5000, 0.6500, 0.9500,
104 float const udarrow[] = {
106 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
108 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
109 1, 0.5, 0.2, 0.5, 0.8,
114 float const Udarrow[] = {
116 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
118 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
119 1, 0.35, 0.2, 0.35, 0.8,
120 1, 0.65, 0.2, 0.65, 0.8,
125 float const brack[] = {
127 0.95, 0.05, 0.05, 0.05, 0.05, 0.95, 0.95, 0.95,
132 float const corner[] = {
134 0.95, 0.05, 0.05, 0.05, 0.05, 0.95,
139 float const angle[] = {
141 1, 0, 0.05, 0.5, 1, 1,
146 float const slash[] = {
147 1, 0.95, 0.05, 0.05, 0.95,
152 float const hline[] = {
153 1, 0.05, 0.5, 0.95, 0.5,
158 float const hline2[] = {
159 1, 0.1, 0.5, 0.3, 0.5,
160 1, 0.7, 0.5, 0.9, 0.5,
165 float const hline3[] = {
167 1, 0.475, 0, 0.525, 0,
173 float const dline3[] = {
174 1, 0.1, 0.1, 0.15, 0.15,
175 1, 0.475, 0.475, 0.525, 0.525,
176 1, 0.85, 0.85, 0.9, 0.9,
181 float const hlinesmall[] = {
182 1, 0.4, 0.5, 0.6, 0.5,
187 float const vert[] = {
188 1, 0.5, 0.05, 0.5, 0.95,
193 float const Vert[] = {
194 1, 0.3, 0.05, 0.3, 0.95,
195 1, 0.7, 0.05, 0.7, 0.95,
200 float const tilde[] = {
202 0.05, 0.8, 0.25, 0.2, 0.75, 0.8, 0.95, 0.2,
207 math_deco_struct math_deco_table[] = {
209 { LM_widehat, &angle[0], 3 },
210 { LM_widetilde, &tilde[0], 0 },
211 { LM_underline, &hline[0], 0 },
212 { LM_overline, &hline[0], 0 },
213 { LM_underbrace, &brace[0], 1 },
214 { LM_overbrace, &brace[0], 3 },
215 { LM_overleftarrow, &arrow[0], 1 },
216 { LM_overightarrow, &arrow[0], 3 },
219 { '(', &parenth[0], 0 },
220 { ')', &parenth[0], 2 },
221 { '{', &brace[0], 0 },
222 { '}', &brace[0], 2 },
223 { '[', &brack[0], 0 },
224 { ']', &brack[0], 2 },
225 { '|', &vert[0], 0 },
226 { '/', &slash[0], 0 },
227 { LM_Vert, &Vert[0], 0 },
228 { LM_backslash, &slash[0], 1 },
229 { LM_langle, &angle[0], 0 },
230 { LM_lceil, &corner[0], 0 },
231 { LM_lfloor, &corner[0], 1 },
232 { LM_rangle, &angle[0], 2 },
233 { LM_rceil, &corner[0], 3 },
234 { LM_rfloor, &corner[0], 2 },
235 { LM_downarrow, &arrow[0], 2 },
236 { LM_Downarrow, &Arrow[0], 2 },
237 { LM_uparrow, &arrow[0], 0 },
238 { LM_Uparrow, &Arrow[0], 0 },
239 { LM_updownarrow, &udarrow[0], 0 },
240 { LM_Updownarrow, &Udarrow[0], 0 },
243 { LM_ddot, &hline2[0], 0 },
244 { LM_hat, &angle[0], 3 },
245 { LM_grave, &slash[0], 1 },
246 { LM_acute, &slash[0], 0 },
247 { LM_tilde, &tilde[0], 0 },
248 { LM_bar, &hline[0], 0 },
249 { LM_dot, &hlinesmall[0], 0 },
250 { LM_check, &angle[0], 1 },
251 { LM_breve, &parenth[0], 1 },
252 { LM_vec, &arrow[0], 3 },
253 { LM_not, &slash[0], 0 },
256 { LM_ldots, &hline3[0], 0 },
257 { LM_cdots, &hline3[0], 0 },
258 { LM_vdots, &hline3[0], 1 },
259 { LM_ddots, &dline3[0], 0 }
263 struct math_deco_compare {
264 /// for use by sort and lower_bound
266 int operator()(math_deco_struct const & a,
267 math_deco_struct const & b) const {
268 return a.code < b.code;
273 int const math_deco_table_size =
274 sizeof(math_deco_table) /sizeof(math_deco_struct);
277 class init_deco_table {
281 sort(math_deco_table,
282 math_deco_table + math_deco_table_size,
283 math_deco_compare());
284 init_deco_table::init = true;
292 bool init_deco_table::init = false;
293 static init_deco_table idt;
298 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
300 LyXFont const font = WhichFont(type, size);
301 des = lyxfont::descent(c, font);
302 asc = lyxfont::ascent(c, font);
307 int mathed_char_width(short type, int size, byte c)
309 if (MathIsBinary(type)) {
312 return mathed_string_width(type, size, s);
314 return lyxfont::width(c, WhichFont(type, size));
318 int mathed_string_height(short type, int size, string const & s,
319 int & asc, int & des)
321 LyXFont const font = WhichFont(type, size);
323 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
324 des = max(des, lyxfont::descent(*it, font));
325 asc = max(asc, lyxfont::ascent(*it, font));
331 int mathed_string_width(short type, int size, string const & s)
334 if (MathIsBinary(type))
335 for (string::const_iterator it = s.begin();
336 it != s.end(); ++it) {
344 LyXFont const f = WhichFont(type, size);
345 return lyxfont::width(st, f);
349 LyXFont mathed_get_font(short type, int size)
351 LyXFont f = WhichFont(type, size);
352 if (type == LM_TC_TEX) {
353 f.setLatex(LyXFont::ON);
359 void mathed_draw_deco(Painter & pain, int x, int y, int w, int h, int code)
370 math_deco_struct mds;
372 mds = search_deco(code);
374 catch (deco_not_found) {
375 // Should this ever happen?
376 lyxerr << "Deco was not found. Programming error?" << endl;
380 int const r = mds.angle;
381 float const * d = mds.data;
383 if (h > 70 && (mds.code == int('(')
384 || mds.code == int(')')))
387 math_deco_struct const * mds = search_deco(code);
389 // Should this ever happen?
390 lyxerr << "Deco was not found. Programming error?" << endl;
395 int const r = mds->angle;
396 float const * d = mds->data;
398 if (h > 70 && (mds->code == int('(')
399 || mds->code == int(')')))
406 int const n = (w < h) ? w : h;
409 if (r > 0 && r < 3) y += h;
418 xx = d[i++]; yy = d[i++];
419 x2 = d[i++]; y2 = d[i++];
421 sqmt.transf(xx, yy, xx, yy);
423 mt.transf(xx, yy, xx, yy);
424 mt.transf(x2, y2, x2, y2);
425 pain.line(x + int(xx), y + int(yy),
426 x + int(x2), y + int(y2),
435 int const n = int(d[i++]);
436 for (int j = 0; j < n; ++j) {
437 xx = d[i++]; yy = d[i++];
438 // lyxerr << " " << xx << " " << yy << " ";
440 sqmt.transf(xx, yy, xx, yy);
442 mt.transf(xx, yy, xx, yy);
445 // lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
447 pain.lines(xp, yp, n, LColor::mathline);
454 #define USE_EXCEPTIONS 0
456 struct deco_not_found {};
459 math_deco_struct const & search_deco(int code)
461 math_deco_struct const * res =
462 lower_bound(math_deco_table,
463 math_deco_table + math_deco_table_size,
464 code, math_deco_compare());
465 if (res != math_deco_table + math_deco_table_size &&
468 throw deco_not_found();
474 math_deco_struct const * search_deco(int code)
476 math_deco_struct search_elem = { code, 0, 0 };
478 math_deco_struct const * res =
479 lower_bound(math_deco_table,
480 math_deco_table + math_deco_table_size,
481 search_elem, math_deco_compare());
482 if (res != math_deco_table + math_deco_table_size &&
490 bool MathIsInset(short x)
492 return LM_TC_INSET <= x && x <= LM_TC_ACTIVE_INSET;
496 bool MathIsFont(short x)
498 return LM_TC_CONST <= x && x <= LM_TC_BSYM;
502 bool MathIsAlphaFont(short x)
504 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
508 bool MathIsActive(short x)
510 return LM_TC_INSET < x && x <= LM_TC_ACTIVE_INSET;
514 bool MathIsUp(short x)
516 return x == LM_TC_UP;
520 bool MathIsDown(short x)
522 return x == LM_TC_DOWN;
526 bool MathIsScript(short x)
528 return x == LM_TC_DOWN || x == LM_TC_UP;
532 bool MathIsBOPS(short x)
534 return MathedLookupBOP(x) > LMB_NONE;
538 bool MathIsBinary(short x)
540 return x == LM_TC_BOP || x == LM_TC_BOPS;
544 bool MathIsSymbol(short x)
546 return LM_TC_SYMB <= x && x <= LM_TC_BSYM;
550 bool is_eqn_type(short int type)
552 return type >= LM_OT_MIN && type < LM_OT_MATRIX;
556 bool is_matrix_type(short int type)
558 return type == LM_OT_MATRIX;
562 bool is_multiline(short int type)
564 return type >= LM_OT_MPAR && type < LM_OT_MATRIX;
568 bool is_ams(short int type)
570 return type > LM_OT_MPARN && type < LM_OT_MATRIX;
574 bool is_singlely_numbered(short int type)
576 return type == LM_OT_PARN || type == LM_OT_MULTLINEN;
580 bool is_multi_numbered(short int type)
582 return type == LM_OT_MPARN || type == LM_OT_ALIGNN
583 || type == LM_OT_ALIGNATN;
587 bool is_numbered(short int type)
589 return is_singlely_numbered(type) || is_multi_numbered(type);
593 bool is_multicolumn(short int type)
595 return type == LM_OT_ALIGN || type == LM_OT_ALIGNN
596 || type == LM_OT_ALIGNAT || type == LM_OT_ALIGNATN;