]> git.lyx.org Git - lyx.git/blob - src/mathed/math_charinset.C
Jean-Marc's fix for wrong descent
[lyx.git] / src / mathed / math_charinset.C
1 #include <config.h>
2
3
4 #include "math_charinset.h"
5 #include "LColor.h"
6 #include "frontends/Painter.h"
7 #include "frontends/font_metrics.h"
8 #include "support/LOstream.h"
9 #include "debug.h"
10 #include "math_support.h"
11 #include "math_mathmlstream.h"
12 #include "LaTeXFeatures.h"
13 #include "textpainter.h"
14
15 #include <cctype>
16 #include <cstring>
17
18
19 using std::ostream;
20 using std::endl;
21
22 #ifndef CXX_GLOBAL_CSTD
23 using std::strchr;
24 using std::isalpha;
25 #endif
26
27 extern bool has_math_fonts;
28
29 namespace {
30
31         bool isBinaryOp(char c)
32         {
33                 return strchr("+-<>=/*", c);
34         }
35
36
37         bool slanted(char c)
38         {
39                 //if (strchr("0123456789;:!|[]().,?+/-*<>=", c)
40                 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
41         }
42
43 }
44
45
46 MathCharInset::MathCharInset(char c)
47         : char_(c)
48 {}
49
50
51
52 MathInset * MathCharInset::clone() const
53 {
54         return new MathCharInset(*this);
55 }
56
57
58 void MathCharInset::metrics(MathMetricsInfo & mi) const
59 {
60 #if 1
61         if (char_ == '=' && has_math_fonts) {
62                 MathFontSetChanger dummy(mi.base, "cmr");
63                 mathed_char_dim(mi.base.font, char_, dim_);
64         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
65                 MathFontSetChanger dummy(mi.base, "cmm");
66                 mathed_char_dim(mi.base.font, char_, dim_);
67         } else if (slanted(char_) && mi.base.fontname == "mathnormal") {
68                 MathShapeChanger dummy(mi.base.font, LyXFont::ITALIC_SHAPE);
69                 mathed_char_dim(mi.base.font, char_, dim_);
70         } else {
71                 mathed_char_dim(mi.base.font, char_, dim_);
72         }
73         int const em = mathed_char_width(mi.base.font, 'M');
74         if (isBinaryOp(char_))
75                 dim_.w += static_cast<int>(0.5*em+0.5);
76         else if (char_ == '\'')
77                 dim_.w += static_cast<int>(0.1667*em+0.5);
78 #else
79         whichFont(font_, code_, mi);
80         mathed_char_dim(font_, char_, dim_);
81         if (isBinaryOp(char_, code_))
82                 width_ += 2 * font_metrics::width(' ', font_);
83         lyxerr << "MathCharInset::metrics: " << dim_ << "\n";
84 #endif
85 }
86
87
88 void MathCharInset::draw(MathPainterInfo & pi, int x, int y) const
89 {
90         //lyxerr << "drawing '" << char_ << "' font: " << pi.base.fontname << endl;
91         int const em = mathed_char_width(pi.base.font, 'M');
92         if (isBinaryOp(char_))
93                 x += static_cast<int>(0.25*em+0.5);
94         else if (char_ == '\'')
95                 x += static_cast<int>(0.0833*em+0.5);
96 #if 1
97         if (char_ == '=' && has_math_fonts) {
98                 MathFontSetChanger dummy(pi.base, "cmr");
99                 pi.draw(x, y, char_);
100         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
101                 MathFontSetChanger dummy(pi.base, "cmm");
102                 pi.draw(x, y, char_);
103         } else if (slanted(char_) && pi.base.fontname == "mathnormal") {
104                 MathShapeChanger dummy(pi.base.font, LyXFont::ITALIC_SHAPE);
105                 pi.draw(x, y, char_);
106         } else {
107                 pi.draw(x, y, char_);
108         }
109 #else
110         drawChar(pain, font_, x, y, char_);
111 #endif
112 }
113
114
115 void MathCharInset::metricsT(TextMetricsInfo const &) const
116 {
117         dim_.w = 1;
118         dim_.a = 1;
119         dim_.d = 0;
120 }
121
122
123 void MathCharInset::drawT(TextPainter & pain, int x, int y) const
124 {
125         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
126         pain.draw(x, y, char_);
127 }
128
129
130 void MathCharInset::write(WriteStream & os) const
131 {
132         os << char_;
133 }
134
135
136 void MathCharInset::normalize(NormalStream & os) const
137 {
138         os << "[char " << char_ << ' ' << "mathalpha]";
139 }
140
141
142 void MathCharInset::octave(OctaveStream & os) const
143 {
144         os << char_;
145 }
146
147
148 bool MathCharInset::isRelOp() const
149 {
150         return char_ == '=' || char_ == '<' || char_ == '>';
151 }
152
153
154 bool MathCharInset::match(MathInset const * p) const
155 {
156         MathCharInset const * q = p->asCharInset();
157         return q && char_ == q->char_;
158 }