5 #include "math_support.h"
7 #include "math_cursor.h"
9 #include "math_inset.h"
10 #include "math_parser.h"
11 #include "frontends/Painter.h"
12 #include "frontends/font_metrics.h"
13 #include "frontends/font_loader.h"
15 #include "commandtags.h"
26 Matrix(int, double, double);
28 void transform(double &, double &);
35 Matrix::Matrix(int code, double x, double y)
37 double const cs = (code & 1) ? 0 : (1 - code);
38 double const sn = (code & 1) ? (2 - code) : 0;
46 void Matrix::transform(double & x, double & y)
48 double xx = m_[0][0] * x + m_[0][1] * y;
49 double yy = m_[1][0] * x + m_[1][1] * y;
59 * Internal struct of a drawing: code n x1 y1 ... xn yn, where code is:
60 * 0 = end, 1 = line, 2 = polyline, 3 = square line, 4 = square polyline
64 double const parenthHigh[] = {
66 0.9840, 0.0014, 0.7143, 0.0323, 0.4603, 0.0772,
67 0.2540, 0.1278, 0.1746, 0.1966, 0.0952, 0.3300,
68 0.0950, 0.5000, 0.0952, 0.6700, 0.1746, 0.8034,
69 0.2540, 0.8722, 0.4603, 0.9228, 0.7143, 0.9677,
75 double const parenth[] = {
77 0.9930, 0.0071, 0.7324, 0.0578, 0.5141, 0.1126,
78 0.3380, 0.1714, 0.2183, 0.2333, 0.0634, 0.3621,
79 0.0141, 0.5000, 0.0563, 0.6369, 0.2113, 0.7647,
80 0.3310, 0.8276, 0.5070, 0.8864, 0.7254, 0.9412,
86 double const brace[] = {
88 0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243,
89 0.5819, 0.0527, 0.4859, 0.0892, 0.4463, 0.1278,
90 0.4463, 0.3732, 0.4011, 0.4199, 0.2712, 0.4615,
91 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
92 0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268,
93 0.4463, 0.8722, 0.4859, 0.9108, 0.5819, 0.9473,
94 0.7458, 0.9757, 0.9379, 0.9980, 0.9492, 0.9980,
99 double const arrow[] = {
101 0.0150, 0.7500, 0.2000, 0.6000, 0.3500, 0.3500,
102 0.5000, 0.0500, 0.6500, 0.3500, 0.8000, 0.6000,
104 3, 0.5000, 0.1500, 0.5000, 0.9500,
109 double const Arrow[] = {
111 0.0150, 0.7500, 0.2000, 0.6000, 0.3500, 0.3500,
112 0.5000, 0.0500, 0.6500, 0.3500, 0.8000, 0.6000,
114 3, 0.3500, 0.5000, 0.3500, 0.9500,
115 3, 0.6500, 0.5000, 0.6500, 0.9500,
120 double const udarrow[] = {
122 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
124 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
125 1, 0.5, 0.2, 0.5, 0.8,
130 double const Udarrow[] = {
132 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
134 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
135 1, 0.35, 0.2, 0.35, 0.8,
136 1, 0.65, 0.2, 0.65, 0.8,
141 double const brack[] = {
143 0.95, 0.05, 0.05, 0.05, 0.05, 0.95, 0.95, 0.95,
148 double const corner[] = {
150 0.95, 0.05, 0.05, 0.05, 0.05, 0.95,
155 double const angle[] = {
157 1, 0, 0.05, 0.5, 1, 1,
162 double const slash[] = {
163 1, 0.95, 0.05, 0.05, 0.95,
168 double const hline[] = {
169 1, 0.00, 0.5, 1.0, 0.5,
174 double const ddot[] = {
175 1, 0.2, 0.5, 0.3, 0.5,
176 1, 0.7, 0.5, 0.8, 0.5,
181 double const dddot[] = {
182 1, 0.1, 0.5, 0.2, 0.5,
183 1, 0.45, 0.5, 0.55, 0.5,
184 1, 0.8, 0.5, 0.9, 0.5,
189 double const hline3[] = {
191 1, 0.475, 0, 0.525, 0,
197 double const dline3[] = {
198 1, 0.1, 0.1, 0.15, 0.15,
199 1, 0.475, 0.475, 0.525, 0.525,
200 1, 0.85, 0.85, 0.9, 0.9,
205 double const hlinesmall[] = {
206 1, 0.4, 0.5, 0.6, 0.5,
211 double const ring[] = {
213 0.5, 0.8, 0.8, 0.5, 0.5, 0.2, 0.2, 0.5, 0.5, 0.8,
218 double const vert[] = {
219 1, 0.5, 0.05, 0.5, 0.95,
224 double const Vert[] = {
225 1, 0.3, 0.05, 0.3, 0.95,
226 1, 0.7, 0.05, 0.7, 0.95,
231 double const tilde[] = {
233 0.00, 0.8, 0.25, 0.2, 0.75, 0.8, 1.00, 0.2,
243 struct named_deco_struct {
249 named_deco_struct deco_table[] = {
251 {"widehat", angle, 3 },
252 {"widetilde", tilde, 0 },
253 {"underbar", hline, 0 },
254 {"underline", hline, 0 },
255 {"overline", hline, 0 },
256 {"underbrace", brace, 1 },
257 {"overbrace", brace, 3 },
258 {"overleftarrow", arrow, 1 },
259 {"overrightarrow", arrow, 3 },
260 {"overleftrightarrow", udarrow, 1 },
261 {"xleftarrow", arrow, 1 },
262 {"xrightarrow", arrow, 3 },
263 {"underleftarrow", arrow, 1 },
264 {"underrightarrow", arrow, 3 },
265 {"underleftrightarrow", udarrow, 1 },
279 {"backslash", slash, 1 },
280 {"langle", angle, 0 },
281 {"lceil", corner, 0 },
282 {"lfloor", corner, 1 },
283 {"rangle", angle, 2 },
284 {"rceil", corner, 3 },
285 {"rfloor", corner, 2 },
286 {"downarrow", arrow, 2 },
287 {"Downarrow", Arrow, 2 },
288 {"uparrow", arrow, 0 },
289 {"Uparrow", Arrow, 0 },
290 {"updownarrow", udarrow, 0 },
291 {"Updownarrow", Udarrow, 0 },
295 {"dddot", dddot, 0 },
297 {"grave", slash, 1 },
298 {"acute", slash, 0 },
299 {"tilde", tilde, 0 },
301 {"dot", hlinesmall, 0 },
302 {"check", angle, 1 },
303 {"breve", parenth, 1 },
306 {"mathring", ring, 0 },
309 {"ldots", hline3, 0 },
310 {"cdots", hline3, 0 },
311 {"vdots", hline3, 1 },
312 {"ddots", dline3, 0 },
313 {"dotsb", hline3, 0 },
314 {"dotsc", hline3, 0 },
315 {"dotsi", hline3, 0 },
316 {"dotsm", hline3, 0 },
317 {"dotso", hline3, 0 }
321 map<string, deco_struct> deco_list;
323 // sort the table on startup
324 struct init_deco_table {
326 unsigned const n = sizeof(deco_table) / sizeof(deco_table[0]);
327 for (named_deco_struct * p = deco_table; p != deco_table + n; ++p) {
331 deco_list[p->name]= d;
336 static init_deco_table dummy;
339 deco_struct const * search_deco(string const & name)
341 map<string, deco_struct>::const_iterator p = deco_list.find(name);
342 return (p == deco_list.end()) ? 0 : &(p->second);
349 void mathed_char_dim(LyXFont const & font,
350 unsigned char c, int & asc, int & des, int & wid)
352 des = font_metrics::descent(c, font);
353 asc = font_metrics::ascent(c, font);
354 wid = mathed_char_width(font, c);
358 int mathed_char_ascent(LyXFont const & font, unsigned char c)
360 return font_metrics::ascent(c, font);
364 int mathed_char_descent(LyXFont const & font, unsigned char c)
366 return font_metrics::descent(c, font);
370 int mathed_char_width(LyXFont const & font, unsigned char c)
372 return font_metrics::width(c, font);
376 void mathed_string_dim(LyXFont const & font,
377 string const & s, int & asc, int & des, int & wid)
380 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
381 des = max(des, font_metrics::descent(*it, font));
382 asc = max(asc, font_metrics::ascent(*it, font));
384 wid = font_metrics::width(s, font);
388 int mathed_string_width(LyXFont const & font, string const & s)
390 return font_metrics::width(s, font);
394 int mathed_string_ascent(LyXFont const & font, string const & s)
397 for (string::const_iterator it = s.begin(); it != s.end(); ++it)
398 asc = max(asc, font_metrics::ascent(*it, font));
403 int mathed_string_descent(LyXFont const & font, string const & s)
406 for (string::const_iterator it = s.begin(); it != s.end(); ++it)
407 des = max(des, font_metrics::descent(*it, font));
413 void mathed_draw_deco(MathPainterInfo & pi, int x, int y, int w, int h,
417 pi.pain.line(x + w/2, y, x + w/2, y + h,
418 LColor::mathcursor, Painter::line_onoffdash);
422 deco_struct const * mds = search_deco(name);
424 lyxerr << "Deco was not found. Programming error?\n";
425 lyxerr << "name: '" << name << "'\n";
429 int const n = (w < h) ? w : h;
430 int const r = mds->angle;
431 double const * d = mds->data;
433 if (h > 70 && (name == "(" || name == ")"))
437 Matrix sqmt(r, n, n);
445 for (int i = 0; d[i]; ) {
446 int code = int(d[i++]);
447 if (code & 1) { // code == 1 || code == 3
453 sqmt.transform(xx, yy);
455 mt.transform(xx, yy);
456 mt.transform(x2, y2);
458 x + int(xx + 0.5), y + int(yy + 0.5),
459 x + int(x2 + 0.5), y + int(y2 + 0.5),
464 int const n = int(d[i++]);
465 for (int j = 0; j < n; ++j) {
468 // lyxerr << " " << xx << " " << yy << " ";
470 sqmt.transform(xx, yy);
472 mt.transform(xx, yy);
473 xp[j] = x + int(xx + 0.5);
474 yp[j] = y + int(yy + 0.5);
475 // lyxerr << "P[" << j " " << xx << " " << yy << " " << x << " " << y << "]";
477 pi.pain.lines(xp, yp, n, LColor::math);
483 void mathed_draw_framebox(MathPainterInfo & pi, int x, int y, MathInset const * p)
485 if (mathcursor && mathcursor->isInside(p))
486 pi.pain.rectangle(x, y - p->ascent(), p->width(), p->height(),
491 // In the future maybe we use a better fonts renderer
492 void drawStr(MathPainterInfo & pi, LyXFont const & font,
493 int x, int y, string const & str)
495 pi.pain.text(x, y, str, font);
499 void drawStrRed(MathPainterInfo & pi, int x, int y, string const & str)
501 LyXFont f = pi.base.font;
502 f.setColor(LColor::latex);
503 pi.pain.text(x, y, str, f);
507 void drawStrBlack(MathPainterInfo & pi, int x, int y, string const & str)
509 LyXFont f = pi.base.font;
510 f.setColor(LColor::black);
511 pi.pain.text(x, y, str, f);
515 void drawChar(MathPainterInfo & pi, LyXFont const & font, int x, int y, char c)
517 pi.pain.text(x, y, c, font);
521 void math_font_max_dim(LyXFont const & font, int & asc, int & des)
523 asc = font_metrics::maxAscent(font);
524 des = font_metrics::maxDescent(font);
528 string convertDelimToLatexName(string const & name)
544 return "\\" + name + " ";
552 LyXFont::FONT_FAMILY family_;
553 LyXFont::FONT_SERIES series_;
554 LyXFont::FONT_SHAPE shape_;
555 LColor::color color_;
559 LyXFont::FONT_FAMILY const inh_family = LyXFont::INHERIT_FAMILY;
560 LyXFont::FONT_SERIES const inh_series = LyXFont::INHERIT_SERIES;
561 LyXFont::FONT_SHAPE const inh_shape = LyXFont::INHERIT_SHAPE;
565 fontinfo fontinfos[] = {
566 {"cmex", LyXFont::CMEX_FAMILY, inh_series, inh_shape, LColor::math},
567 {"cmm", LyXFont::CMM_FAMILY, inh_series, inh_shape, LColor::math},
568 {"cmr", LyXFont::CMR_FAMILY, inh_series, inh_shape, LColor::math},
569 {"cmsy", LyXFont::CMSY_FAMILY, inh_series, inh_shape, LColor::math},
570 {"eufrak", LyXFont::EUFRAK_FAMILY, inh_series, inh_shape, LColor::math},
571 {"mathbf", inh_family, LyXFont::BOLD_SERIES, inh_shape, LColor::math},
572 {"mathcal",LyXFont::CMSY_FAMILY, inh_series, inh_shape, LColor::math},
573 {"mathfrak", LyXFont::EUFRAK_FAMILY, inh_series, inh_shape, LColor::math},
574 {"mathnormal", inh_family,inh_series, LyXFont::UP_SHAPE, LColor::math},
575 {"mathrm", LyXFont::ROMAN_FAMILY, inh_series, inh_shape, LColor::math},
576 {"mathsf", LyXFont::SANS_FAMILY, inh_series, inh_shape, LColor::math},
577 {"msa", LyXFont::MSA_FAMILY, inh_series, inh_shape, LColor::math},
578 {"msb", LyXFont::MSB_FAMILY, inh_series, inh_shape, LColor::math},
579 {"textbf", inh_family, LyXFont::BOLD_SERIES, inh_shape, LColor::black},
580 {"textit", inh_family, inh_series, LyXFont::ITALIC_SHAPE, LColor::black},
581 {"textmd", inh_family, LyXFont::MEDIUM_SERIES, inh_shape, LColor::black},
582 {"textnormal", inh_family, inh_series, LyXFont::UP_SHAPE, LColor::black},
583 {"textrm", LyXFont::ROMAN_FAMILY, inh_series,LyXFont::UP_SHAPE,LColor::black},
584 {"textsc", inh_family, inh_series, LyXFont::SMALLCAPS_SHAPE, LColor::black},
585 {"textsf", LyXFont::SANS_FAMILY, inh_series, inh_shape, LColor::black},
586 {"textsl", inh_family, inh_series, LyXFont::SLANTED_SHAPE, LColor::black},
587 {"texttt", LyXFont::TYPEWRITER_FAMILY, inh_series, inh_shape, LColor::black},
588 {"textup", inh_family, inh_series, LyXFont::UP_SHAPE, LColor::black},
590 {"lyxtex", inh_family, inh_series, inh_shape, LColor::latex},
591 {"lyxsymbol", LyXFont::SYMBOL_FAMILY, inh_series, inh_shape, LColor::math},
593 LyXFont::SYMBOL_FAMILY, LyXFont::BOLD_SERIES, inh_shape, LColor::math},
594 {"lyxitsymbol", LyXFont::SYMBOL_FAMILY,
595 inh_series, LyXFont::ITALIC_SHAPE, LColor::math},
596 {"lyxblacktext", LyXFont::ROMAN_FAMILY,
597 LyXFont::MEDIUM_SERIES, LyXFont::UP_SHAPE, LColor::black},
598 {"lyxnochange", inh_family, inh_series, inh_shape, LColor::black},
600 {"lyxfakebb", LyXFont::TYPEWRITER_FAMILY, LyXFont::BOLD_SERIES,
601 LyXFont::UP_SHAPE, LColor::math},
602 {"lyxfakecal", LyXFont::SANS_FAMILY, LyXFont::MEDIUM_SERIES,
603 LyXFont::ITALIC_SHAPE, LColor::math},
604 {"lyxfakefrak", LyXFont::ROMAN_FAMILY, LyXFont::BOLD_SERIES,
605 LyXFont::ITALIC_SHAPE, LColor::math}
609 fontinfo * searchFont(string const & name)
611 int const n = sizeof(fontinfos) / sizeof(fontinfo);
612 //lyxerr << "searching font '" << name << "'\n";
613 for (int i = 0; i < n; ++i)
614 if (fontinfos[i].cmd_ == name) {
615 //lyxerr << "found '" << i << "'\n";
616 return fontinfos + i;
618 return searchFont("mathnormal");
622 LyXFont getFont(string const & name)
625 augmentFont(font, name);
630 void fakeFont(string const & orig, string const & fake)
632 fontinfo * forig = searchFont(orig);
633 fontinfo * ffake = searchFont(fake);
634 if (forig && ffake) {
635 forig->family_ = ffake->family_;
636 forig->series_ = ffake->series_;
637 forig->shape_ = ffake->shape_;
638 forig->color_ = ffake->color_;
640 lyxerr << "Can't fake font '" << orig << "' with '" << fake << "'\n";
644 void augmentFont(LyXFont & font, string const & name)
646 static bool initialized = false;
650 // fake fonts if necessary
651 if (!fontloader.available(getFont("mathfrak")))
652 fakeFont("mathfrak", "lyxfakefrak");
653 if (!fontloader.available(getFont("mathcal")))
654 fakeFont("mathcal", "lyxfakecal");
656 fontinfo * info = searchFont(name);
657 if (info->family_ != inh_family)
658 font.setFamily(info->family_);
659 if (info->series_ != inh_series)
660 font.setSeries(info->series_);
661 if (info->shape_ != inh_shape)
662 font.setShape(info->shape_);
663 if (info->color_ != LColor::none)
664 font.setColor(info->color_);