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