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