2 * \file math_charinset.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 "math_charinset.h"
15 #include "math_support.h"
16 #include "math_mathmlstream.h"
19 #include "dimension.h"
20 #include "support/lstrings.h"
21 #include "textpainter.h"
25 extern bool has_math_fonts;
29 bool isBinaryOp(char c)
31 return lyx::support::contains("+-<>=/*", c);
37 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
43 MathCharInset::MathCharInset(char c)
49 auto_ptr<InsetBase> MathCharInset::doClone() const
51 return auto_ptr<InsetBase>(new MathCharInset(*this));
55 void MathCharInset::metrics(MetricsInfo & mi, Dimension & dim) const
58 if (char_ == '=' && has_math_fonts) {
59 FontSetChanger dummy(mi.base, "cmr");
60 mathed_char_dim(mi.base.font, char_, dim);
61 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
62 FontSetChanger dummy(mi.base, "cmm");
63 mathed_char_dim(mi.base.font, char_, dim);
64 } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
65 ShapeChanger dummy(mi.base.font, LyXFont::UP_SHAPE);
66 mathed_char_dim(mi.base.font, char_, dim);
68 mathed_char_dim(mi.base.font, char_, dim);
70 int const em = mathed_char_width(mi.base.font, 'M');
71 if (isBinaryOp(char_))
72 dim.wid += static_cast<int>(0.5*em+0.5);
73 else if (char_ == '\'')
74 dim.wid += static_cast<int>(0.1667*em+0.5);
76 whichFont(font_, code_, mi);
77 mathed_char_dim(font_, char_, dim_);
78 if (isBinaryOp(char_, code_))
79 width_ += 2 * font_metrics::width(' ', font_);
80 lyxerr << "MathCharInset::metrics: " << dim << endl;
86 void MathCharInset::draw(PainterInfo & pi, int x, int y) const
88 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
89 int const em = mathed_char_width(pi.base.font, 'M');
90 if (isBinaryOp(char_))
91 x += static_cast<int>(0.25*em+0.5);
92 else if (char_ == '\'')
93 x += static_cast<int>(0.0833*em+0.5);
95 if (char_ == '=' && has_math_fonts) {
96 FontSetChanger dummy(pi.base, "cmr");
98 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
99 FontSetChanger dummy(pi.base, "cmm");
100 pi.draw(x, y, char_);
101 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
102 ShapeChanger dummy(pi.base.font, LyXFont::UP_SHAPE);
103 pi.draw(x, y, char_);
105 pi.draw(x, y, char_);
108 drawChar(pain, font_, x, y, char_);
113 void MathCharInset::metricsT(TextMetricsInfo const &, Dimension & dim) const
121 void MathCharInset::drawT(TextPainter & pain, int x, int y) const
123 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
124 pain.draw(x, y, char_);
128 void MathCharInset::write(WriteStream & os) const
134 void MathCharInset::normalize(NormalStream & os) const
136 os << "[char " << char_ << ' ' << "mathalpha]";
140 void MathCharInset::octave(OctaveStream & os) const
146 bool MathCharInset::isRelOp() const
148 return char_ == '=' || char_ == '<' || char_ == '>';