]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathChar.cpp
g-brief loads babel internally. So don't load it ourselves.
[lyx.git] / src / mathed / InsetMathChar.cpp
index 36bdeaa2ba436f685c6f50af6d47e585d8dc7656..a642a5e6da14e8b02f16f9fbf1efbd9c71be8d95 100644 (file)
@@ -26,7 +26,6 @@
 #include "frontends/FontMetrics.h"
 
 #include "support/debug.h"
-#include "support/lstrings.h"
 #include "support/textutils.h"
 
 #include <algorithm>
@@ -36,8 +35,6 @@ using namespace std;
 
 namespace lyx {
 
-extern bool has_math_fonts;
-
 
 namespace {
 latexkeys const * makeSubstitute(char_type c)
@@ -113,13 +110,18 @@ void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
        if (isMathFont(f) && subst_) {
                // If the char has a substitute, draw the replacement symbol
                // instead, but only in math mode.
-               mathedSymbolDim(mi.base, dim, subst_);
-               kerning_ = mathed_char_kerning(mi.base.font, *subst_->draw.rbegin());
-               return;
+               kerning_ = mathedSymbolDim(mi.base, dim, subst_);
        } else if (!slanted(char_) && f == "mathnormal") {
                Changer dummy = mi.base.font.changeShape(UP_SHAPE);
                dim = theFontMetrics(mi.base.font).dimension(char_);
                kerning_ = 0;
+       } else if (!isASCII(char_) && Encodings::unicodeCharInfo(char_).isUnicodeSymbol()) {
+               Changer dummy1 = mi.base.changeFontSet("mathnormal");
+               Changer dummy2 = Encodings::isMathAlpha(char_)
+                               ? noChange()
+                               : mi.base.font.changeShape(UP_SHAPE);
+               dim = theFontMetrics(mi.base.font).dimension(char_);
+               kerning_ = -mathed_char_kerning(mi.base.font, char_);
        } else {
                frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
                dim = fm.dimension(char_);
@@ -158,6 +160,13 @@ void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
                        Changer dummy = pi.base.font.changeShape(UP_SHAPE);
                        pi.draw(x, y, char_);
                        return;
+               } else if (!isASCII(char_) && Encodings::unicodeCharInfo(char_).isUnicodeSymbol()) {
+                       Changer dummy1 = pi.base.changeFontSet("mathnormal");
+                       Changer dummy2 = Encodings::isMathAlpha(char_)
+                                       ? noChange()
+                                       : pi.base.font.changeShape(UP_SHAPE);
+                       pi.draw(x, y, char_);
+                       return;
                }
        }
        // cmmi has almost no left bearing: simulate this
@@ -183,8 +192,13 @@ void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
 }
 
 
-void InsetMathChar::write(WriteStream & os) const
+void InsetMathChar::write(TeXMathStream & os) const
 {
+       if (os.latex() && os.pendingSpace()) {
+               if (isAlphaASCII(char_))
+                       os.os() << ' ';
+               os.pendingSpace(false);
+       }
        os.os().put(char_);
 }
 
@@ -216,7 +230,9 @@ void InsetMathChar::octave(OctaveStream & os) const
 // mathalpha, then we'll treat it as an identifier, otherwise as an
 // operator.
 // Worst case: We get bad spacing, or bad italics.
-void InsetMathChar::mathmlize(MathStream & ms) const
+// In any case, never let MathML stretch a single character when it
+// is recognised as an operator, to match TeX' behaviour.
+void InsetMathChar::mathmlize(MathMLStream & ms) const
 {
        std::string entity;
        switch (char_) {
@@ -224,10 +240,7 @@ void InsetMathChar::mathmlize(MathStream & ms) const
                case '>': entity = "&gt;"; break;
                case '&': entity = "&amp;"; break;
                case ' ': {
-                       if (ms.xmlMode())
-                               ms << from_ascii("&#0160;");
-                       else
-                               ms << from_ascii("&nbsp;");
+                       ms << from_ascii("&#0160;");
                        return;
                }
                default: break;
@@ -242,18 +255,18 @@ void InsetMathChar::mathmlize(MathStream & ms) const
        }
 
        if (!entity.empty()) {
-               ms << "<" << from_ascii(ms.namespacedTag("mo")) << ">"
+               ms << MTagInline("mo", "stretchy='false'")
                   << from_ascii(entity)
-                  << "</" << from_ascii(ms.namespacedTag("mo")) << ">";
+                  << ETagInline("mo");
                return;
        }
 
        char const * type =
                (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
                        ? "mi" : "mo";
-       // we don't use MTag and ETag because we do not want the spacing before the end tag.
-       docstring tag = from_ascii(ms.namespacedTag(type));
-       ms << "<" << tag << ">" << char_type(char_) << "</" << tag << ">";
+       ms << MTagInline(type, std::string(type) == "mo" ? "stretchy='false'" : "")
+          << char_type(char_)
+          << ETagInline(type);
 }
 
 
@@ -266,7 +279,7 @@ void InsetMathChar::htmlize(HtmlStream & ms) const
                case '<': entity = "&lt;"; break;
                case '>': entity = "&gt;"; break;
                case '&': entity = "&amp;"; break;
-               case ' ': entity = "&nbsp;"; break;
+               case ' ': entity = "&#160;"; break;
                default: break;
        }
 
@@ -298,16 +311,27 @@ void InsetMathChar::htmlize(HtmlStream & ms) const
 MathClass InsetMathChar::mathClass() const
 {
        // this information comes from fontmath.ltx in LaTeX source.
-       char const ch = static_cast<char>(char_);
        if (subst_)
                return string_to_class(subst_->extra);
-       else if (support::contains(",;", ch))
+
+       if (!isASCII(char_))
+               return MC_ORD;
+
+       switch (static_cast<char>(char_)) {
+       case ',':
+       case ';':
                return MC_PUNCT;
-       else if (support::contains("([", ch))
+       case '(':
+       case '[':
                return MC_OPEN;
-       else if (support::contains(")]!?", ch))
+       case ')':
+       case ']':
+       case '!':
+       case '?':
                return MC_CLOSE;
-       else return MC_ORD;
+       default:
+               return MC_ORD;
+       }
 }