2 * \file InsetMathChar.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
9 * Full author contact details are available in file CREDITS.
14 #include "InsetMathChar.h"
16 #include "MathParser.h"
17 #include "MathSupport.h"
18 #include "MathStream.h"
19 #include "MetricsInfo.h"
21 #include "Dimension.h"
22 #include "BufferEncodings.h"
23 #include "LaTeXFeatures.h"
24 #include "TextPainter.h"
26 #include "frontends/FontMetrics.h"
28 #include "support/debug.h"
29 #include "support/textutils.h"
40 latexkeys const * makeSubstitute(char_type c)
44 // Latex replaces ', *, -, and : with specific symbols. With unicode-math,
45 // these symbols are replaced respectively by ^U+2032, U+2217, U+2212 and
46 // U+2236 (the latter substitution can be turned off with a package
47 // option). Unicode-math also replaces ` with \backprime.
48 // prime needs to be placed in superscript unless an opentype font is used.
56 name = "lyxminus";// unicode-math: "minus"
59 name = "ordinarycolon";// unicode-math: "mathratio"
61 // The remaining replacements are not real character substitutions (from a
62 // unicode point of view) but are done here: 1. for cosmetic reasons, in the
63 // context of being stuck with CM fonts at the moment, to ensure consistency
64 // with related symbols: -, \leq, \geq, etc. 2. to get the proper spacing
65 // as defined in lib/symbols.
67 name = "lyxplus";//unicode-math: "mathplus"
70 name = "lyxgt";//unicode-math: "greater"
73 name = "lyxlt";//unicode-math: "less"
76 name = "lyxeqrel";//unicode-math: "equal"
78 //case ','://unicode-math: "mathcomma"
79 //case ';'://unicode-math: "mathsemicolon"
83 return in_word_set(from_ascii(name));
86 } //anonymous namespace
89 static bool slanted(char_type c)
91 return isAlphaASCII(c) || Encodings::isMathAlpha(c);
95 InsetMathChar::InsetMathChar(char_type c)
96 : char_(c), kerning_(0), subst_(makeSubstitute(c))
101 Inset * InsetMathChar::clone() const
103 return new InsetMathChar(*this);
107 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
109 string const & f = mi.base.fontname;
110 if (isMathFont(f) && subst_) {
111 // If the char has a substitute, draw the replacement symbol
112 // instead, but only in math mode.
113 kerning_ = mathedSymbolDim(mi.base, dim, subst_);
114 } else if (!slanted(char_) && f == "mathnormal") {
115 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
116 dim = theFontMetrics(mi.base.font).dimension(char_);
118 } else if (!isASCII(char_) && Encodings::unicodeCharInfo(char_).isUnicodeSymbol()) {
119 Changer dummy1 = mi.base.changeFontSet("mathnormal");
120 Changer dummy2 = Encodings::isMathAlpha(char_)
122 : mi.base.font.changeShape(UP_SHAPE);
123 dim = theFontMetrics(mi.base.font).dimension(char_);
124 kerning_ = -mathed_char_kerning(mi.base.font, char_);
126 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
127 dim = fm.dimension(char_);
128 kerning_ = mathed_char_kerning(mi.base.font, char_);
129 // cmmi has almost no left bearing: simulate this
130 if (f == "mathnormal") {
131 dim.wid += max(-fm.lbearing(char_), 0);
133 // Rule 17 from Appendix G
134 // These are the fonts with fontdimen(2)==0.
135 // To properly handle the case fontdimen(2)!=0 (that is for all other
136 // math fonts), where the italic correction must also be converted into
137 // kerning but only at the end of strings of characters with the same
138 // font, one would need a more elaborate implementation in MathRow. For
139 // now the case fontdimen(2)==0 is the most important.
140 if (f == "mathnormal" || f == "mathscr" || f == "mathcal") {
142 // We use a negative value to tell InsetMathScript to move the
143 // subscript leftwards instead of the superscript rightwards
144 kerning_ = -kerning_;
150 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
152 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
153 if (isMathFont(pi.base.fontname)) {
155 // If the char has a substitute, draw the replacement symbol
156 // instead, but only in math mode.
157 mathedSymbolDraw(pi, x, y, subst_);
159 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
160 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
161 pi.draw(x, y, char_);
163 } else if (!isASCII(char_) && Encodings::unicodeCharInfo(char_).isUnicodeSymbol()) {
164 Changer dummy1 = pi.base.changeFontSet("mathnormal");
165 Changer dummy2 = Encodings::isMathAlpha(char_)
167 : pi.base.font.changeShape(UP_SHAPE);
168 pi.draw(x, y, char_);
172 // cmmi has almost no left bearing: simulate this
173 if (pi.base.fontname == "mathnormal") {
174 x += max(-theFontMetrics(pi.base.font).lbearing(char_), 0);
176 pi.draw(x, y, char_);
180 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
188 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
190 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
191 pain.draw(x, y, char_);
195 void InsetMathChar::write(TeXMathStream & os) const
197 if (os.latex() && os.pendingSpace()) {
198 if (isAlphaASCII(char_))
200 os.pendingSpace(false);
206 void InsetMathChar::validate(LaTeXFeatures & features) const
209 BufferEncodings::validate(char_, features, true);
213 void InsetMathChar::normalize(NormalStream & os) const
221 void InsetMathChar::octave(OctaveStream & os) const
227 // We have a bit of a problem here. MathML wants to know whether the
228 // character represents an "identifier" or an "operator", and we have
229 // no general way of telling. So we shall guess: If it's alpha or
230 // mathalpha, then we'll treat it as an identifier, otherwise as an
232 // Worst case: We get bad spacing, or bad italics.
233 // In any case, never let MathML stretch a single character when it
234 // is recognised as an operator, to match TeX' behaviour.
235 void InsetMathChar::mathmlize(MathMLStream & ms) const
239 case '<': entity = "<"; break;
240 case '>': entity = ">"; break;
241 case '&': entity = "&"; break;
243 ms << from_ascii(" ");
253 ms << from_ascii(entity);
257 if (!entity.empty()) {
258 ms << MTagInline("mo", "stretchy='false'")
259 << from_ascii(entity)
265 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
267 ms << MTagInline(type, std::string(type) == "mo" ? "stretchy='false'" : "")
273 void InsetMathChar::htmlize(HtmlStream & ms) const
276 // Not taking subst_ into account here because the MathML output of
277 // <>=+-* looks correct as it is. FIXME: ' is not output as ^\prime
279 case '<': entity = "<"; break;
280 case '>': entity = ">"; break;
281 case '&': entity = "&"; break;
282 case ' ': entity = " "; break;
286 bool have_entity = !entity.empty();
290 ms << from_ascii(entity);
297 // an operator, so give some space
298 ms << ' ' << from_ascii(entity) << ' ';
302 if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
303 // we don't use MTag and ETag because we do not want the spacing
304 ms << MTag("i") << char_type(char_) << ETag("i");
306 // an operator, so give some space
307 ms << " " << char_type(char_) << " ";
311 MathClass InsetMathChar::mathClass() const
313 // this information comes from fontmath.ltx in LaTeX source.
315 return string_to_class(subst_->extra);
320 switch (static_cast<char>(char_)) {