]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSymbol.cpp
Display properly math characters that behave like symbols
[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, 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 striptable_
74         scriptable_ = false;
75         if (mi.base.style == LM_ST_DISPLAY)
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::isMathBin() const
96 {
97         return sym_->extra == "mathbin";
98 }
99
100
101 bool InsetMathSymbol::isMathRel() const
102 {
103         return sym_->extra == "mathrel";
104 }
105
106
107 bool InsetMathSymbol::isMathPunct() const
108 {
109         return sym_->extra == "mathpunct";
110 }
111
112
113 bool InsetMathSymbol::isOrdAlpha() const
114 {
115         return sym_->extra == "mathord" || sym_->extra == "mathalpha";
116 }
117
118
119 bool InsetMathSymbol::isScriptable() const
120 {
121         return scriptable_;
122 }
123
124
125 bool InsetMathSymbol::takesLimits() const
126 {
127         return
128                 sym_->inset == "cmex" ||
129                 sym_->inset == "lyxboldsymb" ||
130                 sym_->inset == "esint" ||
131                 sym_->extra == "funclim" ||
132                 (sym_->inset == "stmry" && sym_->extra == "mathop");
133 }
134
135
136 void InsetMathSymbol::normalize(NormalStream & os) const
137 {
138         os << "[symbol " << name() << ']';
139 }
140
141
142 void InsetMathSymbol::maple(MapleStream & os) const
143 {
144         if (name() == "cdot")
145                 os << '*';
146         else if (name() == "infty")
147                 os << "infinity";
148         else
149                 os << name();
150 }
151
152 void InsetMathSymbol::maxima(MaximaStream & os) const
153 {
154         if (name() == "cdot")
155                 os << '*';
156         else if (name() == "infty")
157                 os << "inf";
158         else if (name() == "pi")
159                 os << "%pi";
160         else
161                 os << name();
162 }
163
164
165 void InsetMathSymbol::mathematica(MathematicaStream & os) const
166 {
167         if ( name() == "pi")    { os << "Pi"; return;}
168         if ( name() == "infty") { os << "Infinity"; return;}
169         if ( name() == "cdot")  { os << '*'; return;}
170         os << name();
171 }
172
173
174 void InsetMathSymbol::mathmlize(MathStream & os) const
175 {
176         // FIXME We may need to do more interesting things
177         // with MathMLtype.
178         char const * type = sym_->MathMLtype();
179         os << '<' << type << "> ";
180         if (sym_->xmlname == "x")
181                 // unknown so far
182                 os << name();
183         else
184                 os << sym_->xmlname;
185         os << " </" << type << '>';
186 }
187
188
189 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
190 {
191         // FIXME We may need to do more interesting things
192         // with MathMLtype.
193         char const * type = sym_->MathMLtype();
194         bool op = (std::string(type) == "mo");
195
196         if (sym_->xmlname == "x")
197                 // unknown so far
198                 os << ' ' << name() << ' ';
199         else if (op && spacing)
200                 os << ' ' << sym_->xmlname << ' ';
201         else
202                 os << sym_->xmlname;
203 }
204
205
206 void InsetMathSymbol::htmlize(HtmlStream & os) const
207 {
208         htmlize(os, true);
209 }
210
211
212 void InsetMathSymbol::octave(OctaveStream & os) const
213 {
214         if (name() == "cdot")
215                 os << '*';
216         else
217                 os << name();
218 }
219
220
221 void InsetMathSymbol::write(WriteStream & os) const
222 {
223         unique_ptr<MathEnsurer> ensurer;
224         if (currentMode() != TEXT_MODE)
225                 ensurer = make_unique<MathEnsurer>(os);
226         else
227                 ensurer = make_unique<MathEnsurer>(os, false, true, true);
228         os << '\\' << name();
229
230         // $,#, etc. In theory the restriction based on catcodes, but then
231         // we do not handle catcodes very well, let alone cat code changes,
232         // so being outside the alpha range is good enough.
233         if (name().size() == 1 && !isAlphaASCII(name()[0]))
234                 return;
235
236         os.pendingSpace(true);
237 }
238
239
240 void InsetMathSymbol::infoize2(odocstream & os) const
241 {
242         os << from_ascii("Symbol: ") << name();
243 }
244
245
246 void InsetMathSymbol::validate(LaTeXFeatures & features) const
247 {
248         // this is not really the ideal place to do this, but we can't
249         // validate in InsetMathExInt.
250         if (features.runparams().math_flavor == OutputParams::MathAsHTML
251             && sym_->name == from_ascii("int")) {
252                 features.addCSSSnippet(
253                         "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
254                         "span.limits span{display: block;}\n"
255                         "span.intsym{font-size: 150%;}\n"
256                         "sub.limit{font-size: 75%;}\n"
257                         "sup.limit{font-size: 75%;}");
258         } else {
259                 if (!sym_->requires.empty())
260                         features.require(sym_->requires);
261         }
262 }
263
264 } // namespace lyx