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"
35 extern bool has_math_fonts;
39 latexkeys const * makeSubstitute(char_type c)
43 // Latex replaces ', *, -, and : with specific symbols. With unicode-math,
44 // these symbols are replaced respectively by ^U+2032, U+2217, U+2212 and
45 // U+2236 (the latter substitution can be turned off with a package
46 // option). Unicode-math also replaces ` with \backprime.
47 // prime needs to be placed in superscript unless an opentype font is used.
55 name = "lyxminus";// unicode-math: "minus"
58 name = "ordinarycolon";// unicode-math: "mathratio"
60 // The remaining replacements are not real character substitutions (from a
61 // unicode point of view) but are done here: 1. for cosmetic reasons, in the
62 // context of being stuck with CM fonts at the moment, to ensure consistency
63 // with related symbols: -, \leq, \geq, etc. 2. to get the proper spacing
64 // as defined in lib/symbols.
66 name = "lyxplus";//unicode-math: "mathplus"
69 name = "lyxgt";//unicode-math: "greater"
72 name = "lyxlt";//unicode-math: "less"
75 name = "lyxeqrel";//unicode-math: "equal"
77 //case ','://unicode-math: "mathcomma"
78 //case ';'://unicode-math: "mathsemicolon"
82 return in_word_set(from_ascii(name));
85 } //anonymous namespace
88 static bool slanted(char_type c)
90 return isAlphaASCII(c) || Encodings::isMathAlpha(c);
94 InsetMathChar::InsetMathChar(char_type c)
95 : char_(c), kerning_(0), subst_(makeSubstitute(c))
100 Inset * InsetMathChar::clone() const
102 return new InsetMathChar(*this);
106 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
108 bool const mathfont = isMathFont(mi.base.fontname);
109 if (mathfont && subst_) {
110 // If the char has a substitute, draw the replacement symbol
111 // instead, but only in math mode.
112 mathedSymbolDim(mi.base, dim, subst_);
113 kerning_ = mathed_char_kerning(mi.base.font, *subst_->draw.rbegin());
115 } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
116 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
117 dim = theFontMetrics(mi.base.font).dimension(char_);
119 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
120 dim = fm.dimension(char_);
121 kerning_ = fm.rbearing(char_) - dim.wid;
126 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
128 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
129 if (isMathFont(pi.base.fontname)) {
131 // If the char has a substitute, draw the replacement symbol
132 // instead, but only in math mode.
133 mathedSymbolDraw(pi, x, y, subst_);
135 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
136 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
137 pi.draw(x, y, char_);
141 pi.draw(x, y, char_);
145 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
153 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
155 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
156 pain.draw(x, y, char_);
160 void InsetMathChar::write(WriteStream & os) const
166 void InsetMathChar::validate(LaTeXFeatures & features) const
169 BufferEncodings::validate(char_, features, true);
173 void InsetMathChar::normalize(NormalStream & os) const
181 void InsetMathChar::octave(OctaveStream & os) const
187 // We have a bit of a problem here. MathML wants to know whether the
188 // character represents an "identifier" or an "operator", and we have
189 // no general way of telling. So we shall guess: If it's alpha or
190 // mathalpha, then we'll treat it as an identifier, otherwise as an
192 // Worst case: We get bad spacing, or bad italics.
193 void InsetMathChar::mathmlize(MathStream & ms) const
197 case '<': entity = "<"; break;
198 case '>': entity = ">"; break;
199 case '&': entity = "&"; break;
201 ms << from_ascii(" ");
211 ms << from_ascii(entity);
215 if (!entity.empty()) {
216 ms << "<mo>" << from_ascii(entity) << "</mo>";
221 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
223 // we don't use MTag and ETag because we do not want the spacing
224 ms << "<" << type << ">" << char_type(char_) << "</" << type << ">";
228 void InsetMathChar::htmlize(HtmlStream & ms) const
231 // Not taking subst_ into account here because the MathML output of
232 // <>=+-* looks correct as it is. FIXME: ' is not output as ^\prime
234 case '<': entity = "<"; break;
235 case '>': entity = ">"; break;
236 case '&': entity = "&"; break;
237 case ' ': entity = " "; break;
241 bool have_entity = !entity.empty();
245 ms << from_ascii(entity);
252 // an operator, so give some space
253 ms << ' ' << from_ascii(entity) << ' ';
257 if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
258 // we don't use MTag and ETag because we do not want the spacing
259 ms << MTag("i") << char_type(char_) << ETag("i");
261 // an operator, so give some space
262 ms << " " << char_type(char_) << " ";
266 MathClass InsetMathChar::mathClass() const
268 // this information comes from fontmath.ltx in LaTeX source.
269 char const ch = static_cast<char>(char_);
271 return string_to_class(subst_->extra);
272 else if (support::contains(",;", ch))
274 else if (support::contains("([", ch))
276 else if (support::contains(")]!?", ch))