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