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 "MathSupport.h"
17 #include "MathStream.h"
18 #include "MetricsInfo.h"
20 #include "Dimension.h"
21 #include "BufferEncodings.h"
22 #include "LaTeXFeatures.h"
23 #include "TextPainter.h"
25 #include "frontends/FontMetrics.h"
27 #include "support/debug.h"
28 #include "support/lstrings.h"
29 #include "support/textutils.h"
34 extern bool has_math_fonts;
37 static bool slanted(char_type c)
39 return isAlphaASCII(c) || Encodings::isMathAlpha(c);
43 InsetMathChar::InsetMathChar(char_type c)
44 : char_(c), kerning_(0)
49 Inset * InsetMathChar::clone() const
51 return new InsetMathChar(*this);
55 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
58 if (char_ == '=' && has_math_fonts) {
59 Changer dummy = mi.base.changeFontSet("cmr");
60 dim = theFontMetrics(mi.base.font).dimension(char_);
61 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
62 Changer dummy = mi.base.changeFontSet("cmm");
63 dim = theFontMetrics(mi.base.font).dimension(char_);
64 } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
65 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
66 dim = theFontMetrics(mi.base.font).dimension(char_);
68 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
69 dim = fm.dimension(char_);
70 kerning_ = fm.rbearing(char_) - dim.wid;
73 dim.wid += 2 * mathed_medmuskip(mi.base.font);
75 dim.wid += 2 * mathed_thickmuskip(mi.base.font);
76 else if (isMathPunct())
77 dim.wid += mathed_thinmuskip(mi.base.font);
78 else if (char_ == '\'')
79 // FIXME: don't know where this is coming from
80 dim.wid += mathed_thinmuskip(mi.base.font);
82 whichFont(font_, code_, mi);
83 dim = theFontMetrics(font_).dimension(char_);
84 if (isBinaryOp(char_, code_))
85 dim.wid += 2 * theFontMetrics(font_).width(' ');
86 lyxerr << "InsetMathChar::metrics: " << dim << endl;
91 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
93 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
95 x += mathed_medmuskip(pi.base.font);
97 x += mathed_thickmuskip(pi.base.font);
98 else if (char_ == '\'')
99 x += mathed_thinmuskip(pi.base.font) / 2;
101 if (char_ == '=' && has_math_fonts) {
102 Changer dummy = pi.base.changeFontSet("cmr");
103 pi.draw(x, y, char_);
104 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
105 Changer dummy = pi.base.changeFontSet("cmm");
106 pi.draw(x, y, char_);
107 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
108 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
109 pi.draw(x, y, char_);
111 pi.draw(x, y, char_);
114 drawChar(pain, font_, x, y, char_);
119 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
127 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
129 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
130 pain.draw(x, y, char_);
134 void InsetMathChar::write(WriteStream & os) const
140 void InsetMathChar::validate(LaTeXFeatures & features) const
143 BufferEncodings::validate(char_, features, true);
147 void InsetMathChar::normalize(NormalStream & os) const
155 void InsetMathChar::octave(OctaveStream & os) const
161 // We have a bit of a problem here. MathML wants to know whether the
162 // character represents an "identifier" or an "operator", and we have
163 // no general way of telling. So we shall guess: If it's alpha or
164 // mathalpha, then we'll treat it as an identifier, otherwise as an
166 // Worst case: We get bad spacing, or bad italics.
167 void InsetMathChar::mathmlize(MathStream & ms) const
171 case '<': entity = "<"; break;
172 case '>': entity = ">"; break;
173 case '&': entity = "&"; break;
175 ms << from_ascii(" ");
185 ms << from_ascii(entity);
189 if (!entity.empty()) {
190 ms << "<mo>" << from_ascii(entity) << "</mo>";
195 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
197 // we don't use MTag and ETag because we do not want the spacing
198 ms << "<" << type << ">" << char_type(char_) << "</" << type << ">";
202 void InsetMathChar::htmlize(HtmlStream & ms) const
206 case '<': entity = "<"; break;
207 case '>': entity = ">"; break;
208 case '&': entity = "&"; break;
209 case ' ': entity = " "; break;
213 bool have_entity = !entity.empty();
217 ms << from_ascii(entity);
224 // an operator, so give some space
225 ms << ' ' << from_ascii(entity) << ' ';
229 if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
230 // we don't use MTag and ETag because we do not want the spacing
231 ms << MTag("i") << char_type(char_) << ETag("i");
233 // an operator, so give some space
234 ms << " " << char_type(char_) << " ";
238 bool InsetMathChar::isMathBin() const
240 return support::contains("+-*", static_cast<char>(char_));
244 bool InsetMathChar::isMathRel() const
246 return support::contains("<>=:", static_cast<char>(char_));
250 bool InsetMathChar::isMathPunct() const
252 return support::contains(",;", static_cast<char>(char_));