]> git.lyx.org Git - lyx.git/blob - src/mathed/math_symbolinset.C
architectural changes to tex2lyx
[lyx.git] / src / mathed / math_symbolinset.C
1 #include <config.h>
2
3 #include "math_symbolinset.h"
4 #include "dimension.h"
5 #include "math_mathmlstream.h"
6 #include "math_streamstr.h"
7 #include "math_support.h"
8 #include "math_parser.h"
9 #include "math_atom.h"
10 #include "LaTeXFeatures.h"
11 #include "debug.h"
12
13 using std::auto_ptr;
14
15
16 MathSymbolInset::MathSymbolInset(latexkeys const * l)
17         : sym_(l), h_(0)
18 {}
19
20
21 MathSymbolInset::MathSymbolInset(char const * name)
22         : sym_(in_word_set(name)), h_(0)
23 {}
24
25
26 MathSymbolInset::MathSymbolInset(string const & name)
27         : sym_(in_word_set(name.c_str())), h_(0)
28 {}
29
30
31 auto_ptr<InsetBase> MathSymbolInset::clone() const
32 {
33         return auto_ptr<InsetBase>(new MathSymbolInset(*this));
34 }
35
36
37 string MathSymbolInset::name() const
38 {
39         return sym_->name;
40 }
41
42
43 void MathSymbolInset::metrics(MetricsInfo & mi, Dimension & dim) const
44 {
45         //lyxerr << "metrics: symbol: '" << sym_->name
46         //      << "' in font: '" << sym_->inset
47         //      << "' drawn as: '" << sym_->draw
48         //      << "'\n";
49
50         int const em = mathed_char_width(mi.base.font, 'M');
51         FontSetChanger dummy(mi.base, sym_->inset.c_str());
52         mathed_string_dim(mi.base.font, sym_->draw, dim);
53         // correct height for broken cmex and wasy font
54         if (sym_->inset == "cmex" || sym_->inset == "wasy") {
55                 h_ = 4 * dim.des / 5;
56                 dim.asc += h_;
57                 dim.des -= h_;
58         }
59         // seperate things a bit
60         if (isRelOp())
61                 dim.wid += static_cast<int>(0.5 * em + 0.5);
62         else
63                 dim.wid += static_cast<int>(0.1667 * em + 0.5);
64
65         scriptable_ = false;
66         if (mi.base.style == LM_ST_DISPLAY)
67                 if (sym_->inset == "cmex" || sym_->extra == "funclim")
68                         scriptable_ = true;
69 }
70
71
72 void MathSymbolInset::draw(PainterInfo & pi, int x, int y) const
73 {
74         //lyxerr << "metrics: symbol: '" << sym_->name
75         //      << "' in font: '" << sym_->inset
76         //      << "' drawn as: '" << sym_->draw
77         //      << "'\n";
78         int const em = mathed_char_width(pi.base.font, 'M');
79         if (isRelOp())
80                 x += static_cast<int>(0.25*em+0.5);
81         else
82                 x += static_cast<int>(0.0833*em+0.5);
83
84         FontSetChanger dummy(pi.base, sym_->inset.c_str());
85         drawStr(pi, pi.base.font, x, y - h_, sym_->draw);
86 }
87
88
89 bool MathSymbolInset::isRelOp() const
90 {
91         return sym_->extra == "mathrel";
92 }
93
94
95 bool MathSymbolInset::isScriptable() const
96 {
97         return scriptable_;
98 }
99
100
101 bool MathSymbolInset::takesLimits() const
102 {
103         return
104                 sym_->inset == "cmex" ||
105                 sym_->inset == "lyxboldsymb" ||
106                 sym_->extra == "funclim";
107 }
108
109
110 void MathSymbolInset::validate(LaTeXFeatures & features) const
111 {
112         if (sym_->inset == "msa" || sym_->inset == "msb")
113                 features.require("amssymb");
114 }
115
116
117 void MathSymbolInset::normalize(NormalStream & os) const
118 {
119         os << "[symbol " << name() << ']';
120 }
121
122
123 void MathSymbolInset::maple(MapleStream & os) const
124 {
125         if (name() == "cdot")
126                 os << '*';
127         else if (name() == "infty")
128                 os << "infinity";
129         else
130                 os << name();
131 }
132
133 void MathSymbolInset::maxima(MaximaStream & os) const
134 {
135         if (name() == "cdot")
136                 os << '*';
137         else if (name() == "infty")
138                 os << "INF";
139         else
140                 os << name();
141 }
142
143
144 void MathSymbolInset::mathematica(MathematicaStream & os) const
145 {
146         if ( name() == "pi")    { os << "Pi"; return;}
147         if ( name() == "infty") { os << "Infinity"; return;}
148         os << name();
149 }
150
151
152 char const * MathMLtype(string const & s)
153 {
154         if (s == "mathop")
155                 return "mo";
156         return "mi";
157 }
158
159
160 bool MathSymbolInset::match(MathAtom const & at) const
161 {
162         MathSymbolInset const * q = at->asSymbolInset();
163         return q && name() == q->name();
164 }
165
166
167 void MathSymbolInset::mathmlize(MathMLStream & os) const
168 {
169         char const * type = MathMLtype(sym_->extra);
170         os << '<' << type << "> ";
171         if (sym_->xmlname == "x") // unknown so far
172                 os << name();
173         else
174                 os << sym_->xmlname;
175         os << " </" << type << '>';
176 }
177
178
179 void MathSymbolInset::octave(OctaveStream & os) const
180 {
181         if (name() == "cdot")
182                 os << '*';
183         else
184                 os << name();
185 }
186
187
188 void MathSymbolInset::write(WriteStream & os) const
189 {
190         os << '\\' << name();
191         os.pendingSpace(true);
192 }
193
194
195 void MathSymbolInset::infoize2(std::ostream & os) const
196 {
197         os << "Symbol: " << name();
198 }