]> git.lyx.org Git - lyx.git/blob - src/mathed/math_symbolinset.C
to much stuff for my liking...
[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         mi_ = mi;
76         MathTextCodes c = code();
77         if (sym_->latex_font_id > 0 && math_font_available(c)) {
78                 mathed_char_dim(c, mi_, sym_->latex_font_id, ascent_, descent_, width_);
79                 if (c == LM_TC_CMEX) {
80                         h_ = 4 * descent_ / 5;
81                         ascent_  += h_;
82                         descent_ -= h_;
83                 }
84         } else {
85                 if (sym_->id > 0 && sym_->id < 255 && math_font_available(LM_TC_SYMB))
86                         mathed_char_dim(code2(), mi_, sym_->id, ascent_, descent_, width_);
87                 else
88                         mathed_string_dim(LM_TC_TEX, mi_, sym_->name, ascent_, descent_, width_);
89         }
90         if (isRelOp())
91                 width_ += mathed_char_width(LM_TC_TEX, mi_, 'I');
92 }
93
94
95 void MathSymbolInset::draw(Painter & pain, int x, int y) const
96 {  
97         if (isRelOp())
98                 x += mathed_char_width(LM_TC_TEX, mi_, 'I') / 2;
99         MathTextCodes Code = code();
100         if (sym_->latex_font_id > 0 && math_font_available(Code))
101                 drawChar(pain, Code, mi_, x, y - h_, sym_->latex_font_id);
102         else if (sym_->id > 0 && sym_->id < 255 && math_font_available(LM_TC_SYMB))
103                 drawChar(pain, code2(), mi_, x, y, sym_->id);
104         else
105                 drawStr(pain, LM_TC_TEX, mi_, x, y, sym_->name);
106 }
107
108
109 bool MathSymbolInset::isRelOp() const
110 {       
111         return sym_->type == "mathrel";
112 }
113
114
115 bool MathSymbolInset::isScriptable() const
116 {
117         return mi_.style == LM_ST_DISPLAY && sym_->token == LM_TK_CMEX;
118 }
119
120
121 bool MathSymbolInset::takesLimits() const
122 {
123         return sym_->token == LM_TK_CMEX;
124 }
125
126 void MathSymbolInset::validate(LaTeXFeatures & features) const
127 {
128         if (sym_->token == LM_TK_MSA || sym_->token == LM_TK_MSB)
129                 features.require("amssymb");
130 }
131
132 void MathSymbolInset::normalize(NormalStream & os) const
133 {
134         os << "[symbol " << name() << "]";
135 }
136
137
138 void MathSymbolInset::maplize(MapleStream & os) const
139 {
140         if (name() == "cdot")
141                 os << '*';
142         else
143                 os << name();
144 }
145
146
147 char const * MathMLtype(string const & s)
148 {
149         if (s == "mathop")
150                 return "mo";
151         return "mi";
152 }
153
154
155 bool MathSymbolInset::match(MathInset * p) const
156 {
157         MathSymbolInset const * q = p->asSymbolInset();
158         return q && name() == q->name();
159 }
160
161
162 void MathSymbolInset::mathmlize(MathMLStream & os) const
163 {
164         char const * type = MathMLtype(sym_->type);
165         os << '<' << type << "> ";
166         if (sym_->xmlname == "x") // unknown so far
167                 os << name();
168         else
169                 os << sym_->xmlname;
170         os << " </" << type << '>';
171 }
172
173
174 void MathSymbolInset::octavize(OctaveStream & os) const
175 {
176         if (name() == "cdot")
177                 os << '*';
178         else
179                 os << name();
180 }
181
182
183 void MathSymbolInset::write(WriteStream & os) const
184 {
185         os << '\\' << name() << ' ';
186 }
187
188
189 void MathSymbolInset::infoize(ostream & os) const
190 {
191         os << '\\' << name();
192 }
193
194