2 * \file InsetMathDecoration.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
9 * Full author contact details are available in file CREDITS.
14 #include "InsetMathDecoration.h"
16 #include "InsetMathChar.h"
18 #include "MathParser.h"
19 #include "MathSupport.h"
20 #include "MathStream.h"
21 #include "MetricsInfo.h"
23 #include "BufferView.h"
24 #include "LaTeXFeatures.h"
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"
35 using namespace lyx::support;
42 InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
43 : InsetMathNest(buf, 1), key_(key), outer_mode_(UNDECIDED_MODE)
45 // lyxerr << " creating deco " << key->name << endl;
49 Inset * InsetMathDecoration::clone() const
51 return new InsetMathDecoration(*this);
55 bool InsetMathDecoration::upper() const
57 return key_->name.substr(0, 5) != "under" &&
58 key_->name != "utilde" &&
59 key_->name != "uline" &&
60 key_->name != "uuline" &&
61 key_->name != "uwave";
65 MathClass InsetMathDecoration::mathClass() const
67 if (key_->name == "overbrace" || key_->name == "underbrace")
73 Limits InsetMathDecoration::defaultLimits(bool /*display*/) const
75 if (allowsLimitsChange())
82 bool InsetMathDecoration::protect() const
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";
96 bool InsetMathDecoration::wide() const
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";
119 InsetMath::mode_type InsetMathDecoration::currentMode() const
121 if (key_->name == "underbar")
124 if (key_->name == "uline" || key_->name == "uuline" ||
125 key_->name == "uwave")
132 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
134 outer_mode_ = isTextFont(mi.base.fontname) ? TEXT_MODE : MATH_MODE;
136 Changer dummy = mi.base.changeEnsureMath(currentMode());
138 cell(0).metrics(mi, dim);
140 int const l1 = mi.base.bv->zoomedPixels(1);
141 int const l2 = mathed_char_width(mi.base.font, 'x') - l1;
144 dh_ = l2; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
145 dw_ = l3; //mathed_char_width(LM_TC_VAR, mi, 'x');
148 dy_ = -dim.asc - dh_ - l1;
157 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
159 outer_mode_ = isTextFont(pi.base.fontname) ? TEXT_MODE : MATH_MODE;
161 Changer dummy = pi.base.changeEnsureMath(currentMode());
163 cell(0).draw(pi, x, y);
164 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
166 mathed_draw_deco(pi, x, y + dy_, dim0.wid, dh_, key_->name);
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);
182 void InsetMathDecoration::write(TeXMathStream & os) const
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())
190 os << '\\' << key_->name << '{';
191 ModeSpecifier specifier(os, currentMode());
192 os << cell(0) << '}';
197 void InsetMathDecoration::normalize(NormalStream & os) const
199 os << "[deco " << key_->name << ' ' << cell(0) << ']';
203 docstring InsetMathDecoration::name() const
209 void InsetMathDecoration::infoize(odocstream & os) const
211 os << bformat(_("Decoration: %1$s"), key_->name);
217 Attributes() : over(false) {}
218 Attributes(bool o, string const & entity)
219 : over(o), entity(entity) {}
224 typedef map<string, Attributes> TranslationMap;
226 void buildTranslationMap(TranslationMap & t) {
227 // the decorations we need to support are listed in lib/symbols
228 t["acute"] = Attributes(true, "´");
229 t["bar"] = Attributes(true, "¯");
230 t["breve"] = Attributes(true, "˘");
231 t["check"] = Attributes(true, "ˇ");
232 t["ddddot"] = Attributes(true, "⃜");
233 t["dddot"] = Attributes(true, "⃛");
234 t["ddot"] = Attributes(true, "¨");
235 t["dot"] = Attributes(true, "˙");
236 t["grave"] = Attributes(true, "`");
237 t["hat"] = Attributes(true, "ˆ");
238 t["mathring"] = Attributes(true, "˚");
239 t["overbrace"] = Attributes(true, "⏞");
240 t["overleftarrow"] = Attributes(true, "⟵");
241 t["overleftrightarrow"] = Attributes(true, "⟷");
242 t["overline"] = Attributes(true, "¯");
243 t["overrightarrow"] = Attributes(true, "⟶");
244 t["tilde"] = Attributes(true, "˜");
245 t["uline"] = Attributes(false, "¯");
246 t["underbar"] = Attributes(false, "̲");
247 t["underbrace"] = Attributes(false, "⏟");
248 t["underleftarrow"] = Attributes(false, "⟵");
249 t["underleftrightarrow"] = Attributes(false, "⟷");
250 // this is the macron, again, but it works
251 t["underline"] = Attributes(false, "¯");
252 t["underrightarrow"] = Attributes(false, "⟶");
253 t["undertilde"] = Attributes(false, "∼");
254 t["utilde"] = Attributes(false, "∼");
255 t["uuline"] = Attributes(false, "‗");
256 t["uwave"] = Attributes(false, "∼");
257 t["vec"] = Attributes(true, "→");
258 t["widehat"] = Attributes(true, "^");
259 t["widetilde"] = Attributes(true, "∼");
262 TranslationMap const & translationMap() {
263 static TranslationMap t;
265 buildTranslationMap(t);
270 void InsetMathDecoration::mathmlize(MathMLStream & ms) const
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;
279 << MTagInline("mo", "stretchy='true'")
280 << from_ascii(decoration)
286 void InsetMathDecoration::htmlize(HtmlStream & os) const
288 string const name = to_utf8(key_->name);
290 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
294 if (name == "underbar" || name == "underline" || name == "uline"
295 || name == "uuline" || name == "uwave") {
296 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
300 TranslationMap const & t = translationMap();
301 TranslationMap::const_iterator cur = t.find(name);
302 LASSERT(cur != t.end(), return);
304 bool symontop = cur->second.over;
305 string const symclass = symontop ? "symontop" : "symonbot";
306 os << MTag("span", "class='symbolpair " + symclass + "'")
310 os << MTag("span", "class='symbol'") << from_ascii(cur->second.entity);
312 os << MTag("span", "class='base'") << cell(0);
313 os << ETag("span") << '\n';
315 os << MTag("span", "class='base'") << cell(0);
317 os << MTag("span", "class='symbol'") << from_ascii(cur->second.entity);
318 os << ETag("span") << '\n' << ETag("span") << '\n';
322 // ideas borrowed from the eLyXer code
323 void InsetMathDecoration::validate(LaTeXFeatures & features) const
325 if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
326 string const name = to_utf8(key_->name);
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;}");
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;}");
341 if (!key_->required.empty())
342 features.require(key_->required);
344 InsetMathNest::validate(features);