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"
17 #include "MathParser.h"
18 #include "MathSupport.h"
19 #include "MathStream.h"
20 #include "MetricsInfo.h"
22 #include "LaTeXFeatures.h"
24 #include "support/debug.h"
25 #include "support/docstring.h"
26 #include "support/gettext.h"
27 #include "support/lassert.h"
28 #include "support/lstrings.h"
33 using namespace lyx::support;
40 InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
41 : InsetMathNest(buf, 1), key_(key), dh_(0), dy_(0), dw_(0)
43 // lyxerr << " creating deco " << key->name << endl;
47 Inset * InsetMathDecoration::clone() const
49 return new InsetMathDecoration(*this);
53 bool InsetMathDecoration::upper() const
55 return key_->name.substr(0, 5) != "under" && key_->name != "utilde";
59 MathClass InsetMathDecoration::mathClass() const
61 if (key_->name == "overbrace" || key_->name == "underbrace")
67 bool InsetMathDecoration::isScriptable() const
69 return mathClass() == MC_OP;
73 bool InsetMathDecoration::protect() const
76 key_->name == "overbrace" ||
77 key_->name == "underbrace" ||
78 key_->name == "overleftarrow" ||
79 key_->name == "overrightarrow" ||
80 key_->name == "overleftrightarrow" ||
81 key_->name == "underleftarrow" ||
82 key_->name == "underrightarrow" ||
83 key_->name == "underleftrightarrow";
87 bool InsetMathDecoration::wide() const
90 key_->name == "overline" ||
91 key_->name == "underline" ||
92 key_->name == "overbrace" ||
93 key_->name == "underbrace" ||
94 key_->name == "overleftarrow" ||
95 key_->name == "overrightarrow" ||
96 key_->name == "overleftrightarrow" ||
97 key_->name == "widehat" ||
98 key_->name == "widetilde" ||
99 key_->name == "underleftarrow" ||
100 key_->name == "underrightarrow" ||
101 key_->name == "underleftrightarrow" ||
102 key_->name == "undertilde" ||
103 key_->name == "utilde";
107 InsetMath::mode_type InsetMathDecoration::currentMode() const
109 return key_->name == "underbar" ? TEXT_MODE : MATH_MODE;
113 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
115 Changer dummy = mi.base.changeEnsureMath(currentMode());
117 cell(0).metrics(mi, dim);
119 dh_ = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
120 dw_ = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
123 dy_ = -dim.asc - dh_;
132 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
134 Changer dummy = pi.base.changeEnsureMath(currentMode());
136 cell(0).draw(pi, x, y);
137 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
139 mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
141 mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
142 y + dy_, dw_, dh_, key_->name);
146 void InsetMathDecoration::write(WriteStream & os) const
148 MathEnsurer ensurer(os);
149 if (os.fragile() && protect())
151 os << '\\' << key_->name << '{';
152 ModeSpecifier specifier(os, currentMode());
153 os << cell(0) << '}';
157 void InsetMathDecoration::normalize(NormalStream & os) const
159 os << "[deco " << key_->name << ' ' << cell(0) << ']';
163 void InsetMathDecoration::infoize(odocstream & os) const
165 os << bformat(_("Decoration: %1$s"), key_->name);
171 Attributes() : over(false) {}
172 Attributes(bool o, string const & t, string const & entity)
173 : over(o), tag(t), entity(entity) {}
179 typedef map<string, Attributes> TranslationMap;
181 void buildTranslationMap(TranslationMap & t) {
182 // the decorations we need to support are listed in lib/symbols
183 t["acute"] = Attributes(true, "´", "´");
184 t["bar"] = Attributes(true, "‾", "¯");
185 t["breve"] = Attributes(true, "˘", "˘");
186 t["check"] = Attributes(true, "ˇ", "ˇ");
187 t["ddddot"] = Attributes(true, "⃜", "⃜");
188 t["dddot"] = Attributes(true, "⃛", "⃛");
189 t["ddot"] = Attributes(true, "¨", "¨");
190 t["dot"] = Attributes(true, "˙", "˙");
191 t["grave"] = Attributes(true, "`", "`");
192 t["hat"] = Attributes(true, "ˆ", "ˆ");
193 t["mathring"] = Attributes(true, "˚", "˚");
194 t["overbrace"] = Attributes(true, "⏞", "︷");
195 t["overleftarrow"] = Attributes(true, "⟵", "⟵");
196 t["overleftrightarrow"] = Attributes(true, "⟷", "⟷");
197 t["overline"] = Attributes(true, "¯", "¯");
198 t["overrightarrow"] = Attributes(true, "⟶", "⟶");
199 t["tilde"] = Attributes(true, "˜", "˜");
200 t["underbar"] = Attributes(false, "_", "̲");
201 t["underbrace"] = Attributes(false, "⏟", "︸");
202 t["underleftarrow"] = Attributes(false, "⟵", "⟵");
203 t["underleftrightarrow"] = Attributes(false, "⟷", "⟷");
204 // this is the macron, again, but it works
205 t["underline"] = Attributes(false, "¯", "¯");
206 t["underrightarrow"] = Attributes(false, "⟶", "⟶");
207 t["undertilde"] = Attributes(false, "∼", "∼");
208 t["utilde"] = Attributes(false, "∼", "∼");
209 t["vec"] = Attributes(true, "→", "→");
210 t["widehat"] = Attributes(true, "^", "^");
211 t["widetilde"] = Attributes(true, "∼", "∼");
214 TranslationMap const & translationMap() {
215 static TranslationMap t;
217 buildTranslationMap(t);
222 void InsetMathDecoration::mathmlize(MathStream & ms) const
224 TranslationMap const & t = translationMap();
225 TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
226 LASSERT(cur != t.end(), return);
227 char const * const outag = cur->second.over ? "mover" : "munder";
228 std::string decoration = ms.xmlMode() ? cur->second.entity : cur->second.tag;
230 << MTag("mrow") << cell(0) << ETag("mrow")
231 << "<" << from_ascii(ms.namespacedTag("mo")) << " stretchy=\"true\">"
232 << from_ascii(decoration)
233 << "</" << from_ascii(ms.namespacedTag("mo")) << ">"
238 void InsetMathDecoration::htmlize(HtmlStream & os) const
240 string const name = to_utf8(key_->name);
242 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
246 if (name == "underbar" || name == "underline") {
247 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
251 TranslationMap const & t = translationMap();
252 TranslationMap::const_iterator cur = t.find(name);
253 LASSERT(cur != t.end(), return);
255 bool symontop = cur->second.over;
256 string const symclass = symontop ? "symontop" : "symonbot";
257 os << MTag("span", "class='symbolpair " + symclass + "'")
261 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
263 os << MTag("span", "class='base'") << cell(0);
264 os << ETag("span") << '\n';
266 os << MTag("span", "class='base'") << cell(0);
268 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
269 os << ETag("span") << '\n' << ETag("span") << '\n';
273 // ideas borrowed from the eLyXer code
274 void InsetMathDecoration::validate(LaTeXFeatures & features) const
276 if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
277 string const name = to_utf8(key_->name);
279 features.addCSSSnippet("span.overbar{border-top: thin black solid;}");
280 } else if (name == "underbar" || name == "underline") {
281 features.addCSSSnippet("span.underbar{border-bottom: thin black solid;}");
283 features.addCSSSnippet(
284 "span.symbolpair{display: inline-block; text-align:center;}\n"
285 "span.symontop{vertical-align: top;}\n"
286 "span.symonbot{vertical-align: bottom;}\n"
287 "span.symbolpair span{display: block;}\n"
288 "span.symbol{height: 0.5ex;}");
291 if (!key_->required.empty())
292 features.require(key_->required);
294 InsetMathNest::validate(features);