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