]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSymbol.cpp
Merge branch 'master' of git.lyx.org:lyx
[lyx.git] / src / mathed / InsetMathSymbol.cpp
1 /**
2  * \file InsetMathSymbol.cpp
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
15 #include "Dimension.h"
16 #include "LaTeXFeatures.h"
17 #include "MathAtom.h"
18 #include "MathParser.h"
19 #include "MathStream.h"
20 #include "MathSupport.h"
21
22 #include "support/debug.h"
23 #include "support/docstream.h"
24 #include "support/textutils.h"
25
26
27 namespace lyx {
28
29 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
30         : sym_(l), h_(0), scriptable_(false)
31 {}
32
33
34 InsetMathSymbol::InsetMathSymbol(char const * name)
35         : sym_(in_word_set(from_ascii(name))), h_(0), scriptable_(false)
36 {}
37
38
39 InsetMathSymbol::InsetMathSymbol(docstring const & name)
40         : sym_(in_word_set(name)), h_(0), scriptable_(false)
41 {}
42
43
44 Inset * InsetMathSymbol::clone() const
45 {
46         return new InsetMathSymbol(*this);
47 }
48
49
50 docstring InsetMathSymbol::name() const
51 {
52         return sym_->name;
53 }
54
55
56 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
57 {
58         //lyxerr << "metrics: symbol: '" << sym_->name
59         //      << "' in font: '" << sym_->inset
60         //      << "' drawn as: '" << sym_->draw
61         //      << "'" << endl;
62
63         bool const italic_upcase_greek = sym_->inset == "cmr" &&
64                                          sym_->extra == "mathalpha" &&
65                                          mi.base.fontname == "mathit";
66         docstring const font = italic_upcase_greek ? from_ascii("cmm") : sym_->inset;
67         int const em = mathed_char_width(mi.base.font, 'M');
68         FontSetChanger dummy(mi.base, font);
69         mathed_string_dim(mi.base.font, sym_->draw, dim);
70         docstring::const_reverse_iterator rit = sym_->draw.rbegin();
71         kerning_ = mathed_char_kerning(mi.base.font, *rit);
72         // correct height for broken cmex and wasy font
73         if (sym_->inset == "cmex" || sym_->inset == "wasy") {
74                 h_ = 4 * dim.des / 5;
75                 dim.asc += h_;
76                 dim.des -= h_;
77         }
78         // seperate things a bit
79         if (isRelOp())
80                 dim.wid += static_cast<int>(0.5 * em + 0.5);
81         else
82                 dim.wid += static_cast<int>(0.1667 * em + 0.5);
83
84         scriptable_ = false;
85         if (mi.base.style == LM_ST_DISPLAY)
86                 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
87                     sym_->extra == "funclim")
88                         scriptable_ = true;
89 }
90
91
92 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
93 {
94         //lyxerr << "metrics: symbol: '" << sym_->name
95         //      << "' in font: '" << sym_->inset
96         //      << "' drawn as: '" << sym_->draw
97         //      << "'" << endl;
98
99         bool const italic_upcase_greek = sym_->inset == "cmr" &&
100                                          sym_->extra == "mathalpha" &&
101                                          pi.base.fontname == "mathit";
102         docstring const font = italic_upcase_greek ? from_ascii("cmm") : sym_->inset;
103         int const em = mathed_char_width(pi.base.font, 'M');
104         if (isRelOp())
105                 x += static_cast<int>(0.25*em+0.5);
106         else
107                 x += static_cast<int>(0.0833*em+0.5);
108
109         FontSetChanger dummy(pi.base, font);
110         pi.draw(x, y - h_, sym_->draw);
111 }
112
113
114 bool InsetMathSymbol::isRelOp() const
115 {
116         return sym_->extra == "mathrel";
117 }
118
119
120 bool InsetMathSymbol::isOrdAlpha() const
121 {
122         return sym_->extra == "mathord" || sym_->extra == "mathalpha";
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_->inset == "esint" ||
138                 sym_->extra == "funclim";
139 }
140
141
142 void InsetMathSymbol::normalize(NormalStream & os) const
143 {
144         os << "[symbol " << name() << ']';
145 }
146
147
148 void InsetMathSymbol::maple(MapleStream & os) const
149 {
150         if (name() == "cdot")
151                 os << '*';
152         else if (name() == "infty")
153                 os << "infinity";
154         else
155                 os << name();
156 }
157
158 void InsetMathSymbol::maxima(MaximaStream & os) const
159 {
160         if (name() == "cdot")
161                 os << '*';
162         else if (name() == "infty")
163                 os << "inf";
164         else if (name() == "pi")
165                 os << "%pi";
166         else
167                 os << name();
168 }
169
170
171 void InsetMathSymbol::mathematica(MathematicaStream & os) const
172 {
173         if ( name() == "pi")    { os << "Pi"; return;}
174         if ( name() == "infty") { os << "Infinity"; return;}
175         if ( name() == "cdot")  { os << '*'; return;}
176         os << name();
177 }
178
179
180 // FIXME This will likely need some work.
181 char const * MathMLtype(docstring const & s)
182 {
183         if (s == "mathord")
184                 return "mi";
185         return "mo";
186 }
187
188
189 void InsetMathSymbol::mathmlize(MathStream & os) const
190 {
191         // FIXME We may need to do more interesting things 
192         // with MathMLtype.
193         char const * type = MathMLtype(sym_->extra);
194         os << '<' << type << "> ";
195         if (sym_->xmlname == "x") 
196                 // unknown so far
197                 os << name();
198         else
199                 os << sym_->xmlname;
200         os << " </" << type << '>';
201 }
202
203
204 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
205 {
206         // FIXME We may need to do more interesting things 
207         // with MathMLtype.
208         char const * type = MathMLtype(sym_->extra);
209         bool op = (std::string(type) == "mo");
210         
211         if (sym_->xmlname == "x") 
212                 // unknown so far
213                 os << ' ' << name() << ' ';
214         else if (op && spacing) 
215                 os << ' ' << sym_->xmlname << ' ';
216         else
217                 os << sym_->xmlname;
218 }
219
220
221 void InsetMathSymbol::htmlize(HtmlStream & os) const
222 {
223         htmlize(os, true);
224 }
225
226
227 void InsetMathSymbol::octave(OctaveStream & os) const
228 {
229         if (name() == "cdot")
230                 os << '*';
231         else
232                 os << name();
233 }
234
235
236 void InsetMathSymbol::write(WriteStream & os) const
237 {
238         MathEnsurer ensurer(os);
239         os << '\\' << name();
240
241         // $,#, etc. In theory the restriction based on catcodes, but then
242         // we do not handle catcodes very well, let alone cat code changes,
243         // so being outside the alpha range is good enough.
244         if (name().size() == 1 && !isAlphaASCII(name()[0]))
245                 return;
246
247         os.pendingSpace(true);
248 }
249
250
251 void InsetMathSymbol::infoize2(odocstream & os) const
252 {
253         os << from_ascii("Symbol: ") << name();
254 }
255
256
257 void InsetMathSymbol::validate(LaTeXFeatures & features) const
258 {
259         // this is not really the ideal place to do this, but we can't
260         // validate in InsetMathExInt.
261         if (features.runparams().math_flavor == OutputParams::MathAsHTML
262             && sym_->name == from_ascii("int")) {
263                 features.addCSSSnippet(
264                         "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
265                         "span.limits span{display: block;}\n"
266                         "span.intsym{font-size: 150%;}\n"
267                         "sub.limit{font-size: 75%;}\n"
268                         "sup.limit{font-size: 75%;}");
269         } else {
270                 if (!sym_->requires.empty())
271                         features.require(to_utf8(sym_->requires));
272         }
273 }
274
275 } // namespace lyx