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
58 // Need to do something special for tags here.
59 // Probably will have to involve deferring them, which
60 // means returning something from this routine.
61 SetMode textmode(ms, true);
62 ms << MTag("mstyle", "class='mathbox'")
68 void InsetMathBox::htmlize(HtmlStream & ms) const
70 SetHTMLMode textmode(ms, true);
71 ms << MTag("span", "class='mathbox'")
77 void InsetMathBox::metrics(MetricsInfo & mi, Dimension & dim) const
79 FontSetChanger dummy(mi.base, "textnormal");
80 cell(0).metrics(mi, dim);
85 void InsetMathBox::draw(PainterInfo & pi, int x, int y) const
87 FontSetChanger dummy(pi.base, "textnormal");
88 cell(0).draw(pi, x, y);
89 drawMarkers(pi, x, y);
93 void InsetMathBox::infoize(odocstream & os) const
95 os << "Box: " << name_;
99 void InsetMathBox::validate(LaTeXFeatures & features) const
102 // It'd be better to be able to get this from an InsetLayout, but at present
103 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
104 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
105 features.addCSSSnippet("mstyle.mathbox { font-style: normal; }");
106 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
107 features.addCSSSnippet("span.mathbox { font-style: normal; }");
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.addCSSSnippet(
192 "mstyle.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }");
193 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
194 features.addCSSSnippet(
195 "span.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }");
197 cell(0).validate(features);
198 InsetMathNest::validate(features);
203 /////////////////////////////////////////////////////////////////////
207 /////////////////////////////////////////////////////////////////////
210 InsetMathMakebox::InsetMathMakebox(Buffer * buf, bool framebox)
211 : InsetMathNest(buf, 3), framebox_(framebox)
215 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
217 FontSetChanger dummy(mi.base, "textnormal");
220 static docstring bracket = from_ascii("[");
221 mathed_string_dim(mi.base.font, bracket, wdim);
227 cell(0).metrics(mi, dim0);
228 cell(1).metrics(mi, dim1);
229 cell(2).metrics(mi, dim2);
231 dim.wid = w + dim0.wid + w + w + dim1.wid + w + 2 + dim2.wid;
232 dim.asc = std::max(std::max(wdim.asc, dim0.asc), std::max(dim1.asc, dim2.asc));
233 dim.des = std::max(std::max(wdim.des, dim0.des), std::max(dim1.des, dim2.des));
248 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
250 drawMarkers(pi, x, y);
252 FontSetChanger dummy(pi.base, "textnormal");
253 BufferView const & bv = *pi.base.bv;
254 int w = mathed_char_width(pi.base.font, '[');
257 Dimension const dim = dimension(*pi.base.bv);
258 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
259 dim.width() - 2, dim.height() - 2, Color_foreground);
263 drawStrBlack(pi, x, y, from_ascii("["));
265 cell(0).draw(pi, x, y);
266 x += cell(0).dimension(bv).wid;
267 drawStrBlack(pi, x, y, from_ascii("]"));
270 drawStrBlack(pi, x, y, from_ascii("["));
272 cell(1).draw(pi, x, y);
273 x += cell(1).dimension(bv).wid;
274 drawStrBlack(pi, x, y, from_ascii("]"));
277 cell(2).draw(pi, x, y);
281 void InsetMathMakebox::write(WriteStream & os) const
283 ModeSpecifier specifier(os, TEXT_MODE);
284 os << (framebox_ ? "\\framebox" : "\\makebox");
285 if (cell(0).size() || !os.latex()) {
286 os << '[' << cell(0) << ']';
287 if (cell(1).size() || !os.latex())
288 os << '[' << cell(1) << ']';
290 os << '{' << cell(2) << '}';
294 void InsetMathMakebox::normalize(NormalStream & os) const
296 os << (framebox_ ? "[framebox " : "[makebox ")
297 << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
301 void InsetMathMakebox::infoize(odocstream & os) const
303 os << (framebox_ ? "Framebox" : "Makebox")
304 << " (width: " << cell(0)
305 << " pos: " << cell(1) << ")";
309 void InsetMathMakebox::mathmlize(MathStream & ms) const
311 // FIXME We could do something with the other arguments.
312 std::string const cssclass = framebox_ ? "framebox" : "makebox";
313 SetMode textmode(ms, true);
314 ms << MTag("mstyle", "class='" + cssclass + "'")
320 void InsetMathMakebox::htmlize(HtmlStream & ms) const
322 // FIXME We could do something with the other arguments.
323 SetHTMLMode textmode(ms, true);
324 std::string const cssclass = framebox_ ? "framebox" : "makebox";
325 ms << MTag("span", "class='" + cssclass + "'")
331 void InsetMathMakebox::validate(LaTeXFeatures & features) const
334 // It'd be better to be able to get this from an InsetLayout, but at present
335 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
336 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
337 features.addCSSSnippet("mstyle.framebox { border: 1px solid black; }");
338 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
339 features.addCSSSnippet("span.framebox { border: 1px solid black; }");
340 InsetMathNest::validate(features);
344 /////////////////////////////////////////////////////////////////////
348 /////////////////////////////////////////////////////////////////////
350 InsetMathBoxed::InsetMathBoxed(Buffer * buf)
351 : InsetMathNest(buf, 1)
355 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
357 cell(0).metrics(mi, dim);
358 metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
362 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
364 Dimension const dim = dimension(*pi.base.bv);
365 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
366 dim.width() - 2, dim.height() - 2, Color_foreground);
367 cell(0).draw(pi, x + 3, y);
368 setPosCache(pi, x, y);
372 void InsetMathBoxed::write(WriteStream & os) const
374 ModeSpecifier specifier(os, MATH_MODE);
375 os << "\\boxed{" << cell(0) << '}';
379 void InsetMathBoxed::normalize(NormalStream & os) const
381 os << "[boxed " << cell(0) << ']';
385 void InsetMathBoxed::infoize(odocstream & os) const
391 void InsetMathBoxed::mathmlize(MathStream & ms) const
393 ms << MTag("mstyle", "class='boxed'")
399 void InsetMathBoxed::htmlize(HtmlStream & ms) const
401 ms << MTag("span", "class='boxed'")
407 void InsetMathBoxed::validate(LaTeXFeatures & features) const
409 features.require("amsmath");
412 // It'd be better to be able to get this from an InsetLayout, but at present
413 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
414 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
415 features.addCSSSnippet("mstyle.boxed { border: 1px solid black; }");
416 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
417 features.addCSSSnippet("span.boxed { border: 1px solid black; }");
419 InsetMathNest::validate(features);