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