]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSymbol.C
fix reading UTF8 encoded symbol file
[lyx.git] / src / mathed / InsetMathSymbol.C
1 /**
2  * \file InsetMathSymbol.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathSymbol.h"
14 #include "dimension.h"
15 #include "MathStream.h"
16 #include "MathStream.h"
17 #include "MathSupport.h"
18 #include "MathParser.h"
19 #include "MathAtom.h"
20 #include "LaTeXFeatures.h"
21 #include "debug.h"
22
23
24 namespace lyx {
25
26 using std::string;
27 using std::auto_ptr;
28
29
30 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
31         : sym_(l), h_(0), width_(0), scriptable_(false)
32 {}
33
34
35 InsetMathSymbol::InsetMathSymbol(char const * name)
36         : sym_(in_word_set(from_ascii(name))), h_(0), width_(0), scriptable_(false)
37 {}
38
39
40 InsetMathSymbol::InsetMathSymbol(docstring const & name)
41         : sym_(in_word_set(name)), h_(0), width_(0), scriptable_(false)
42 {}
43
44
45 auto_ptr<InsetBase> InsetMathSymbol::doClone() const
46 {
47         return auto_ptr<InsetBase>(new InsetMathSymbol(*this));
48 }
49
50
51 docstring InsetMathSymbol::name() const
52 {
53         return sym_->name;
54 }
55
56
57 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
58 {
59         //lyxerr << "metrics: symbol: '" << sym_->name
60         //      << "' in font: '" << sym_->inset
61         //      << "' drawn as: '" << sym_->draw
62         //      << "'" << std::endl;
63
64         int const em = mathed_char_width(mi.base.font, 'M');
65         FontSetChanger dummy(mi.base, sym_->inset);
66         mathed_string_dim(mi.base.font, sym_->draw, dim);
67         // correct height for broken cmex and wasy font
68 #if defined(__APPLE__) && defined(__GNUC__)
69         if (sym_->inset == "cmex") {
70                 h_ = 4 * dim.des / 5;
71                 dim.asc += 0*h_;
72                 dim.des -= h_;
73                 h_ = dim.asc;
74         } else if (sym_->inset == "wasy") {
75                 h_ = 4 * dim.des / 5;
76                 dim.asc += h_;
77                 dim.des -= h_;
78         }
79 #else
80         if (sym_->inset == "cmex" || sym_->inset == "wasy") {
81                 h_ = 4 * dim.des / 5;
82                 dim.asc += h_;
83                 dim.des -= h_;
84         }
85 #endif
86         // seperate things a bit
87         if (isRelOp())
88                 dim.wid += static_cast<int>(0.5 * em + 0.5);
89         else
90                 dim.wid += static_cast<int>(0.1667 * em + 0.5);
91
92         scriptable_ = false;
93         if (mi.base.style == LM_ST_DISPLAY)
94                 if (sym_->inset == "cmex" || sym_->extra == "funclim")
95                         scriptable_ = true;
96
97         width_ = dim.wid;
98 }
99
100
101 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
102 {
103         //lyxerr << "metrics: symbol: '" << sym_->name
104         //      << "' in font: '" << sym_->inset
105         //      << "' drawn as: '" << sym_->draw
106         //      << "'" << std::endl;
107         int const em = mathed_char_width(pi.base.font, 'M');
108         if (isRelOp())
109                 x += static_cast<int>(0.25*em+0.5);
110         else
111                 x += static_cast<int>(0.0833*em+0.5);
112
113         FontSetChanger dummy(pi.base, sym_->inset.c_str());
114         // FIXME UNICODE
115         docstring n(sym_->draw.begin(), sym_->draw.end());
116         pi.draw(x, y - h_, n);
117 }
118
119
120 bool InsetMathSymbol::isRelOp() const
121 {
122         return sym_->extra == "mathrel";
123 }
124
125
126 bool InsetMathSymbol::isScriptable() const
127 {
128         return scriptable_;
129 }
130
131
132 bool InsetMathSymbol::takesLimits() const
133 {
134         return
135                 sym_->inset == "cmex" ||
136                 sym_->inset == "lyxboldsymb" ||
137                 sym_->extra == "funclim";
138 }
139
140
141 void InsetMathSymbol::validate(LaTeXFeatures & features) const
142 {
143         if (!sym_->requires.empty())
144                 features.require(to_utf8(sym_->requires));
145 }
146
147
148 void InsetMathSymbol::normalize(NormalStream & os) const
149 {
150         os << "[symbol " << name() << ']';
151 }
152
153
154 void InsetMathSymbol::maple(MapleStream & os) const
155 {
156         if (name() == "cdot")
157                 os << '*';
158         else if (name() == "infty")
159                 os << "infinity";
160         else
161                 os << name();
162 }
163
164 void InsetMathSymbol::maxima(MaximaStream & os) const
165 {
166         if (name() == "cdot")
167                 os << '*';
168         else if (name() == "infty")
169                 os << "inf";
170         else if (name() == "pi")
171                 os << "%pi";
172         else
173                 os << name();
174 }
175
176
177 void InsetMathSymbol::mathematica(MathematicaStream & os) const
178 {
179         if ( name() == "pi")    { os << "Pi"; return;}
180         if ( name() == "infty") { os << "Infinity"; return;}
181         if ( name() == "cdot")  { os << '*'; return;}
182         os << name();
183 }
184
185
186 char const * MathMLtype(docstring const & s)
187 {
188         if (s == "mathop")
189                 return "mo";
190         return "mi";
191 }
192
193
194 void InsetMathSymbol::mathmlize(MathStream & os) const
195 {
196         char const * type = MathMLtype(sym_->extra);
197         os << '<' << type << "> ";
198         if (sym_->xmlname == "x") // unknown so far
199                 os << name();
200         else
201                 os << sym_->xmlname;
202         os << " </" << type << '>';
203 }
204
205
206 void InsetMathSymbol::octave(OctaveStream & os) const
207 {
208         if (name() == "cdot")
209                 os << '*';
210         else
211                 os << name();
212 }
213
214
215 void InsetMathSymbol::write(WriteStream & os) const
216 {
217         os << '\\' << name();
218         os.pendingSpace(true);
219 }
220
221
222 void InsetMathSymbol::infoize2(odocstream & os) const
223 {
224         os << "Symbol: " << name();
225 }
226
227
228 } // namespace lyx