]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDecoration.cpp
MathML: Convert HTML entities to XML entities.
[lyx.git] / src / mathed / InsetMathDecoration.cpp
1 /**
2  * \file InsetMathDecoration.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetMathDecoration.h"
15
16 #include "MathData.h"
17 #include "MathParser.h"
18 #include "MathSupport.h"
19 #include "MathStream.h"
20 #include "MetricsInfo.h"
21
22 #include "LaTeXFeatures.h"
23
24 #include "support/debug.h"
25 #include "support/docstring.h"
26 #include "support/gettext.h"
27 #include "support/lassert.h"
28 #include "support/lstrings.h"
29
30 #include <algorithm>
31 #include <ostream>
32
33 using namespace lyx::support;
34
35 using namespace std;
36
37 namespace lyx {
38
39
40 InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
41         : InsetMathNest(buf, 1), key_(key), dh_(0), dy_(0), dw_(0)
42 {
43 //      lyxerr << " creating deco " << key->name << endl;
44 }
45
46
47 Inset * InsetMathDecoration::clone() const
48 {
49         return new InsetMathDecoration(*this);
50 }
51
52
53 bool InsetMathDecoration::upper() const
54 {
55         return key_->name.substr(0, 5) != "under" && key_->name != "utilde";
56 }
57
58
59 MathClass InsetMathDecoration::mathClass() const
60 {
61         if (key_->name == "overbrace" || key_->name == "underbrace")
62                 return MC_OP;
63         return MC_ORD;
64 }
65
66
67 bool InsetMathDecoration::isScriptable() const
68 {
69         return mathClass() == MC_OP;
70 }
71
72
73 bool InsetMathDecoration::protect() const
74 {
75         return
76                         key_->name == "overbrace" ||
77                         key_->name == "underbrace" ||
78                         key_->name == "overleftarrow" ||
79                         key_->name == "overrightarrow" ||
80                         key_->name == "overleftrightarrow" ||
81                         key_->name == "underleftarrow" ||
82                         key_->name == "underrightarrow" ||
83                         key_->name == "underleftrightarrow";
84 }
85
86
87 bool InsetMathDecoration::wide() const
88 {
89         return
90                         key_->name == "overline" ||
91                         key_->name == "underline" ||
92                         key_->name == "overbrace" ||
93                         key_->name == "underbrace" ||
94                         key_->name == "overleftarrow" ||
95                         key_->name == "overrightarrow" ||
96                         key_->name == "overleftrightarrow" ||
97                         key_->name == "widehat" ||
98                         key_->name == "widetilde" ||
99                         key_->name == "underleftarrow" ||
100                         key_->name == "underrightarrow" ||
101                         key_->name == "underleftrightarrow" ||
102                         key_->name == "undertilde" ||
103                         key_->name == "utilde";
104 }
105
106
107 InsetMath::mode_type InsetMathDecoration::currentMode() const
108 {
109         return key_->name == "underbar" ? TEXT_MODE : MATH_MODE;
110 }
111
112
113 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
114 {
115         Changer dummy = mi.base.changeEnsureMath(currentMode());
116
117         cell(0).metrics(mi, dim);
118
119         dh_  = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
120         dw_  = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
121
122         if (upper()) {
123                 dy_ = -dim.asc - dh_;
124                 dim.asc += dh_ + 1;
125         } else {
126                 dy_ = dim.des + 1;
127                 dim.des += dh_ + 2;
128         }
129 }
130
131
132 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
133 {
134         Changer dummy = pi.base.changeEnsureMath(currentMode());
135
136         cell(0).draw(pi, x, y);
137         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
138         if (wide())
139                 mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
140         else
141                 mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
142                         y + dy_, dw_, dh_, key_->name);
143 }
144
145
146 void InsetMathDecoration::write(WriteStream & os) const
147 {
148         MathEnsurer ensurer(os);
149         if (os.fragile() && protect())
150                 os << "\\protect";
151         os << '\\' << key_->name << '{';
152         ModeSpecifier specifier(os, currentMode());
153         os << cell(0) << '}';
154 }
155
156
157 void InsetMathDecoration::normalize(NormalStream & os) const
158 {
159         os << "[deco " << key_->name << ' ' <<  cell(0) << ']';
160 }
161
162
163 void InsetMathDecoration::infoize(odocstream & os) const
164 {
165         os << bformat(_("Decoration: %1$s"), key_->name);
166 }
167
168
169 namespace {
170         struct Attributes {
171                 Attributes() : over(false) {}
172                 Attributes(bool o, string const & t, string const & entity)
173                         : over(o), tag(t), entity(entity) {}
174                 bool over;
175                 string tag;
176                 string entity;
177         };
178
179         typedef map<string, Attributes> TranslationMap;
180
181         void buildTranslationMap(TranslationMap & t) {
182                 // the decorations we need to support are listed in lib/symbols
183                 t["acute"] = Attributes(true, "&acute;", "&#x00B4;");
184                 t["bar"]   = Attributes(true, "&OverBar;", "&#x00AF;");
185                 t["breve"] = Attributes(true, "&breve;", "&#x02D8;");
186                 t["check"] = Attributes(true, "&caron;", "&#x02C7;");
187                 t["ddddot"] = Attributes(true, "&DotDot;", "&#x20DC;");
188                 t["dddot"] = Attributes(true, "&TripleDot;", "&#x20DB;");
189                 t["ddot"] = Attributes(true, "&Dot;", "&#x00A8;");
190                 t["dot"] = Attributes(true, "&dot;", "&#x02D9;");
191                 t["grave"] = Attributes(true, "&grave;", "&#x0060;");
192                 t["hat"] = Attributes(true, "&circ;", "&#x02C6;");
193                 t["mathring"] = Attributes(true, "&ring;", "&#x02DA;");
194                 t["overbrace"] = Attributes(true, "&OverBrace;", "&#xFE37;");
195                 t["overleftarrow"] = Attributes(true, "&xlarr;", "&#x27F5;");
196                 t["overleftrightarrow"] = Attributes(true, "&xharr;", "&#x27F7;");
197                 t["overline"] = Attributes(true, "&macr;", "&#x00AF;");
198                 t["overrightarrow"] = Attributes(true, "&xrarr;", "&#x27F6;");
199                 t["tilde"] = Attributes(true, "&tilde;", "&#x02DC;");
200                 t["underbar"] = Attributes(false, "&UnderBar;", "&#x0332;");
201                 t["underbrace"] = Attributes(false, "&UnderBrace;", "&#xFE38;");
202                 t["underleftarrow"] = Attributes(false, "&xlarr;", "&#x27F5;");
203                 t["underleftrightarrow"] = Attributes(false, "&xharr;", "&#x27F7;");
204                 // this is the macron, again, but it works
205                 t["underline"] = Attributes(false, "&macr;", "&#x00AF;");
206                 t["underrightarrow"] = Attributes(false, "&xrarr;", "&#x27F6;");
207                 t["undertilde"] = Attributes(false, "&Tilde;", "&#x223C;");
208                 t["utilde"] = Attributes(false, "&Tilde;", "&#x223C;");
209                 t["vec"] = Attributes(true, "&rarr;", "&#x2192;");
210                 t["widehat"] = Attributes(true, "&Hat;", "&#x005E;");
211                 t["widetilde"] = Attributes(true, "&Tilde;", "&#x223C;");
212         }
213
214         TranslationMap const & translationMap() {
215                 static TranslationMap t;
216                 if (t.empty())
217                         buildTranslationMap(t);
218                 return t;
219         }
220 } // namespace
221
222 void InsetMathDecoration::mathmlize(MathStream & ms) const
223 {
224         TranslationMap const & t = translationMap();
225         TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
226         LASSERT(cur != t.end(), return);
227         char const * const outag = cur->second.over ? "mover" : "munder";
228         std::string decoration = ms.xmlMode() ? cur->second.entity : cur->second.tag;
229         ms << MTag(outag)
230            << MTag("mrow") << cell(0) << ETag("mrow")
231            << "<" << from_ascii(ms.namespacedTag("mo")) << " stretchy=\"true\">"
232            << from_ascii(decoration)
233            << "</" << from_ascii(ms.namespacedTag("mo")) << ">"
234            << ETag(outag);
235 }
236
237
238 void InsetMathDecoration::htmlize(HtmlStream & os) const
239 {
240         string const name = to_utf8(key_->name);
241         if (name == "bar") {
242                 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
243                 return;
244         }
245
246         if (name == "underbar" || name == "underline") {
247                 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
248                 return;
249         }
250
251         TranslationMap const & t = translationMap();
252         TranslationMap::const_iterator cur = t.find(name);
253         LASSERT(cur != t.end(), return);
254
255         bool symontop = cur->second.over;
256         string const symclass = symontop ? "symontop" : "symonbot";
257         os << MTag("span", "class='symbolpair " + symclass + "'")
258            << '\n';
259
260         if (symontop)
261                 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
262         else
263                 os << MTag("span", "class='base'") << cell(0);
264         os << ETag("span") << '\n';
265         if (symontop)
266                 os << MTag("span", "class='base'") << cell(0);
267         else
268                 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
269         os << ETag("span") << '\n' << ETag("span") << '\n';
270 }
271
272
273 // ideas borrowed from the eLyXer code
274 void InsetMathDecoration::validate(LaTeXFeatures & features) const
275 {
276         if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
277                 string const name = to_utf8(key_->name);
278                 if (name == "bar") {
279                         features.addCSSSnippet("span.overbar{border-top: thin black solid;}");
280                 } else if (name == "underbar" || name == "underline") {
281                         features.addCSSSnippet("span.underbar{border-bottom: thin black solid;}");
282                 } else {
283                         features.addCSSSnippet(
284                                 "span.symbolpair{display: inline-block; text-align:center;}\n"
285                                 "span.symontop{vertical-align: top;}\n"
286                                 "span.symonbot{vertical-align: bottom;}\n"
287                                 "span.symbolpair span{display: block;}\n"
288                                 "span.symbol{height: 0.5ex;}");
289                 }
290         } else {
291                 if (!key_->required.empty())
292                         features.require(key_->required);
293         }
294         InsetMathNest::validate(features);
295 }
296
297 } // namespace lyx