]> git.lyx.org Git - lyx.git/blob - src/mathed/math_charinset.C
Switch from SigC signals to boost::signals
[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
31 bool isBinaryOp(char c, MathTextCodes type)
32 {
33         return type < LM_TC_SYMB && strchr("+-<>=/*", c);
34 }
35
36
37 MathCharInset::MathCharInset(char c)
38         : char_(c), code_(nativeCode(c))
39 {
40 //lyxerr << "creating char '" << char_ << "' with code " << int(code_) << endl;
41 }
42
43
44 MathCharInset::MathCharInset(char c, MathTextCodes t)
45         : char_(c), code_((t == LM_TC_MIN) ? nativeCode(c) : t)
46 {
47 //lyxerr << "creating char '" << char_ << "' with code " << int(code_) << endl;
48 }
49
50
51 MathTextCodes MathCharInset::nativeCode(char c)
52 {
53         if (isalpha(c))
54                 return LM_TC_VAR;
55         //if (strchr("0123456789;:!|[]().,?+/-*<>=", c)
56         return LM_TC_CONST;
57 }
58
59
60 MathInset * MathCharInset::clone() const
61 {
62         return new MathCharInset(*this);
63 }
64
65
66 void MathCharInset::metrics(MathMetricsInfo const & mi) const
67 {
68         whichFont(font_, code_, mi);
69         mathed_char_dim(font_, char_, ascent_, descent_, width_);
70         if (isBinaryOp(char_, code_))
71                 width_ += 2 * font_metrics::width(' ', font_);
72 }
73
74
75 void MathCharInset::draw(Painter & pain, int x, int y) const
76 {
77         //lyxerr << "drawing '" << char_ << "' code: " << code_ << endl;
78         if (isBinaryOp(char_, code_))
79                 x += font_metrics::width(' ', font_);
80         drawChar(pain, font_, x, y, char_);
81 }
82
83
84 void MathCharInset::metricsT(TextMetricsInfo const &) const
85 {
86         width_   = 1;
87         ascent_  = 1;
88         descent_ = 0;
89 }
90
91
92 void MathCharInset::drawT(TextPainter & pain, int x, int y) const
93 {
94         //lyxerr << "drawing text '" << char_ << "' code: " << code_ << endl;
95         pain.draw(x, y, char_);
96 }
97
98
99 void MathCharInset::writeHeader(ostream & os) const
100 {
101         if (math_font_name(code_))
102                 os << '\\' << math_font_name(code_) << '{';
103 }
104
105
106 void MathCharInset::writeTrailer(ostream & os) const
107 {
108         if (math_font_name(code_))
109                 os << '}';
110 }
111
112
113 void MathCharInset::writeRaw(ostream & os) const
114 {
115         os << char_;
116 }
117
118
119 void MathCharInset::write(WriteStream & os) const
120 {
121         writeHeader(os.os());
122         writeRaw(os.os());
123         writeTrailer(os.os());
124 }
125
126
127 void MathCharInset::normalize(NormalStream & os) const
128 {
129         os << "[char " << char_ << " " << "mathalpha" << "]";
130 }
131
132
133 bool MathCharInset::isRelOp() const
134 {
135         return char_ == '=' || char_ == '<' || char_ == '>';
136 }
137
138
139 void MathCharInset::handleFont(MathTextCodes t)
140 {
141         code_ = (code_ == t) ? LM_TC_VAR : t;
142 }
143
144
145 void MathCharInset::validate(LaTeXFeatures & features) const
146 {
147         // Make sure amssymb is put in preamble if Blackboard Bold or
148         // Fraktur used:
149         if ((code_ == LM_TC_BB) || (code_ == LM_TC_EUFRAK))
150                 features.require("amssymb");
151 }
152
153
154 bool MathCharInset::match(MathInset * p) const
155 {
156         MathCharInset const * q = p->asCharInset();
157         return q && char_ == q->char_ && code_ == q->code_;
158 }