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 "textpainter.h"
24 #ifndef CXX_GLOBAL_CSTD
29 extern bool has_math_fonts;
33 bool isBinaryOp(char c)
35 return strchr("+-<>=/*", c);
41 //if (strchr("0123456789;:!|[]().,?+/-*<>=", c)
42 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
48 MathCharInset::MathCharInset(char c)
54 auto_ptr<InsetBase> MathCharInset::clone() const
56 return auto_ptr<InsetBase>(new MathCharInset(*this));
60 void MathCharInset::metrics(MetricsInfo & mi, Dimension & dim) const
63 if (char_ == '=' && has_math_fonts) {
64 FontSetChanger dummy(mi.base, "cmr");
65 mathed_char_dim(mi.base.font, char_, dim);
66 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
67 FontSetChanger dummy(mi.base, "cmm");
68 mathed_char_dim(mi.base.font, char_, dim);
69 } else if (!slanted(char_) && mi.base.fontname == "mathnormal") {
70 ShapeChanger dummy(mi.base.font, LyXFont::UP_SHAPE);
71 mathed_char_dim(mi.base.font, char_, dim);
73 mathed_char_dim(mi.base.font, char_, dim);
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 mathed_char_dim(font_, char_, dim_);
83 if (isBinaryOp(char_, code_))
84 width_ += 2 * font_metrics::width(' ', font_);
85 lyxerr << "MathCharInset::metrics: " << dim << endl;
91 void MathCharInset::draw(PainterInfo & pi, int x, int y) const
93 //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
94 int const em = mathed_char_width(pi.base.font, 'M');
95 if (isBinaryOp(char_))
96 x += static_cast<int>(0.25*em+0.5);
97 else if (char_ == '\'')
98 x += static_cast<int>(0.0833*em+0.5);
100 if (char_ == '=' && has_math_fonts) {
101 FontSetChanger dummy(pi.base, "cmr");
102 pi.draw(x, y, char_);
103 } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
104 FontSetChanger dummy(pi.base, "cmm");
105 pi.draw(x, y, char_);
106 } else if (!slanted(char_) && pi.base.fontname == "mathnormal") {
107 ShapeChanger dummy(pi.base.font, LyXFont::UP_SHAPE);
108 pi.draw(x, y, char_);
110 pi.draw(x, y, char_);
113 drawChar(pain, font_, x, y, char_);
118 void MathCharInset::metricsT(TextMetricsInfo const &, Dimension & dim) const
126 void MathCharInset::drawT(TextPainter & pain, int x, int y) const
128 //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
129 pain.draw(x, y, char_);
133 void MathCharInset::write(WriteStream & os) const
139 void MathCharInset::normalize(NormalStream & os) const
141 os << "[char " << char_ << ' ' << "mathalpha]";
145 void MathCharInset::octave(OctaveStream & os) const
151 bool MathCharInset::isRelOp() const
153 return char_ == '=' || char_ == '<' || char_ == '>';