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"
32 using namespace lyx::support;
39 InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
40 : InsetMathNest(buf, 1), key_(key), dh_(0), dy_(0), dw_(0)
42 // lyxerr << " creating deco " << key->name << endl;
46 Inset * InsetMathDecoration::clone() const
48 return new InsetMathDecoration(*this);
52 bool InsetMathDecoration::upper() const
54 return key_->name.substr(0, 5) != "under" && key_->name != "utilde";
58 bool InsetMathDecoration::isScriptable() const
61 key_->name == "overbrace" ||
62 key_->name == "underbrace";
66 bool InsetMathDecoration::protect() const
69 key_->name == "overbrace" ||
70 key_->name == "underbrace" ||
71 key_->name == "overleftarrow" ||
72 key_->name == "overrightarrow" ||
73 key_->name == "overleftrightarrow" ||
74 key_->name == "underleftarrow" ||
75 key_->name == "underrightarrow" ||
76 key_->name == "underleftrightarrow";
80 bool InsetMathDecoration::wide() const
83 key_->name == "overline" ||
84 key_->name == "underline" ||
85 key_->name == "overbrace" ||
86 key_->name == "underbrace" ||
87 key_->name == "overleftarrow" ||
88 key_->name == "overrightarrow" ||
89 key_->name == "overleftrightarrow" ||
90 key_->name == "widehat" ||
91 key_->name == "widetilde" ||
92 key_->name == "underleftarrow" ||
93 key_->name == "underrightarrow" ||
94 key_->name == "underleftrightarrow" ||
95 key_->name == "undertilde" ||
96 key_->name == "utilde";
100 InsetMath::mode_type InsetMathDecoration::currentMode() const
102 return key_->name == "underbar" ? TEXT_MODE : MATH_MODE;
106 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
108 Changer dummy = mi.base.changeEnsureMath(currentMode());
110 cell(0).metrics(mi, dim);
112 dh_ = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
113 dw_ = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
116 dy_ = -dim.asc - dh_;
123 metricsMarkers(mi, dim);
127 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
129 Changer dummy = pi.base.changeEnsureMath(currentMode());
131 cell(0).draw(pi, x + 1, y);
132 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
134 mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
136 mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
137 y + dy_, dw_, dh_, key_->name);
138 drawMarkers(pi, x, y);
142 void InsetMathDecoration::write(WriteStream & os) const
144 MathEnsurer ensurer(os);
145 if (os.fragile() && protect())
147 os << '\\' << key_->name << '{';
148 ModeSpecifier specifier(os, currentMode());
149 os << cell(0) << '}';
153 void InsetMathDecoration::normalize(NormalStream & os) const
155 os << "[deco " << key_->name << ' ' << cell(0) << ']';
159 void InsetMathDecoration::infoize(odocstream & os) const
161 os << bformat(_("Decoration: %1$s"), key_->name);
167 Attributes() : over(false) {}
168 Attributes(bool o, string t)
174 typedef map<string, Attributes> TranslationMap;
176 void buildTranslationMap(TranslationMap & t) {
177 // the decorations we need to support are listed in lib/symbols
178 t["acute"] = Attributes(true, "´");
179 t["bar"] = Attributes(true, "‾");
180 t["breve"] = Attributes(true, "˘");
181 t["check"] = Attributes(true, "ˇ");
182 t["ddddot"] = Attributes(true, "⃜");
183 t["dddot"] = Attributes(true, "⃛");
184 t["ddot"] = Attributes(true, "¨");
185 t["dot"] = Attributes(true, "˙");
186 t["grave"] = Attributes(true, "`");
187 t["hat"] = Attributes(true, "ˆ");
188 t["mathring"] = Attributes(true, "˚");
189 t["overbrace"] = Attributes(true, "⏞");
190 t["overleftarrow"] = Attributes(true, "⟵");
191 t["overleftrightarrow"] = Attributes(true, "⟷");
192 t["overline"] = Attributes(true, "¯");
193 t["overrightarrow"] = Attributes(true, "⟶");
194 t["tilde"] = Attributes(true, "˜");
195 t["underbar"] = Attributes(false, "_");
196 t["underbrace"] = Attributes(false, "⏟");
197 t["underleftarrow"] = Attributes(false, "⟵");
198 t["underleftrightarrow"] = Attributes(false, "⟷");
199 // this is the macron, again, but it works
200 t["underline"] = Attributes(false, "¯");
201 t["underrightarrow"] = Attributes(false, "⟶");
202 t["undertilde"] = Attributes(false, "∼");
203 t["utilde"] = Attributes(false, "∼");
204 t["vec"] = Attributes(true, "→");
205 t["widehat"] = Attributes(true, "^");
206 t["widetilde"] = Attributes(true, "∼");
209 TranslationMap const & translationMap() {
210 static TranslationMap t;
212 buildTranslationMap(t);
217 void InsetMathDecoration::mathmlize(MathStream & os) const
219 TranslationMap const & t = translationMap();
220 TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
221 LASSERT(cur != t.end(), return);
222 char const * const outag = cur->second.over ? "mover" : "munder";
224 << MTag("mrow") << cell(0) << ETag("mrow")
225 << from_ascii("<mo stretchy=\"true\">" + cur->second.tag + "</mo>")
230 void InsetMathDecoration::htmlize(HtmlStream & os) const
232 string const name = to_utf8(key_->name);
234 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
238 if (name == "underbar" || name == "underline") {
239 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
243 TranslationMap const & t = translationMap();
244 TranslationMap::const_iterator cur = t.find(name);
245 LASSERT(cur != t.end(), return);
247 bool symontop = cur->second.over;
248 string const symclass = symontop ? "symontop" : "symonbot";
249 os << MTag("span", "class='symbolpair " + symclass + "'")
253 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
255 os << MTag("span", "class='base'") << cell(0);
256 os << ETag("span") << '\n';
258 os << MTag("span", "class='base'") << cell(0);
260 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
261 os << ETag("span") << '\n' << ETag("span") << '\n';
265 // ideas borrowed from the eLyXer code
266 void InsetMathDecoration::validate(LaTeXFeatures & features) const
268 if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
269 string const name = to_utf8(key_->name);
271 features.addCSSSnippet("span.overbar{border-top: thin black solid;}");
272 } else if (name == "underbar" || name == "underline") {
273 features.addCSSSnippet("span.underbar{border-bottom: thin black solid;}");
275 features.addCSSSnippet(
276 "span.symbolpair{display: inline-block; text-align:center;}\n"
277 "span.symontop{vertical-align: top;}\n"
278 "span.symonbot{vertical-align: bottom;}\n"
279 "span.symbolpair span{display: block;}\n"
280 "span.symbol{height: 0.5ex;}");
283 if (!key_->requires.empty())
284 features.require(key_->requires);
286 InsetMathNest::validate(features);