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/lstrings.h"
30 #include "support/textutils.h"
41 latexkeys const * makeSubstitute(char_type c)
45 // Latex replaces ', *, -, and : with specific symbols. With unicode-math,
46 // these symbols are replaced respectively by ^U+2032, U+2217, U+2212 and
47 // U+2236 (the latter substitution can be turned off with a package
48 // option). Unicode-math also replaces ` with \backprime.
49 // prime needs to be placed in superscript unless an opentype font is used.
57 name = "lyxminus";// unicode-math: "minus"
60 name = "ordinarycolon";// unicode-math: "mathratio"
62 // The remaining replacements are not real character substitutions (from a
63 // unicode point of view) but are done here: 1. for cosmetic reasons, in the
64 // context of being stuck with CM fonts at the moment, to ensure consistency
65 // with related symbols: -, \leq, \geq, etc. 2. to get the proper spacing
66 // as defined in lib/symbols.
68 name = "lyxplus";//unicode-math: "mathplus"
71 name = "lyxgt";//unicode-math: "greater"
74 name = "lyxlt";//unicode-math: "less"
77 name = "lyxeqrel";//unicode-math: "equal"
79 //case ','://unicode-math: "mathcomma"
80 //case ';'://unicode-math: "mathsemicolon"
84 return in_word_set(from_ascii(name));
87 } //anonymous namespace
90 static bool slanted(char_type c)
92 return isAlphaASCII(c) || Encodings::isMathAlpha(c);
96 InsetMathChar::InsetMathChar(char_type c)
97 : char_(c), kerning_(0), subst_(makeSubstitute(c))
102 Inset * InsetMathChar::clone() const
104 return new InsetMathChar(*this);
108 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
110 string const & f = mi.base.fontname;
111 if (isMathFont(f) && subst_) {
112 // If the char has a substitute, draw the replacement symbol
113 // instead, but only in math mode.
114 kerning_ = mathedSymbolDim(mi.base, dim, subst_);
115 } else if (!slanted(char_) && f == "mathnormal") {
116 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
117 dim = theFontMetrics(mi.base.font).dimension(char_);
119 } else if (!isASCII(char_) && Encodings::unicodeCharInfo(char_).isUnicodeSymbol()) {
120 Changer dummy1 = mi.base.changeFontSet("mathnormal");
121 Changer dummy2 = Encodings::isMathAlpha(char_)
123 : mi.base.font.changeShape(UP_SHAPE);
124 dim = theFontMetrics(mi.base.font).dimension(char_);
125 kerning_ = -mathed_char_kerning(mi.base.font, char_);
127 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
128 dim = fm.dimension(char_);
129 kerning_ = mathed_char_kerning(mi.base.font, char_);
130 // cmmi has almost no left bearing: simulate this
131 if (f == "mathnormal") {
132 dim.wid += max(-fm.lbearing(char_), 0);
134 // Rule 17 from Appendix G
135 // These are the fonts with fontdimen(2)==0.
136 // To properly handle the case fontdimen(2)!=0 (that is for all other
137 // math fonts), where the italic correction must also be converted into
138 // kerning but only at the end of strings of characters with the same
139 // font, one would need a more elaborate implementation in MathRow. For
140 // now the case fontdimen(2)==0 is the most important.
141 if (f == "mathnormal" || f == "mathscr" || f == "mathcal") {
143 // We use a negative value to tell InsetMathScript to move the
144 // subscript leftwards instead of the superscript rightwards
145 kerning_ = -kerning_;
151 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
153 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
154 if (isMathFont(pi.base.fontname)) {
156 // If the char has a substitute, draw the replacement symbol
157 // instead, but only in math mode.
158 mathedSymbolDraw(pi, x, y, subst_);
160 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
161 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
162 pi.draw(x, y, char_);
164 } else if (!isASCII(char_) && Encodings::unicodeCharInfo(char_).isUnicodeSymbol()) {
165 Changer dummy1 = pi.base.changeFontSet("mathnormal");
166 Changer dummy2 = Encodings::isMathAlpha(char_)
168 : pi.base.font.changeShape(UP_SHAPE);
169 pi.draw(x, y, char_);
173 // cmmi has almost no left bearing: simulate this
174 if (pi.base.fontname == "mathnormal") {
175 x += max(-theFontMetrics(pi.base.font).lbearing(char_), 0);
177 pi.draw(x, y, char_);
181 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
189 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
191 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
192 pain.draw(x, y, char_);
196 void InsetMathChar::write(TeXMathStream & os) const
202 void InsetMathChar::validate(LaTeXFeatures & features) const
205 BufferEncodings::validate(char_, features, true);
209 void InsetMathChar::normalize(NormalStream & os) const
217 void InsetMathChar::octave(OctaveStream & os) const
223 // We have a bit of a problem here. MathML wants to know whether the
224 // character represents an "identifier" or an "operator", and we have
225 // no general way of telling. So we shall guess: If it's alpha or
226 // mathalpha, then we'll treat it as an identifier, otherwise as an
228 // Worst case: We get bad spacing, or bad italics.
229 void InsetMathChar::mathmlize(MathMLStream & ms) const
233 case '<': entity = "<"; break;
234 case '>': entity = ">"; break;
235 case '&': entity = "&"; break;
238 ms << from_ascii(" ");
240 ms << from_ascii(" ");
250 ms << from_ascii(entity);
254 if (!entity.empty()) {
255 ms << MTagInline("mo")
256 << from_ascii(entity)
262 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
264 // we don't use MTag and ETag because we do not want the spacing before the end tag.
265 docstring tag = from_ascii(ms.namespacedTag(type));
266 ms << MTagInline(type)
272 void InsetMathChar::htmlize(HtmlStream & ms) const
275 // Not taking subst_ into account here because the MathML output of
276 // <>=+-* looks correct as it is. FIXME: ' is not output as ^\prime
278 case '<': entity = "<"; break;
279 case '>': entity = ">"; break;
280 case '&': entity = "&"; break;
281 case ' ': entity = " "; break;
285 bool have_entity = !entity.empty();
289 ms << from_ascii(entity);
296 // an operator, so give some space
297 ms << ' ' << from_ascii(entity) << ' ';
301 if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
302 // we don't use MTag and ETag because we do not want the spacing
303 ms << MTag("i") << char_type(char_) << ETag("i");
305 // an operator, so give some space
306 ms << " " << char_type(char_) << " ";
310 MathClass InsetMathChar::mathClass() const
312 // this information comes from fontmath.ltx in LaTeX source.
313 char const ch = static_cast<char>(char_);
315 return string_to_class(subst_->extra);
316 else if (support::contains(",;", ch))
318 else if (support::contains("([", ch))
320 else if (support::contains(")]!?", ch))