]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/math_support.C
whichFont down to 5.3%
[lyx.git] / src / mathed / math_support.C
index 30f21f4ed522ec6fc89341d92be5a8c4e9f10fbc..8bea104b7d35149920c49c1c57dea63906b26f36 100644 (file)
@@ -6,11 +6,13 @@
 #include "lyxfont.h"
 #include "FontLoader.h"
 #include "font.h"
+#include "math_cursor.h"
 #include "math_defs.h"
 #include "math_inset.h"
 #include "math_parser.h"
 #include "Painter.h"
 #include "debug.h"
+#include "commandtags.h"
 
 using std::map;
 using std::endl;
@@ -67,8 +69,6 @@ enum MathFont {
        FONT_SYMBOL,
        FONT_SYMBOLI,
        FONT_BF,
-       FONT_BB,
-       FONT_CAL,
        FONT_TT,
        FONT_RM,
        FONT_SF,
@@ -78,17 +78,16 @@ enum MathFont {
        FONT_CMEX,
        FONT_MSA,
        FONT_MSB,
+       FONT_EUFRAK,
+       FONT_FAKEBB,
+       FONT_FAKECAL,
+       FONT_FAKEFRAK,
        FONT_NUM
 };
 
 void mathed_init_fonts()
 {
-       MathFonts = new LyXFont[FONT_NUM]; //DEC cxx cannot initialize all fonts
-       //at once (JMarc) rc
-
-       for (int i = 0 ; i < FONT_NUM ; ++i) {
-               MathFonts[i] = LyXFont(LyXFont::ALL_SANE);
-       }
+       MathFonts = new LyXFont[FONT_NUM];
 
        MathFonts[FONT_IT].setShape(LyXFont::ITALIC_SHAPE);
 
@@ -99,16 +98,8 @@ void mathed_init_fonts()
 
        MathFonts[FONT_BF].setSeries(LyXFont::BOLD_SERIES);
 
-       MathFonts[FONT_BB].setSeries(LyXFont::BOLD_SERIES);
-       MathFonts[FONT_BB].setFamily(LyXFont::TYPEWRITER_FAMILY);
-
-       MathFonts[FONT_CAL].setFamily(LyXFont::SANS_FAMILY);
-       MathFonts[FONT_CAL].setShape(LyXFont::ITALIC_SHAPE);
-
        MathFonts[FONT_TT].setFamily(LyXFont::TYPEWRITER_FAMILY);
-
        MathFonts[FONT_RM].setFamily(LyXFont::ROMAN_FAMILY);
-
        MathFonts[FONT_SF].setFamily(LyXFont::SANS_FAMILY);
 
        MathFonts[FONT_CMR].setFamily(LyXFont::CMR_FAMILY);
@@ -117,13 +108,23 @@ void mathed_init_fonts()
        MathFonts[FONT_CMEX].setFamily(LyXFont::CMEX_FAMILY);
        MathFonts[FONT_MSA].setFamily(LyXFont::MSA_FAMILY);
        MathFonts[FONT_MSB].setFamily(LyXFont::MSB_FAMILY);
+       MathFonts[FONT_EUFRAK].setFamily(LyXFont::EUFRAK_FAMILY);
+
+       MathFonts[FONT_FAKEBB].setFamily(LyXFont::TYPEWRITER_FAMILY);
+       MathFonts[FONT_FAKEBB].setSeries(LyXFont::BOLD_SERIES);
+
+       MathFonts[FONT_FAKECAL].setFamily(LyXFont::SANS_FAMILY);
+       MathFonts[FONT_FAKECAL].setShape(LyXFont::ITALIC_SHAPE);
+
+       MathFonts[FONT_FAKEFRAK].setFamily(LyXFont::SANS_FAMILY);
+       MathFonts[FONT_FAKEFRAK].setSeries(LyXFont::BOLD_SERIES);
 
        for (int i = 0; i < LM_FONT_END; ++i)
                font_available_initialized[i] = false;
 }
 
 
-LyXFont const & whichFontBase(MathTextCodes type)
+LyXFont const & whichFontBaseIntern(MathTextCodes type)
 {
        if (!MathFonts)
                mathed_init_fonts();
@@ -141,20 +142,15 @@ LyXFont const & whichFontBase(MathTextCodes type)
                return MathFonts[FONT_BF];
 
        case LM_TC_BB:
-               if (math_font_available(LM_TC_MSB))
-                       return MathFonts[FONT_MSB];
-               else
-                       return MathFonts[FONT_BB];
+               return MathFonts[FONT_MSB];
 
        case LM_TC_CAL:
-               if (math_font_available(LM_TC_CMSY))
-                       return MathFonts[FONT_CMSY];
-               else
-                       return MathFonts[FONT_CAL];
+               return MathFonts[FONT_CMSY];
 
        case LM_TC_TT:
                return MathFonts[FONT_TT];
 
+       case LM_TC_BOX:
        case LM_TC_TEXTRM:
        case LM_TC_CONST:
        case LM_TC_TEX:
@@ -182,6 +178,9 @@ LyXFont const & whichFontBase(MathTextCodes type)
        case LM_TC_MSB:
                return MathFonts[FONT_MSB];
 
+       case LM_TC_EUFRAK:
+               return MathFonts[FONT_EUFRAK];
+
        default:
                break;
        }
@@ -189,9 +188,40 @@ LyXFont const & whichFontBase(MathTextCodes type)
 }
 
 
