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/lyxlib.h"
30 #include "support/textutils.h"
35 extern bool has_math_fonts;
38 static bool isBinaryOp(char_type c)
40 return support::contains("+-<>=/*", static_cast<char>(c));
44 static bool slanted(char_type c)
46 return isAlphaASCII(c) || Encodings::isMathAlpha(c);
50 InsetMathChar::InsetMathChar(char_type c)
51 : char_(c), kerning_(0)
56 Inset * InsetMathChar::clone() const
58 return new InsetMathChar(*this);
62 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
65 if (char_ == '=' && has_math_fonts) {
66 FontSetChanger dummy(mi.base, "cmr");
67 dim = theFontMetrics(mi.base.font).dimension(char_);
68 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
69 FontSetChanger dummy(mi.base, "cmm");
70 dim = theFontMetrics(mi.base.font).dimension(char_);
71 } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
72 ShapeChanger dummy(mi.base.font, UP_SHAPE);
73 dim = theFontMetrics(mi.base.font).dimension(char_);
75 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
76 dim = fm.dimension(char_);
77 kerning_ = fm.rbearing(char_) - dim.wid;
79 int const em = mathed_font_em(mi.base.font);
80 if (isBinaryOp(char_))
81 dim.wid += support::iround(0.5 * em);
82 else if (char_ == '\'')
83 dim.wid += support::iround(0.1667 * em);
85 whichFont(font_, code_, mi);
86 dim = theFontMetrics(font_).dimension(char_);
87 if (isBinaryOp(char_, code_))
88 dim.wid += 2 * theFontMetrics(font_).width(' ');
89 lyxerr << "InsetMathChar::metrics: " << dim << endl;
94 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
96 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << std::endl;
97 int const em = mathed_font_em(pi.base.font);
98 if (isBinaryOp(char_))
99 x += support::iround(0.25 * em);
100 else if (char_ == '\'')
101 x += support::iround(0.0833 * em);
103 if (char_ == '=' && has_math_fonts) {
104 FontSetChanger dummy(pi.base, "cmr");
105 pi.draw(x, y, char_);
106 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
107 FontSetChanger dummy(pi.base, "cmm");
108 pi.draw(x, y, char_);
109 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
110 ShapeChanger dummy(pi.base.font, UP_SHAPE);
111 pi.draw(x, y, char_);
113 pi.draw(x, y, char_);
116 drawChar(pain, font_, x, y, char_);
121 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
129 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
131 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
132 pain.draw(x, y, char_);
136 void InsetMathChar::write(WriteStream & os) const
142 void InsetMathChar::validate(LaTeXFeatures & features) const
145 BufferEncodings::validate(char_, features, true);
149 void InsetMathChar::normalize(NormalStream & os) const
157 void InsetMathChar::octave(OctaveStream & os) const
163 // We have a bit of a problem here. MathML wants to know whether the
164 // character represents an "identifier" or an "operator", and we have
165 // no general way of telling. So we shall guess: If it's alpha or
166 // mathalpha, then we'll treat it as an identifier, otherwise as an
168 // Worst case: We get bad spacing, or bad italics.
169 void InsetMathChar::mathmlize(MathStream & ms) const
173 case '<': entity = "<"; break;
174 case '>': entity = ">"; break;
175 case '&': entity = "&"; break;
177 ms << from_ascii(" ");
187 ms << from_ascii(entity);
191 if (!entity.empty()) {
192 ms << "<mo>" << from_ascii(entity) << "</mo>";
197 (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
199 // we don't use MTag and ETag because we do not want the spacing
200 ms << "<" << type << ">" << char_type(char_) << "</" << type << ">";
204 void InsetMathChar::htmlize(HtmlStream & ms) const
208 case '<': entity = "<"; break;
209 case '>': entity = ">"; break;
210 case '&': entity = "&"; break;
211 case ' ': entity = " "; break;
215 bool have_entity = !entity.empty();
219 ms << from_ascii(entity);
226 // an operator, so give some space
227 ms << ' ' << from_ascii(entity) << ' ';
231 if (isAlphaASCII(char_) || Encodings::isMathAlpha(char_))
232 // we don't use MTag and ETag because we do not want the spacing
233 ms << MTag("i") << char_type(char_) << ETag("i");
235 // an operator, so give some space
236 ms << " " << char_type(char_) << " ";
240 bool InsetMathChar::isRelOp() const
242 return char_ == '=' || char_ == '<' || char_ == '>';