]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathFactory.cpp
Native support for \smash[t] and \smash[b]
[lyx.git] / src / mathed / MathFactory.cpp
index 160196c643697154c441061b37baa7202bf96559..85fdf478565f6fa6ad11d4562161f6ec55839932 100644 (file)
@@ -32,6 +32,7 @@
 #include "InsetMathPhantom.h"
 #include "InsetMathRef.h"
 #include "InsetMathRoot.h"
+#include "InsetMathSideset.h"
 #include "InsetMathSize.h"
 #include "InsetMathSpace.h"
 #include "InsetMathSpecialChar.h"
@@ -62,6 +63,7 @@
 #include "support/FileName.h"
 #include "support/filetools.h" // LibFileSearch
 #include "support/lstrings.h"
+#include "support/textutils.h"
 
 #include "frontends/FontLoader.h"
 
@@ -108,6 +110,37 @@ bool isMathFontAvailable(docstring & name)
 }
 
 
+bool canBeDisplayed(char_type c)
+{
+       if (!use_gui)
+               return true;
+       return theFontLoader().canBeDisplayed(c);
+}
+
+
+bool isUnicodeSymbolAvailable(docstring const & /*name*/, char_type & /*c*/)
+{
+       // this is too fragile, e.g. prodes W instead of capital omega on OS X
+#if 0
+       docstring cmd(from_ascii("\\") + name);
+       bool is_combining;
+       bool termination;
+       c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
+                                       is_combining, termination);
+       if (c == 0 && name == "varOmega") {
+               // fallback for bug 7954, unicodesymbols does not list
+               // \\varOmega because of requirements, but this might change
+               cmd = from_ascii("\\mathit{\\Omega}");
+               c = Encodings::fromLaTeXCommand(cmd, Encodings::MATH_CMD,
+                                               is_combining, termination);
+       }
+       return c != 0 && !is_combining;
+#else
+       return false;
+#endif
+}
+
+
 void initSymbols()
 {
        FileName const filename = libFileSearch(string(), "symbols");
@@ -151,7 +184,15 @@ void initSymbols()
                        string macro;
                        string requires;
                        is >> macro >> requires;
-                       MacroTable::globalMacros().insert(0, from_utf8(macro), requires);
+                       MacroTable::iterator it = MacroTable::globalMacros().insert(
+                                       0, from_utf8(macro), requires);
+                       // If you change the following output, please adjust
+                       // development/tools/generate_symbols_images.py.
+                       LYXERR(Debug::MATHED, "read symbol '" << to_utf8(it->first)
+                               << "  inset: macro"
+                               << "  draw: 0"
+                               << "  extra: "
+                               << "  requires: " << requires << '\'');
                        continue;
                }
 
@@ -191,20 +232,26 @@ void initSymbols()
 
                        // symbol font is not available sometimes
                        docstring symbol_font = from_ascii("lyxsymbol");
