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