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