]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDecoration.cpp
Small improvement for bug #7509 as suggested by JMarc
[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/lassert.h"
27
28 #include <ostream>
29
30 using namespace std;
31
32 namespace lyx {
33
34
35 InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
36         : InsetMathNest(buf, 1), key_(key)
37 {
38 //      lyxerr << " creating deco " << key->name << endl;
39 }
40
41
42 Inset * InsetMathDecoration::clone() const
43 {
44         return new InsetMathDecoration(*this);
45 }
46
47
48 bool InsetMathDecoration::upper() const
49 {
50         return key_->name.substr(0, 5) != "under";
51 }
52
53
54 bool InsetMathDecoration::isScriptable() const
55 {
56         return
57                         key_->name == "overbrace" ||
58                         key_->name == "underbrace" ||
59                         key_->name == "overleftarrow" ||
60                         key_->name == "overrightarrow" ||
61                         key_->name == "overleftrightarrow" ||
62                         key_->name == "underleftarrow" ||
63                         key_->name == "underrightarrow" ||
64                         key_->name == "underleftrightarrow";
65 }
66
67
68 bool InsetMathDecoration::protect() const
69 {
70         return
71                         key_->name == "overbrace" ||
72                         key_->name == "underbrace" ||
73                         key_->name == "overleftarrow" ||
74                         key_->name == "overrightarrow" ||
75                         key_->name == "overleftrightarrow" ||
76                         key_->name == "underleftarrow" ||
77                         key_->name == "underrightarrow" ||
78                         key_->name == "underleftrightarrow";
79 }
80
81
82 bool InsetMathDecoration::wide() const
83 {
84         return
85                         key_->name == "overline" ||
86                         key_->name == "underline" ||
87                         key_->name == "overbrace" ||
88                         key_->name == "underbrace" ||
89                         key_->name == "overleftarrow" ||
90                         key_->name == "overrightarrow" ||
91                         key_->name == "overleftrightarrow" ||
92                         key_->name == "widehat" ||
93                         key_->name == "widetilde" ||
94                         key_->name == "underleftarrow" ||
95                         key_->name == "underrightarrow" ||
96                         key_->name == "underleftrightarrow";
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         bool really_change_font = currentMode() == TEXT_MODE
109                                 && isMathFont(from_ascii(mi.base.fontname));
110         FontSetChanger dummy(mi.base, "textnormal", really_change_font);
111
112         cell(0).metrics(mi, dim);
113
114         dh_  = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
115         dw_  = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
116
117         if (upper()) {
118                 dy_ = -dim.asc - dh_;
119                 dim.asc += dh_ + 1;
120         } else {
121                 dy_ = dim.des + 1;
122                 dim.des += dh_ + 2;
123         }
124
125         metricsMarkers(dim);
126 }
127
128
129 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
130 {
131         bool really_change_font = currentMode() == TEXT_MODE
132                                 && isMathFont(from_ascii(pi.base.fontname));
133         FontSetChanger dummy(pi.base, "textnormal", really_change_font);
134
135         cell(0).draw(pi, x + 1, y);
136         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
137         if (wide())
138                 mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
139         else
140                 mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
141                         y + dy_, dw_, dh_, key_->name);
142         drawMarkers(pi, x, y);
143         setPosCache(pi, x, y);
144 }
145
146
147 void InsetMathDecoration::write(WriteStream & os) const
148 {
149         MathEnsurer ensurer(os);
150         if (os.fragile() && protect())
151                 os << "\\protect";
152         os << '\\' << key_->name << '{';
153         ModeSpecifier specifier(os, currentMode());
154         os << cell(0) << '}';
155 }
156
157
158 void InsetMathDecoration::normalize(NormalStream & os) const
159 {
160         os << "[deco " << key_->name << ' ' <<  cell(0) << ']';
161 }
162
163
164 void InsetMathDecoration::infoize(odocstream & os) const
165 {
166         os << "Deco: " << key_->name;
167 }
168
169
170 namespace {
171         struct Attributes {
172                 Attributes() {}
173                 Attributes(bool o, string t)
174                         : over(o), tag(t) {}
175                 bool over;
176                 string tag;
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;");
184                 t["bar"]   = Attributes(true, "&OverBar;");
185                 t["breve"] = Attributes(true, "&breve;");
186                 t["check"] = Attributes(true, "&caron;");
187                 t["ddddot"] = Attributes(true, "&DotDot;");
188                 t["dddot"] = Attributes(true, "&TripleDot;");
189                 t["ddot"] = Attributes(true, "&Dot;");
190                 t["dot"] = Attributes(true, "&dot;");
191                 t["grave"] = Attributes(true, "&grave;");
192                 t["hat"] = Attributes(true, "&circ;");
193                 t["mathring"] = Attributes(true, "&ring;");
194                 t["overbrace"] = Attributes(true, "&OverBrace;");
195                 t["overleftarrow"] = Attributes(true, "&xlarr;");
196                 t["overleftrightarrow"] = Attributes(true, "&xharr;");
197                 t["overline"] = Attributes(true, "&macr;");
198                 t["overrightarrow"] = Attributes(true, "&xrarr;");
199                 t["tilde"] = Attributes(true, "&tilde;");
200                 t["underbar"] = Attributes(false, "&UnderBar;");
201                 t["underbrace"] = Attributes(false, "&UnderBrace;");
202                 t["underleftarrow"] = Attributes(false, "&xlarr;");
203                 t["underleftrightarrow"] = Attributes(false, "&xharr;");
204                 // this is the macron, again, but it works
205                 t["underline"] = Attributes(false, "&macr;");
206                 t["underrightarrow"] = Attributes(false, "&xrarr;");
207                 t["vec"] = Attributes(true, "&rarr;");
208                 t["widehat"] = Attributes(true, "&Hat;");
209                 t["widetilde"] = Attributes(true, "&Tilde;");
210         }
211
212         TranslationMap const & translationMap() {
213                 static TranslationMap t;
214                 if (t.empty())
215                         buildTranslationMap(t);
216                 return t;
217         }
218 }
219
220 void InsetMathDecoration::mathmlize(MathStream & os) const
221 {
222         TranslationMap const & t = translationMap();
223         TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
224         LASSERT(cur != t.end(), return);
225         char const * const outag = cur->second.over ? "mover" : "munder";
226         os << MTag(outag)
227                  << MTag("mrow") << cell(0) << ETag("mrow")
228                  << from_ascii("<mo stretchy=\"true\">" + cur->second.tag + "</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.addPreambleSnippet("<style type=\"text/css\">\n"
275                                 "span.overbar{border-top: thin black solid;}\n"
276                                 "</style>");
277                 } else if (name == "underbar" || name == "underline") {
278                         features.addPreambleSnippet("<style type=\"text/css\">\n"
279                                 "span.underbar{border-bottom: thin black solid;}\n"
280                                 "</style>");
281                 } else {
282                         features.addPreambleSnippet("<style type=\"text/css\">\n"
283                                 "span.symbolpair{display: inline-block; text-align:center;}\n"
284                                 "span.symontop{vertical-align: top;}\n"
285                                 "span.symonbot{vertical-align: bottom;}\n"
286                                 "span.symbolpair span{display: block;}\n"                       
287                                 "span.symbol{height: 0.5ex;}\n"
288                                 "</style>");
289                 }
290         } else {
291                 if (!key_->requires.empty())
292                         features.require(to_utf8(key_->requires));
293         }
294         InsetMathNest::validate(features);
295 }
296
297 } // namespace lyx