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