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