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/lassert.h"
35 InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
36 : InsetMathNest(buf, 1), key_(key)
38 // lyxerr << " creating deco " << key->name << endl;
42 Inset * InsetMathDecoration::clone() const
44 return new InsetMathDecoration(*this);
48 bool InsetMathDecoration::upper() const
50 return key_->name.substr(0, 5) != "under" && key_->name != "utilde";
54 bool InsetMathDecoration::isScriptable() const
57 key_->name == "overbrace" ||
58 key_->name == "underbrace" ||
59 key_->name == "overleftarrow" ||
60 key_->name == "overrightarrow" ||
61 key_->name == "overleftrightarrow" ||
62 key_->name == "underleftarrow" ||
63 key_->name == "underrightarrow" ||
64 key_->name == "underleftrightarrow";
68 bool InsetMathDecoration::protect() const
71 key_->name == "overbrace" ||
72 key_->name == "underbrace" ||
73 key_->name == "overleftarrow" ||
74 key_->name == "overrightarrow" ||
75 key_->name == "overleftrightarrow" ||
76 key_->name == "underleftarrow" ||
77 key_->name == "underrightarrow" ||
78 key_->name == "underleftrightarrow";
82 bool InsetMathDecoration::wide() const
85 key_->name == "overline" ||
86 key_->name == "underline" ||
87 key_->name == "overbrace" ||
88 key_->name == "underbrace" ||
89 key_->name == "overleftarrow" ||
90 key_->name == "overrightarrow" ||
91 key_->name == "overleftrightarrow" ||
92 key_->name == "widehat" ||
93 key_->name == "widetilde" ||
94 key_->name == "underleftarrow" ||
95 key_->name == "underrightarrow" ||
96 key_->name == "underleftrightarrow" ||
97 key_->name == "utilde";
101 InsetMath::mode_type InsetMathDecoration::currentMode() const
103 return key_->name == "underbar" ? TEXT_MODE : MATH_MODE;
107 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
109 bool really_change_font = currentMode() == TEXT_MODE
110 && isMathFont(from_ascii(mi.base.fontname));
111 FontSetChanger dummy(mi.base, "textnormal", really_change_font);
113 cell(0).metrics(mi, dim);
115 dh_ = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
116 dw_ = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
119 dy_ = -dim.asc - dh_;
130 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
132 bool really_change_font = currentMode() == TEXT_MODE
133 && isMathFont(from_ascii(pi.base.fontname));
134 FontSetChanger dummy(pi.base, "textnormal", really_change_font);
136 cell(0).draw(pi, x + 1, 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);
143 drawMarkers(pi, x, y);
144 setPosCache(pi, x, y);
148 void InsetMathDecoration::write(WriteStream & os) const
150 MathEnsurer ensurer(os);
151 if (os.fragile() && protect())
153 os << '\\' << key_->name << '{';
154 ModeSpecifier specifier(os, currentMode());
155 os << cell(0) << '}';
159 void InsetMathDecoration::normalize(NormalStream & os) const
161 os << "[deco " << key_->name << ' ' << cell(0) << ']';
165 void InsetMathDecoration::infoize(odocstream & os) const
167 os << "Deco: " << key_->name;
174 Attributes(bool o, string t)
180 typedef map<string, Attributes> TranslationMap;
182 void buildTranslationMap(TranslationMap & t) {
183 // the decorations we need to support are listed in lib/symbols
184 t["acute"] = Attributes(true, "´");
185 t["bar"] = Attributes(true, "‾");
186 t["breve"] = Attributes(true, "˘");
187 t["check"] = Attributes(true, "ˇ");
188 t["ddddot"] = Attributes(true, "⃜");
189 t["dddot"] = Attributes(true, "⃛");
190 t["ddot"] = Attributes(true, "¨");
191 t["dot"] = Attributes(true, "˙");
192 t["grave"] = Attributes(true, "`");
193 t["hat"] = Attributes(true, "ˆ");
194 t["mathring"] = Attributes(true, "˚");
195 t["overbrace"] = Attributes(true, "⏞");
196 t["overleftarrow"] = Attributes(true, "⟵");
197 t["overleftrightarrow"] = Attributes(true, "⟷");
198 t["overline"] = Attributes(true, "¯");
199 t["overrightarrow"] = Attributes(true, "⟶");
200 t["tilde"] = Attributes(true, "˜");
201 t["underbar"] = Attributes(false, "_");
202 t["underbrace"] = Attributes(false, "⏟");
203 t["underleftarrow"] = Attributes(false, "⟵");
204 t["underleftrightarrow"] = Attributes(false, "⟷");
205 // this is the macron, again, but it works
206 t["underline"] = Attributes(false, "¯");
207 t["underrightarrow"] = 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 & os) 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";
229 << MTag("mrow") << cell(0) << ETag("mrow")
230 << from_ascii("<mo stretchy=\"true\">" + cur->second.tag + "</mo>")
235 void InsetMathDecoration::htmlize(HtmlStream & os) const
237 string const name = to_utf8(key_->name);
239 os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
243 if (name == "underbar" || name == "underline") {
244 os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
248 TranslationMap const & t = translationMap();
249 TranslationMap::const_iterator cur = t.find(name);
250 LASSERT(cur != t.end(), return);
252 bool symontop = cur->second.over;
253 string const symclass = symontop ? "symontop" : "symonbot";
254 os << MTag("span", "class='symbolpair " + symclass + "'")
258 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
260 os << MTag("span", "class='base'") << cell(0);
261 os << ETag("span") << '\n';
263 os << MTag("span", "class='base'") << cell(0);
265 os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
266 os << ETag("span") << '\n' << ETag("span") << '\n';
270 // ideas borrowed from the eLyXer code
271 void InsetMathDecoration::validate(LaTeXFeatures & features) const
273 if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
274 string const name = to_utf8(key_->name);
276 features.addPreambleSnippet("<style type=\"text/css\">\n"
277 "span.overbar{border-top: thin black solid;}\n"
279 } else if (name == "underbar" || name == "underline") {
280 features.addPreambleSnippet("<style type=\"text/css\">\n"
281 "span.underbar{border-bottom: thin black solid;}\n"
284 features.addPreambleSnippet("<style type=\"text/css\">\n"
285 "span.symbolpair{display: inline-block; text-align:center;}\n"
286 "span.symontop{vertical-align: top;}\n"
287 "span.symonbot{vertical-align: bottom;}\n"
288 "span.symbolpair span{display: block;}\n"
289 "span.symbol{height: 0.5ex;}\n"
293 if (!key_->requires.empty())
294 features.require(to_utf8(key_->requires));
296 InsetMathNest::validate(features);