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