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