]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathSymbol.cpp
Make members of FuncRequest private, per the FIXME there. Again, this is
[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 "Dimension.h"
16 #include "LaTeXFeatures.h"
17 #include "MathAtom.h"
18 #include "MathParser.h"
19 #include "MathStream.h"
20 #include "MathSupport.h"
21
22 #include "support/debug.h"
23 #include "support/docstream.h"
24 #include "support/textutils.h"
25
26
27 namespace lyx {
28
29 InsetMathSymbol::InsetMathSymbol(latexkeys const * l)
30         : sym_(l), h_(0), scriptable_(false)
31 {}
32
33
34 InsetMathSymbol::InsetMathSymbol(char const * name)
35         : sym_(in_word_set(from_ascii(name))), h_(0), scriptable_(false)
36 {}
37
38
39 InsetMathSymbol::InsetMathSymbol(docstring const & name)
40         : sym_(in_word_set(name)), h_(0), scriptable_(false)
41 {}
42
43
44 Inset * InsetMathSymbol::clone() const
45 {
46         return new InsetMathSymbol(*this);
47 }
48
49
50 docstring InsetMathSymbol::name() const
51 {
52         return sym_->name;
53 }
54
55
56 void InsetMathSymbol::metrics(MetricsInfo & mi, Dimension & dim) const
57 {
58         //lyxerr << "metrics: symbol: '" << sym_->name
59         //      << "' in font: '" << sym_->inset
60         //      << "' drawn as: '" << sym_->draw
61         //      << "'" << endl;
62
63         int const em = mathed_char_width(mi.base.font, 'M');
64         FontSetChanger dummy(mi.base, sym_->inset);
65         mathed_string_dim(mi.base.font, sym_->draw, dim);
66         docstring::const_reverse_iterator rit = sym_->draw.rbegin();
67         kerning_ = mathed_char_kerning(mi.base.font, *rit);
68         // correct height for broken cmex and wasy font
69         if (sym_->inset == "cmex" || sym_->inset == "wasy") {
70                 h_ = 4 * dim.des / 5;
71                 dim.asc += h_;
72                 dim.des -= h_;
73         }
74         // seperate things a bit
75         if (isRelOp())
76                 dim.wid += static_cast<int>(0.5 * em + 0.5);
77         else
78                 dim.wid += static_cast<int>(0.1667 * em + 0.5);
79
80         scriptable_ = false;
81         if (mi.base.style == LM_ST_DISPLAY)
82                 if (sym_->inset == "cmex" || sym_->inset == "esint" ||
83                     sym_->extra == "funclim")
84                         scriptable_ = true;
85 }
86
87
88 void InsetMathSymbol::draw(PainterInfo & pi, int x, int y) const
89 {
90         //lyxerr << "metrics: symbol: '" << sym_->name
91         //      << "' in font: '" << sym_->inset
92         //      << "' drawn as: '" << sym_->draw
93         //      << "'" << endl;
94         int const em = mathed_char_width(pi.base.font, 'M');
95         if (isRelOp())
96                 x += static_cast<int>(0.25*em+0.5);
97         else
98                 x += static_cast<int>(0.0833*em+0.5);
99
100         FontSetChanger dummy(pi.base, sym_->inset.c_str());
101         pi.draw(x, y - h_, sym_->draw);
102 }
103
104
105 bool InsetMathSymbol::isRelOp() const
106 {
107         return sym_->extra == "mathrel";
108 }
109
110
111 bool InsetMathSymbol::isOrdAlpha() const
112 {
113         return sym_->extra == "mathord" || sym_->extra == "mathalpha";
114 }
115
116
117 bool InsetMathSymbol::isScriptable() const
118 {
119         return scriptable_;
120 }
121
122
123 bool InsetMathSymbol::takesLimits() const
124 {
125         return
126                 sym_->inset == "cmex" ||
127                 sym_->inset == "lyxboldsymb" ||
128                 sym_->inset == "esint" ||
129                 sym_->extra == "funclim";
130 }
131
132
133 void InsetMathSymbol::normalize(NormalStream & os) const
134 {
135         os << "[symbol " << name() << ']';
136 }
137
138
139 void InsetMathSymbol::maple(MapleStream & os) const
140 {
141         if (name() == "cdot")
142                 os << '*';
143         else if (name() == "infty")
144                 os << "infinity";
145         else
146                 os << name();
147 }
148
149 void InsetMathSymbol::maxima(MaximaStream & os) const
150 {
151         if (name() == "cdot")
152                 os << '*';
153         else if (name() == "infty")
154                 os << "inf";
155         else if (name() == "pi")
156                 os << "%pi";
157         else
158                 os << name();
159 }
160
161
162 void InsetMathSymbol::mathematica(MathematicaStream & os) const
163 {
164         if ( name() == "pi")    { os << "Pi"; return;}
165         if ( name() == "infty") { os << "Infinity"; return;}
166         if ( name() == "cdot")  { os << '*'; return;}
167         os << name();
168 }
169
170
171 // FIXME This will likely need some work.
172 char const * MathMLtype(docstring const & s)
173 {
174         if (s == "mathord")
175                 return "mi";
176         return "mo";
177 }
178
179
180 void InsetMathSymbol::mathmlize(MathStream & os) const
181 {
182         // FIXME We may need to do more interesting things 
183         // with MathMLtype.
184         char const * type = MathMLtype(sym_->extra);
185         os << '<' << type << "> ";
186         if (sym_->xmlname == "x") 
187                 // unknown so far
188                 os << name();
189         else
190                 os << sym_->xmlname;
191         os << " </" << type << '>';
192 }
193
194
195 void InsetMathSymbol::htmlize(HtmlStream & os, bool spacing) const
196 {
197         // FIXME We may need to do more interesting things 
198         // with MathMLtype.
199         char const * type = MathMLtype(sym_->extra);
200         bool op = (std::string(type) == "mo");
201         
202         if (sym_->xmlname == "x") 
203                 // unknown so far
204                 os << ' ' << name() << ' ';
205         else if (op && spacing) 
206                 os << ' ' << sym_->xmlname << ' ';
207         else
208                 os << sym_->xmlname;
209 }
210
211
212 void InsetMathSymbol::htmlize(HtmlStream & os) const
213 {
214         htmlize(os, true);
215 }
216
217
218 void InsetMathSymbol::octave(OctaveStream & os) const
219 {
220         if (name() == "cdot")
221                 os << '*';
222         else
223                 os << name();
224 }
225
226
227 void InsetMathSymbol::write(WriteStream & os) const
228 {
229         MathEnsurer ensurer(os);
230         os << '\\' << name();
231
232         // $,#, etc. In theory the restriction based on catcodes, but then
233         // we do not handle catcodes very well, let alone cat code changes,
234         // so being outside the alpha range is good enough.
235         if (name().size() == 1 && !isAlphaASCII(name()[0]))
236                 return;
237
238         os.pendingSpace(true);
239 }
240
241
242 void InsetMathSymbol::infoize2(odocstream & os) const
243 {
244         os << from_ascii("Symbol: ") << name();
245 }
246
247
248 void InsetMathSymbol::validate(LaTeXFeatures & features) const
249 {
250         // this is not really the ideal place to do this, but we can't
251         // validate in InsetMathExInt.
252         if (features.runparams().math_flavor == OutputParams::MathAsHTML
253             && sym_->name == from_ascii("int")) {
254                 features.addPreambleSnippet("<style type=\"text/css\">\n"
255                         "span.limits{display: inline-block; vertical-align: middle; text-align:center; font-size: 75%;}\n"
256                         "span.limits span{display: block;}\n"
257                         "span.intsym{font-size: 150%;}\n"
258                         "sub.limit{font-size: 75%;}\n"
259                         "sup.limit{font-size: 75%;}\n"
260                         "</style>");
261         } else {
262                 if (!sym_->requires.empty())
263                         features.require(to_utf8(sym_->requires));
264         }
265 }
266
267 } // namespace lyx