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