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