]> git.lyx.org Git - lyx.git/blob - src/mathed/math_charinset.C
split LyXText::rowlist_ into individual Paragraph::rows_ chunks
[lyx.git] / src / mathed / math_charinset.C
1 #include <config.h>
2
3 #include "math_charinset.h"
4 #include "LColor.h"
5 #include "dimension.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 using std::auto_ptr;
22
23 #ifndef CXX_GLOBAL_CSTD
24 using std::strchr;
25 using std::isalpha;
26 #endif
27
28 extern bool has_math_fonts;
29
30 namespace {
31
32         bool isBinaryOp(char c)
33         {
34                 return strchr("+-<>=/*", c);
35         }
36
37
38         bool slanted(char c)
39         {
40                 //if (strchr("0123456789;:!|[]().,?+/-*<>=", c)
41                 return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
42         }
43
44 }
45
46
47 MathCharInset::MathCharInset(char c)
48         : char_(c)
49 {}
50
51
52
53 auto_ptr<InsetBase> MathCharInset::clone() const
54 {
55         return auto_ptr<InsetBase>(new MathCharInset(*this));
56 }
57
58
59 void MathCharInset::metrics(MetricsInfo & mi, Dimension & dim) const
60 {
61 #if 1
62         if (char_ == '=' && has_math_fonts) {
63                 FontSetChanger dummy(mi.base, "cmr");
64                 mathed_char_dim(mi.base.font, char_, dim);
65         } else if ((char_ == '>' || char_ == '<') && has_math_fonts) {
66                 FontSetChanger dummy(mi.base, "cmm");
67                 mathed_char_dim(mi.base.font, char_, dim);
68         } else if (slanted(char_) && mi.base.fontname == "mathnormal") {
69                 ShapeChanger dummy(mi.base.font, LyXFont::ITALIC_SHAPE);
70                 mathed_char_dim(mi.base.font, char_, dim);
71         } else {
72                 mathed_char_dim(mi.base.font, char_, dim);
73         }
74         int const em = mathed_char_width(mi.base.font, 'M');
75         if (isBinaryOp(char_))
76                 dim.wid += static_cast<int>(0.5*em+0.5);
77         else if (char_ == '\'')
78                 dim.wid += static_cast<int>(0.1667*em+0.5);
79 #else
80         whichFont(font_, code_, mi);
81         mathed_char_dim(font_, char_, dim_);
82         if (isBinaryOp(char_, code_))
83                 width_ += 2 * font_metrics::width(' ', font_);
84         lyxerr << "MathCharInset::metrics: " << dim << endl;
85 #endif
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 }