]> git.lyx.org Git - lyx.git/blob - src/mathed/math_symbolinset.C
fix typo that put too many include paths for most people
[lyx.git] / src / mathed / math_symbolinset.C
1 #include <config.h>
2
3 #include "math_symbolinset.h"
4 #include "math_mathmlstream.h"
5 #include "math_streamstr.h"
6 #include "math_support.h"
7 #include "math_parser.h"
8 #include "LaTeXFeatures.h"
9 #include "debug.h"
10
11
12 using std::ostream;
13
14
15 MathSymbolInset::MathSymbolInset(const latexkeys * l)
16         : sym_(l), h_(0)
17 {}
18
19
20 MathSymbolInset::MathSymbolInset(const char * name)
21         : sym_(in_word_set(name)), h_(0)
22 {}
23
24
25 MathSymbolInset::MathSymbolInset(string const & name)
26         : sym_(in_word_set(name.c_str())), h_(0)
27 {}
28
29
30
31 MathInset * MathSymbolInset::clone() const
32 {
33         return new MathSymbolInset(*this);
34 }
35
36
37 MathTextCodes MathSymbolInset::code() const
38 {
39         switch (sym_->token) {
40         case LM_TK_CMR:
41                 return LM_TC_CMR;
42         case LM_TK_CMSY:
43                 return LM_TC_CMSY;
44         case LM_TK_CMM:
45                 return LM_TC_CMM;
46         case LM_TK_CMEX:
47                 return LM_TC_CMEX;
48         case LM_TK_MSA:
49                 return LM_TC_MSA;
50         case LM_TK_MSB:
51                 return LM_TC_MSB;
52         default:
53                 return LM_TC_SYMB;
54         }
55 }
56
57
58 MathTextCodes MathSymbolInset::code2() const
59 {
60         if (sym_->token == LM_TK_CMEX)
61                 return LM_TC_BOLDSYMB;
62         else
63                 return LM_TC_SYMB;
64 }
65
66
67 string MathSymbolInset::name() const
68 {
69         return sym_->name;
70 }
71
72
73 void MathSymbolInset::metrics(MathMetricsInfo const & mi) const
74 {
75         LyXFont font;
76         mi_ = mi;
77         MathTextCodes c = code();
78         if (sym_->latex_font_id > 0 && math_font_available(c)) {
79                 whichFont(font, c, mi_);
80                 mathed_char_dim(font, char(sym_->latex_font_id), ascent_, descent_, width_);
81                 if (c == LM_TC_CMEX) {
82                         h_ = 4 * descent_ / 5;
83                         ascent_  += h_;
84                         descent_ -= h_;
85                 }
86         } else {
87                 if (sym_->id > 0 && sym_->id < 255 && math_font_available(LM_TC_SYMB)) {
88                         whichFont(font, code2(), mi_);
89                         mathed_char_dim(font, char(sym_->id), ascent_, descent_, width_);
90                 } else {
91                         whichFont(font, LM_TC_TEX, mi_);
92                         mathed_string_dim(font, sym_->name, ascent_, descent_, width_);
93                 }
94         }
95         if (isRelOp())
96                 width_ += 6;
97 }
98
99
100 void MathSymbolInset::draw(Painter & pain, int x, int y) const
101 {
102         if (isRelOp())
103                 x += 3;
104         MathTextCodes Code = code();
105         LyXFont font;
106         if (sym_->latex_font_id > 0 && math_font_available(Code)) {
107                 whichFont(font, Code, mi_);
108                 drawChar(pain, font, x, y - h_, char(sym_->latex_font_id));
109         } else if (sym_->id > 0 && sym_->id < 255 && math_font_available(LM_TC_SYMB)){
110                 whichFont(font, code2(), mi_);
111                 drawChar(pain, font, x, y, char(sym_->id));
112         } else {
113                 whichFont(font, LM_TC_TEX, mi_);
114                 drawStr(pain, font, x, y, sym_->name);
115         }
116 }
117
118
119 bool MathSymbolInset::isRelOp() const
120 {
121         return sym_->type == "mathrel";
122 }
123
124
125 bool MathSymbolInset::isScriptable() const
126 {
127         return mi_.style == LM_ST_DISPLAY && sym_->token == LM_TK_CMEX;
128 }
129
130
131 bool MathSymbolInset::takesLimits() const
132 {
133         return sym_->token == LM_TK_CMEX;
134 }
135
136 void MathSymbolInset::validate(LaTeXFeatures & features) const
137 {
138         if (sym_->token == LM_TK_MSA || sym_->token == LM_TK_MSB)
139                 features.require("amssymb");
140 }
141
142 void MathSymbolInset::normalize(NormalStream & os) const
143 {
144         os << "[symbol " << name() << "]";
145 }
146
147
148 void MathSymbolInset::maplize(MapleStream & os) const
149 {
150         if (name() == "cdot")
151                 os << '*';
152         else
153                 os << name();
154 }
155
156
157 char const * MathMLtype(string const & s)
158 {
159         if (s == "mathop")
160                 return "mo";
161         return "mi";
162 }
163
164
165 bool MathSymbolInset::match(MathInset * p) const
166 {
167         MathSymbolInset const * q = p->asSymbolInset();
168         return q && name() == q->name();
169 }
170
171
172 void MathSymbolInset::mathmlize(MathMLStream & os) const
173 {
174         char const * type = MathMLtype(sym_->type);
175         os << '<' << type << "> ";
176         if (sym_->xmlname == "x") // unknown so far
177                 os << name();
178         else
179                 os << sym_->xmlname;
180         os << " </" << type << '>';
181 }
182
183
184 void MathSymbolInset::octavize(OctaveStream & os) const
185 {
186         if (name() == "cdot")
187                 os << '*';
188         else
189                 os << name();
190 }
191
192
193 void MathSymbolInset::write(WriteStream & os) const
194 {
195         os << '\\' << name() << ' ';
196 }
197
198
199 void MathSymbolInset::infoize(ostream & os) const
200 {
201         os << '\\' << name();
202 }