2 * \file InsetMathChar.C
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"
15 #include "MathSupport.h"
16 #include "MathMLStream.h"
19 #include "dimension.h"
20 #include "support/lstrings.h"
21 #include "TextPainter.h"
23 #include "frontends/FontMetrics.h"
27 extern bool has_math_fonts;
31 bool isBinaryOp(char c)
33 return lyx::support::contains("+-<>=/*", c);
39 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
45 InsetMathChar::InsetMathChar(char c)
51 auto_ptr<InsetBase> InsetMathChar::doClone() const
53 return auto_ptr<InsetBase>(new InsetMathChar(*this));
57 void InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
60 if (char_ == '=' && has_math_fonts) {
61 FontSetChanger dummy(mi.base, "cmr");
62 mathed_char_dim(mi.base.font, char_, dim);
63 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
64 FontSetChanger dummy(mi.base, "cmm");
65 mathed_char_dim(mi.base.font, char_, dim);
66 } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
67 ShapeChanger dummy(mi.base.font, LyXFont::UP_SHAPE);
68 mathed_char_dim(mi.base.font, char_, dim);
70 mathed_char_dim(mi.base.font, char_, dim);
72 int const em = mathed_char_width(mi.base.font, 'M');
73 if (isBinaryOp(char_))
74 dim.wid += static_cast<int>(0.5*em+0.5);
75 else if (char_ == '\'')
76 dim.wid += static_cast<int>(0.1667*em+0.5);
78 whichFont(font_, code_, mi);
79 mathed_char_dim(font_, char_, dim_);
80 if (isBinaryOp(char_, code_))
81 width_ += 2 * theFontMetrics(font_).width(' ');
82 lyxerr << "InsetMathChar::metrics: " << dim << endl;
88 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
90 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
91 int const em = mathed_char_width(pi.base.font, 'M');
92 if (isBinaryOp(char_))
93 x += static_cast<int>(0.25*em+0.5);
94 else if (char_ == '\'')
95 x += static_cast<int>(0.0833*em+0.5);
97 if (char_ == '=' && has_math_fonts) {
98 FontSetChanger dummy(pi.base, "cmr");
100 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
101 FontSetChanger dummy(pi.base, "cmm");
102 pi.draw(x, y, char_);
103 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
104 ShapeChanger dummy(pi.base.font, LyXFont::UP_SHAPE);
105 pi.draw(x, y, char_);
107 pi.draw(x, y, char_);
110 drawChar(pain, font_, x, y, char_);
115 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
123 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
125 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
126 pain.draw(x, y, char_);
130 void InsetMathChar::write(WriteStream & os) const
136 void InsetMathChar::normalize(NormalStream & os) const
138 os << "[char " << char_ << " mathalpha]";
142 void InsetMathChar::octave(OctaveStream & os) const
148 void InsetMathChar::mathmlize(MathMLStream & ms) const
151 case '<': ms << "<"; break;
152 case '>': ms << ">"; break;
153 case '&': ms << "&"; break;
154 default: ms << char_; break;
159 bool InsetMathChar::isRelOp() const
161 return char_ == '=' || char_ == '<' || char_ == '>';