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