]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDecoration.cpp
a717484c055d2a248e339ebe4e8361d1fa585288
[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         bool const upshape = currentMode() == TEXT_MODE
108                                 && mi.base.font.color() == Color_math;
109         ShapeChanger dummy(mi.base.font, upshape ?
110                                 UP_SHAPE : mi.base.font.shape());
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 const upshape = currentMode() == TEXT_MODE
132                                 && pi.base.font.color() == Color_math;
133         ShapeChanger dummy(pi.base.font, upshape ?
134                                 UP_SHAPE : pi.base.font.shape());
135
136         cell(0).draw(pi, x + 1, y);
137         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
138         if (wide())
139                 mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
140         else
141                 mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
142                         y + dy_, dw_, dh_, key_->name);
143         drawMarkers(pi, x, y);
144         setPosCache(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 << "Deco: " << key_->name;
168 }
169
170
171 namespace {
172         struct Attributes {
173                 Attributes() {}
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["vec"] = Attributes(true, "&rarr;");
209                 t["widehat"] = Attributes(true, "&Hat;");
210                 t["widetilde"] = Attributes(true, "&Tilde;");
211         }
212
213         TranslationMap const & translationMap() {
214                 static TranslationMap t;
215                 if (t.empty())
216                         buildTranslationMap(t);
217                 return t;
218         }
219 }
220
221 void InsetMathDecoration::mathmlize(MathStream & os) const
222 {
223         TranslationMap const & t = translationMap();
224         TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
225         LASSERT(cur != t.end(), return);
226         char const * const outag = cur->second.over ? "mover" : "munder";
227         os << MTag(outag)
228                  << MTag("mrow") << cell(0) << ETag("mrow")
229                  << from_ascii("<mo stretchy=\"true\">" + cur->second.tag + "</mo>")
230                  << ETag(outag);
231 }
232
233
234 void InsetMathDecoration::htmlize(HtmlStream & os) const
235 {
236         string const name = to_utf8(key_->name);
237         if (name == "bar") {
238                 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
239                 return;
240         }
241         
242         if (name == "underbar" || name == "underline") {
243                 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
244                 return;
245         }
246
247         TranslationMap const & t = translationMap();
248         TranslationMap::const_iterator cur = t.find(name);
249         LASSERT(cur != t.end(), return);
250         
251         bool symontop = cur->second.over;
252         string const symclass = symontop ? "symontop" : "symonbot";
253         os << MTag("span", "class='symbolpair " + symclass + "'")
254            << '\n';
255         
256         if (symontop)
257                 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
258         else
259                 os << MTag("span", "class='base'") << cell(0);
260         os << ETag("span") << '\n';
261         if (symontop)
262                 os << MTag("span", "class='base'") << cell(0);
263         else
264                 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
265         os << ETag("span") << '\n' << ETag("span") << '\n';
266 }
267
268
269 // ideas borrowed from the eLyXer code
270 void InsetMathDecoration::validate(LaTeXFeatures & features) const
271 {
272         if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
273                 string const name = to_utf8(key_->name);
274                 if (name == "bar") {
275                         features.addPreambleSnippet("<style type=\"text/css\">\n"
276                                 "span.overbar{border-top: thin black solid;}\n"
277                                 "</style>");
278                 } else if (name == "underbar" || name == "underline") {
279                         features.addPreambleSnippet("<style type=\"text/css\">\n"
280                                 "span.underbar{border-bottom: thin black solid;}\n"
281                                 "</style>");
282                 } else {
283                         features.addPreambleSnippet("<style type=\"text/css\">\n"
284                                 "span.symbolpair{display: inline-block; text-align:center;}\n"
285                                 "span.symontop{vertical-align: top;}\n"
286                                 "span.symonbot{vertical-align: bottom;}\n"
287                                 "span.symbolpair span{display: block;}\n"                       
288                                 "span.symbol{height: 0.5ex;}\n"
289                                 "</style>");
290                 }
291         } else {
292                 if (!key_->requires.empty())
293                         features.require(to_utf8(key_->requires));
294         }
295         InsetMathNest::validate(features);
296 }
297
298 } // namespace lyx