1 /**
2  * \file InsetMathDecoration.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11 
12 #include <config.h>
13 
14 #include "InsetMathDecoration.h"
15 
16 #include "MathData.h"
17 #include "MathParser.h"
18 #include "MathSupport.h"
19 #include "MathStream.h"
20 #include "MetricsInfo.h"
21 
22 #include "LaTeXFeatures.h"
23 
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"
29 
30 #include <ostream>
31 
32 using namespace lyx::support;
33 
34 using namespace std;
35 
36 namespace lyx {
37 
38 
InsetMathDecoration(Buffer * buf,latexkeys const * key)39 InsetMathDecoration::InsetMathDecoration(Buffer * buf, latexkeys const * key)
40 	: InsetMathNest(buf, 1), key_(key), dh_(0), dy_(0), dw_(0)
41 {
42 //	lyxerr << " creating deco " << key->name << endl;
43 }
44 
45 
clone() const46 Inset * InsetMathDecoration::clone() const
47 {
48 	return new InsetMathDecoration(*this);
49 }
50 
51 
upper() const52 bool InsetMathDecoration::upper() const
53 {
54 	return key_->name.substr(0, 5) != "under" && key_->name != "utilde";
55 }
56 
57 
mathClass() const58 MathClass InsetMathDecoration::mathClass() const
59 {
60 	if (key_->name == "overbrace" || key_->name == "underbrace")
61 		return MC_OP;
62 	return MC_ORD;
63 }
64 
65 
isScriptable() const66 bool InsetMathDecoration::isScriptable() const
67 {
68 	return mathClass() == MC_OP;
69 }
70 
71 
protect() const72 bool InsetMathDecoration::protect() const
73 {
74 	return
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";
83 }
84 
85 
wide() const86 bool InsetMathDecoration::wide() const
87 {
88 	return
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";
103 }
104 
105 
currentMode() const106 InsetMath::mode_type InsetMathDecoration::currentMode() const
107 {
108 	return key_->name == "underbar" ? TEXT_MODE : MATH_MODE;
109 }
110 
111 
metrics(MetricsInfo & mi,Dimension & dim) const112 void InsetMathDecoration::metrics(MetricsInfo & mi, Dimension & dim) const
113 {
114 	Changer dummy = mi.base.changeEnsureMath(currentMode());
115 
116 	cell(0).metrics(mi, dim);
117 
118 	dh_  = 6; //mathed_char_height(LM_TC_VAR, mi, 'I', ascent_, descent_);
119 	dw_  = 6; //mathed_char_width(LM_TC_VAR, mi, 'x');
120 
121 	if (upper()) {
122 		dy_ = -dim.asc - dh_;
123 		dim.asc += dh_ + 1;
124 	} else {
125 		dy_ = dim.des + 1;
126 		dim.des += dh_ + 2;
127 	}
128 }
129 
130 
draw(PainterInfo & pi,int x,int y) const131 void InsetMathDecoration::draw(PainterInfo & pi, int x, int y) const
132 {
133 	Changer dummy = pi.base.changeEnsureMath(currentMode());
134 
135 	cell(0).draw(pi, x, y);
136 	Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
137 	if (wide())
138 		mathed_draw_deco(pi, x + 1, y + dy_, dim0.wid, dh_, key_->name);
139 	else
140 		mathed_draw_deco(pi, x + 1 + (dim0.wid - dw_) / 2,
141 			y + dy_, dw_, dh_, key_->name);
142 }
143 
144 
write(WriteStream & os) const145 void InsetMathDecoration::write(WriteStream & os) const
146 {
147 	MathEnsurer ensurer(os);
148 	if (os.fragile() && protect())
149 		os << "\\protect";
150 	os << '\\' << key_->name << '{';
151 	ModeSpecifier specifier(os, currentMode());
152 	os << cell(0) << '}';
153 }
154 
155 
normalize(NormalStream & os) const156 void InsetMathDecoration::normalize(NormalStream & os) const
157 {
158 	os << "[deco " << key_->name << ' ' <<  cell(0) << ']';
159 }
160 
161 
infoize(odocstream & os) const162 void InsetMathDecoration::infoize(odocstream & os) const
163 {
164 	os << bformat(_("Decoration: %1$s"), key_->name);
165 }
166 
167 
168 namespace {
169 	struct Attributes {
Attributeslyx::__anon1ea5db7f0111::Attributes170 		Attributes() : over(false) {}
Attributeslyx::__anon1ea5db7f0111::Attributes171 		Attributes(bool o, string t)
172 			: over(o), tag(t) {}
173 		bool over;
174 		string tag;
175 	};
176 
177 	typedef map<string, Attributes> TranslationMap;
178 
buildTranslationMap(TranslationMap & t)179 	void buildTranslationMap(TranslationMap & t) {
180 		// the decorations we need to support are listed in lib/symbols
181 		t["acute"] = Attributes(true, "&acute;");
182 		t["bar"]   = Attributes(true, "&OverBar;");
183 		t["breve"] = Attributes(true, "&breve;");
184 		t["check"] = Attributes(true, "&caron;");
185 		t["ddddot"] = Attributes(true, "&DotDot;");
186 		t["dddot"] = Attributes(true, "&TripleDot;");
187 		t["ddot"] = Attributes(true, "&Dot;");
188 		t["dot"] = Attributes(true, "&dot;");
189 		t["grave"] = Attributes(true, "&grave;");
190 		t["hat"] = Attributes(true, "&circ;");
191 		t["mathring"] = Attributes(true, "&ring;");
192 		t["overbrace"] = Attributes(true, "&OverBrace;");
193 		t["overleftarrow"] = Attributes(true, "&xlarr;");
194 		t["overleftrightarrow"] = Attributes(true, "&xharr;");
195 		t["overline"] = Attributes(true, "&macr;");
196 		t["overrightarrow"] = Attributes(true, "&xrarr;");
197 		t["tilde"] = Attributes(true, "&tilde;");
198 		t["underbar"] = Attributes(false, "&UnderBar;");
199 		t["underbrace"] = Attributes(false, "&UnderBrace;");
200 		t["underleftarrow"] = Attributes(false, "&xlarr;");
201 		t["underleftrightarrow"] = Attributes(false, "&xharr;");
202 		// this is the macron, again, but it works
203 		t["underline"] = Attributes(false, "&macr;");
204 		t["underrightarrow"] = Attributes(false, "&xrarr;");
205 		t["undertilde"] = Attributes(false, "&Tilde;");
206 		t["utilde"] = Attributes(false, "&Tilde;");
207 		t["vec"] = Attributes(true, "&rarr;");
208 		t["widehat"] = Attributes(true, "&Hat;");
209 		t["widetilde"] = Attributes(true, "&Tilde;");
210 	}
211 
translationMap()212 	TranslationMap const & translationMap() {
213 		static TranslationMap t;
214 		if (t.empty())
215 			buildTranslationMap(t);
216 		return t;
217 	}
218 } // namespace
219 
mathmlize(MathStream & os) const220 void InsetMathDecoration::mathmlize(MathStream & os) const
221 {
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";
226 	os << MTag(outag)
227 		 << MTag("mrow") << cell(0) << ETag("mrow")
228 		 << from_ascii("<mo stretchy=\"true\">" + cur->second.tag + "</mo>")
229 		 << ETag(outag);
230 }
231 
232 
htmlize(HtmlStream & os) const233 void InsetMathDecoration::htmlize(HtmlStream & os) const
234 {
235 	string const name = to_utf8(key_->name);
236 	if (name == "bar") {
237 		os << MTag("span", "class='overbar'") << cell(0) << ETag("span");
238 		return;
239 	}
240 
241 	if (name == "underbar" || name == "underline") {
242 		os << MTag("span", "class='underbar'") << cell(0) << ETag("span");
243 		return;
244 	}
245 
246 	TranslationMap const & t = translationMap();
247 	TranslationMap::const_iterator cur = t.find(name);
248 	LASSERT(cur != t.end(), return);
249 
250 	bool symontop = cur->second.over;
251 	string const symclass = symontop ? "symontop" : "symonbot";
252 	os << MTag("span", "class='symbolpair " + symclass + "'")
253 	   << '\n';
254 
255 	if (symontop)
256 		os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
257 	else
258 		os << MTag("span", "class='base'") << cell(0);
259 	os << ETag("span") << '\n';
260 	if (symontop)
261 		os << MTag("span", "class='base'") << cell(0);
262 	else
263 		os << MTag("span", "class='symbol'") << from_ascii(cur->second.tag);
264 	os << ETag("span") << '\n' << ETag("span") << '\n';
265 }
266 
267 
268 // ideas borrowed from the eLyXer code
validate(LaTeXFeatures & features) const269 void InsetMathDecoration::validate(LaTeXFeatures & features) const
270 {
271 	if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
272 		string const name = to_utf8(key_->name);
273 		if (name == "bar") {
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;}");
277 		} else {
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;}");
284 		}
285 	} else {
286 		if (!key_->requires.empty())
287 			features.require(key_->requires);
288 	}
289 	InsetMathNest::validate(features);
290 }
291 
292 } // namespace lyx
293