+                       char_type unicodesymbol = 0;
 
                        if (tmp.extra == "func" || tmp.extra == "funclim" || tmp.extra == "special") {
                                LYXERR(Debug::MATHED, "symbol abuse for " << to_utf8(tmp.name));
                                tmp.draw = tmp.name;
-                       } else if (isMathFontAvailable(tmp.inset)) {
+                       } else if (isMathFontAvailable(tmp.inset) && canBeDisplayed(charid)) {
                                LYXERR(Debug::MATHED, "symbol available for " << to_utf8(tmp.name));
                                tmp.draw.push_back(char_type(charid));
-                       } else if (fallbackid && isMathFontAvailable(symbol_font)) {
+                       } else if (fallbackid && isMathFontAvailable(symbol_font) &&
+                                  canBeDisplayed(fallbackid)) {
                                if (tmp.inset == "cmex")
                                        tmp.inset = from_ascii("lyxsymbol");
                                else
                                        tmp.inset = from_ascii("lyxboldsymbol");
                                LYXERR(Debug::MATHED, "symbol fallback for " << to_utf8(tmp.name));
                                tmp.draw.push_back(char_type(fallbackid));
+                       } else if (isUnicodeSymbolAvailable(tmp.name, unicodesymbol)) {
+                               LYXERR(Debug::MATHED, "unicode fallback for " << to_utf8(tmp.name));
+                               tmp.inset = from_ascii("mathnormal");
+                               tmp.draw.push_back(unicodesymbol);
                        } else {
                                LYXERR(Debug::MATHED, "faking " << to_utf8(tmp.name));
                                tmp.draw = tmp.name;
@@ -222,6 +269,8 @@ void initSymbols()
                else
                        theMathWordList[tmp.name] = tmp;
 
+               // If you change the following output, please adjust
+               // development/tools/generate_symbols_images.py.
                LYXERR(Debug::MATHED, "read symbol '" << to_utf8(tmp.name)
                        << "  inset: " << to_utf8(tmp.inset)
                        << "  draw: " << int(tmp.draw.empty() ? 0 : tmp.draw[0])
@@ -344,10 +393,6 @@ MathAtom createInsetMath(docstring const & s, Buffer * buf)
                if (inset == "dots")
                        return MathAtom(new InsetMathDots(l));
                if (inset == "mbox")
-                       // return MathAtom(new InsetMathMBox);
-                       // InsetMathMBox is proposed to replace InsetMathBox,
-                       // but is not ready yet (it needs a BufferView for
-                       // construction)
                        return MathAtom(new InsetMathBox(buf, l->name));
 //             if (inset == "fbox")
 //                     return MathAtom(new InsetMathFBox(l));
@@ -443,7 +488,10 @@ MathAtom createInsetMath(docstring const & s, Buffer * buf)
        if (s == "tabular")
                return MathAtom(new InsetMathTabular(buf, s, 1, 1));
        if (s == "stackrel")
-               return MathAtom(new InsetMathStackrel(buf));
+               return MathAtom(new InsetMathStackrel(buf, false));
+       // This string value is only for math toolbar use, no LaTeX name
+       if (s == "stackrelthree")
+               return MathAtom(new InsetMathStackrel(buf, true));
        if (s == "binom")
                return MathAtom(new InsetMathBinom(buf, InsetMathBinom::BINOM));
        if (s == "dbinom")
@@ -513,6 +561,11 @@ MathAtom createInsetMath(docstring const & s, Buffer * buf)
                return MathAtom(new InsetMathCancelto(buf));
        if (s == "smash")
                return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smash));
+       // The following 2 string values are only for math toolbar use, no LaTeX names
+       if (s == "smashb")
+               return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smashb));
+       if (s == "smasht")
+               return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::smasht));
        if (s == "mathclap")
                return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathclap));
        if (s == "mathllap")
@@ -521,6 +574,15 @@ MathAtom createInsetMath(docstring const & s, Buffer * buf)
                return MathAtom(new InsetMathPhantom(buf, InsetMathPhantom::mathrlap));
        if (s == "ensuremath")
                return MathAtom(new InsetMathEnsureMath(buf));
+       if (s == "sideset")
+               return MathAtom(new InsetMathSideset(buf, true, true));
+       // The following 3 string values are only for math toolbar use, no LaTeX names
+       if (s == "sidesetr")
+               return MathAtom(new InsetMathSideset(buf, false, true));
+       if (s == "sidesetl")
+               return MathAtom(new InsetMathSideset(buf, true, false));
+       if (s == "sidesetn")
+               return MathAtom(new InsetMathSideset(buf, false, false));
        if (isSpecialChar(s))
                return MathAtom(new InsetMathSpecialChar(s));
        if (s == " ")
@@ -575,7 +637,7 @@ bool createInsetMath_fromDialogStr(docstring const & str, MathData & ar)
 
 bool isAsciiOrMathAlpha(char_type c)
 {
-       return c < 0x80 || Encodings::isMathAlpha(c);
+       return isASCII(c) || Encodings::isMathAlpha(c);
 }