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