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