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