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 & t, string const & entity)
192 : over(o), tag(t), entity(entity) {}
198 typedef map<string, Attributes> TranslationMap;
200 void buildTranslationMap(TranslationMap & t) {
201 // the decorations we need to support are listed in lib/symbols
202 t["acute"] = Attributes(true, "´", "´");
203 t["bar"] = Attributes(true, "‾", "¯");
204 t["breve"] = Attributes(true, "˘", "˘");
205 t["check"] = Attributes(true, "ˇ", "ˇ");
206 t["ddddot"] = Attributes(true, "⃜", "⃜");
207 t["dddot"] = Attributes(true, "⃛", "⃛");
208 t["ddot"] = Attributes(true, "¨", "¨");
209 t["dot"] = Attributes(true, "˙", "˙");
210 t["grave"] = Attributes(true, "`", "`");
211 t["hat"] = Attributes(true, "ˆ", "ˆ");
212 t["mathring"] = Attributes(true, "˚", "˚");
213 t["overbrace"] = Attributes(true, "⏞", "︷");
214 t["overleftarrow"] = Attributes(true, "⟵", "⟵");
215 t["overleftrightarrow"] = Attributes(true, "⟷", "⟷");
216 t["overline"] = Attributes(true, "¯", "¯");
217 t["overrightarrow"] = Attributes(true, "⟶", "⟶");
218 t["tilde"] = Attributes(true, "˜", "˜");
219 t["underbar"] = Attributes(false, "_", "̲");
220 t["underbrace"] = Attributes(false, "⏟", "︸");
221 t["underleftarrow"] = Attributes(false, "⟵", "⟵");
222 t["underleftrightarrow"] = Attributes(false, "⟷", "⟷");
223 // this is the macron, again, but it works
224 t["underline"] = Attributes(false, "¯", "¯");
225 t["underrightarrow"] = Attributes(false, "⟶", "⟶");
226 t["undertilde"] = Attributes(false, "∼", "∼");
227 t["utilde"] = Attributes(false, "∼", "∼");
228 t["vec"] = Attributes(true, "→", "→");
229 t["widehat"] = Attributes(true, "^", "^");
230 t["widetilde"] = Attributes(true, "∼", "∼");
233 TranslationMap const & translationMap() {
234 static TranslationMap t;
236 buildTranslationMap(t);
241 void InsetMathDecoration::mathmlize(MathMLStream & ms) const
243 TranslationMap const & t = translationMap();
244 TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
245 LASSERT(cur != t.end(), return);
246 char const * const outag = cur->second.over ? "mover" : "munder";
247 std::string decoration = ms.xmlMode() ? cur->second.entity : cur->second.tag;
250 << "<" << from_ascii(ms.namespacedTag("mo")) << " stretchy=\"true\">"
251 << from_ascii(decoration)
252 << "</" << from_ascii(ms.namespacedTag("mo")) << ">"
257 void InsetMathDecoration::htmlize(HtmlStream & os) const
259 string const name = to_utf8(key_->name);
261 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
265 if (name == "underbar" || name == "underline") {
266 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
270 TranslationMap const & t = translationMap();
271 TranslationMap::const_iterator cur = t.find(name);
272 LASSERT(cur != t.end(), return);
274 bool symontop = cur->second.over;
275 string const symclass = symontop ? "symontop" : "symonbot";
276 os << MTag("span", "class='symbolpair " + symclass + "'")
280 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
282 os << MTag("span", "class='base'") << cell(0);
283 os << ETag("span") << '\n';
285 os << MTag("span", "class='base'") << cell(0);
287 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
288 os << ETag("span") << '\n' << ETag("span") << '\n';
292 // ideas borrowed from the eLyXer code
293 void InsetMathDecoration::validate(LaTeXFeatures & features) const
295 if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
296 string const name = to_utf8(key_->name);
298 features.addCSSSnippet("span.overbar{border-top: thin black solid;}");
299 } else if (name == "underbar" || name == "underline") {
300 features.addCSSSnippet("span.underbar{border-bottom: thin black solid;}");
302 features.addCSSSnippet(
303 "span.symbolpair{display: inline-block; text-align:center;}\n"
304 "span.symontop{vertical-align: top;}\n"
305 "span.symonbot{vertical-align: bottom;}\n"
306 "span.symbolpair span{display: block;}\n"
307 "span.symbol{height: 0.5ex;}");
310 if (!key_->required.empty())
311 features.require(key_->required);
313 InsetMathNest::validate(features);