]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_support.C
Andreas' patch to prevent crash on click on previewd inset
[lyx.git] / src / mathed / math_support.C
index 6d83d3aa749abb01e3730f0a38e3f3f871daf0cb..f857d8e753c09c41c888ff3a1fb9bdad593b0e50 100644 (file)
@@ -1,25 +1,36 @@
-#ifdef __GNUG__
-#pragma implementation
-#endif
+/**
+ * \file math_support.C
+ * This file is part of LyX, the document processor.
+ * Licence details can be found in the file COPYING.
+ *
+ * \author Alejandro Aguilar Sierra
+ * \author André Pönitz
+ *
+ * Full author contact details are available in file CREDITS.
+ */
 
 #include <config.h>
 
 #include "math_support.h"
-#include "lyxfont.h"
-#include "math_cursor.h"
+#include "math_data.h"
 #include "math_inset.h"
+#include "math_mathmlstream.h"
 #include "math_parser.h"
-#include "math_metricsinfo.h"
+
+#include "debug.h"
+#include "LColor.h"
+
 #include "frontends/Painter.h"
 #include "frontends/font_metrics.h"
 #include "frontends/lyx_gui.h"
-#include "debug.h"
-#include "commandtags.h"
-#include "dimension.h"
 
 #include <map>
+#include <sstream>
+
 
+using std::string;
 using std::max;
+using std::endl;
 
 
 ///
