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)
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" && key_->name != "utilde";
61 MathClass InsetMathDecoration::mathClass() const
63 if (key_->name == "overbrace" || key_->name == "underbrace")
69 Limits InsetMathDecoration::defaultLimits(bool /*display*/) const
71 if (allowsLimitsChange())
78 bool InsetMathDecoration::protect() const
81 key_->name == "overbrace" ||
82 key_->name == "underbrace" ||
83 key_->name == "overleftarrow" ||
84 key_->name == "overrightarrow" ||
85 key_->name == "overleftrightarrow" ||
86 key_->name == "underleftarrow" ||
87 key_->name == "underrightarrow" ||
88 key_->name == "underleftrightarrow";
92 bool InsetMathDecoration::wide() const
95 key_->name == "overline" ||
96 key_->name == "underline" ||
97 key_->name == "overbrace" ||
98 key_->name == "underbrace" ||
99 key_->name == "overleftarrow" ||
100 key_->name == "overrightarrow" ||
101 key_->name == "overleftrightarrow" ||
102 key_->name == "widehat" ||
103 key_->name == "widetilde" ||
104 key_->name == "underleftarrow" ||
105 key_->name == "underrightarrow" ||
106 key_->name == "underleftrightarrow" ||
107 key_->name == "undertilde" ||
108 key_->name == "utilde";
112 InsetMath::mode_type InsetMathDecoration::currentMode() const
114 return key_->name == "underbar" ? TEXT_MODE : MATH_MODE;
118 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
120 Changer dummy = mi.base.changeEnsureMath(currentMode());
122 cell(0).metrics(mi, dim);
124 int const l1 = mi.base.bv->zoomedPixels(1);
125 int const l2 = mathed_char_width(mi.base.font, 'x') - l1;
128 dh_ = l2; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
129 dw_ = l3; //mathed_char_width(LM_TC_VAR, mi, 'x');
132 dy_ = -dim.asc - dh_ - l1;
141 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
143 Changer dummy = pi.base.changeEnsureMath(currentMode());
145 cell(0).draw(pi, x, y);
146 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
148 mathed_draw_deco(pi, x, y + dy_, dim0.wid, dh_, key_->name);
151 // Lacking the necessary font parameters, in order to properly align
152 // the decoration we have to resort to heuristics for choosing a
153 // suitable value for shift
154 char_type c = (cell(0).empty() || !cell(0)[0]->asCharInset())
155 ? 0 : cell(0)[0]->asCharInset()->getChar();
156 double slope = (c == 0) ? 0.0 : mathed_char_slope(pi.base, c);
157 int kerning = (c == 0) ? 0 : mathed_char_kerning(pi.base.font, c);
158 int shift = (kerning == 0) ? int(dim0.asc * slope) : kerning;
159 mathed_draw_deco(pi, x + (dim0.wid - dw_) / 2 + shift,
160 y + dy_, dw_, dh_, key_->name);
164 void InsetMathDecoration::write(TeXMathStream & os) const
166 MathEnsurer ensurer(os);
167 if (os.fragile() && protect())
169 os << '\\' << key_->name << '{';
170 ModeSpecifier specifier(os, currentMode());
171 os << cell(0) << '}';
176 void InsetMathDecoration::normalize(NormalStream & os) const
178 os << "[deco " << key_->name << ' ' << cell(0) << ']';
182 void InsetMathDecoration::infoize(odocstream & os) const
184 os << bformat(_("Decoration: %1$s"), key_->name);
190 Attributes() : over(false) {}
191 Attributes(bool o, string const & entity)
192 : over(o), entity(entity) {}
197 typedef map<string, Attributes> TranslationMap;
199 void buildTranslationMap(TranslationMap & t) {
200 // the decorations we need to support are listed in lib/symbols
201 t["acute"] = Attributes(true, "´");
202 t["bar"] = Attributes(true, "¯");
203 t["breve"] = Attributes(true, "˘");
204 t["check"] = Attributes(true, "ˇ");
205 t["ddddot"] = Attributes(true, "⃜");
206 t["dddot"] = Attributes(true, "⃛");
207 t["ddot"] = Attributes(true, "¨");
208 t["dot"] = Attributes(true, "˙");
209 t["grave"] = Attributes(true, "`");
210 t["hat"] = Attributes(true, "ˆ");
211 t["mathring"] = Attributes(true, "˚");
212 t["overbrace"] = Attributes(true, "⏞");
213 t["overleftarrow"] = Attributes(true, "⟵");
214 t["overleftrightarrow"] = Attributes(true, "⟷");
215 t["overline"] = Attributes(true, "¯");
216 t["overrightarrow"] = Attributes(true, "⟶");
217 t["tilde"] = Attributes(true, "˜");
218 t["underbar"] = Attributes(false, "̲");
219 t["underbrace"] = Attributes(false, "⏟");
220 t["underleftarrow"] = Attributes(false, "⟵");
221 t["underleftrightarrow"] = Attributes(false, "⟷");
222 // this is the macron, again, but it works
223 t["underline"] = Attributes(false, "¯");
224 t["underrightarrow"] = Attributes(false, "⟶");
225 t["undertilde"] = Attributes(false, "∼");
226 t["utilde"] = Attributes(false, "∼");
227 t["vec"] = Attributes(true, "→");
228 t["widehat"] = Attributes(true, "^");
229 t["widetilde"] = Attributes(true, "∼");
232 TranslationMap const & translationMap() {
233 static TranslationMap t;
235 buildTranslationMap(t);
240 void InsetMathDecoration::mathmlize(MathMLStream & ms) const
242 TranslationMap const & t = translationMap();
243 TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
244 LASSERT(cur != t.end(), return);
245 char const * const outag = cur->second.over ? "mover" : "munder";
246 std::string decoration = cur->second.entity;
249 << "<" << from_ascii(ms.namespacedTag("mo")) << " stretchy=\"true\">"
250 << from_ascii(decoration)
251 << "</" << from_ascii(ms.namespacedTag("mo")) << ">"
256 void InsetMathDecoration::htmlize(HtmlStream & os) const
258 string const name = to_utf8(key_->name);
260 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
264 if (name == "underbar" || name == "underline") {
265 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
269 TranslationMap const & t = translationMap();
270 TranslationMap::const_iterator cur = t.find(name);
271 LASSERT(cur != t.end(), return);
273 bool symontop = cur->second.over;
274 string const symclass = symontop ? "symontop" : "symonbot";
275 os << MTag("span", "class='symbolpair " + symclass + "'")
279 os << MTag("span", "class='symbol'") << from_ascii(cur->second.entity);
281 os << MTag("span", "class='base'") << cell(0);
282 os << ETag("span") << '\n';
284 os << MTag("span", "class='base'") << cell(0);
286 os << MTag("span", "class='symbol'") << from_ascii(cur->second.entity);
287 os << ETag("span") << '\n' << ETag("span") << '\n';
291 // ideas borrowed from the eLyXer code
292 void InsetMathDecoration::validate(LaTeXFeatures & features) const
294 if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
295 string const name = to_utf8(key_->name);
297 features.addCSSSnippet("span.overbar{border-top: thin black solid;}");
298 } else if (name == "underbar" || name == "underline") {
299 features.addCSSSnippet("span.underbar{border-bottom: thin black solid;}");
301 features.addCSSSnippet(
302 "span.symbolpair{display: inline-block; text-align:center;}\n"
303 "span.symontop{vertical-align: top;}\n"
304 "span.symonbot{vertical-align: bottom;}\n"
305 "span.symbolpair span{display: block;}\n"
306 "span.symbol{height: 0.5ex;}");
309 if (!key_->required.empty())
310 features.require(key_->required);
312 InsetMathNest::validate(features);