]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathFont.cpp
Remove hardcoded values
[lyx.git] / src / mathed / InsetMathFont.cpp
index 93a266553759d0b4530d893993e7028dacf761d0..05f9e66e73b081b51fa5653343dfa75d8ff3abd9 100644 (file)
 #include "MathParser.h"
 #include "MetricsInfo.h"
 
+#include "support/gettext.h"
+#include "support/lassert.h"
+#include "support/lstrings.h"
+
 #include <ostream>
 
+using namespace lyx::support;
 
 namespace lyx {
 
@@ -34,6 +39,13 @@ Inset * InsetMathFont::clone() const
 }
 
 
+std::string InsetMathFont::font() const
+{
+       LASSERT(isAscii(key_->name), return "mathnormal");
+       return to_ascii(key_->name);
+}
+
+
 InsetMath::mode_type InsetMathFont::currentMode() const
 {
        if (key_->extra == "mathmode")
@@ -52,20 +64,37 @@ bool InsetMathFont::lockedMode() const
 }
 
 
+void InsetMathFont::write(WriteStream & os) const
+{
+       // Close the mode changing command inserted during export if
+       // we are going to output another mode changing command that
+       // actually doesn't change mode. This avoids exporting things
+       // such as \ensuremath{a\mathit{b}} or \textit{a\text{b}} and
+       // produce instead \ensuremath{a}\mathit{b} and \textit{a}\text{b}.
+       if (os.pendingBrace()
+           && ((currentMode() == TEXT_MODE && os.textMode())
+                   || (currentMode() == MATH_MODE && !os.textMode()))) {
+               os.os() << '}';
+               os.pendingBrace(false);
+               os.textMode(!os.textMode());
+       }
+       InsetMathNest::write(os);
+}
+
+
 void InsetMathFont::metrics(MetricsInfo & mi, Dimension & dim) const
 {
-       FontSetChanger dummy(mi.base, key_->name);
+       Changer dummy = mi.base.changeFontSet(font());
        cell(0).metrics(mi, dim);
-       metricsMarkers(dim);
+       metricsMarkers(mi, dim);
 }
 
 
 void InsetMathFont::draw(PainterInfo & pi, int x, int y) const
 {
-       FontSetChanger dummy(pi.base, key_->name.c_str());
+       Changer dummy = pi.base.changeFontSet(font());
        cell(0).draw(pi, x + 1, y);
        drawMarkers(pi, x, y);
-       setPosCache(pi, x, y);
 }
 
 
@@ -92,20 +121,23 @@ docstring InsetMathFont::name() const
 void InsetMathFont::validate(LaTeXFeatures & features) const
 {
        InsetMathNest::validate(features);
+       std::string fontname = font();
        if (features.runparams().isLaTeX()) {
                // Make sure amssymb is put in preamble if Blackboard Bold or
                // Fraktur used:
-               if (key_->name == "mathfrak" || key_->name == "mathbb")
+               if (fontname == "mathfrak" || fontname == "mathbb")
                        features.require("amssymb");
-               if (key_->name == "text" || key_->name == "textnormal"
-                               || (key_->name.length() == 6 && key_->name.substr(0, 4) == "text"))
+               if (fontname == "text" || fontname == "textnormal"
+                   || (fontname.length() == 6 && fontname.substr(0, 4) == "text"))
                        features.require("amstext");
-               if (key_->name == "textipa")
+               if (fontname == "mathscr")
+                       features.require("mathrsfs");
+               if (fontname == "textipa")
                        features.require("tipa");
-               if (key_->name == "ce" || key_->name == "cf")
+               if (fontname == "ce" || fontname == "cf")
                        features.require("mhchem");
        } else if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
-               features.addPreambleSnippet("<style type=\"text/css\">\n"
+               features.addCSSSnippet(
                        "span.normal{font: normal normal normal inherit serif;}\n"
                        "span.fraktur{font: normal normal normal inherit cursive;}\n"
                        "span.bold{font: normal normal bold inherit serif;}\n"
@@ -113,8 +145,7 @@ void InsetMathFont::validate(LaTeXFeatures & features) const
                        "span.italic{font: italic normal normal inherit serif;}\n"
                        "span.sans{font: normal normal normal inherit sans-serif;}\n"
                        "span.monospace{font: normal normal normal inherit monospace;}\n"
-                       "span.noun{font: normal small-caps normal inherit normal;}\n"
-                       "</style>");
+                       "span.noun{font: normal small-caps normal inherit normal;}");
        }
 }
 
@@ -138,7 +169,7 @@ void InsetMathFont::htmlize(HtmlStream & os) const
                 || tag == "textbf")
                variant = "bold";
        else if (tag == "mathcal")
-               variant == "script";
+               variant = "script";
        else if (tag == "mathit" || tag == "textsl"
                 || tag == "emph" || tag == "textit")
                variant = "italic";
@@ -148,12 +179,12 @@ void InsetMathFont::htmlize(HtmlStream & os) const
                variant = "monospace";
        else if (tag == "textipa" || tag == "textsc" || tag == "noun")
                variant = "noun";
-       
+
        docstring const beg = (tag.size() < 4) ? from_ascii("") : tag.substr(0, 4);
-       bool const textmode = (beg == "text");
        if (!variant.empty()) {
-               SetHTMLMode sm(os, textmode, "class='" + variant + "'");
-               os << cell(0);
+               os << MTag("span", "class='" + variant + "'")
+                  << cell(0)
+                  << ETag("span");
        } else
                os << cell(0);
 }
@@ -178,7 +209,7 @@ void InsetMathFont::mathmlize(MathStream & os) const
                 || tag == "textbf")
                variant = "bold";
        else if (tag == "mathcal")
-               variant == "script";
+               variant = "script";
        else if (tag == "mathit" || tag == "textsl"
                 || tag == "emph" || tag == "textit")
                variant = "italic";
@@ -187,13 +218,17 @@ void InsetMathFont::mathmlize(MathStream & os) const
        else if (tag == "mathtt" || tag == "texttt")
                variant = "monospace";
        // no support at present for textipa, textsc, noun
-       
-       docstring const beg = (tag.size() < 4) ? from_ascii("") : tag.substr(0, 4);
-       bool const textmode = (beg == "text");
+
        if (!variant.empty()) {
-               std::string const attrs = "mathvariant='" + variant + "'";
-               SetMode sm(os, textmode, attrs);
-               os << cell(0);
+               if (tag == "mathbb") {
+                       os << MTag("mstyle", "class='mathbb' mathvariant='" + variant + "'")
+                          << cell(0)
+                          << ETag("mstyle");
+               } else {
+                       os << MTag("mstyle", "mathvariant='" + variant + "'")
+                                << cell(0)
+                                << ETag("mstyle");
+               }
        } else
                os << cell(0);
 }
@@ -201,7 +236,7 @@ void InsetMathFont::mathmlize(MathStream & os) const
 
 void InsetMathFont::infoize(odocstream & os) const
 {
-       os << "Font: " << key_->name;
+       os << bformat(_("Font: %1$s"), key_->name);
 }