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