5 #include "mathed/support.h"
9 #include "math_parser.h"
12 #include "symbol_def.h"
14 #include "math_utils.h"
17 using std::lower_bound;
21 extern LyXFont WhichFont(short type, int size);
23 char const * math_font_name[] = {
34 char const * latex_mathspace[] = {
35 "!", ",", ":", ";", "quad", "qquad"
42 * Internal struct of a drawing: code n x1 y1 ... xn yn, where code is:
43 * 0 = end, 1 = line, 2 = polyline, 3 = square line, 4= square polyline
47 float const parenthHigh[] = {
49 0.9840, 0.0014, 0.7143, 0.0323, 0.4603, 0.0772,
50 0.2540, 0.1278, 0.1746, 0.1966, 0.0952, 0.3300,
51 0.0950, 0.5000, 0.0952, 0.6700, 0.1746, 0.8034,
52 0.2540, 0.8722, 0.4603, 0.9228, 0.7143, 0.9677,
58 float const parenth[] = {
60 0.9930, 0.0071, 0.7324, 0.0578, 0.5141, 0.1126,
61 0.3380, 0.1714, 0.2183, 0.2333, 0.0634, 0.3621,
62 0.0141, 0.5000, 0.0563, 0.6369, 0.2113, 0.7647,
63 0.3310, 0.8276, 0.5070, 0.8864, 0.7254, 0.9412,
69 float const brace[] = {
71 0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243,
72 0.5819, 0.0527, 0.4859, 0.0892, 0.4463, 0.1278,
73 0.4463, 0.3732, 0.4011, 0.4199, 0.2712, 0.4615,
74 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
75 0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268,
76 0.4463, 0.8722, 0.4859, 0.9108, 0.5819, 0.9473,
77 0.7458, 0.9757, 0.9379, 0.9980, 0.9492, 0.9980,
82 // Is this correct? (Lgb)
83 float const arrow[] = {
85 0.0150, 0.7500, 0.2000, 0.6000, 0.3500, 0.3500,
86 0.5000, 0.0500, 0.6500, 0.3500, 0.8000, 0.6000,
88 3, 0.5000, 0.1500, 0.5000, 0.9500,
93 // Is this correct? (Lgb)
94 float const Arrow[] = {
96 0.0150, 0.7500, 0.2000, 0.6000, 0.3500, 0.3500,
97 0.5000, 0.0500, 0.6500, 0.3500, 0.8000, 0.6000,
99 3, 0.3500, 0.5000, 0.3500, 0.9500,
100 3, 0.6500, 0.5000, 0.6500, 0.9500,
105 float const udarrow[] = {
107 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
109 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
110 1, 0.5, 0.2, 0.5, 0.8,
115 float const Udarrow[] = {
117 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
119 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
120 1, 0.35, 0.2, 0.35, 0.8,
121 1, 0.65, 0.2, 0.65, 0.8,
126 float const brack[] = {
128 0.95, 0.05, 0.05, 0.05, 0.05, 0.95, 0.95, 0.95,
133 float const corner[] = {
135 0.95, 0.05, 0.05, 0.05, 0.05, 0.95,
140 float const angle[] = {
142 1, 0, 0.05, 0.5, 1, 1,
147 float const slash[] = {
148 1, 0.95, 0.05, 0.05, 0.95,
153 float const hline[] = {
154 1, 0.05, 0.5, 0.95, 0.5,
159 float const hline2[] = {
160 1, 0.1, 0.5, 0.3, 0.5,
161 1, 0.7, 0.5, 0.9, 0.5,
166 float const hline3[] = {
168 1, 0.475, 0, 0.525, 0,
174 float const dline3[] = {
175 1, 0.1, 0.1, 0.15, 0.15,
176 1, 0.475, 0.475, 0.525, 0.525,
177 1, 0.85, 0.85, 0.9, 0.9,
182 float const hlinesmall[] = {
183 1, 0.4, 0.5, 0.6, 0.5,
188 float const vert[] = {
189 1, 0.5, 0.05, 0.5, 0.95,
194 float const Vert[] = {
195 1, 0.3, 0.05, 0.3, 0.95,
196 1, 0.7, 0.05, 0.7, 0.95,
201 float const tilde[] = {
203 0.05, 0.8, 0.25, 0.2, 0.75, 0.8, 0.95, 0.2,
208 math_deco_struct math_deco_table[] = {
210 { LM_widehat, &angle[0], 3 },
211 { LM_widetilde, &tilde[0], 0 },
212 { LM_underline, &hline[0], 0 },
213 { LM_overline, &hline[0], 0 },
214 { LM_underbrace, &brace[0], 1 },
215 { LM_overbrace, &brace[0], 3 },
216 { LM_overleftarrow, &arrow[0], 1 },
217 { LM_overightarrow, &arrow[0], 3 },
220 { '(', &parenth[0], 0 },
221 { ')', &parenth[0], 2 },
222 { '{', &brace[0], 0 },
223 { '}', &brace[0], 2 },
224 { '[', &brack[0], 0 },
225 { ']', &brack[0], 2 },
226 { '|', &vert[0], 0 },
227 { '/', &slash[0], 0 },
228 { LM_Vert, &Vert[0], 0 },
229 { LM_backslash, &slash[0], 1 },
230 { LM_langle, &angle[0], 0 },
231 { LM_lceil, &corner[0], 0 },
232 { LM_lfloor, &corner[0], 1 },
233 { LM_rangle, &angle[0], 2 },
234 { LM_rceil, &corner[0], 3 },
235 { LM_rfloor, &corner[0], 2 },
236 { LM_downarrow, &arrow[0], 2 },
237 { LM_Downarrow, &Arrow[0], 2 },
238 { LM_uparrow, &arrow[0], 0 },
239 { LM_Uparrow, &Arrow[0], 0 },
240 { LM_updownarrow, &udarrow[0], 0 },
241 { LM_Updownarrow, &Udarrow[0], 0 },
244 { LM_ddot, &hline2[0], 0 },
245 { LM_hat, &angle[0], 3 },
246 { LM_grave, &slash[0], 1 },
247 { LM_acute, &slash[0], 0 },
248 { LM_tilde, &tilde[0], 0 },
249 { LM_bar, &hline[0], 0 },
250 { LM_dot, &hlinesmall[0], 0 },
251 { LM_check, &angle[0], 1 },
252 { LM_breve, &parenth[0], 1 },
253 { LM_vec, &arrow[0], 3 },
254 { LM_not, &slash[0], 0 },
257 { LM_ldots, &hline3[0], 0 },
258 { LM_cdots, &hline3[0], 0 },
259 { LM_vdots, &hline3[0], 1 },
260 { LM_ddots, &dline3[0], 0 }
264 struct math_deco_compare {
265 /// for use by sort and lower_bound
267 int operator()(math_deco_struct const & a,
268 math_deco_struct const & b) const {
269 return a.code < b.code;
274 int const math_deco_table_size =
275 sizeof(math_deco_table) /sizeof(math_deco_struct);
278 class init_deco_table {
282 sort(math_deco_table,
283 math_deco_table + math_deco_table_size,
284 math_deco_compare());
285 init_deco_table::init = true;
293 bool init_deco_table::init = false;
294 static init_deco_table idt;
299 (short type, int size, byte c, int & asc, int & des, int & wid)
301 LyXFont const font = WhichFont(type, size);
302 des = lyxfont::descent(c, font);
303 asc = lyxfont::ascent(c, font);
304 wid = mathed_char_width(type, size, c);
307 int mathed_char_height(short type, int size, byte c, int & asc, int & des)
309 LyXFont const font = WhichFont(type, size);
310 des = lyxfont::descent(c, font);
311 asc = lyxfont::ascent(c, font);
316 int mathed_char_width(short type, int size, byte c)
318 if (MathIsBinary(type)) {
321 return mathed_string_width(type, size, s);
323 return lyxfont::width(c, WhichFont(type, size));
327 void mathed_string_dim(short type, int size, string const & s,
328 int & asc, int & des, int & wid)
330 mathed_string_height(type, size, s, asc, des);
331 wid = mathed_string_width(type, size, s);
334 int mathed_string_height(short type, int size, string const & s,
335 int & asc, int & des)
337 LyXFont const font = WhichFont(type, size);
339 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
340 des = max(des, lyxfont::descent(*it, font));
341 asc = max(asc, lyxfont::ascent(*it, font));
347 int mathed_string_width(short type, int size, string const & s)
350 if (MathIsBinary(type))
351 for (string::const_iterator it = s.begin();
352 it != s.end(); ++it) {
360 LyXFont const f = WhichFont(type, size);
361 return lyxfont::width(st, f);
365 LyXFont mathed_get_font(short type, int size)
367 LyXFont f = WhichFont(type, size);
369 if (type == LM_TC_TEX) {
370 f.setLatex(LyXFont::ON);
377 void mathed_draw_deco(Painter & pain, int x, int y, int w, int h, int code)
388 math_deco_struct mds;
390 mds = search_deco(code);
392 catch (deco_not_found) {
393 // Should this ever happen?
394 lyxerr << "Deco was not found. Programming error?" << endl;
398 int const r = mds.angle;
399 float const * d = mds.data;
401 if (h > 70 && (mds.code == int('(')
402 || mds.code == int(')')))
405 math_deco_struct const * mds = search_deco(code);
407 // Should this ever happen?
408 lyxerr << "Deco was not found. Programming error?" << endl;
413 int const r = mds->angle;
414 float const * d = mds->data;
416 if (h > 70 && (mds->code == int('(')
417 || mds->code == int(')')))
424 int const n = (w < h) ? w : h;
427 if (r > 0 && r < 3) y += h;
436 xx = d[i++]; yy = d[i++];
437 x2 = d[i++]; y2 = d[i++];
439 sqmt.transf(xx, yy, xx, yy);
441 mt.transf(xx, yy, xx, yy);
442 mt.transf(x2, y2, x2, y2);
443 pain.line(x + int(xx), y + int(yy),
444 x + int(x2), y + int(y2),
453 int const n = int(d[i++]);
454 for (int j = 0; j < n; ++j) {
455 xx = d[i++]; yy = d[i++];
456 // lyxerr << " " << xx << " " << yy << " ";
458 sqmt.transf(xx, yy, xx, yy);
460 mt.transf(xx, yy, xx, yy);
463 // lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
465 pain.lines(xp, yp, n, LColor::mathline);
472 math_deco_struct const * search_deco(int code)
474 math_deco_struct search_elem = { code, 0, 0 };
476 math_deco_struct const * res =
477 lower_bound(math_deco_table,
478 math_deco_table + math_deco_table_size,
479 search_elem, math_deco_compare());
480 if (res != math_deco_table + math_deco_table_size &&
487 bool MathIsInset(short x)
489 return LM_TC_INSET == x;
493 bool MathIsAlphaFont(short x)
495 return LM_TC_VAR <= x && x <= LM_TC_TEXTRM;
499 bool MathIsBOPS(short x)
501 return MathLookupBOP(x) > LMB_NONE;
505 bool MathIsBinary(short x)
507 return x == LM_TC_BOP || x == LM_TC_BOPS;
511 bool MathIsSymbol(short x)
513 return x == LM_TC_SYMB || x == LM_TC_BOPS || x == LM_TC_BSYM;
517 bool is_matrix_type(short int type)
519 return type == LM_OT_MATRIX;
522 // In a near future maybe we use a better fonts renderer
523 void drawStr(Painter & pain, short type, int siz,
524 int x, int y, string const & s)
527 if (MathIsBinary(type))
528 for (string::const_iterator it = s.begin();
529 it != s.end(); ++it) {
537 LyXFont const mf = mathed_get_font(type, siz);
538 pain.text(x, y, st, mf);
541 void drawChar(Painter & pain, short type, int siz, int x, int y, char c)
545 drawStr(pain, type, siz, x, y, s);
548 // decrease math size for super- and subscripts
549 MathStyles smallerStyleScript(MathStyles st)
553 case LM_ST_TEXT: st = LM_ST_SCRIPT; break;
554 default: st = LM_ST_SCRIPTSCRIPT;
559 // decrease math size for fractions
560 MathStyles smallerStyleFrac(MathStyles st)
563 case LM_ST_DISPLAY: st = LM_ST_TEXT; break;
564 case LM_ST_TEXT: st = LM_ST_SCRIPT; break;
565 default: st = LM_ST_SCRIPTSCRIPT;
570 bool MathIsRelOp(byte c, MathTextCodes f)
572 if (f == LM_TC_BOP && (c == '=' || c == '<' || c == '>'))
574 #ifndef WITH_WARNINGS
575 #warning implement me properly
577 if (f == LM_TC_SYMB && (c == LM_leq || c == LM_geq))