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