7 #include "math_support.h"
9 #include "math_cursor.h"
10 #include "math_inset.h"
11 #include "math_parser.h"
12 #include "math_metricsinfo.h"
13 #include "frontends/Painter.h"
14 #include "frontends/font_metrics.h"
15 #include "frontends/lyx_gui.h"
17 #include "commandtags.h"
18 #include "dimension.h"
29 Matrix(int, double, double);
31 void transform(double &, double &);
38 Matrix::Matrix(int code, double x, double y)
40 double const cs = (code & 1) ? 0 : (1 - code);
41 double const sn = (code & 1) ? (2 - code) : 0;
49 void Matrix::transform(double & x, double & y)
51 double xx = m_[0][0] * x + m_[0][1] * y;
52 double yy = m_[1][0] * x + m_[1][1] * y;
62 * Internal struct of a drawing: code n x1 y1 ... xn yn, where code is:
63 * 0 = end, 1 = line, 2 = polyline, 3 = square line, 4 = square polyline
67 double const parenthHigh[] = {
69 0.9840, 0.0014, 0.7143, 0.0323, 0.4603, 0.0772,
70 0.2540, 0.1278, 0.1746, 0.1966, 0.0952, 0.3300,
71 0.0950, 0.5000, 0.0952, 0.6700, 0.1746, 0.8034,
72 0.2540, 0.8722, 0.4603, 0.9228, 0.7143, 0.9677,
78 double const parenth[] = {
80 0.9930, 0.0071, 0.7324, 0.0578, 0.5141, 0.1126,
81 0.3380, 0.1714, 0.2183, 0.2333, 0.0634, 0.3621,
82 0.0141, 0.5000, 0.0563, 0.6369, 0.2113, 0.7647,
83 0.3310, 0.8276, 0.5070, 0.8864, 0.7254, 0.9412,
89 double const brace[] = {
91 0.9492, 0.0020, 0.9379, 0.0020, 0.7458, 0.0243,
92 0.5819, 0.0527, 0.4859, 0.0892, 0.4463, 0.1278,
93 0.4463, 0.3732, 0.4011, 0.4199, 0.2712, 0.4615,
94 0.0734, 0.4919, 0.0113, 0.5000, 0.0734, 0.5081,
95 0.2712, 0.5385, 0.4011, 0.5801, 0.4463, 0.6268,
96 0.4463, 0.8722, 0.4859, 0.9108, 0.5819, 0.9473,
97 0.7458, 0.9757, 0.9379, 0.9980, 0.9492, 0.9980,
102 double const arrow[] = {
104 0.0150, 0.7500, 0.2000, 0.6000, 0.3500, 0.3500,
105 0.5000, 0.0500, 0.6500, 0.3500, 0.8000, 0.6000,
107 3, 0.5000, 0.1500, 0.5000, 0.9500,
112 double const Arrow[] = {
114 0.0150, 0.7500, 0.2000, 0.6000, 0.3500, 0.3500,
115 0.5000, 0.0500, 0.6500, 0.3500, 0.8000, 0.6000,
117 3, 0.3500, 0.5000, 0.3500, 0.9500,
118 3, 0.6500, 0.5000, 0.6500, 0.9500,
123 double const udarrow[] = {
125 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
127 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
128 1, 0.5, 0.2, 0.5, 0.8,
133 double const Udarrow[] = {
135 0.015, 0.25, 0.5, 0.05, 0.95, 0.25,
137 0.015, 0.75, 0.5, 0.95, 0.95, 0.75,
138 1, 0.35, 0.2, 0.35, 0.8,
139 1, 0.65, 0.2, 0.65, 0.8,
144 double const brack[] = {
146 0.95, 0.05, 0.05, 0.05, 0.05, 0.95, 0.95, 0.95,
151 double const corner[] = {
153 0.95, 0.05, 0.05, 0.05, 0.05, 0.95,
158 double const angle[] = {
160 1, 0, 0.05, 0.5, 1, 1,
165 double const slash[] = {
166 1, 0.95, 0.05, 0.05, 0.95,
171 double const hline[] = {
172 1, 0.00, 0.5, 1.0, 0.5,
177 double const ddot[] = {
178 1, 0.2, 0.5, 0.3, 0.5,
179 1, 0.7, 0.5, 0.8, 0.5,
184 double const dddot[] = {
185 1, 0.1, 0.5, 0.2, 0.5,
186 1, 0.45, 0.5, 0.55, 0.5,
187 1, 0.8, 0.5, 0.9, 0.5,
192 double const hline3[] = {
194 1, 0.475, 0, 0.525, 0,
200 double const dline3[] = {
201 1, 0.1, 0.1, 0.15, 0.15,
202 1, 0.475, 0.475, 0.525, 0.525,
203 1, 0.85, 0.85, 0.9, 0.9,
208 double const hlinesmall[] = {
209 1, 0.4, 0.5, 0.6, 0.5,
214 double const ring[] = {
216 0.5, 0.8, 0.8, 0.5, 0.5, 0.2, 0.2, 0.5, 0.5, 0.8,
221 double const vert[] = {
222 1, 0.5, 0.05, 0.5, 0.95,
227 double const Vert[] = {
228 1, 0.3, 0.05, 0.3, 0.95,
229 1, 0.7, 0.05, 0.7, 0.95,
234 double const tilde[] = {
236 0.00, 0.8, 0.25, 0.2, 0.75, 0.8, 1.00, 0.2,
246 struct named_deco_struct {
252 named_deco_struct deco_table[] = {
254 {"widehat", angle, 3 },
255 {"widetilde", tilde, 0 },
256 {"underbar", hline, 0 },
257 {"underline", hline, 0 },
258 {"overline", hline, 0 },
259 {"underbrace", brace, 1 },
260 {"overbrace", brace, 3 },
261 {"overleftarrow", arrow, 1 },
262 {"overrightarrow", arrow, 3 },
263 {"overleftrightarrow", udarrow, 1 },
264 {"xleftarrow", arrow, 1 },
265 {"xrightarrow", arrow, 3 },
266 {"underleftarrow", arrow, 1 },
267 {"underrightarrow", arrow, 3 },
268 {"underleftrightarrow", udarrow, 1 },
282 {"backslash", slash, 1 },
283 {"langle", angle, 0 },
284 {"lceil", corner, 0 },
285 {"lfloor", corner, 1 },
286 {"rangle", angle, 2 },
287 {"rceil", corner, 3 },
288 {"rfloor", corner, 2 },
289 {"downarrow", arrow, 2 },
290 {"Downarrow", Arrow, 2 },
291 {"uparrow", arrow, 0 },
292 {"Uparrow", Arrow, 0 },
293 {"updownarrow", udarrow, 0 },
294 {"Updownarrow", Udarrow, 0 },
298 {"dddot", dddot, 0 },
300 {"grave", slash, 1 },
301 {"acute", slash, 0 },
302 {"tilde", tilde, 0 },
304 {"dot", hlinesmall, 0 },
305 {"check", angle, 1 },
306 {"breve", parenth, 1 },
308 {"mathring", ring, 0 },
311 {"ldots", hline3, 0 },
312 {"cdots", hline3, 0 },
313 {"vdots", hline3, 1 },
314 {"ddots", dline3, 0 },
315 {"dotsb", hline3, 0 },
316 {"dotsc", hline3, 0 },
317 {"dotsi", hline3, 0 },
318 {"dotsm", hline3, 0 },
319 {"dotso", hline3, 0 }
323 std::map<string, deco_struct> deco_list;
325 // sort the table on startup
326 struct init_deco_table {
328 unsigned const n = sizeof(deco_table) / sizeof(deco_table[0]);
329 for (named_deco_struct * p = deco_table; p != deco_table + n; ++p) {
333 deco_list[p->name]= d;
338 static init_deco_table dummy;
341 deco_struct const * search_deco(string const & name)
343 std::map<string, deco_struct>::const_iterator p = deco_list.find(name);
344 return (p == deco_list.end()) ? 0 : &(p->second);
351 void mathed_char_dim(LyXFont const & font, unsigned char c, Dimension & dim)
353 dim.d = font_metrics::descent(c, font);
354 dim.a = font_metrics::ascent(c, font);
355 dim.w = mathed_char_width(font, c);
359 int mathed_char_ascent(LyXFont const & font, unsigned char c)
361 return font_metrics::ascent(c, font);
365 int mathed_char_descent(LyXFont const & font, unsigned char c)
367 return font_metrics::descent(c, font);
371 int mathed_char_width(LyXFont const & font, unsigned char c)
373 return font_metrics::width(c, font);
377 void mathed_string_dim(LyXFont const & font, string const & s, Dimension & dim)
382 for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
383 dim.a = max(dim.a, font_metrics::ascent(*it, font));
384 dim.d = max(dim.d, font_metrics::descent(*it, font));
387 dim.a = font_metrics::maxAscent(font);
388 dim.d = font_metrics::maxDescent(font);
390 dim.w = font_metrics::width(s, font);
394 int mathed_string_width(LyXFont const & font, string const & s)
396 return font_metrics::width(s, font);
400 void mathed_draw_deco(MathPainterInfo & pi, int x, int y, int w, int h,
404 pi.pain.line(x + w/2, y, x + w/2, y + h,
405 LColor::mathcursor, Painter::line_onoffdash);
409 deco_struct const * mds = search_deco(name);
411 lyxerr << "Deco was not found. Programming error?\n";
412 lyxerr << "name: '" << name << "'\n";
416 int const n = (w < h) ? w : h;
417 int const r = mds->angle;
418 double const * d = mds->data;
420 if (h > 70 && (name == "(" || name == ")"))
424 Matrix sqmt(r, n, n);
432 for (int i = 0; d[i]; ) {
433 int code = int(d[i++]);
434 if (code & 1) { // code == 1 || code == 3
440 sqmt.transform(xx, yy);
442 mt.transform(xx, yy);
443 mt.transform(x2, y2);
445 int(x + xx + 0.5), int(y + yy + 0.5),
446 int(x + x2 + 0.5), int(y + y2 + 0.5),
451 int const n = int(d[i++]);
452 for (int j = 0; j < n; ++j) {
455 // lyxerr << ' ' << xx << ' ' << yy << ' ';
457 sqmt.transform(xx, yy);
459 mt.transform(xx, yy);
460 xp[j] = int(x + xx + 0.5);
461 yp[j] = int(y + yy + 0.5);
462 // lyxerr << "P[" << j ' ' << xx << ' ' << yy << ' ' << x << ' ' << y << ']';
464 pi.pain.lines(xp, yp, n, LColor::math);
470 void mathed_draw_framebox(MathPainterInfo & pi, int x, int y, MathInset const * p)
472 if (mathcursor && mathcursor->isInside(p))
473 pi.pain.rectangle(x, y - p->ascent(), p->width(), p->height(),
478 // In the future maybe we use a better fonts renderer
479 void drawStr(MathPainterInfo & pi, LyXFont const & font,
480 int x, int y, string const & str)
482 pi.pain.text(x, y, str, font);
486 void drawStrRed(MathPainterInfo & pi, int x, int y, string const & str)
488 LyXFont f = pi.base.font;
489 f.setColor(LColor::latex);
490 pi.pain.text(x, y, str, f);
494 void drawStrBlack(MathPainterInfo & pi, int x, int y, string const & str)
496 LyXFont f = pi.base.font;
497 f.setColor(LColor::black);
498 pi.pain.text(x, y, str, f);
502 void drawChar(MathPainterInfo & pi, LyXFont const & font, int x, int y, char c)
504 pi.pain.text(x, y, c, font);
508 void math_font_max_dim(LyXFont const & font, int & asc, int & des)
510 asc = font_metrics::maxAscent(font);
511 des = font_metrics::maxDescent(font);
517 LyXFont::FONT_FAMILY family_;
518 LyXFont::FONT_SERIES series_;
519 LyXFont::FONT_SHAPE shape_;
520 LColor::color color_;
524 LyXFont::FONT_FAMILY const inh_family = LyXFont::INHERIT_FAMILY;
525 LyXFont::FONT_SERIES const inh_series = LyXFont::INHERIT_SERIES;
526 LyXFont::FONT_SHAPE const inh_shape = LyXFont::INHERIT_SHAPE;
529 // mathnormal should be the first, otherwise the fallback further down
531 fontinfo fontinfos[] = {
533 inh_family, LyXFont::MEDIUM_SERIES, LyXFont::UP_SHAPE, LColor::math},
534 {"mathbf", inh_family, LyXFont::BOLD_SERIES, inh_shape, LColor::math},
535 {"mathcal",LyXFont::CMSY_FAMILY, inh_series, inh_shape, LColor::math},
536 {"mathfrak", LyXFont::EUFRAK_FAMILY, inh_series, inh_shape, LColor::math},
537 {"mathrm", LyXFont::ROMAN_FAMILY, inh_series, inh_shape, LColor::math},
538 {"mathsf", LyXFont::SANS_FAMILY, inh_series, inh_shape, LColor::math},
539 {"cmex", LyXFont::CMEX_FAMILY, inh_series, inh_shape, LColor::math},
540 {"cmm", LyXFont::CMM_FAMILY, inh_series, inh_shape, LColor::math},
541 {"cmr", LyXFont::CMR_FAMILY, inh_series, inh_shape, LColor::math},
542 {"cmsy", LyXFont::CMSY_FAMILY, inh_series, inh_shape, LColor::math},
543 {"eufrak", LyXFont::EUFRAK_FAMILY, inh_series, inh_shape, LColor::math},
544 {"msa", LyXFont::MSA_FAMILY, inh_series, inh_shape, LColor::math},
545 {"msb", LyXFont::MSB_FAMILY, inh_series, inh_shape, LColor::math},
546 {"wasy", LyXFont::WASY_FAMILY, inh_series, inh_shape, LColor::math},
547 {"text", inh_family, inh_series, inh_shape, LColor::black},
548 {"textbf", inh_family, LyXFont::BOLD_SERIES, inh_shape, LColor::black},
549 {"textit", inh_family, inh_series, LyXFont::ITALIC_SHAPE, LColor::black},
550 {"textmd", inh_family, LyXFont::MEDIUM_SERIES, inh_shape, LColor::black},
551 {"textnormal", inh_family, inh_series, LyXFont::UP_SHAPE, LColor::black},
552 {"textrm", LyXFont::ROMAN_FAMILY, inh_series,LyXFont::UP_SHAPE,LColor::black},
553 {"textsc", inh_family, inh_series, LyXFont::SMALLCAPS_SHAPE, LColor::black},
554 {"textsf", LyXFont::SANS_FAMILY, inh_series, inh_shape, LColor::black},
555 {"textsl", inh_family, inh_series, LyXFont::SLANTED_SHAPE, LColor::black},
556 {"texttt", LyXFont::TYPEWRITER_FAMILY, inh_series, inh_shape, LColor::black},
557 {"textup", inh_family, inh_series, LyXFont::UP_SHAPE, LColor::black},
560 {"textipa", inh_family, inh_series, inh_shape, LColor::black},
562 {"lyxtex", inh_family, inh_series, inh_shape, LColor::latex},
563 {"lyxert", LyXFont::TYPEWRITER_FAMILY, inh_series, inh_shape, LColor::latex},
564 {"lyxsymbol", LyXFont::SYMBOL_FAMILY, inh_series, inh_shape, LColor::math},
566 LyXFont::SYMBOL_FAMILY, LyXFont::BOLD_SERIES, inh_shape, LColor::math},
567 {"lyxitsymbol", LyXFont::SYMBOL_FAMILY,
568 inh_series, LyXFont::ITALIC_SHAPE, LColor::math},
569 {"lyxblacktext", LyXFont::ROMAN_FAMILY,
570 LyXFont::MEDIUM_SERIES, LyXFont::UP_SHAPE, LColor::black},
571 {"lyxnochange", inh_family, inh_series, inh_shape, LColor::black},
573 {"lyxfakebb", LyXFont::TYPEWRITER_FAMILY, LyXFont::BOLD_SERIES,
574 LyXFont::UP_SHAPE, LColor::math},
575 {"lyxfakecal", LyXFont::SANS_FAMILY, LyXFont::MEDIUM_SERIES,
576 LyXFont::ITALIC_SHAPE, LColor::math},
577 {"lyxfakefrak", LyXFont::ROMAN_FAMILY, LyXFont::BOLD_SERIES,
578 LyXFont::ITALIC_SHAPE, LColor::math}
582 fontinfo * lookupFont(string const & name)
584 //lyxerr << "searching font '" << name << "'\n";
585 int const n = sizeof(fontinfos) / sizeof(fontinfo);
586 for (int i = 0; i < n; ++i)
587 if (fontinfos[i].cmd_ == name) {
588 //lyxerr << "found '" << i << "'\n";
589 return fontinfos + i;
595 fontinfo * searchFont(string const & name)
597 fontinfo * f = lookupFont(name);
598 return f ? f : fontinfos;
599 // this should be mathnormal
600 //return searchFont("mathnormal");
604 bool isFontName(string const & name)
606 return lookupFont(name);
610 LyXFont getFont(string const & name)
613 augmentFont(font, name);
618 void fakeFont(string const & orig, string const & fake)
620 fontinfo * forig = searchFont(orig);
621 fontinfo * ffake = searchFont(fake);
622 if (forig && ffake) {
623 forig->family_ = ffake->family_;
624 forig->series_ = ffake->series_;
625 forig->shape_ = ffake->shape_;
626 forig->color_ = ffake->color_;
628 lyxerr << "Can't fake font '" << orig << "' with '" << fake << "'\n";
633 void augmentFont(LyXFont & font, string const & name)
635 static bool initialized = false;
638 // fake fonts if necessary
639 if (!lyx_gui::font_available(getFont("mathfrak")))
640 fakeFont("mathfrak", "lyxfakefrak");
641 if (!lyx_gui::font_available(getFont("mathcal")))
642 fakeFont("mathcal", "lyxfakecal");
644 fontinfo * info = searchFont(name);
645 if (info->family_ != inh_family)
646 font.setFamily(info->family_);
647 if (info->series_ != inh_series)
648 font.setSeries(info->series_);
649 if (info->shape_ != inh_shape)
650 font.setShape(info->shape_);
651 if (info->color_ != LColor::none)
652 font.setColor(info->color_);