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 "MathStream.h"
19 #include "dimension.h"
20 #include "support/lstrings.h"
21 #include "TextPainter.h"
23 #include "frontends/FontMetrics.h"
30 extern bool has_math_fonts;
34 bool isBinaryOp(char_type c)
36 return support::contains("+-<>=/*", static_cast<char>(c));
40 bool slanted(char_type c)
42 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
48 InsetMathChar::InsetMathChar(char_type c)
54 auto_ptr<InsetBase> InsetMathChar::doClone() const
56 return auto_ptr<InsetBase>(new InsetMathChar(*this));
60 bool InsetMathChar::metrics(MetricsInfo & mi, Dimension & dim) const
63 if (char_ == '=' && has_math_fonts) {
64 FontSetChanger dummy(mi.base, "cmr");
65 dim = theFontMetrics(mi.base.font).dimension(char_);
66 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
67 FontSetChanger dummy(mi.base, "cmm");
68 dim = theFontMetrics(mi.base.font).dimension(char_);
69 } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
70 ShapeChanger dummy(mi.base.font, LyXFont::UP_SHAPE);
71 dim = theFontMetrics(mi.base.font).dimension(char_);
73 dim = theFontMetrics(mi.base.font).dimension(char_);
75 int const em = mathed_char_width(mi.base.font, 'M');
76 if (isBinaryOp(char_))
77 dim.wid += static_cast<int>(0.5*em+0.5);
78 else if (char_ == '\'')
79 dim.wid += static_cast<int>(0.1667*em+0.5);
81 whichFont(font_, code_, mi);
82 dim = theFontMetrics(font_).dimension(char_);
83 if (isBinaryOp(char_, code_))
84 width_ += 2 * theFontMetrics(font_).width(' ');
85 lyxerr << "InsetMathChar::metrics: " << dim << endl;
95 void InsetMathChar::draw(PainterInfo & pi, int x, int y) const
97 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
98 int const em = mathed_char_width(pi.base.font, 'M');
99 if (isBinaryOp(char_))
100 x += static_cast<int>(0.25*em+0.5);
101 else if (char_ == '\'')
102 x += static_cast<int>(0.0833*em+0.5);
104 if (char_ == '=' && has_math_fonts) {
105 FontSetChanger dummy(pi.base, "cmr");
106 pi.draw(x, y, char_);
107 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
108 FontSetChanger dummy(pi.base, "cmm");
109 pi.draw(x, y, char_);
110 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
111 ShapeChanger dummy(pi.base.font, LyXFont::UP_SHAPE);
112 pi.draw(x, y, char_);
114 pi.draw(x, y, char_);
117 drawChar(pain, font_, x, y, char_);
122 void InsetMathChar::metricsT(TextMetricsInfo const &, Dimension & dim) const
130 void InsetMathChar::drawT(TextPainter & pain, int x, int y) const
132 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
133 pain.draw(x, y, char_);
137 void InsetMathChar::write(WriteStream & os) const
143 void InsetMathChar::normalize(NormalStream & os) const
151 void InsetMathChar::octave(OctaveStream & os) const
157 void InsetMathChar::mathmlize(MathStream & ms) const
160 case '<': ms << "<"; break;
161 case '>': ms << ">"; break;
162 case '&': ms << "&"; break;
163 default: ms.os().put(char_); break;
168 bool InsetMathChar::isRelOp() const
170 return char_ == '=' || char_ == '<' || char_ == '>';