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