]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSymbol.cpp
8e98db747ed19a5536a9845c88f2991124788195
[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         // set dim
64         mathedSymbolDim(mi.base, dim, sym_);
65         // set kerning_
66         kerning_ = mathed_char_kerning(mi.base.font, *sym_->draw.rbegin());
67         // correct height for broken cmex and wasy font
68         if (sym_->inset == "cmex" || sym_->inset == "wasy") {
69                 h_ = 4 * dim.des / 5;
70                 dim.asc += h_;
71                 dim.des -= h_;
72         }
73         // set scriptable_
74         scriptable_ = false;
75         if (mi.base.font.style() == DISPLAY_STYLE)
76                 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
77                     sym_->extra == "funclim" ||
78                     (sym_->inset == "stmry" && sym_->extra == "mathop"))
79                         scriptable_ = true;
80 }
81
82
83 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
84 {
85         mathedSymbolDraw(pi, x, y - h_, sym_);
86 }
87
88
89 InsetMath::mode_type InsetMathSymbol::currentMode() const
90 {
91         return sym_->extra == "textmode" ? TEXT_MODE : MATH_MODE;
92 }
93
94
95 bool InsetMathSymbol::isOrdAlpha() const
96 {
97         return sym_->extra == "mathord" || sym_->extra == "mathalpha";
98 }
99
100
101 MathClass InsetMathSymbol::mathClass() const
102 {
103         if (sym_->extra == "func" || sym_->extra == "funclim")
104                 return MC_OP;
105         MathClass const mc = string_to_class(sym_->extra);
106         return (mc == MC_UNKNOWN) ? MC_ORD : mc;
107 }
108
109
110 bool InsetMathSymbol::isScriptable() const
111 {
112         return scriptable_;
113 }
114
115
116 bool InsetMathSymbol::takesLimits() const
117 {
118         return
119                 sym_->inset == "cmex" ||
120                 sym_->inset == "lyxboldsymb" ||
121                 sym_->inset == "esint" ||
122                 sym_->extra == "funclim" ||
123                 (sym_->inset == "stmry" && sym_->extra == "mathop");
124 }
125
126
127 void InsetMathSymbol::normalize(NormalStream & os) const
128 {
129         os << "[symbol " << name() << ']';
130 }
131
132
133 void InsetMathSymbol::maple(MapleStream & os) const
134 {
135         if (name() == "cdot")
136                 os << '*';
137         else if (name() == "infty")
138                 os << "infinity";
139         else
140                 os << name();
141 }
142
143 void InsetMathSymbol::maxima(MaximaStream & os) const
144 {
145         if (name() == "cdot")
146                 os << '*';
147         else if (name() == "infty")
148                 os << "inf";
149         else if (name() == "pi")
150                 os << "%pi";
151         else
152                 os << name();
153 }
154
155
156 void InsetMathSymbol::mathematica(MathematicaStream & os) const
157 {
158         if ( name() == "pi")    { os << "Pi"; return;}
159         if ( name() == "infty") { os << "Infinity"; return;}
160         if ( name() == "cdot")  { os << '*'; return;}
161         os << name();
162 }
163
164
165 void InsetMathSymbol::mathmlize(MathStream & ms) const
166 {
167         // FIXME We may need to do more interesting things
168         // with MathMLtype.
169         docstring tag = from_ascii(ms.namespacedTag(sym_->MathMLtype()));
170         ms << '<' << tag << ">";
171         if ((ms.xmlMode() && sym_->xmlname == "x") || (!ms.xmlMode() && sym_->htmlname == "x"))
172                 // unknown so far
173                 ms << name();
174         else if (ms.xmlMode())
175                 ms << sym_->xmlname;
176         else
177                 ms << sym_->htmlname;
178         ms << "</" << tag << '>';
179 }
180
181
182 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
183 {
184         // FIXME We may need to do more interesting things
185         // with MathMLtype.
186         char const * type = sym_->MathMLtype();
187         bool op = (std::string(type) == "mo");
188
189         if (sym_->htmlname == "x")
190                 // unknown so far
191                 os << ' ' << name() << ' ';
192         else if (op && spacing)
193                 os << ' ' << sym_->htmlname << ' ';
194         else
195                 os << sym_->htmlname;
196 }
197
198
199 void InsetMathSymbol::htmlize(HtmlStream & os) const
200 {
201         htmlize(os, true);
202 }
203
204
205 void InsetMathSymbol::octave(OctaveStream & os) const
206 {
207         if (name() == "cdot")
208                 os << '*';
209         else
210                 os << name();
211 }
212
213
214 void InsetMathSymbol::write(WriteStream & os) const
215 {
216         unique_ptr<MathEnsurer> ensurer;
217         if (currentMode() != TEXT_MODE)
218                 ensurer = make_unique<MathEnsurer>(os);
219         else
220                 ensurer = make_unique<MathEnsurer>(os, false, true, true);
221         os << '\\' << name();
222
223         // $,#, etc. In theory the restriction based on catcodes, but then
224         // we do not handle catcodes very well, let alone cat code changes,
225         // so being outside the alpha range is good enough.
226         if (name().size() == 1 && !isAlphaASCII(name()[0]))
227                 return;
228
229         os.pendingSpace(true);
230 }
231
232
233 void InsetMathSymbol::infoize2(odocstream & os) const
234 {
235         os << from_ascii("Symbol: ") << name();
236 }
237
238
239 void InsetMathSymbol::validate(LaTeXFeatures & features) const
240 {
241         // this is not really the ideal place to do this, but we can't
242         // validate in InsetMathExInt.
243         if (features.runparams().math_flavor == OutputParams::MathAsHTML
244             && sym_->name == from_ascii("int")) {
245                 features.addCSSSnippet(
246                         "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
247                         "span.limits span{display: block;}\n"
248                         "span.intsym{font-size: 150%;}\n"
249                         "sub.limit{font-size: 75%;}\n"
250                         "sup.limit{font-size: 75%;}");
251         } else {
252                 if (!sym_->required.empty())
253                         features.require(sym_->required);
254         }
255 }
256
257 } // namespace lyx