]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDecoration.cpp
d47c1a9c57786d0af753df1cc771b24e2d4f8b5f
[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/lassert.h"
26
27 #include <ostream>
28
29 using namespace std;
30
31 namespace lyx {
32
33
34 InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
35         : InsetMathNest(buf, 1), key_(key)
36 {
37 //      lyxerr << " creating deco " << key->name << endl;
38 }
39
40
41 Inset * InsetMathDecoration::clone() const
42 {
43         return new InsetMathDecoration(*this);
44 }
45
46
47 bool InsetMathDecoration::upper() const
48 {
49         return key_->name.substr(0, 5) != "under";
50 }
51
52
53 bool InsetMathDecoration::isScriptable() const
54 {
55         return
56                         key_->name == "overbrace" ||
57                         key_->name == "underbrace" ||
58                         key_->name == "overleftarrow" ||
59                         key_->name == "overrightarrow" ||
60                         key_->name == "overleftrightarrow" ||
61                         key_->name == "underleftarrow" ||
62                         key_->name == "underrightarrow" ||
63                         key_->name == "underleftrightarrow";
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 }
97
98
99 InsetMath::mode_type InsetMathDecoration::currentMode() const
100 {
101         return key_->name == "underbar" ? TEXT_MODE : MATH_MODE;
102 }
103
104
105 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
106 {
107         FontSetChanger dummy(mi.base, currentMode() == TEXT_MODE ?
108                                 "textnormal" : "mathnormal");
109
110         cell(0).metrics(mi, dim);
111
112         dh_  = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
113         dw_  = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
114
115         if (upper()) {
116                 dy_ = -dim.asc - dh_;
117                 dim.asc += dh_ + 1;
118         } else {
119                 dy_ = dim.des + 1;
120                 dim.des += dh_ + 2;
121         }
122
123         metricsMarkers(dim);
124 }
125
126
127 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
128 {
129         FontSetChanger dummy(pi.base, currentMode() == TEXT_MODE ?
130                                 "textnormal" : "mathnormal");
131
132         cell(0).draw(pi, x + 1, y);
133         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
134         if (wide())
135                 mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
136         else
137                 mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
138                         y + dy_, dw_, dh_, key_->name);
139         drawMarkers(pi, x, y);
140         setPosCache(pi, x, y);
141 }
142
143
144 void InsetMathDecoration::write(WriteStream & os) const
145 {
146         MathEnsurer ensurer(os);
147         if (os.fragile() && protect())
148                 os << "\\protect";
149         os << '\\' << key_->name << '{';
150         ModeSpecifier specifier(os, currentMode());
151         os << cell(0) << '}';
152 }
153
154
155 void InsetMathDecoration::normalize(NormalStream & os) const
156 {
157         os << "[deco " << key_->name << ' ' <<  cell(0) << ']';
158 }
159
160
161 void InsetMathDecoration::infoize(odocstream & os) const
162 {
163         os << "Deco: " << key_->name;
164 }
165
166
167 namespace {
168         struct Attributes {
169                 Attributes() {}
170                 Attributes(bool o, string t)
171                         : over(o), tag(t) {}
172                 bool over;
173                 string tag;
174         };
175
176         typedef map<string, Attributes> TranslationMap;
177
178         void buildTranslationMap(TranslationMap & t) {
179                 // the decorations we need to support are listed in lib/symbols
180                 t["acute"] = Attributes(true, "&acute;");
181                 t["bar"]   = Attributes(true, "&OverBar;");
182                 t["breve"] = Attributes(true, "&breve;");
183                 t["check"] = Attributes(true, "&caron;");
184                 t["ddddot"] = Attributes(true, "&DotDot;");
185                 t["dddot"] = Attributes(true, "&TripleDot;");
186                 t["ddot"] = Attributes(true, "&Dot;");
187                 t["dot"] = Attributes(true, "&dot;");
188                 t["grave"] = Attributes(true, "&grave;");
189                 t["hat"] = Attributes(true, "&circ;");
190                 t["mathring"] = Attributes(true, "&ring;");
191                 t["overbrace"] = Attributes(true, "&OverBrace;");
192                 t["overleftarrow"] = Attributes(true, "&xlarr;");
193                 t["overleftrightarrow"] = Attributes(true, "&xharr;");
194                 t["overline"] = Attributes(true, "&macr;");
195                 t["overrightarrow"] = Attributes(true, "&xrarr;");
196                 t["tilde"] = Attributes(true, "&tilde;");
197                 t["underbar"] = Attributes(false, "&UnderBar;");
198                 t["underbrace"] = Attributes(false, "&UnderBrace;");
199                 t["underleftarrow"] = Attributes(false, "&xlarr;");
200                 t["underleftrightarrow"] = Attributes(false, "&xharr;");
201                 // this is the macron, again, but it works
202                 t["underline"] = Attributes(false, "&macr;");
203                 t["underrightarrow"] = Attributes(false, "&xrarr;");
204                 t["vec"] = Attributes(true, "&rarr;");
205                 t["widehat"] = Attributes(true, "&Hat;");
206                 t["widetilde"] = Attributes(true, "&Tilde;");
207         }
208
209         TranslationMap const & translationMap() {
210                 static TranslationMap t;
211                 if (t.empty())
212                         buildTranslationMap(t);
213                 return t;
214         }
215 }
216
217 void InsetMathDecoration::mathmlize(MathStream & os) 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         os << MTag(outag)
224                  << MTag("mrow") << cell(0) << ETag("mrow")
225                  << from_ascii("<mo stretchy=\"true\">" + cur->second.tag + "</mo>")
226                  << ETag(outag);
227 }
228
229
230 void InsetMathDecoration::htmlize(HtmlStream & os) const
231 {
232         string const name = to_utf8(key_->name);
233         if (name == "bar") {
234                 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
235                 return;
236         }
237         
238         if (name == "underbar" || name == "underline") {
239                 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
240                 return;
241         }
242
243         TranslationMap const & t = translationMap();
244         TranslationMap::const_iterator cur = t.find(name);
245         LASSERT(cur != t.end(), return);
246         
247         bool symontop = cur->second.over;
248         string const symclass = symontop ? "symontop" : "symonbot";
249         os << MTag("span", "class='symbolpair " + symclass + "'")
250            << '\n';
251         
252         if (symontop)
253                 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
254         else
255                 os << MTag("span", "class='base'") << cell(0);
256         os << ETag("span") << '\n';
257         if (symontop)
258                 os << MTag("span", "class='base'") << cell(0);
259         else
260                 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
261         os << ETag("span") << '\n' << ETag("span") << '\n';
262 }
263
264
265 // ideas borrowed from the eLyXer code
266 void InsetMathDecoration::validate(LaTeXFeatures & features) const
267 {
268         if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
269                 string const name = to_utf8(key_->name);
270                 if (name == "bar") {
271                         features.addPreambleSnippet("<style type=\"text/css\">\n"
272                                 "span.overbar{border-top: thin black solid;}\n"
273                                 "</style>");
274                 } else if (name == "underbar" || name == "underline") {
275                         features.addPreambleSnippet("<style type=\"text/css\">\n"
276                                 "span.underbar{border-bottom: thin black solid;}\n"
277                                 "</style>");
278                 } else {
279                         features.addPreambleSnippet("<style type=\"text/css\">\n"
280                                 "span.symbolpair{display: inline-block; text-align:center;}\n"
281                                 "span.symontop{vertical-align: top;}\n"
282                                 "span.symonbot{vertical-align: bottom;}\n"
283                                 "span.symbolpair span{display: block;}\n"                       
284                                 "span.symbol{height: 0.5ex;}\n"
285                                 "</style>");
286                 }
287         } else {
288                 if (!key_->requires.empty())
289                         features.require(to_utf8(key_->requires));
290         }
291         InsetMathNest::validate(features);
292 }
293
294 } // namespace lyx