]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDecoration.cpp
Fix functions that used functions but did not defined it
[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 "InsetMathChar.h"
17 #include "MathData.h"
18 #include "MathParser.h"
19 #include "MathSupport.h"
20 #include "MathStream.h"
21 #include "MetricsInfo.h"
22
23 #include "BufferView.h"
24 #include "LaTeXFeatures.h"
25
26 #include "support/debug.h"
27 #include "support/docstring.h"
28 #include "support/gettext.h"
29 #include "support/lassert.h"
30 #include "support/lstrings.h"
31
32 #include <algorithm>
33 #include <ostream>
34
35 using namespace lyx::support;
36
37 using namespace std;
38
39 namespace lyx {
40
41
42 InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
43         : InsetMathNest(buf, 1), key_(key), outer_mode_(UNDECIDED_MODE)
44 {
45 //      lyxerr << " creating deco " << key->name << endl;
46 }
47
48
49 Inset * InsetMathDecoration::clone() const
50 {
51         return new InsetMathDecoration(*this);
52 }
53
54
55 bool InsetMathDecoration::upper() const
56 {
57         return key_->name.substr(0, 5) != "under" &&
58                key_->name != "utilde" &&
59                key_->name != "uline" &&
60                key_->name != "uuline" &&
61                key_->name != "uwave";
62 }
63
64
65 MathClass InsetMathDecoration::mathClass() const
66 {
67         if (key_->name == "overbrace" || key_->name == "underbrace")
68                 return MC_OP;
69         return MC_ORD;
70 }
71
72
73 Limits InsetMathDecoration::defaultLimits(bool /*display*/) const
74 {
75         if (allowsLimitsChange())
76                 return LIMITS;
77         else
78                 return NO_LIMITS;
79 }
80
81
82 bool InsetMathDecoration::protect() const
83 {
84         return
85                         key_->name == "overbrace" ||
86                         key_->name == "underbrace" ||
87                         key_->name == "overleftarrow" ||
88                         key_->name == "overrightarrow" ||
89                         key_->name == "overleftrightarrow" ||
90                         key_->name == "underleftarrow" ||
91                         key_->name == "underrightarrow" ||
92                         key_->name == "underleftrightarrow";
93 }
94
95
96 bool InsetMathDecoration::wide() const
97 {
98         return
99                         key_->name == "overline" ||
100                         key_->name == "underline" ||
101                         key_->name == "uline" ||
102                         key_->name == "uuline" ||
103                         key_->name == "uwave" ||
104                         key_->name == "overbrace" ||
105                         key_->name == "underbrace" ||
106                         key_->name == "overleftarrow" ||
107                         key_->name == "overrightarrow" ||
108                         key_->name == "overleftrightarrow" ||
109                         key_->name == "widehat" ||
110                         key_->name == "widetilde" ||
111                         key_->name == "underleftarrow" ||
112                         key_->name == "underrightarrow" ||
113                         key_->name == "underleftrightarrow" ||
114                         key_->name == "undertilde" ||
115                         key_->name == "utilde";
116 }
117
118
119 InsetMath::mode_type InsetMathDecoration::currentMode() const
120 {
121         if (key_->name == "underbar")
122                 return TEXT_MODE;
123
124         if (key_->name == "uline" || key_->name == "uuline" ||
125             key_->name == "uwave")
126                 return outer_mode_;
127
128         return MATH_MODE;
129 }
130
131
132 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
133 {
134         outer_mode_ = isTextFont(mi.base.fontname) ? TEXT_MODE : MATH_MODE;
135
136         Changer dummy = mi.base.changeEnsureMath(currentMode());
137
138         cell(0).metrics(mi, dim);
139
140         int const l1 = mi.base.bv->zoomedPixels(1);
141         int const l2 = mathed_char_width(mi.base.font, 'x') - l1;
142         int const l3 = l2;
143
144         dh_  = l2; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
145         dw_  = l3; //mathed_char_width(LM_TC_VAR, mi, 'x');
146
147         if (upper()) {
148                 dy_ = -dim.asc - dh_ - l1;
149                 dim.asc += dh_ + l1;
150         } else {
151                 dy_ = dim.des + l1;
152                 dim.des += dh_ + l2;
153         }
154 }
155
156
157 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
158 {
159         outer_mode_ = isTextFont(pi.base.fontname) ? TEXT_MODE : MATH_MODE;
160
161         Changer dummy = pi.base.changeEnsureMath(currentMode());
162
163         cell(0).draw(pi, x, y);
164         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
165         if (wide()) {
166                 mathed_draw_deco(pi, x, y + dy_, dim0.wid, dh_, key_->name);
167                 return;
168         }
169         // Lacking the necessary font parameters, in order to properly align
170         // the decoration we have to resort to heuristics for choosing a
171         // suitable value for shift
172         char_type c = (cell(0).empty() || !cell(0)[0]->asCharInset())
173                 ? 0 : cell(0)[0]->asCharInset()->getChar();
174         double slope = (c == 0) ? 0.0 : mathed_char_slope(pi.base, c);
175         int kerning = (c == 0) ? 0 : mathed_char_kerning(pi.base.font, c);
176         int shift = (kerning == 0) ? int(dim0.asc * slope) : kerning;
177         mathed_draw_deco(pi, x + (dim0.wid - dw_) / 2 + shift,
178                          y + dy_, dw_, dh_, key_->name);
179 }
180
181
182 void InsetMathDecoration::write(TeXMathStream & os) const
183 {
184         bool needs_mathmode = currentMode() == MATH_MODE;
185         bool textmode_macro = currentMode() == TEXT_MODE
186                               && key_->extra != "everymode";
187         MathEnsurer ensurer(os, needs_mathmode, true, textmode_macro);
188         if (os.fragile() && protect())
189                 os << "\\protect";
190         os << '\\' << key_->name << '{';
191         ModeSpecifier specifier(os, currentMode());
192         os << cell(0) << '}';
193         writeLimits(os);
194 }
195
196
197 void InsetMathDecoration::normalize(NormalStream & os) const
198 {
199         os << "[deco " << key_->name << ' ' <<  cell(0) << ']';
200 }
201
202
203 docstring InsetMathDecoration::name() const
204 {
205         return key_->name;
206 }
207
208
209 void InsetMathDecoration::infoize(odocstream & os) const
210 {
211         os << bformat(_("Decoration: %1$s"), key_->name);
212 }
213
214
215 namespace {
216         struct Attributes {
217                 Attributes() : over(false) {}
218                 Attributes(bool o, string const & entity)
219                         : over(o), entity(entity) {}
220                 bool over;
221                 string entity;
222         };
223
224         typedef map<string, Attributes> TranslationMap;
225
226         void buildTranslationMap(TranslationMap & t) {
227                 // the decorations we need to support are listed in lib/symbols
228                 t["acute"] = Attributes(true, "&#x00B4;");
229                 t["bar"]   = Attributes(true, "&#x00AF;");
230                 t["breve"] = Attributes(true, "&#x02D8;");
231                 t["check"] = Attributes(true, "&#x02C7;");
232                 t["ddddot"] = Attributes(true, "&#x20DC;");
233                 t["dddot"] = Attributes(true, "&#x20DB;");
234                 t["ddot"] = Attributes(true, "&#x00A8;");
235                 t["dot"] = Attributes(true, "&#x02D9;");
236                 t["grave"] = Attributes(true, "&#x0060;");
237                 t["hat"] = Attributes(true, "&#x02C6;");
238                 t["mathring"] = Attributes(true, "&#x02DA;");
239                 t["overbrace"] = Attributes(true, "&#x23DE;");
240                 t["overleftarrow"] = Attributes(true, "&#x27F5;");
241                 t["overleftrightarrow"] = Attributes(true, "&#x27F7;");
242                 t["overline"] = Attributes(true, "&#x00AF;");
243                 t["overrightarrow"] = Attributes(true, "&#x27F6;");
244                 t["tilde"] = Attributes(true, "&#x02DC;");
245                 t["uline"] = Attributes(false, "&#x00AF;");
246                 t["underbar"] = Attributes(false, "&#x0332;");
247                 t["underbrace"] = Attributes(false, "&#x23DF;");
248                 t["underleftarrow"] = Attributes(false, "&#x27F5;");
249                 t["underleftrightarrow"] = Attributes(false, "&#x27F7;");
250                 // this is the macron, again, but it works
251                 t["underline"] = Attributes(false, "&#x00AF;");
252                 t["underrightarrow"] = Attributes(false, "&#x27F6;");
253                 t["undertilde"] = Attributes(false, "&#x223C;");
254                 t["utilde"] = Attributes(false, "&#x223C;");
255                 t["uuline"] = Attributes(false, "&#x2017;");
256                 t["uwave"] = Attributes(false, "&#x223C;");
257                 t["vec"] = Attributes(true, "&#x2192;");
258                 t["widehat"] = Attributes(true, "&#x005E;");
259                 t["widetilde"] = Attributes(true, "&#x223C;");
260         }
261
262         TranslationMap const & translationMap() {
263                 static TranslationMap t;
264                 if (t.empty())
265                         buildTranslationMap(t);
266                 return t;
267         }
268 } // namespace
269
270 void InsetMathDecoration::mathmlize(MathMLStream & ms) const
271 {
272         TranslationMap const & t = translationMap();
273         TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
274         LASSERT(cur != t.end(), return);
275         char const * const outag = cur->second.over ? "mover" : "munder";
276         std::string decoration = cur->second.entity;
277         ms << MTag(outag)
278            << cell(0)
279            << MTagInline("mo", "stretchy='true'")
280            << from_ascii(decoration)
281            << ETagInline("mo")
282            << ETag(outag);
283 }
284
285
286 void InsetMathDecoration::htmlize(HtmlStream & os) const
287 {
288         string const name = to_utf8(key_->name);
289         if (name == "bar") {
290                 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
291                 return;
292         }
293
294         if (name == "underbar" || name == "underline" || name == "uline"
295             || name == "uuline" || name == "uwave") {
296                 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
297                 return;
298         }
299
300         TranslationMap const & t = translationMap();
301         TranslationMap::const_iterator cur = t.find(name);
302         LASSERT(cur != t.end(), return);
303
304         bool symontop = cur->second.over;
305         string const symclass = symontop ? "symontop" : "symonbot";
306         os << MTag("span", "class='symbolpair " + symclass + "'")
307            << '\n';
308
309         if (symontop)
310                 os << MTag("span", "class='symbol'") << from_ascii(cur->second.entity);
311         else
312                 os << MTag("span", "class='base'") << cell(0);
313         os << ETag("span") << '\n';
314         if (symontop)
315                 os << MTag("span", "class='base'") << cell(0);
316         else
317                 os << MTag("span", "class='symbol'") << from_ascii(cur->second.entity);
318         os << ETag("span") << '\n' << ETag("span") << '\n';
319 }
320
321
322 // ideas borrowed from the eLyXer code
323 void InsetMathDecoration::validate(LaTeXFeatures & features) const
324 {
325         if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
326                 string const name = to_utf8(key_->name);
327                 if (name == "bar") {
328                         features.addCSSSnippet("span.overbar{border-top: thin black solid;}");
329                 } else if (name == "underbar" || name == "underline" ||
330                            name == "uline" || name == "uuline" || name == "uwave") {
331                         features.addCSSSnippet("span.underbar{border-bottom: thin black solid;}");
332                 } else {
333                         features.addCSSSnippet(
334                                 "span.symbolpair{display: inline-block; text-align:center;}\n"
335                                 "span.symontop{vertical-align: top;}\n"
336                                 "span.symonbot{vertical-align: bottom;}\n"
337                                 "span.symbolpair span{display: block;}\n"
338                                 "span.symbol{height: 0.5ex;}");
339                 }
340         } else {
341                 if (!key_->required.empty())
342                         features.require(key_->required);
343         }
344         InsetMathNest::validate(features);
345 }
346
347 } // namespace lyx