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)
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 Limits InsetMathDecoration::defaultLimits(bool display) const
69 if (allowsLimitsChange() && display)
76 bool InsetMathDecoration::protect() const
79 key_->name == "overbrace" ||
80 key_->name == "underbrace" ||
81 key_->name == "overleftarrow" ||
82 key_->name == "overrightarrow" ||
83 key_->name == "overleftrightarrow" ||
84 key_->name == "underleftarrow" ||
85 key_->name == "underrightarrow" ||
86 key_->name == "underleftrightarrow";
90 bool InsetMathDecoration::wide() const
93 key_->name == "overline" ||
94 key_->name == "underline" ||
95 key_->name == "overbrace" ||
96 key_->name == "underbrace" ||
97 key_->name == "overleftarrow" ||
98 key_->name == "overrightarrow" ||
99 key_->name == "overleftrightarrow" ||
100 key_->name == "widehat" ||
101 key_->name == "widetilde" ||
102 key_->name == "underleftarrow" ||
103 key_->name == "underrightarrow" ||
104 key_->name == "underleftrightarrow" ||
105 key_->name == "undertilde" ||
106 key_->name == "utilde";
110 InsetMath::mode_type InsetMathDecoration::currentMode() const
112 return key_->name == "underbar" ? TEXT_MODE : MATH_MODE;
116 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
118 Changer dummy = mi.base.changeEnsureMath(currentMode());
120 cell(0).metrics(mi, dim);
122 dh_ = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
123 dw_ = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
126 dy_ = -dim.asc - dh_;
135 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
137 Changer dummy = pi.base.changeEnsureMath(currentMode());
139 cell(0).draw(pi, x, y);
140 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
142 mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
144 mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
145 y + dy_, dw_, dh_, key_->name);
149 void InsetMathDecoration::write(TeXMathStream & os) const
151 MathEnsurer ensurer(os);
152 if (os.fragile() && protect())
154 os << '\\' << key_->name << '{';
155 ModeSpecifier specifier(os, currentMode());
156 os << cell(0) << '}';
161 void InsetMathDecoration::normalize(NormalStream & os) const
163 os << "[deco " << key_->name << ' ' << cell(0) << ']';
167 void InsetMathDecoration::infoize(odocstream & os) const
169 os << bformat(_("Decoration: %1$s"), key_->name);
175 Attributes() : over(false) {}
176 Attributes(bool o, string const & t, string const & entity)
177 : over(o), tag(t), entity(entity) {}
183 typedef map<string, Attributes> TranslationMap;
185 void buildTranslationMap(TranslationMap & t) {
186 // the decorations we need to support are listed in lib/symbols
187 t["acute"] = Attributes(true, "´", "´");
188 t["bar"] = Attributes(true, "‾", "¯");
189 t["breve"] = Attributes(true, "˘", "˘");
190 t["check"] = Attributes(true, "ˇ", "ˇ");
191 t["ddddot"] = Attributes(true, "⃜", "⃜");
192 t["dddot"] = Attributes(true, "⃛", "⃛");
193 t["ddot"] = Attributes(true, "¨", "¨");
194 t["dot"] = Attributes(true, "˙", "˙");
195 t["grave"] = Attributes(true, "`", "`");
196 t["hat"] = Attributes(true, "ˆ", "ˆ");
197 t["mathring"] = Attributes(true, "˚", "˚");
198 t["overbrace"] = Attributes(true, "⏞", "︷");
199 t["overleftarrow"] = Attributes(true, "⟵", "⟵");
200 t["overleftrightarrow"] = Attributes(true, "⟷", "⟷");
201 t["overline"] = Attributes(true, "¯", "¯");
202 t["overrightarrow"] = Attributes(true, "⟶", "⟶");
203 t["tilde"] = Attributes(true, "˜", "˜");
204 t["underbar"] = Attributes(false, "_", "̲");
205 t["underbrace"] = Attributes(false, "⏟", "︸");
206 t["underleftarrow"] = Attributes(false, "⟵", "⟵");
207 t["underleftrightarrow"] = Attributes(false, "⟷", "⟷");
208 // this is the macron, again, but it works
209 t["underline"] = Attributes(false, "¯", "¯");
210 t["underrightarrow"] = Attributes(false, "⟶", "⟶");
211 t["undertilde"] = Attributes(false, "∼", "∼");
212 t["utilde"] = Attributes(false, "∼", "∼");
213 t["vec"] = Attributes(true, "→", "→");
214 t["widehat"] = Attributes(true, "^", "^");
215 t["widetilde"] = Attributes(true, "∼", "∼");
218 TranslationMap const & translationMap() {
219 static TranslationMap t;
221 buildTranslationMap(t);
226 void InsetMathDecoration::mathmlize(MathMLStream & ms) const
228 TranslationMap const & t = translationMap();
229 TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
230 LASSERT(cur != t.end(), return);
231 char const * const outag = cur->second.over ? "mover" : "munder";
232 std::string decoration = ms.xmlMode() ? cur->second.entity : cur->second.tag;
234 << MTag("mrow") << cell(0) << ETag("mrow")
235 << "<" << from_ascii(ms.namespacedTag("mo")) << " stretchy=\"true\">"
236 << from_ascii(decoration)
237 << "</" << from_ascii(ms.namespacedTag("mo")) << ">"
242 void InsetMathDecoration::htmlize(HtmlStream & os) const
244 string const name = to_utf8(key_->name);
246 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
250 if (name == "underbar" || name == "underline") {
251 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
255 TranslationMap const & t = translationMap();
256 TranslationMap::const_iterator cur = t.find(name);
257 LASSERT(cur != t.end(), return);
259 bool symontop = cur->second.over;
260 string const symclass = symontop ? "symontop" : "symonbot";
261 os << MTag("span", "class='symbolpair " + symclass + "'")
265 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
267 os << MTag("span", "class='base'") << cell(0);
268 os << ETag("span") << '\n';
270 os << MTag("span", "class='base'") << cell(0);
272 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
273 os << ETag("span") << '\n' << ETag("span") << '\n';
277 // ideas borrowed from the eLyXer code
278 void InsetMathDecoration::validate(LaTeXFeatures & features) const
280 if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
281 string const name = to_utf8(key_->name);
283 features.addCSSSnippet("span.overbar{border-top: thin black solid;}");
284 } else if (name == "underbar" || name == "underline") {
285 features.addCSSSnippet("span.underbar{border-bottom: thin black solid;}");
287 features.addCSSSnippet(
288 "span.symbolpair{display: inline-block; text-align:center;}\n"
289 "span.symontop{vertical-align: top;}\n"
290 "span.symonbot{vertical-align: bottom;}\n"
291 "span.symbolpair span{display: block;}\n"
292 "span.symbol{height: 0.5ex;}");
295 if (!key_->required.empty())
296 features.require(key_->required);
298 InsetMathNest::validate(features);