-LyXFont whichFont(MathTextCodes type, MathMetricsInfo const & size)
+LyXFont const & whichFontBase(MathTextCodes type)
+{
+       if (!MathFonts)
+               mathed_init_fonts();
+
+       switch (type) {
+       case LM_TC_BB:
+               if (math_font_available(LM_TC_MSB))
+                       return MathFonts[FONT_MSB];
+               else
+                       return MathFonts[FONT_FAKEBB];
+
+       case LM_TC_CAL:
+               if (math_font_available(LM_TC_CMSY))
+                       return MathFonts[FONT_CMSY];
+               else
+                       return MathFonts[FONT_FAKECAL];
+
+       case LM_TC_EUFRAK:
+               if (math_font_available(LM_TC_EUFRAK))
+                       return MathFonts[FONT_EUFRAK];
+               else
+                       return MathFonts[FONT_FAKEFRAK];
+
+       default:
+               break;
+       }
+       return whichFontBaseIntern(type);
+}
+
+
+void whichFont(LyXFont & f, MathTextCodes type, MathMetricsInfo const & size)
 {
-       LyXFont f = whichFontBase(type);
+       f = whichFontBase(type);
        // use actual size
        f.setSize(size.font.size());
 
@@ -222,13 +252,11 @@ LyXFont whichFont(MathTextCodes type, MathMetricsInfo const & size)
                break;
        }
 
-       if (type != LM_TC_TEXTRM)
+       if (type != LM_TC_TEXTRM && type != LM_TC_BOX)
                f.setColor(LColor::math);
 
        if (type == LM_TC_TEX)
                f.setColor(LColor::latex);
-
-       return f;
 }
 
 } // namespace
@@ -238,7 +266,9 @@ bool math_font_available(MathTextCodes type)
 {
        if (!font_available_initialized[type]) {
                font_available_initialized[type] = true;
-               font_available[type] = fontloader.available(whichFontBase(type));
+               font_available[type] = fontloader.available(whichFontBaseIntern(type));
+               if (!font_available[type])
+                       lyxerr[Debug::FONT] << "Math font " << type << " not available.\n";
        }
        return font_available[type];
 }
@@ -362,9 +392,17 @@ double const hline[] = {
 };
 
 
