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 "support/gettext.h"
23 #include "support/lstrings.h"
25 #include "frontends/Painter.h"
30 using namespace lyx::support;
34 /////////////////////////////////////////////////////////////////////
38 /////////////////////////////////////////////////////////////////////
40 InsetMathBox::InsetMathBox(Buffer * buf, docstring const & name)
41 : InsetMathNest(buf, 1), name_(name)
45 void InsetMathBox::write(TeXMathStream & os) const
47 ModeSpecifier specifier(os, TEXT_MODE);
48 os << '\\' << name_ << '{' << cell(0) << '}';
52 void InsetMathBox::normalize(NormalStream & os) const
54 os << '[' << name_ << ' ';
55 //text_->write(buffer(), os);
60 void InsetMathBox::mathmlize(MathMLStream & ms) const
63 // Need to do something special for tags here.
64 // Probably will have to involve deferring them, which
65 // means returning something from this routine.
66 SetMode textmode(ms, true);
67 ms << MTag("mstyle", "class='mathbox'")
73 void InsetMathBox::htmlize(HtmlStream & ms) const
75 SetHTMLMode textmode(ms, true);
76 ms << MTag("span", "class='mathbox'")
82 void InsetMathBox::metrics(MetricsInfo & mi, Dimension & dim) const
84 Changer dummy = mi.base.changeFontSet("textnormal");
85 cell(0).metrics(mi, dim);
89 void InsetMathBox::draw(PainterInfo & pi, int x, int y) const
91 Changer dummy = pi.base.changeFontSet("textnormal");
92 cell(0).draw(pi, x, y);
96 void InsetMathBox::infoize(odocstream & os) const
98 os << bformat(_("Box: %1$s"), name_);
102 void InsetMathBox::validate(LaTeXFeatures & features) const
105 // It'd be better to be able to get this from an InsetLayout, but at present
106 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
107 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
108 features.addCSSSnippet("mstyle.mathbox { font-style: normal; }");
109 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
110 features.addCSSSnippet("span.mathbox { font-style: normal; }");
112 if (name_ == "tag" || name_ == "tag*")
113 features.require("amsmath");
115 InsetMathNest::validate(features);
120 /////////////////////////////////////////////////////////////////////
124 /////////////////////////////////////////////////////////////////////
127 InsetMathFBox::InsetMathFBox(Buffer * buf)
128 : InsetMathNest(buf, 1)
132 void InsetMathFBox::metrics(MetricsInfo & mi, Dimension & dim) const
134 Changer dummy = mi.base.changeFontSet("textnormal");
135 cell(0).metrics(mi, dim);
136 // 1 pixel space, 1 frame, 1 space
143 void InsetMathFBox::draw(PainterInfo & pi, int x, int y) const
145 Dimension const dim = dimension(*pi.base.bv);
146 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
147 dim.width() - 2, dim.height() - 2, Color_foreground);
148 Changer dummy = pi.base.changeFontSet("textnormal");
149 cell(0).draw(pi, x + 3, y);
153 void InsetMathFBox::write(TeXMathStream & os) const
155 ModeSpecifier specifier(os, TEXT_MODE);
156 os << "\\fbox{" << cell(0) << '}';
160 void InsetMathFBox::normalize(NormalStream & os) const
162 os << "[fbox " << cell(0) << ']';
166 void InsetMathFBox::mathmlize(MathMLStream & ms) const
168 SetMode textmode(ms, true);
169 ms << MTag("mstyle", "class='fbox'")
175 void InsetMathFBox::htmlize(HtmlStream & ms) const
177 SetHTMLMode textmode(ms, true);
178 ms << MTag("span", "class='fbox'")
184 void InsetMathFBox::infoize(odocstream & os) const
190 void InsetMathFBox::validate(LaTeXFeatures & features) const
193 // It'd be better to be able to get this from an InsetLayout, but at present
194 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
195 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
196 features.addCSSSnippet(
197 "mstyle.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }");
198 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
199 features.addCSSSnippet(
200 "span.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }");
202 cell(0).validate(features);
203 InsetMathNest::validate(features);
208 /////////////////////////////////////////////////////////////////////
212 /////////////////////////////////////////////////////////////////////
215 InsetMathMakebox::InsetMathMakebox(Buffer * buf, bool framebox)
216 : InsetMathNest(buf, 3), framebox_(framebox)
220 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
222 Changer dummy = mi.base.changeFontSet("textnormal");
225 static docstring bracket = from_ascii("[");
226 metricsStrRedBlack(mi, wdim, bracket);
232 cell(0).metrics(mi, dim0);
233 cell(1).metrics(mi, dim1);
234 cell(2).metrics(mi, dim2);
236 dim.wid = w + dim0.wid + w + w + dim1.wid + w + 2 + dim2.wid;
237 dim.asc = std::max(std::max(wdim.asc, dim0.asc), std::max(dim1.asc, dim2.asc));
238 dim.des = std::max(std::max(wdim.des, dim0.des), std::max(dim1.des, dim2.des));
251 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
253 Changer dummy = pi.base.changeFontSet("textnormal");
254 BufferView const & bv = *pi.base.bv;
255 int w = mathed_char_width(pi.base.font, '[');
258 Dimension const dim = dimension(*pi.base.bv);
259 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
260 dim.width() - 2, dim.height() - 2, Color_foreground);
264 drawStrBlack(pi, x, y, from_ascii("["));
266 cell(0).draw(pi, x, y);
267 x += cell(0).dimension(bv).wid;
268 drawStrBlack(pi, x, y, from_ascii("]"));
271 drawStrBlack(pi, x, y, from_ascii("["));
273 cell(1).draw(pi, x, y);
274 x += cell(1).dimension(bv).wid;
275 drawStrBlack(pi, x, y, from_ascii("]"));
278 cell(2).draw(pi, x, y);
282 void InsetMathMakebox::write(TeXMathStream & os) const
284 ModeSpecifier specifier(os, TEXT_MODE);
285 os << (framebox_ ? "\\framebox" : "\\makebox");
286 if (!cell(0).empty() || !os.latex()) {
287 os << '[' << cell(0) << ']';
288 if (!cell(1).empty() || !os.latex())
289 os << '[' << cell(1) << ']';
291 os << '{' << cell(2) << '}';
295 void InsetMathMakebox::normalize(NormalStream & os) const
297 os << (framebox_ ? "[framebox " : "[makebox ")
298 << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
302 void InsetMathMakebox::infoize(odocstream & os) const
304 os << (framebox_ ? "Framebox" : "Makebox")
305 << " (width: " << cell(0)
306 << " pos: " << cell(1) << ")";
310 void InsetMathMakebox::mathmlize(MathMLStream & ms) const
312 // FIXME We could do something with the other arguments.
313 std::string const cssclass = framebox_ ? "framebox" : "makebox";
314 SetMode textmode(ms, true);
315 ms << MTag("mstyle", "class='" + cssclass + "'")
321 void InsetMathMakebox::htmlize(HtmlStream & ms) const
323 // FIXME We could do something with the other arguments.
324 SetHTMLMode textmode(ms, true);
325 std::string const cssclass = framebox_ ? "framebox" : "makebox";
326 ms << MTag("span", "class='" + cssclass + "'")
332 void InsetMathMakebox::validate(LaTeXFeatures & features) const
335 // It'd be better to be able to get this from an InsetLayout, but at present
336 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
337 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
338 features.addCSSSnippet("mstyle.framebox { border: 1px solid black; }");
339 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
340 features.addCSSSnippet("span.framebox { border: 1px solid black; }");
341 InsetMathNest::validate(features);
345 /////////////////////////////////////////////////////////////////////
349 /////////////////////////////////////////////////////////////////////
351 InsetMathBoxed::InsetMathBoxed(Buffer * buf)
352 : InsetMathNest(buf, 1)
356 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
358 cell(0).metrics(mi, dim);
359 // 1 pixel space, 1 frame, 1 space
366 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
368 Dimension const dim = dimension(*pi.base.bv);
369 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
370 dim.width() - 2, dim.height() - 2, Color_foreground);
371 cell(0).draw(pi, x + 3, y);
375 void InsetMathBoxed::write(TeXMathStream & os) const
377 ModeSpecifier specifier(os, MATH_MODE);
378 os << "\\boxed{" << cell(0) << '}';
382 void InsetMathBoxed::normalize(NormalStream & os) const
384 os << "[boxed " << cell(0) << ']';
388 void InsetMathBoxed::infoize(odocstream & os) const
394 void InsetMathBoxed::mathmlize(MathMLStream & ms) const
396 ms << MTag("mstyle", "class='boxed'")
402 void InsetMathBoxed::htmlize(HtmlStream & ms) const
404 ms << MTag("span", "class='boxed'")
410 void InsetMathBoxed::validate(LaTeXFeatures & features) const
412 features.require("amsmath");
415 // It'd be better to be able to get this from an InsetLayout, but at present
416 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
417 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
418 features.addCSSSnippet("mstyle.boxed { border: 1px solid black; }");
419 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
420 features.addCSSSnippet("span.boxed { border: 1px solid black; }");
422 InsetMathNest::validate(features);