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"
39 extern bool has_math_fonts;
43 latexkeys const * makeSubstitute(char_type c)
47 // Latex replaces ', *, -, and : with specific symbols. With unicode-math,
48 // these symbols are replaced respectively by ^U+2032, U+2217, U+2212 and
49 // U+2236 (the latter substitution can be turned off with a package
50 // option). Unicode-math also replaces ` with \backprime.
51 // prime needs to be placed in superscript unless an opentype font is used.
59 name = "lyxminus";// unicode-math: "minus"
62 name = "ordinarycolon";// unicode-math: "mathratio"
64 // The remaining replacements are not real character substitutions (from a
65 // unicode point of view) but are done here: 1. for cosmetic reasons, in the
66 // context of being stuck with CM fonts at the moment, to ensure consistency
67 // with related symbols: -, \leq, \geq, etc. 2. to get the proper spacing
68 // as defined in lib/symbols.
70 name = "lyxplus";//unicode-math: "mathplus"
73 name = "lyxgt";//unicode-math: "greater"
76 name = "lyxlt";//unicode-math: "less"
79 name = "lyxeqrel";//unicode-math: "equal"
81 //case ','://unicode-math: "mathcomma"
82 //case ';'://unicode-math: "mathsemicolon"
86 return in_word_set(from_ascii(name));
89 } //anonymous namespace
92 static bool slanted(char_type c)
94 return isAlphaASCII(c) || Encodings::isMathAlpha(c);
98 InsetMathChar::InsetMathChar(char_type c)
99 : char_(c), kerning_(0), subst_(makeSubstitute(c))
104 Inset * InsetMathChar::clone() const
106 return new InsetMathChar(*this);
110 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
112 string const & f = mi.base.fontname;
113 if (isMathFont(f) && subst_) {
114 // If the char has a substitute, draw the replacement symbol
115 // instead, but only in math mode.
116 kerning_ = mathedSymbolDim(mi.base, dim, subst_);
117 } else if (!slanted(char_) && f == "mathnormal") {
118 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
119 dim = theFontMetrics(mi.base.font).dimension(char_);
122 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
123 dim = fm.dimension(char_);
124 kerning_ = mathed_char_kerning(mi.base.font, char_);
125 // cmmi has almost no left bearing: simulate this
126 if (f == "mathnormal") {
127 dim.wid += max(-fm.lbearing(char_), 0);
129 // Rule 17 from Appendix G
130 // These are the fonts with fontdimen(2)==0.
131 // To properly handle the case fontdimen(2)!=0 (that is for all other
132 // math fonts), where the italic correction must also be converted into
133 // kerning but only at the end of strings of characters with the same
134 // font, one would need a more elaborate implementation in MathRow. For
135 // now the case fontdimen(2)==0 is the most important.
136 if (f == "mathnormal" || f == "mathscr" || f == "mathcal") {
138 // We use a negative value to tell InsetMathScript to move the
139 // subscript leftwards instead of the superscript rightwards
140 kerning_ = -kerning_;
146 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
148 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
149 if (isMathFont(pi.base.fontname)) {
151 // If the char has a substitute, draw the replacement symbol
152 // instead, but only in math mode.
153 mathedSymbolDraw(pi, x, y, subst_);
155 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
156 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
157 pi.draw(x, y, char_);
161 // cmmi has almost no left bearing: simulate this
162 if (pi.base.fontname == "mathnormal") {
163 x += max(-theFontMetrics(pi.base.font).lbearing(char_), 0);
165 pi.draw(x, y, char_);
169 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
177 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
179 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
180 pain.draw(x, y, char_);
184 void InsetMathChar::write(WriteStream & os) const
190 void InsetMathChar::validate(LaTeXFeatures & features) const
193 BufferEncodings::validate(char_, features, true);
197 void InsetMathChar::normalize(NormalStream & os) const
205 void InsetMathChar::octave(OctaveStream & os) const
211 // We have a bit of a problem here. MathML wants to know whether the
212 // character represents an "identifier" or an "operator", and we have
213 // no general way of telling. So we shall guess: If it's alpha or
214 // mathalpha, then we'll treat it as an identifier, otherwise as an
216 // Worst case: We get bad spacing, or bad italics.
217 void InsetMathChar::mathmlize(MathStream & ms) const
221 case '<': entity = "<"; break;
222 case '>': entity = ">"; break;
223 case '&': entity = "&"; break;
226 ms << from_ascii(" ");
228 ms << from_ascii(" ");
238 ms << from_ascii(entity);
242 if (!entity.empty()) {
243 ms << "<" << from_ascii(ms.namespacedTag("mo")) << ">"
244 << from_ascii(entity)
245 << "</" << from_ascii(ms.namespacedTag("mo")) << ">";
250 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
252 // we don't use MTag and ETag because we do not want the spacing before the end tag.
253 docstring tag = from_ascii(ms.namespacedTag(type));
254 ms << "<" << tag << ">" << char_type(char_) << "</" << tag << ">";
258 void InsetMathChar::htmlize(HtmlStream & ms) const
261 // Not taking subst_ into account here because the MathML output of
262 // <>=+-* looks correct as it is. FIXME: ' is not output as ^\prime
264 case '<': entity = "<"; break;
265 case '>': entity = ">"; break;
266 case '&': entity = "&"; break;
267 case ' ': entity = " "; break;
271 bool have_entity = !entity.empty();
275 ms << from_ascii(entity);
282 // an operator, so give some space
283 ms << ' ' << from_ascii(entity) << ' ';
287 if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
288 // we don't use MTag and ETag because we do not want the spacing
289 ms << MTag("i") << char_type(char_) << ETag("i");
291 // an operator, so give some space
292 ms << " " << char_type(char_) << " ";
296 MathClass InsetMathChar::mathClass() const
298 // this information comes from fontmath.ltx in LaTeX source.
299 char const ch = static_cast<char>(char_);
301 return string_to_class(subst_->extra);
302 else if (support::contains(",;", ch))
304 else if (support::contains("([", ch))
306 else if (support::contains(")]!?", ch))