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