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;
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 bool const mathfont = isMathFont(mi.base.fontname);
110 if (mathfont && subst_) {
111 // If the char has a substitute, draw the replacement symbol
112 // instead, but only in math mode.
113 mathedSymbolDim(mi, dim, subst_);
114 kerning_ = mathed_char_kerning(mi.base.font, *subst_->draw.rbegin());
116 } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
117 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
118 dim = theFontMetrics(mi.base.font).dimension(char_);
120 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
121 dim = fm.dimension(char_);
122 kerning_ = fm.rbearing(char_) - dim.wid;
127 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
129 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
130 if (isMathFont(pi.base.fontname)) {
132 // If the char has a substitute, draw the replacement symbol
133 // instead, but only in math mode.
134 mathedSymbolDraw(pi, x, y, subst_);
136 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
137 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
138 pi.draw(x, y, char_);
142 pi.draw(x, y, char_);
146 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
154 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
156 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
157 pain.draw(x, y, char_);
161 void InsetMathChar::write(WriteStream & os) const
167 void InsetMathChar::validate(LaTeXFeatures & features) const
170 BufferEncodings::validate(char_, features, true);
174 void InsetMathChar::normalize(NormalStream & os) const
182 void InsetMathChar::octave(OctaveStream & os) const
188 // We have a bit of a problem here. MathML wants to know whether the
189 // character represents an "identifier" or an "operator", and we have
190 // no general way of telling. So we shall guess: If it's alpha or
191 // mathalpha, then we'll treat it as an identifier, otherwise as an
193 // Worst case: We get bad spacing, or bad italics.
194 void InsetMathChar::mathmlize(MathStream & ms) const
198 case '<': entity = "<"; break;
199 case '>': entity = ">"; break;
200 case '&': entity = "&"; break;
202 ms << from_ascii(" ");
212 ms << from_ascii(entity);
216 if (!entity.empty()) {
217 ms << "<mo>" << from_ascii(entity) << "</mo>";
222 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
224 // we don't use MTag and ETag because we do not want the spacing
225 ms << "<" << type << ">" << char_type(char_) << "</" << type << ">";
229 void InsetMathChar::htmlize(HtmlStream & ms) const
232 // Not taking subst_ into account here because the MathML output of
233 // <>=+-* looks correct as it is. FIXME: ' is not output as ^\prime
235 case '<': entity = "<"; break;
236 case '>': entity = ">"; break;
237 case '&': entity = "&"; break;
238 case ' ': entity = " "; break;
242 bool have_entity = !entity.empty();
246 ms << from_ascii(entity);
253 // an operator, so give some space
254 ms << ' ' << from_ascii(entity) << ' ';
258 if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
259 // we don't use MTag and ETag because we do not want the spacing
260 ms << MTag("i") << char_type(char_) << ETag("i");
262 // an operator, so give some space
263 ms << " " << char_type(char_) << " ";
267 MathClass InsetMathChar::mathClass() const
269 // this information comes from fontmath.ltx in LaTeX source.
270 char const ch = static_cast<char>(char_);
272 return string_to_class(subst_->extra);
273 else if (support::contains(",;", ch))
275 else if (support::contains("([", ch))
277 else if (support::contains(")]!?", ch))