2 * \file InsetMathBox.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Ling Li (InsetMathMakebox)
9 * Full author contact details are available in file CREDITS.
14 #include "InsetMathBox.h"
16 #include "LaTeXFeatures.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "MetricsInfo.h"
22 #include "frontends/Painter.h"
29 /////////////////////////////////////////////////////////////////////
33 /////////////////////////////////////////////////////////////////////
35 InsetMathBox::InsetMathBox(Buffer * buf, docstring const & name)
36 : InsetMathNest(buf, 1), name_(name)
40 void InsetMathBox::write(WriteStream & os) const
42 ModeSpecifier specifier(os, TEXT_MODE);
43 os << '\\' << name_ << '{' << cell(0) << '}';
47 void InsetMathBox::normalize(NormalStream & os) const
49 os << '[' << name_ << ' ';
50 //text_->write(buffer(), os);
55 void InsetMathBox::mathmlize(MathStream & ms) const
57 SetMode textmode(ms, true);
58 ms << MTag("mstyle", "class='mathbox'")
64 void InsetMathBox::htmlize(HtmlStream & ms) const
66 SetHTMLMode textmode(ms, true);
67 ms << MTag("span", "class='mathbox'")
73 void InsetMathBox::metrics(MetricsInfo & mi, Dimension & dim) const
75 FontSetChanger dummy(mi.base, "textnormal");
76 cell(0).metrics(mi, dim);
81 void InsetMathBox::draw(PainterInfo & pi, int x, int y) const
83 FontSetChanger dummy(pi.base, "textnormal");
84 cell(0).draw(pi, x, y);
85 drawMarkers(pi, x, y);
89 void InsetMathBox::infoize(odocstream & os) const
91 os << "Box: " << name_;
95 void InsetMathBox::validate(LaTeXFeatures & features) const
98 // It'd be better to be able to get this from an InsetLayout, but at present
99 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
100 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
101 features.addPreambleSnippet("<style type=\"text/css\">\n"
102 "mstyle.mathbox { font-style: normal; }\n"
104 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
105 features.addPreambleSnippet("<style type=\"text/css\">\n"
106 "span.mathbox { font-style: normal; }\n"
109 if (name_ == "tag" || name_ == "tag*")
110 features.require("amsmath");
112 InsetMathNest::validate(features);
117 /////////////////////////////////////////////////////////////////////
121 /////////////////////////////////////////////////////////////////////
124 InsetMathFBox::InsetMathFBox(Buffer * buf)
125 : InsetMathNest(buf, 1)
129 void InsetMathFBox::metrics(MetricsInfo & mi, Dimension & dim) const
131 FontSetChanger dummy(mi.base, "textnormal");
132 cell(0).metrics(mi, dim);
133 metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
137 void InsetMathFBox::draw(PainterInfo & pi, int x, int y) const
139 Dimension const dim = dimension(*pi.base.bv);
140 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
141 dim.width() - 2, dim.height() - 2, Color_foreground);
142 FontSetChanger dummy(pi.base, "textnormal");
143 cell(0).draw(pi, x + 3, y);
144 setPosCache(pi, x, y);
148 void InsetMathFBox::write(WriteStream & os) const
150 ModeSpecifier specifier(os, TEXT_MODE);
151 os << "\\fbox{" << cell(0) << '}';
155 void InsetMathFBox::normalize(NormalStream & os) const
157 os << "[fbox " << cell(0) << ']';
161 void InsetMathFBox::mathmlize(MathStream & ms) const
163 SetMode textmode(ms, true);
164 ms << MTag("mstyle", "class='fbox'")
170 void InsetMathFBox::htmlize(HtmlStream & ms) const
172 SetHTMLMode textmode(ms, true);
173 ms << MTag("span", "class='fbox'")
179 void InsetMathFBox::infoize(odocstream & os) const
185 void InsetMathFBox::validate(LaTeXFeatures & features) const
188 // It'd be better to be able to get this from an InsetLayout, but at present
189 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
190 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
191 features.addPreambleSnippet("<style type=\"text/css\">\n"
192 "mstyle.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }\n"
194 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
195 features.addPreambleSnippet("<style type=\"text/css\">\n"
196 "span.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }\n"
199 cell(0).validate(features);
200 InsetMathNest::validate(features);
205 /////////////////////////////////////////////////////////////////////
209 /////////////////////////////////////////////////////////////////////
212 InsetMathMakebox::InsetMathMakebox(Buffer * buf, bool framebox)
213 : InsetMathNest(buf, 3), framebox_(framebox)
217 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
219 FontSetChanger dummy(mi.base, "textnormal");
222 static docstring bracket = from_ascii("[");
223 mathed_string_dim(mi.base.font, bracket, wdim);
229 cell(0).metrics(mi, dim0);
230 cell(1).metrics(mi, dim1);
231 cell(2).metrics(mi, dim2);
233 dim.wid = w + dim0.wid + w + w + dim1.wid + w + 2 + dim2.wid;
234 dim.asc = std::max(std::max(wdim.asc, dim0.asc), std::max(dim1.asc, dim2.asc));
235 dim.des = std::max(std::max(wdim.des, dim0.des), std::max(dim1.des, dim2.des));
250 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
252 drawMarkers(pi, x, y);
254 FontSetChanger dummy(pi.base, "textnormal");
255 BufferView const & bv = *pi.base.bv;
256 int w = mathed_char_width(pi.base.font, '[');
259 Dimension const dim = dimension(*pi.base.bv);
260 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
261 dim.width() - 2, dim.height() - 2, Color_foreground);
265 drawStrBlack(pi, x, y, from_ascii("["));
267 cell(0).draw(pi, x, y);
268 x += cell(0).dimension(bv).wid;
269 drawStrBlack(pi, x, y, from_ascii("]"));
272 drawStrBlack(pi, x, y, from_ascii("["));
274 cell(1).draw(pi, x, y);
275 x += cell(1).dimension(bv).wid;
276 drawStrBlack(pi, x, y, from_ascii("]"));
279 cell(2).draw(pi, x, y);
283 void InsetMathMakebox::write(WriteStream & os) const
285 ModeSpecifier specifier(os, TEXT_MODE);
286 os << (framebox_ ? "\\framebox" : "\\makebox");
287 if (cell(0).size() || !os.latex()) {
288 os << '[' << cell(0) << ']';
289 if (cell(1).size() || !os.latex())
290 os << '[' << cell(1) << ']';
292 os << '{' << cell(2) << '}';
296 void InsetMathMakebox::normalize(NormalStream & os) const
298 os << (framebox_ ? "[framebox " : "[makebox ")
299 << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
303 void InsetMathMakebox::infoize(odocstream & os) const
305 os << (framebox_ ? "Framebox" : "Makebox")
306 << " (width: " << cell(0)
307 << " pos: " << cell(1) << ")";
311 void InsetMathMakebox::mathmlize(MathStream & ms) const
313 // FIXME We could do something with the other arguments.
314 std::string const cssclass = framebox_ ? "framebox" : "makebox";
315 SetMode textmode(ms, true);
316 ms << MTag("mstyle", "class='" + cssclass + "'")
322 void InsetMathMakebox::htmlize(HtmlStream & ms) const
324 // FIXME We could do something with the other arguments.
325 SetHTMLMode textmode(ms, true);
326 std::string const cssclass = framebox_ ? "framebox" : "makebox";
327 ms << MTag("span", "class='" + cssclass + "'")
333 void InsetMathMakebox::validate(LaTeXFeatures & features) const
336 // It'd be better to be able to get this from an InsetLayout, but at present
337 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
338 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
339 features.addPreambleSnippet("<style type=\"text/css\">\n"
340 "mstyle.framebox { border: 1px solid black; }\n"
342 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
343 features.addPreambleSnippet("<style type=\"text/css\">\n"
344 "span.framebox { border: 1px solid black; }\n"
346 InsetMathNest::validate(features);
350 /////////////////////////////////////////////////////////////////////
354 /////////////////////////////////////////////////////////////////////
356 InsetMathBoxed::InsetMathBoxed(Buffer * buf)
357 : InsetMathNest(buf, 1)
361 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
363 cell(0).metrics(mi, dim);
364 metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
368 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
370 Dimension const dim = dimension(*pi.base.bv);
371 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
372 dim.width() - 2, dim.height() - 2, Color_foreground);
373 cell(0).draw(pi, x + 3, y);
374 setPosCache(pi, x, y);
378 void InsetMathBoxed::write(WriteStream & os) const
380 ModeSpecifier specifier(os, MATH_MODE);
381 os << "\\boxed{" << cell(0) << '}';
385 void InsetMathBoxed::normalize(NormalStream & os) const
387 os << "[boxed " << cell(0) << ']';
391 void InsetMathBoxed::infoize(odocstream & os) const
397 void InsetMathBoxed::mathmlize(MathStream & ms) const
399 ms << MTag("mstyle", "class='boxed'")
405 void InsetMathBoxed::htmlize(HtmlStream & ms) const
407 ms << MTag("span", "class='boxed'")
413 void InsetMathBoxed::validate(LaTeXFeatures & features) const
415 features.require("amsmath");
418 // It'd be better to be able to get this from an InsetLayout, but at present
419 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
420 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
421 features.addPreambleSnippet("<style type=\"text/css\">\n"
422 "mstyle.boxed { border: 1px solid black; }\n"
424 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
425 features.addPreambleSnippet("<style type=\"text/css\">\n"
426 "span.boxed { border: 1px solid black; }\n"
429 InsetMathNest::validate(features);