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 "BufferView.h"
18 #include "MathParser.h"
19 #include "MathSupport.h"
20 #include "MathStream.h"
21 #include "MetricsInfo.h"
23 #include "LaTeXFeatures.h"
25 #include "support/debug.h"
26 #include "support/docstring.h"
27 #include "support/gettext.h"
28 #include "support/lassert.h"
29 #include "support/lstrings.h"
34 using namespace lyx::support;
41 InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
42 : InsetMathNest(buf, 1), key_(key)
44 // lyxerr << " creating deco " << key->name << endl;
48 Inset * InsetMathDecoration::clone() const
50 return new InsetMathDecoration(*this);
54 bool InsetMathDecoration::upper() const
56 return key_->name.substr(0, 5) != "under" && key_->name != "utilde";
60 MathClass InsetMathDecoration::mathClass() const
62 if (key_->name == "overbrace" || key_->name == "underbrace")
68 Limits InsetMathDecoration::defaultLimits(bool display) const
70 if (allowsLimitsChange() && display)
77 bool InsetMathDecoration::protect() const
80 key_->name == "overbrace" ||
81 key_->name == "underbrace" ||
82 key_->name == "overleftarrow" ||
83 key_->name == "overrightarrow" ||
84 key_->name == "overleftrightarrow" ||
85 key_->name == "underleftarrow" ||
86 key_->name == "underrightarrow" ||
87 key_->name == "underleftrightarrow";
91 bool InsetMathDecoration::wide() const
94 key_->name == "overline" ||
95 key_->name == "underline" ||
96 key_->name == "overbrace" ||
97 key_->name == "underbrace" ||
98 key_->name == "overleftarrow" ||
99 key_->name == "overrightarrow" ||
100 key_->name == "overleftrightarrow" ||
101 key_->name == "widehat" ||
102 key_->name == "widetilde" ||
103 key_->name == "underleftarrow" ||
104 key_->name == "underrightarrow" ||
105 key_->name == "underleftrightarrow" ||
106 key_->name == "undertilde" ||
107 key_->name == "utilde";
111 InsetMath::mode_type InsetMathDecoration::currentMode() const
113 return key_->name == "underbar" ? TEXT_MODE : MATH_MODE;
117 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
119 Changer dummy = mi.base.changeEnsureMath(currentMode());
121 cell(0).metrics(mi, dim);
123 int const l1 = mi.base.bv->zoomedPixels(1);
124 int const l2 = 2 * l1;
125 int const l3 = 3 * l1;
127 dh_ = l2; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
128 dw_ = l3; //mathed_char_width(LM_TC_VAR, mi, 'x');
131 dy_ = -dim.asc - dh_ - l1;
140 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
142 Changer dummy = pi.base.changeEnsureMath(currentMode());
144 cell(0).draw(pi, x, y);
145 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
147 mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
149 mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
150 y + dy_, dw_, dh_, key_->name);
154 void InsetMathDecoration::write(TeXMathStream & os) const
156 MathEnsurer ensurer(os);
157 if (os.fragile() && protect())
159 os << '\\' << key_->name << '{';
160 ModeSpecifier specifier(os, currentMode());
161 os << cell(0) << '}';
166 void InsetMathDecoration::normalize(NormalStream & os) const
168 os << "[deco " << key_->name << ' ' << cell(0) << ']';
172 void InsetMathDecoration::infoize(odocstream & os) const
174 os << bformat(_("Decoration: %1$s"), key_->name);
180 Attributes() : over(false) {}
181 Attributes(bool o, string const & t, string const & entity)
182 : over(o), tag(t), entity(entity) {}
188 typedef map<string, Attributes> TranslationMap;
190 void buildTranslationMap(TranslationMap & t) {
191 // the decorations we need to support are listed in lib/symbols
192 t["acute"] = Attributes(true, "´", "´");
193 t["bar"] = Attributes(true, "‾", "¯");
194 t["breve"] = Attributes(true, "˘", "˘");
195 t["check"] = Attributes(true, "ˇ", "ˇ");
196 t["ddddot"] = Attributes(true, "⃜", "⃜");
197 t["dddot"] = Attributes(true, "⃛", "⃛");
198 t["ddot"] = Attributes(true, "¨", "¨");
199 t["dot"] = Attributes(true, "˙", "˙");
200 t["grave"] = Attributes(true, "`", "`");
201 t["hat"] = Attributes(true, "ˆ", "ˆ");
202 t["mathring"] = Attributes(true, "˚", "˚");
203 t["overbrace"] = Attributes(true, "⏞", "︷");
204 t["overleftarrow"] = Attributes(true, "⟵", "⟵");
205 t["overleftrightarrow"] = Attributes(true, "⟷", "⟷");
206 t["overline"] = Attributes(true, "¯", "¯");
207 t["overrightarrow"] = Attributes(true, "⟶", "⟶");
208 t["tilde"] = Attributes(true, "˜", "˜");
209 t["underbar"] = Attributes(false, "_", "̲");
210 t["underbrace"] = Attributes(false, "⏟", "︸");
211 t["underleftarrow"] = Attributes(false, "⟵", "⟵");
212 t["underleftrightarrow"] = Attributes(false, "⟷", "⟷");
213 // this is the macron, again, but it works
214 t["underline"] = Attributes(false, "¯", "¯");
215 t["underrightarrow"] = Attributes(false, "⟶", "⟶");
216 t["undertilde"] = Attributes(false, "∼", "∼");
217 t["utilde"] = Attributes(false, "∼", "∼");
218 t["vec"] = Attributes(true, "→", "→");
219 t["widehat"] = Attributes(true, "^", "^");
220 t["widetilde"] = Attributes(true, "∼", "∼");
223 TranslationMap const & translationMap() {
224 static TranslationMap t;
226 buildTranslationMap(t);
231 void InsetMathDecoration::mathmlize(MathMLStream & ms) const
233 TranslationMap const & t = translationMap();
234 TranslationMap::const_iterator cur = t.find(to_utf8(key_->name));
235 LASSERT(cur != t.end(), return);
236 char const * const outag = cur->second.over ? "mover" : "munder";
237 std::string decoration = ms.xmlMode() ? cur->second.entity : cur->second.tag;
239 << MTag("mrow") << cell(0) << ETag("mrow")
240 << "<" << from_ascii(ms.namespacedTag("mo")) << " stretchy=\"true\">"
241 << from_ascii(decoration)
242 << "</" << from_ascii(ms.namespacedTag("mo")) << ">"
247 void InsetMathDecoration::htmlize(HtmlStream & os) const
249 string const name = to_utf8(key_->name);
251 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
255 if (name == "underbar" || name == "underline") {
256 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
260 TranslationMap const & t = translationMap();
261 TranslationMap::const_iterator cur = t.find(name);
262 LASSERT(cur != t.end(), return);
264 bool symontop = cur->second.over;
265 string const symclass = symontop ? "symontop" : "symonbot";
266 os << MTag("span", "class='symbolpair " + symclass + "'")
270 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
272 os << MTag("span", "class='base'") << cell(0);
273 os << ETag("span") << '\n';
275 os << MTag("span", "class='base'") << cell(0);
277 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
278 os << ETag("span") << '\n' << ETag("span") << '\n';
282 // ideas borrowed from the eLyXer code
283 void InsetMathDecoration::validate(LaTeXFeatures & features) const
285 if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
286 string const name = to_utf8(key_->name);
288 features.addCSSSnippet("span.overbar{border-top: thin black solid;}");
289 } else if (name == "underbar" || name == "underline") {
290 features.addCSSSnippet("span.underbar{border-bottom: thin black solid;}");
292 features.addCSSSnippet(
293 "span.symbolpair{display: inline-block; text-align:center;}\n"
294 "span.symontop{vertical-align: top;}\n"
295 "span.symonbot{vertical-align: bottom;}\n"
296 "span.symbolpair span{display: block;}\n"
297 "span.symbol{height: 0.5ex;}");
300 if (!key_->required.empty())
301 features.require(key_->required);
303 InsetMathNest::validate(features);