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 "dimension.h"
16 #include "support/LOstream.h"
17 #include "math_support.h"
18 #include "math_mathmlstream.h"
19 #include "textpainter.h"
27 #ifndef CXX_GLOBAL_CSTD
32 extern bool has_math_fonts;
36 bool isBinaryOp(char c)
38 return strchr("+-<>=/*", c);
44 //if (strchr("0123456789;:!|[]().,?+/-*<>=", c)
45 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
51 MathCharInset::MathCharInset(char c)
57 auto_ptr<InsetBase> MathCharInset::clone() const
59 return auto_ptr<InsetBase>(new MathCharInset(*this));
63 void MathCharInset::metrics(MetricsInfo & mi, Dimension & dim) const
66 if (char_ == '=' && has_math_fonts) {
67 FontSetChanger dummy(mi.base, "cmr");
68 mathed_char_dim(mi.base.font, char_, dim);
69 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
70 FontSetChanger dummy(mi.base, "cmm");
71 mathed_char_dim(mi.base.font, char_, dim);
72 } else if (slanted(char_) && mi.base.fontname == "mathnormal") {
73 ShapeChanger dummy(mi.base.font, LyXFont::ITALIC_SHAPE);
74 mathed_char_dim(mi.base.font, char_, dim);
76 mathed_char_dim(mi.base.font, char_, dim);
78 int const em = mathed_char_width(mi.base.font, 'M');
79 if (isBinaryOp(char_))
80 dim.wid += static_cast<int>(0.5*em+0.5);
81 else if (char_ == '\'')
82 dim.wid += static_cast<int>(0.1667*em+0.5);
84 whichFont(font_, code_, mi);
85 mathed_char_dim(font_, char_, dim_);
86 if (isBinaryOp(char_, code_))
87 width_ += 2 * font_metrics::width(' ', font_);
88 lyxerr << "MathCharInset::metrics: " << dim << endl;
93 void MathCharInset::draw(PainterInfo & pi, int x, int y) const
95 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
96 int const em = mathed_char_width(pi.base.font, 'M');
97 if (isBinaryOp(char_))
98 x += static_cast<int>(0.25*em+0.5);
99 else if (char_ == '\'')
100 x += static_cast<int>(0.0833*em+0.5);
102 if (char_ == '=' && has_math_fonts) {
103 FontSetChanger dummy(pi.base, "cmr");
104 pi.draw(x, y, char_);
105 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
106 FontSetChanger dummy(pi.base, "cmm");
107 pi.draw(x, y, char_);
108 } else if (slanted(char_) && pi.base.fontname == "mathnormal") {
109 ShapeChanger dummy(pi.base.font, LyXFont::ITALIC_SHAPE);
110 pi.draw(x, y, char_);
112 pi.draw(x, y, char_);
115 drawChar(pain, font_, x, y, char_);
120 void MathCharInset::metricsT(TextMetricsInfo const &, Dimension & dim) const
128 void MathCharInset::drawT(TextPainter & pain, int x, int y) const
130 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
131 pain.draw(x, y, char_);
135 void MathCharInset::write(WriteStream & os) const
141 void MathCharInset::normalize(NormalStream & os) const
143 os << "[char " << char_ << ' ' << "mathalpha]";
147 void MathCharInset::octave(OctaveStream & os) const
153 bool MathCharInset::isRelOp() const
155 return char_ == '=' || char_ == '<' || char_ == '>';
159 bool MathCharInset::match(MathInset const * p) const
161 MathCharInset const * q = p->asCharInset();
162 return q && char_ == q->char_;