@@ -324,7 +335,8 @@ named_deco_struct deco_table[] = {
 std::map<string, deco_struct> deco_list;
 
 // sort the table on startup
-struct init_deco_table {
+class init_deco_table {
+public:
        init_deco_table() {
                unsigned const n = sizeof(deco_table) / sizeof(deco_table[0]);
                for (named_deco_struct * p = deco_table; p != deco_table + n; ++p) {
@@ -351,9 +363,9 @@ deco_struct const * search_deco(string const & name)
 
 void mathed_char_dim(LyXFont const & font, unsigned char c, Dimension & dim)
 {
-       dim.d = font_metrics::descent(c, font);
-       dim.a = font_metrics::ascent(c, font);
-       dim.w = mathed_char_width(font, c);
+       dim.des = font_metrics::descent(c, font);
+       dim.asc = font_metrics::ascent(c, font);
+       dim.wid = mathed_char_width(font, c);
 }
 
 
@@ -378,17 +390,17 @@ int mathed_char_width(LyXFont const & font, unsigned char c)
 void mathed_string_dim(LyXFont const & font, string const & s, Dimension & dim)
 {
 #if 1
-       dim.a = 0;
-       dim.d = 0;
+       dim.asc = 0;
+       dim.des = 0;
        for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
-               dim.a = max(dim.a, font_metrics::ascent(*it, font));
-               dim.d = max(dim.d, font_metrics::descent(*it, font));
+               dim.asc = max(dim.asc, font_metrics::ascent(*it, font));
+               dim.des = max(dim.des, font_metrics::descent(*it, font));
        }
 #else
-       dim.a = font_metrics::maxAscent(font);
-       dim.d = font_metrics::maxDescent(font);
+       dim.asc = font_metrics::maxAscent(font);
+       dim.des = font_metrics::maxDescent(font);
 #endif
-       dim.w = font_metrics::width(s, font);
+       dim.wid = font_metrics::width(s, font);
 }
 
 
@@ -398,19 +410,19 @@ int mathed_string_width(LyXFont const & font, string const & s)
 }
 
 
-void mathed_draw_deco(MathPainterInfo & pi, int x, int y, int w, int h,
-       const string & name)
+void mathed_draw_deco(PainterInfo & pi, int x, int y, int w, int h,
+       string const & name)
 {
        if (name == ".") {
                pi.pain.line(x + w/2, y, x + w/2, y + h,
-                         LColor::mathcursor, Painter::line_onoffdash);
+                         LColor::cursor, Painter::line_onoffdash);
                return;
        }
 
        deco_struct const * mds = search_deco(name);
        if (!mds) {
-               lyxerr << "Deco was not found. Programming error?\n";
-               lyxerr << "name: '" << name << "'\n";
+               lyxerr << "Deco was not found. Programming error?" << endl;
+               lyxerr << "name: '" << name << "'" << endl;
                return;
        }
 
@@ -446,7 +458,7 @@ void mathed_draw_deco(MathPainterInfo & pi, int x, int y, int w, int h,
                                int(x + xx + 0.5), int(y + yy + 0.5),
                                int(x + x2 + 0.5), int(y + y2 + 0.5),
                                LColor::math);
-               }       else {
+               } else {
                        int xp[32];
                        int yp[32];
                        int const n = int(d[i++]);
@@ -468,23 +480,7 @@ void mathed_draw_deco(MathPainterInfo & pi, int x, int y, int w, int h,
 }
 
 
-void mathed_draw_framebox(MathPainterInfo & pi, int x, int y, MathInset const * p)
-{
-       if (mathcursor && mathcursor->isInside(p))
-               pi.pain.rectangle(x, y - p->ascent(), p->width(), p->height(),
-                       LColor::mathframe);
-}
-
-
-// In the future maybe we use a better fonts renderer
-void drawStr(MathPainterInfo & pi, LyXFont const & font,
-       int x, int y, string const & str)
-{
-       pi.pain.text(x, y, str, font);
-}
-
-
-void drawStrRed(MathPainterInfo & pi, int x, int y, string const & str)
+void drawStrRed(PainterInfo & pi, int x, int y, string const & str)
 {
        LyXFont f = pi.base.font;
        f.setColor(LColor::latex);
@@ -492,20 +488,14 @@ void drawStrRed(MathPainterInfo & pi, int x, int y, string const & str)
 }
 
 
-void drawStrBlack(MathPainterInfo & pi, int x, int y, string const & str)
+void drawStrBlack(PainterInfo & pi, int x, int y, string const & str)
 {
        LyXFont f = pi.base.font;
-       f.setColor(LColor::black);
+       f.setColor(LColor::foreground);
        pi.pain.text(x, y, str, f);
 }
 
 
-void drawChar(MathPainterInfo & pi, LyXFont const & font, int x, int y, char c)
-{
-       pi.pain.text(x, y, c, font);
-}
-
-
 void math_font_max_dim(LyXFont const & font, int & asc, int & des)
 {
        asc = font_metrics::maxAscent(font);
@@ -530,63 +520,97 @@ LyXFont::FONT_SHAPE  const inh_shape  = LyXFont::INHERIT_SHAPE;
 // mathnormal should be the first, otherwise the fallback further down
 // does not work
 fontinfo fontinfos[] = {
-       {"mathnormal",
-               inh_family, LyXFont::MEDIUM_SERIES, LyXFont::UP_SHAPE, LColor::math},
-       {"mathbf", inh_family, LyXFont::BOLD_SERIES, inh_shape, LColor::math},
-       {"mathcal",LyXFont::CMSY_FAMILY, inh_series, inh_shape, LColor::math},
-       {"mathfrak", LyXFont::EUFRAK_FAMILY, inh_series, inh_shape, LColor::math},
-       {"mathrm", LyXFont::ROMAN_FAMILY, inh_series, inh_shape, LColor::math},
-       {"mathsf", LyXFont::SANS_FAMILY, inh_series, inh_shape, LColor::math},
-       {"cmex",   LyXFont::CMEX_FAMILY, inh_series, inh_shape, LColor::math},
-       {"cmm",    LyXFont::CMM_FAMILY, inh_series, inh_shape, LColor::math},
-       {"cmr",    LyXFont::CMR_FAMILY, inh_series, inh_shape, LColor::math},
-       {"cmsy",   LyXFont::CMSY_FAMILY, inh_series, inh_shape, LColor::math},
-       {"eufrak", LyXFont::EUFRAK_FAMILY, inh_series, inh_shape, LColor::math},
-       {"msa",    LyXFont::MSA_FAMILY, inh_series, inh_shape, LColor::math},
-       {"msb",    LyXFont::MSB_FAMILY, inh_series, inh_shape, LColor::math},
-       {"wasy",   LyXFont::WASY_FAMILY, inh_series, inh_shape, LColor::math},
-       {"text",   inh_family, inh_series, inh_shape, LColor::black},
-       {"textbf", inh_family, LyXFont::BOLD_SERIES, inh_shape, LColor::black},
-       {"textit", inh_family, inh_series, LyXFont::ITALIC_SHAPE, LColor::black},
-       {"textmd", inh_family, LyXFont::MEDIUM_SERIES, inh_shape, LColor::black},
-       {"textnormal", inh_family, inh_series, LyXFont::UP_SHAPE, LColor::black},
-       {"textrm", LyXFont::ROMAN_FAMILY, inh_series,LyXFont::UP_SHAPE,LColor::black},
-       {"textsc", inh_family, inh_series, LyXFont::SMALLCAPS_SHAPE, LColor::black},
-       {"textsf", LyXFont::SANS_FAMILY, inh_series, inh_shape, LColor::black},
-       {"textsl", inh_family, inh_series, LyXFont::SLANTED_SHAPE, LColor::black},
-       {"texttt", LyXFont::TYPEWRITER_FAMILY, inh_series, inh_shape, LColor::black},
-       {"textup", inh_family, inh_series, LyXFont::UP_SHAPE, LColor::black},
+       // math fonts
+       {"mathnormal",    LyXFont::ROMAN_FAMILY, LyXFont::MEDIUM_SERIES,
+                         LyXFont::ITALIC_SHAPE, LColor::math},
+       {"mathbf",        inh_family, LyXFont::BOLD_SERIES,
+                         inh_shape, LColor::math},
+       {"mathcal",       LyXFont::CMSY_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"mathfrak",      LyXFont::EUFRAK_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"mathrm",        LyXFont::ROMAN_FAMILY, inh_series,
+                         LyXFont::UP_SHAPE, LColor::math},
+       {"mathsf",        LyXFont::SANS_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"mathbb",        LyXFont::MSB_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"mathtt",        LyXFont::TYPEWRITER_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"mathit",        inh_family, inh_series,
+                         LyXFont::ITALIC_SHAPE, LColor::math},
+       {"cmex",          LyXFont::CMEX_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"cmm",           LyXFont::CMM_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"cmr",           LyXFont::CMR_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"cmsy",          LyXFont::CMSY_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"eufrak",        LyXFont::EUFRAK_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"msa",           LyXFont::MSA_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"msb",           LyXFont::MSB_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"wasy",          LyXFont::WASY_FAMILY, inh_series,
+                         inh_shape, LColor::none},
+
+       // Text fonts
+       {"text",          inh_family, inh_series,
+                         inh_shape, LColor::foreground},
+       {"textbf",        inh_family, LyXFont::BOLD_SERIES,
+                         inh_shape, LColor::foreground},
+       {"textit",        inh_family, inh_series,
+                         LyXFont::ITALIC_SHAPE, LColor::foreground},
+       {"textmd",        inh_family, LyXFont::MEDIUM_SERIES,
+                         inh_shape, LColor::foreground},
+       {"textnormal",    inh_family, inh_series,
+                         LyXFont::UP_SHAPE, LColor::foreground},
+       {"textrm",        LyXFont::ROMAN_FAMILY,
+                         inh_series, LyXFont::UP_SHAPE,LColor::foreground},
+       {"textsc",        inh_family, inh_series,
+                         LyXFont::SMALLCAPS_SHAPE, LColor::foreground},
+       {"textsf",        LyXFont::SANS_FAMILY, inh_series,
+                         inh_shape, LColor::foreground},
+       {"textsl",        inh_family, inh_series,
+                         LyXFont::SLANTED_SHAPE, LColor::foreground},
+       {"texttt",        LyXFont::TYPEWRITER_FAMILY, inh_series,
+                         inh_shape, LColor::foreground},
+       {"textup",        inh_family, inh_series,
+                         LyXFont::UP_SHAPE, LColor::foreground},
 
        // TIPA support
-       {"textipa",   inh_family, inh_series, inh_shape, LColor::black},
-
-       {"lyxtex", inh_family, inh_series, inh_shape, LColor::latex},
-       {"lyxert", LyXFont::TYPEWRITER_FAMILY, inh_series, inh_shape, LColor::latex},
-       {"lyxsymbol", LyXFont::SYMBOL_FAMILY, inh_series, inh_shape, LColor::math},
-       {"lyxboldsymbol",
-               LyXFont::SYMBOL_FAMILY, LyXFont::BOLD_SERIES, inh_shape, LColor::math},
-       {"lyxitsymbol", LyXFont::SYMBOL_FAMILY,
-               inh_series, LyXFont::ITALIC_SHAPE, LColor::math},
-       {"lyxblacktext", LyXFont::ROMAN_FAMILY,
-               LyXFont::MEDIUM_SERIES, LyXFont::UP_SHAPE, LColor::black},
-       {"lyxnochange", inh_family, inh_series, inh_shape, LColor::black},
-
-       {"lyxfakebb", LyXFont::TYPEWRITER_FAMILY, LyXFont::BOLD_SERIES,
-               LyXFont::UP_SHAPE, LColor::math},
-       {"lyxfakecal", LyXFont::SANS_FAMILY, LyXFont::MEDIUM_SERIES,
-               LyXFont::ITALIC_SHAPE, LColor::math},
-       {"lyxfakefrak", LyXFont::ROMAN_FAMILY, LyXFont::BOLD_SERIES,
-               LyXFont::ITALIC_SHAPE, LColor::math}
+       {"textipa",       inh_family, inh_series,
+                         inh_shape, LColor::foreground},
+
+       // LyX internal usage
+       {"lyxtex",        inh_family, inh_series,
+                         LyXFont::UP_SHAPE, LColor::latex},
+       {"lyxsymbol",     LyXFont::SYMBOL_FAMILY, inh_series,
+                         inh_shape, LColor::math},
+       {"lyxboldsymbol", LyXFont::SYMBOL_FAMILY, LyXFont::BOLD_SERIES,
+                         inh_shape, LColor::math},
+       {"lyxblacktext",  LyXFont::ROMAN_FAMILY, LyXFont::MEDIUM_SERIES,
+                         LyXFont::UP_SHAPE, LColor::foreground},
+       {"lyxnochange",   inh_family, inh_series,
+                         inh_shape, LColor::foreground},
+       {"lyxfakebb",     LyXFont::TYPEWRITER_FAMILY, LyXFont::BOLD_SERIES,
+                         LyXFont::UP_SHAPE, LColor::math},
+       {"lyxfakecal",    LyXFont::SANS_FAMILY, LyXFont::MEDIUM_SERIES,
+                         LyXFont::ITALIC_SHAPE, LColor::math},
+       {"lyxfakefrak",   LyXFont::ROMAN_FAMILY, LyXFont::BOLD_SERIES,
+                         LyXFont::ITALIC_SHAPE, LColor::math}
 };
 
 
 fontinfo * lookupFont(string const & name)
 {
-       //lyxerr << "searching font '" << name << "'\n";
+       //lyxerr << "searching font '" << name << "'" << endl;
        int const n = sizeof(fontinfos) / sizeof(fontinfo);
        for (int i = 0; i < n; ++i)
                if (fontinfos[i].cmd_ == name) {
-                       //lyxerr << "found '" << i << "'\n";
+                       //lyxerr << "found '" << i << "'" << endl;
                        return fontinfos + i;
                }
        return 0;
@@ -626,7 +650,8 @@ void fakeFont(string const & orig, string const & fake)
                forig->shape_  = ffake->shape_;
                forig->color_  = ffake->color_;
        } else {
-               lyxerr << "Can't fake font '" << orig << "' with '" << fake << "'\n";
+               lyxerr << "Can't fake font '" << orig << "' with '"
+                      << fake << "'" << endl;
        }
 }
 
@@ -652,3 +677,36 @@ void augmentFont(LyXFont & font, string const & name)
        if (info->color_ != LColor::none)
                font.setColor(info->color_);
 }
+
+
+string asString(MathArray const & ar)
+{
+       std::ostringstream os;
+       WriteStream ws(os);
+       ws << ar;
+       return os.str();
+}
+
+
+void asArray(string const & str, MathArray & ar)
+{
+       mathed_parse_cell(ar, str);
+}
+
+
+string asString(MathInset const & inset)
+{
+       std::ostringstream os;
+       WriteStream ws(os);
+       inset.write(ws);
+       return os.str();
+}
+
+
+string asString(MathAtom const & at)
+{
+       std::ostringstream os;
+       WriteStream ws(os);
+       at->write(ws);
+       return os.str();
+}