]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDecoration.cpp
typo
[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 & t, string const & entity)
192                         : over(o), tag(t), entity(entity) {}
193                 bool over;
194                 string tag;
195                 string entity;
196         };
197
198         typedef map<string, Attributes> TranslationMap;
199
200         void buildTranslationMap(TranslationMap & t) {
201                 // the decorations we need to support are listed in lib/symbols
202                 t["acute"] = Attributes(true, "&acute;", "&#x00B4;");
203                 t["bar"]   = Attributes(true, "&OverBar;", "&#x00AF;");
204                 t["breve"] = Attributes(true, "&breve;", "&#x02D8;");
205                 t["check"] = Attributes(true, "&caron;", "&#x02C7;");
206                 t["ddddot"] = Attributes(true, "&DotDot;", "&#x20DC;");
207                 t["dddot"] = Attributes(true, "&TripleDot;", "&#x20DB;");
208                 t["ddot"] = Attributes(true, "&Dot;", "&#x00A8;");
209                 t["dot"] = Attributes(true, "&dot;", "&#x02D9;");
210                 t["grave"] = Attributes(true, "&grave;", "&#x0060;");
211                 t["hat"] = Attributes(true, "&circ;", "&#x02C6;");
212                 t["mathring"] = Attributes(true, "&ring;", "&#x02DA;");
213                 t["overbrace"] = Attributes(true, "&OverBrace;", "&#xFE37;");
214                 t["overleftarrow"] = Attributes(true, "&xlarr;", "&#x27F5;");
215                 t["overleftrightarrow"] = Attributes(true, "&xharr;", "&#x27F7;");
216                 t["overline"] = Attributes(true, "&macr;", "&#x00AF;");
217                 t["overrightarrow"] = Attributes(true, "&xrarr;", "&#x27F6;");
218                 t["tilde"] = Attributes(true, "&tilde;", "&#x02DC;");
219                 t["underbar"] = Attributes(false, "&UnderBar;", "&#x0332;");
220                 t["underbrace"] = Attributes(false, "&UnderBrace;", "&#xFE38;");
221                 t["underleftarrow"] = Attributes(false, "&xlarr;", "&#x27F5;");
222                 t["underleftrightarrow"] = Attributes(false, "&xharr;", "&#x27F7;");
223                 // this is the macron, again, but it works
224                 t["underline"] = Attributes(false, "&macr;", "&#x00AF;");
225                 t["underrightarrow"] = Attributes(false, "&xrarr;", "&#x27F6;");
226                 t["undertilde"] = Attributes(false, "&Tilde;", "&#x223C;");
227                 t["utilde"] = Attributes(false, "&Tilde;", "&#x223C;");
228                 t["vec"] = Attributes(true, "&rarr;", "&#x2192;");
229                 t["widehat"] = Attributes(true, "&Hat;", "&#x005E;");
230                 t["widetilde"] = Attributes(true, "&Tilde;", "&#x223C;");
231         }
232
233         TranslationMap const & translationMap() {
234                 static TranslationMap t;
235                 if (t.empty())
236                         buildTranslationMap(t);
237                 return t;
238         }
239 } // namespace
240
241 void InsetMathDecoration::mathmlize(MathMLStream & ms) const
242 {
243         TranslationMap const & t = translationMap();
244         TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
245         LASSERT(cur != t.end(), return);
246         char const * const outag = cur->second.over ? "mover" : "munder";
247         std::string decoration = ms.xmlMode() ? cur->second.entity : cur->second.tag;
248         ms << MTag(outag)
249            << cell(0)
250            << "<" << from_ascii(ms.namespacedTag("mo")) << " stretchy=\"true\">"
251            << from_ascii(decoration)
252            << "</" << from_ascii(ms.namespacedTag("mo")) << ">"
253            << ETag(outag);
254 }
255
256
257 void InsetMathDecoration::htmlize(HtmlStream & os) const
258 {
259         string const name = to_utf8(key_->name);
260         if (name == "bar") {
261                 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
262                 return;
263         }
264
265         if (name == "underbar" || name == "underline") {
266                 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
267                 return;
268         }
269
270         TranslationMap const & t = translationMap();
271         TranslationMap::const_iterator cur = t.find(name);
272         LASSERT(cur != t.end(), return);
273
274         bool symontop = cur->second.over;
275         string const symclass = symontop ? "symontop" : "symonbot";
276         os << MTag("span", "class='symbolpair " + symclass + "'")
277            << '\n';
278
279         if (symontop)
280                 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
281         else
282                 os << MTag("span", "class='base'") << cell(0);
283         os << ETag("span") << '\n';
284         if (symontop)
285                 os << MTag("span", "class='base'") << cell(0);
286         else
287                 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
288         os << ETag("span") << '\n' << ETag("span") << '\n';
289 }
290
291
292 // ideas borrowed from the eLyXer code
293 void InsetMathDecoration::validate(LaTeXFeatures & features) const
294 {
295         if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
296                 string const name = to_utf8(key_->name);
297                 if (name == "bar") {
298                         features.addCSSSnippet("span.overbar{border-top: thin black solid;}");
299                 } else if (name == "underbar" || name == "underline") {
300                         features.addCSSSnippet("span.underbar{border-bottom: thin black solid;}");
301                 } else {
302                         features.addCSSSnippet(
303                                 "span.symbolpair{display: inline-block; text-align:center;}\n"
304                                 "span.symontop{vertical-align: top;}\n"
305                                 "span.symonbot{vertical-align: bottom;}\n"
306                                 "span.symbolpair span{display: block;}\n"
307                                 "span.symbol{height: 0.5ex;}");
308                 }
309         } else {
310                 if (!key_->required.empty())
311                         features.require(key_->required);
312         }
313         InsetMathNest::validate(features);
314 }
315
316 } // namespace lyx