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 MathClass InsetMathDecoration::mathClass() const
60 if (key_->name == "overbrace" || key_->name == "underbrace")
66 bool InsetMathDecoration::isScriptable() const
68 return mathClass() == MC_OP;
72 bool InsetMathDecoration::protect() const
75 key_->name == "overbrace" ||
76 key_->name == "underbrace" ||
77 key_->name == "overleftarrow" ||
78 key_->name == "overrightarrow" ||
79 key_->name == "overleftrightarrow" ||
80 key_->name == "underleftarrow" ||
81 key_->name == "underrightarrow" ||
82 key_->name == "underleftrightarrow";
86 bool InsetMathDecoration::wide() const
89 key_->name == "overline" ||
90 key_->name == "underline" ||
91 key_->name == "overbrace" ||
92 key_->name == "underbrace" ||
93 key_->name == "overleftarrow" ||
94 key_->name == "overrightarrow" ||
95 key_->name == "overleftrightarrow" ||
96 key_->name == "widehat" ||
97 key_->name == "widetilde" ||
98 key_->name == "underleftarrow" ||
99 key_->name == "underrightarrow" ||
100 key_->name == "underleftrightarrow" ||
101 key_->name == "undertilde" ||
102 key_->name == "utilde";
106 InsetMath::mode_type InsetMathDecoration::currentMode() const
108 return key_->name == "underbar" ? TEXT_MODE : MATH_MODE;
112 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
114 Changer dummy = mi.base.changeEnsureMath(currentMode());
116 cell(0).metrics(mi, dim);
118 dh_ = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
119 dw_ = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
122 dy_ = -dim.asc - dh_;
131 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
133 Changer dummy = pi.base.changeEnsureMath(currentMode());
135 cell(0).draw(pi, x, y);
136 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
138 mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
140 mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
141 y + dy_, dw_, dh_, key_->name);
145 void InsetMathDecoration::write(WriteStream & os) const
147 MathEnsurer ensurer(os);
148 if (os.fragile() && protect())
150 os << '\\' << key_->name << '{';
151 ModeSpecifier specifier(os, currentMode());
152 os << cell(0) << '}';
156 void InsetMathDecoration::normalize(NormalStream & os) const
158 os << "[deco " << key_->name << ' ' << cell(0) << ']';
162 void InsetMathDecoration::infoize(odocstream & os) const
164 os << bformat(_("Decoration: %1$s"), key_->name);
170 Attributes() : over(false) {}
171 Attributes(bool o, string t)
177 typedef map<string, Attributes> TranslationMap;
179 void buildTranslationMap(TranslationMap & t) {
180 // the decorations we need to support are listed in lib/symbols
181 t["acute"] = Attributes(true, "´");
182 t["bar"] = Attributes(true, "‾");
183 t["breve"] = Attributes(true, "˘");
184 t["check"] = Attributes(true, "ˇ");
185 t["ddddot"] = Attributes(true, "⃜");
186 t["dddot"] = Attributes(true, "⃛");
187 t["ddot"] = Attributes(true, "¨");
188 t["dot"] = Attributes(true, "˙");
189 t["grave"] = Attributes(true, "`");
190 t["hat"] = Attributes(true, "ˆ");
191 t["mathring"] = Attributes(true, "˚");
192 t["overbrace"] = Attributes(true, "⏞");
193 t["overleftarrow"] = Attributes(true, "⟵");
194 t["overleftrightarrow"] = Attributes(true, "⟷");
195 t["overline"] = Attributes(true, "¯");
196 t["overrightarrow"] = Attributes(true, "⟶");
197 t["tilde"] = Attributes(true, "˜");
198 t["underbar"] = Attributes(false, "_");
199 t["underbrace"] = Attributes(false, "⏟");
200 t["underleftarrow"] = Attributes(false, "⟵");
201 t["underleftrightarrow"] = Attributes(false, "⟷");
202 // this is the macron, again, but it works
203 t["underline"] = Attributes(false, "¯");
204 t["underrightarrow"] = Attributes(false, "⟶");
205 t["undertilde"] = Attributes(false, "∼");
206 t["utilde"] = Attributes(false, "∼");
207 t["vec"] = Attributes(true, "→");
208 t["widehat"] = Attributes(true, "^");
209 t["widetilde"] = Attributes(true, "∼");
212 TranslationMap const & translationMap() {
213 static TranslationMap t;
215 buildTranslationMap(t);
220 void InsetMathDecoration::mathmlize(MathStream & os) const
222 TranslationMap const & t = translationMap();
223 TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
224 LASSERT(cur != t.end(), return);
225 char const * const outag = cur->second.over ? "mover" : "munder";
227 << MTag("mrow") << cell(0) << ETag("mrow")
228 << from_ascii("<mo stretchy=\"true\">" + cur->second.tag + "</mo>")
233 void InsetMathDecoration::htmlize(HtmlStream & os) const
235 string const name = to_utf8(key_->name);
237 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
241 if (name == "underbar" || name == "underline") {
242 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
246 TranslationMap const & t = translationMap();
247 TranslationMap::const_iterator cur = t.find(name);
248 LASSERT(cur != t.end(), return);
250 bool symontop = cur->second.over;
251 string const symclass = symontop ? "symontop" : "symonbot";
252 os << MTag("span", "class='symbolpair " + symclass + "'")
256 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
258 os << MTag("span", "class='base'") << cell(0);
259 os << ETag("span") << '\n';
261 os << MTag("span", "class='base'") << cell(0);
263 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
264 os << ETag("span") << '\n' << ETag("span") << '\n';
268 // ideas borrowed from the eLyXer code
269 void InsetMathDecoration::validate(LaTeXFeatures & features) const
271 if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
272 string const name = to_utf8(key_->name);
274 features.addCSSSnippet("span.overbar{border-top: thin black solid;}");
275 } else if (name == "underbar" || name == "underline") {
276 features.addCSSSnippet("span.underbar{border-bottom: thin black solid;}");
278 features.addCSSSnippet(
279 "span.symbolpair{display: inline-block; text-align:center;}\n"
280 "span.symontop{vertical-align: top;}\n"
281 "span.symonbot{vertical-align: bottom;}\n"
282 "span.symbolpair span{display: block;}\n"
283 "span.symbol{height: 0.5ex;}");
286 if (!key_->requires.empty())
287 features.require(key_->requires);
289 InsetMathNest::validate(features);