-double const hline2[] = {
-       1, 0.1, 0.5,  0.3, 0.5,
-       1, 0.7, 0.5,  0.9, 0.5,
+double const ddot[] = {
+       1, 0.2, 0.5,  0.3, 0.5,
+       1, 0.7, 0.5,  0.8, 0.5,
+       0
+};
+
+
+double const dddot[] = {
+       1, 0.1, 0.5,  0.2, 0.5,
+       1, 0.45, 0.5, 0.55, 0.5,
+       1, 0.8, 0.5,  0.9, 0.5,
        0
 };
 
@@ -426,13 +464,20 @@ named_deco_struct deco_table[] = {
        // Decorations
        {"widehat",        angle,      3 },
        {"widetilde",      tilde,      0 },
+       {"underbar",       hline,      0 },
        {"underline",      hline,      0 },
        {"overline",       hline,      0 },
        {"underbrace",     brace,      1 },
        {"overbrace",      brace,      3 },
        {"overleftarrow",  arrow,      1 },
        {"overrightarrow", arrow,      3 },
-       
+       {"overleftrightarrow", udarrow, 1 },
+       {"xleftarrow",     arrow,      1 },
+       {"xrightarrow",    arrow,      3 },
+       {"underleftarrow", arrow,      1 },
+       {"underrightarrow", arrow,     3 },
+       {"underleftrightarrow",udarrow, 1 },
+
        // Delimiters
        {"(",              parenth,    0 },
        {")",              parenth,    2 },
@@ -459,7 +504,8 @@ named_deco_struct deco_table[] = {
        {"Updownarrow",    Udarrow,    0 },     
        
        // Accents
-       {"ddot",           hline2,     0 },
+       {"ddot",           ddot,       0 },
+       {"dddot",          dddot,      0 },
        {"hat",            angle,      3 },
        {"grave",          slash,      1 },
        {"acute",          slash,      0 },
@@ -510,7 +556,8 @@ deco_struct const * search_deco(string const & name)
 void mathed_char_dim(MathTextCodes type, MathMetricsInfo const & size,
        unsigned char c, int & asc, int & des, int & wid)
 {
-       LyXFont const font = whichFont(type, size);
+       LyXFont font;
+       whichFont(font, type, size);
        des = lyxfont::descent(c, font);
        asc = lyxfont::ascent(c, font);
        wid = mathed_char_width(type, size, c);
@@ -520,7 +567,8 @@ void mathed_char_dim(MathTextCodes type, MathMetricsInfo const & size,
 int mathed_char_height(MathTextCodes type, MathMetricsInfo const & size,
        unsigned char c, int & asc, int & des)
 {
-       LyXFont const font = whichFont(type, size);
+       LyXFont font;
+       whichFont(font, type, size);
        des = lyxfont::descent(c, font);
        asc = lyxfont::ascent(c, font);
        return asc + des;
@@ -539,7 +587,8 @@ int mathed_char_height(MathTextCodes type, MathMetricsInfo const & size,
 int mathed_char_ascent(MathTextCodes type, MathMetricsInfo const & size,
        unsigned char c)
 {
-       LyXFont const font = whichFont(type, size);
+       LyXFont font;
+       whichFont(font, type, size);
        return lyxfont::ascent(c, font);
 }
 
@@ -547,7 +596,8 @@ int mathed_char_ascent(MathTextCodes type, MathMetricsInfo const & size,
 int mathed_char_descent(MathTextCodes type, MathMetricsInfo const & size,
        unsigned char c)
 {
-       LyXFont const font = whichFont(type, size);
+       LyXFont font;
+       whichFont(font, type, size);
        return lyxfont::descent(c, font);
 }
 
@@ -555,7 +605,8 @@ int mathed_char_descent(MathTextCodes type, MathMetricsInfo const & size,
 int mathed_char_width(MathTextCodes type, MathMetricsInfo const & size,
        unsigned char c)
 {
-       LyXFont const font = whichFont(type, size);
+       LyXFont font;
+       whichFont(font, type, size);
        if (isBinaryOp(c, type))
                return lyxfont::width(c, font) + 2 * lyxfont::width(' ', font);
        else
@@ -574,7 +625,8 @@ void mathed_string_dim(MathTextCodes type, MathMetricsInfo const & size,
 int mathed_string_height(MathTextCodes type, MathMetricsInfo const & size,
        string const & s, int & asc, int & des)
 {
-       LyXFont const font = whichFont(type, size);
+       LyXFont font;
+       whichFont(font, type, size);
        asc = des = 0;
        for (string::const_iterator it = s.begin(); it != s.end(); ++it) {
                des = max(des, lyxfont::descent(*it, font));
@@ -587,14 +639,17 @@ int mathed_string_height(MathTextCodes type, MathMetricsInfo const & size,
 int mathed_string_width(MathTextCodes type, MathMetricsInfo const & size,
        string const & s)
 {
-       return lyxfont::width(s, whichFont(type, size));
+       LyXFont font;
+       whichFont(font, type, size);
+       return lyxfont::width(s, font);
 }
 
 
 int mathed_string_ascent(MathTextCodes type, MathMetricsInfo const & size,
        string const & s)
 {
-       LyXFont const font = whichFont(type, size);
+       LyXFont font;
+       whichFont(font, type, size);
        int asc = 0;
        for (string::const_iterator it = s.begin(); it != s.end(); ++it)
                asc = max(asc, lyxfont::ascent(*it, font));
@@ -605,7 +660,8 @@ int mathed_string_ascent(MathTextCodes type, MathMetricsInfo const & size,
 int mathed_string_descent(MathTextCodes type, MathMetricsInfo const & size,
        string const & s)
 {
-       LyXFont const font = whichFont(type, size);
+       LyXFont font;
+       whichFont(font, type, size);
        int des = 0;
        for (string::const_iterator it = s.begin(); it != s.end(); ++it)
                des = max(des, lyxfont::descent(*it, font));
@@ -682,15 +738,25 @@ void mathed_draw_deco(Painter & pain, int x, int y, int w, int h,
 }
 
 
+void mathed_draw_framebox(Painter & pain, int x, int y, MathInset const * p)
+{
+       if (mathcursor && mathcursor->isInside(p))
+               pain.rectangle(x, y - p->ascent(), p->width(), p->height(),
+                       LColor::mathframe);
+}
+
+
 // In the future maybe we use a better fonts renderer
-void drawStr(Painter & pain, MathTextCodes type, MathMetricsInfo const & siz,
-       int x, int y, string const & s)
+void drawStr(Painter & pain, MathTextCodes type, MathMetricsInfo const & size,
+       int x, int y, string const & str)
 {
-       pain.text(x, y, s, whichFont(type, siz));
+       LyXFont font;
+       whichFont(font, type, size);
+       pain.text(x, y, str, font);
 }
 
 
-void drawChar(Painter & pain, MathTextCodes type, MathMetricsInfo const & siz,
+void drawChar(Painter & pain, MathTextCodes type, MathMetricsInfo const & size,
        int x, int y, char c)
 {
        string s;
@@ -699,7 +765,7 @@ void drawChar(Painter & pain, MathTextCodes type, MathMetricsInfo const & siz,
        s += c;
        if (isBinaryOp(c, type))
                s += ' ';
-       drawStr(pain, type, siz, x, y, s);
+       drawStr(pain, type, size, x, y, s);
 }
 
 
@@ -725,10 +791,11 @@ void smallerStyleFrac(MathMetricsInfo & st)
 }
 
 
-void math_font_max_dim(MathTextCodes code, MathMetricsInfo const & siz,
+void math_font_max_dim(MathTextCodes code, MathMetricsInfo const & size,
        int & asc, int & des)
 {
-       LyXFont font = whichFont(code, siz);
+       LyXFont font;
+       whichFont(font, code, size);
        asc = lyxfont::maxAscent(font);
        des = lyxfont::maxDescent(font);
 }
@@ -739,11 +806,13 @@ char const * latex_mathspace[] = {
 };
 
 
+
 char const * math_font_name(MathTextCodes code)
 {
        static char const * theFontNames[] = {
                "mathrm",
                "mathcal",
+               "mathfrak",
                "mathbf",
                "mathbb",
                "mathsf",
@@ -756,3 +825,24 @@ char const * math_font_name(MathTextCodes code)
                return theFontNames[code - LM_TC_RM];
        return 0;
 }
+
+string convertDelimToLatexName(string const & name)
+{
+       if (name == "(")
+               return name;
+       if (name == "[")
+               return name;
+       if (name == ".")
+               return name;
+       if (name == ")")
+               return name;
+       if (name == "]")
+               return name;
+       if (name == "/")
+               return name;
+       if (name == "|")
+               return name;
+       return "\\" + name + " ";
+}
+
+