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"
16 #include "dimension.h"
17 #include "frontends/Painter.h"
18 #include "frontends/font_metrics.h"
19 #include "support/LOstream.h"
21 #include "math_support.h"
22 #include "math_mathmlstream.h"
23 #include "LaTeXFeatures.h"
24 #include "textpainter.h"
34 #ifndef CXX_GLOBAL_CSTD
39 extern bool has_math_fonts;
43 bool isBinaryOp(char c)
45 return strchr("+-<>=/*", c);
51 //if (strchr("0123456789;:!|[]().,?+/-*<>=", c)
52 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
58 MathCharInset::MathCharInset(char c)
64 auto_ptr<InsetBase> MathCharInset::clone() const
66 return auto_ptr<InsetBase>(new MathCharInset(*this));
70 void MathCharInset::metrics(MetricsInfo & mi, Dimension & dim) const
73 if (char_ == '=' && has_math_fonts) {
74 FontSetChanger dummy(mi.base, "cmr");
75 mathed_char_dim(mi.base.font, char_, dim);
76 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
77 FontSetChanger dummy(mi.base, "cmm");
78 mathed_char_dim(mi.base.font, char_, dim);
79 } else if (slanted(char_) && mi.base.fontname == "mathnormal") {
80 ShapeChanger dummy(mi.base.font, LyXFont::ITALIC_SHAPE);
81 mathed_char_dim(mi.base.font, char_, dim);
83 mathed_char_dim(mi.base.font, char_, dim);
85 int const em = mathed_char_width(mi.base.font, 'M');
86 if (isBinaryOp(char_))
87 dim.wid += static_cast<int>(0.5*em+0.5);
88 else if (char_ == '\'')
89 dim.wid += static_cast<int>(0.1667*em+0.5);
91 whichFont(font_, code_, mi);
92 mathed_char_dim(font_, char_, dim_);
93 if (isBinaryOp(char_, code_))
94 width_ += 2 * font_metrics::width(' ', font_);
95 lyxerr << "MathCharInset::metrics: " << dim << endl;
100 void MathCharInset::draw(PainterInfo & pi, int x, int y) const
102 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
103 int const em = mathed_char_width(pi.base.font, 'M');
104 if (isBinaryOp(char_))
105 x += static_cast<int>(0.25*em+0.5);
106 else if (char_ == '\'')
107 x += static_cast<int>(0.0833*em+0.5);
109 if (char_ == '=' && has_math_fonts) {
110 FontSetChanger dummy(pi.base, "cmr");
111 pi.draw(x, y, char_);
112 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
113 FontSetChanger dummy(pi.base, "cmm");
114 pi.draw(x, y, char_);
115 } else if (slanted(char_) && pi.base.fontname == "mathnormal") {
116 ShapeChanger dummy(pi.base.font, LyXFont::ITALIC_SHAPE);
117 pi.draw(x, y, char_);
119 pi.draw(x, y, char_);
122 drawChar(pain, font_, x, y, char_);
127 void MathCharInset::metricsT(TextMetricsInfo const &, Dimension & dim) const
135 void MathCharInset::drawT(TextPainter & pain, int x, int y) const
137 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
138 pain.draw(x, y, char_);
142 void MathCharInset::write(WriteStream & os) const
148 void MathCharInset::normalize(NormalStream & os) const
150 os << "[char " << char_ << ' ' << "mathalpha]";
154 void MathCharInset::octave(OctaveStream & os) const
160 bool MathCharInset::isRelOp() const
162 return char_ == '=' || char_ == '<' || char_ == '>';
166 bool MathCharInset::match(MathInset const * p) const
168 MathCharInset const * q = p->asCharInset();
169 return q && char_ == q->char_;