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.addPreambleSnippet("<style type=\"text/css\">\n"
106 "mstyle.mathbox { font-style: normal; }\n"
108 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
109 features.addPreambleSnippet("<style type=\"text/css\">\n"
110 "span.mathbox { font-style: normal; }\n"
113 if (name_ == "tag" || name_ == "tag*")
114 features.require("amsmath");
116 InsetMathNest::validate(features);
121 /////////////////////////////////////////////////////////////////////
125 /////////////////////////////////////////////////////////////////////
128 InsetMathFBox::InsetMathFBox(Buffer * buf)
129 : InsetMathNest(buf, 1)
133 void InsetMathFBox::metrics(MetricsInfo & mi, Dimension & dim) const
135 FontSetChanger dummy(mi.base, "textnormal");
136 cell(0).metrics(mi, dim);
137 metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
141 void InsetMathFBox::draw(PainterInfo & pi, int x, int y) const
143 Dimension const dim = dimension(*pi.base.bv);
144 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
145 dim.width() - 2, dim.height() - 2, Color_foreground);
146 FontSetChanger dummy(pi.base, "textnormal");
147 cell(0).draw(pi, x + 3, y);
148 setPosCache(pi, x, y);
152 void InsetMathFBox::write(WriteStream & os) const
154 ModeSpecifier specifier(os, TEXT_MODE);
155 os << "\\fbox{" << cell(0) << '}';
159 void InsetMathFBox::normalize(NormalStream & os) const
161 os << "[fbox " << cell(0) << ']';
165 void InsetMathFBox::mathmlize(MathStream & ms) const
167 SetMode textmode(ms, true);
168 ms << MTag("mstyle", "class='fbox'")
174 void InsetMathFBox::htmlize(HtmlStream & ms) const
176 SetHTMLMode textmode(ms, true);
177 ms << MTag("span", "class='fbox'")
183 void InsetMathFBox::infoize(odocstream & os) const
189 void InsetMathFBox::validate(LaTeXFeatures & features) const
192 // It'd be better to be able to get this from an InsetLayout, but at present
193 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
194 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
195 features.addPreambleSnippet("<style type=\"text/css\">\n"
196 "mstyle.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }\n"
198 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
199 features.addPreambleSnippet("<style type=\"text/css\">\n"
200 "span.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }\n"
203 cell(0).validate(features);
204 InsetMathNest::validate(features);
209 /////////////////////////////////////////////////////////////////////
213 /////////////////////////////////////////////////////////////////////
216 InsetMathMakebox::InsetMathMakebox(Buffer * buf, bool framebox)
217 : InsetMathNest(buf, 3), framebox_(framebox)
221 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
223 FontSetChanger dummy(mi.base, "textnormal");
226 static docstring bracket = from_ascii("[");
227 mathed_string_dim(mi.base.font, bracket, wdim);
233 cell(0).metrics(mi, dim0);
234 cell(1).metrics(mi, dim1);
235 cell(2).metrics(mi, dim2);
237 dim.wid = w + dim0.wid + w + w + dim1.wid + w + 2 + dim2.wid;
238 dim.asc = std::max(std::max(wdim.asc, dim0.asc), std::max(dim1.asc, dim2.asc));
239 dim.des = std::max(std::max(wdim.des, dim0.des), std::max(dim1.des, dim2.des));
254 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
256 drawMarkers(pi, x, y);
258 FontSetChanger dummy(pi.base, "textnormal");
259 BufferView const & bv = *pi.base.bv;
260 int w = mathed_char_width(pi.base.font, '[');
263 Dimension const dim = dimension(*pi.base.bv);
264 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
265 dim.width() - 2, dim.height() - 2, Color_foreground);
269 drawStrBlack(pi, x, y, from_ascii("["));
271 cell(0).draw(pi, x, y);
272 x += cell(0).dimension(bv).wid;
273 drawStrBlack(pi, x, y, from_ascii("]"));
276 drawStrBlack(pi, x, y, from_ascii("["));
278 cell(1).draw(pi, x, y);
279 x += cell(1).dimension(bv).wid;
280 drawStrBlack(pi, x, y, from_ascii("]"));
283 cell(2).draw(pi, x, y);
287 void InsetMathMakebox::write(WriteStream & os) const
289 ModeSpecifier specifier(os, TEXT_MODE);
290 os << (framebox_ ? "\\framebox" : "\\makebox");
291 if (cell(0).size() || !os.latex()) {
292 os << '[' << cell(0) << ']';
293 if (cell(1).size() || !os.latex())
294 os << '[' << cell(1) << ']';
296 os << '{' << cell(2) << '}';
300 void InsetMathMakebox::normalize(NormalStream & os) const
302 os << (framebox_ ? "[framebox " : "[makebox ")
303 << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
307 void InsetMathMakebox::infoize(odocstream & os) const
309 os << (framebox_ ? "Framebox" : "Makebox")
310 << " (width: " << cell(0)
311 << " pos: " << cell(1) << ")";
315 void InsetMathMakebox::mathmlize(MathStream & ms) const
317 // FIXME We could do something with the other arguments.
318 std::string const cssclass = framebox_ ? "framebox" : "makebox";
319 SetMode textmode(ms, true);
320 ms << MTag("mstyle", "class='" + cssclass + "'")
326 void InsetMathMakebox::htmlize(HtmlStream & ms) const
328 // FIXME We could do something with the other arguments.
329 SetHTMLMode textmode(ms, true);
330 std::string const cssclass = framebox_ ? "framebox" : "makebox";
331 ms << MTag("span", "class='" + cssclass + "'")
337 void InsetMathMakebox::validate(LaTeXFeatures & features) const
340 // It'd be better to be able to get this from an InsetLayout, but at present
341 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
342 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
343 features.addPreambleSnippet("<style type=\"text/css\">\n"
344 "mstyle.framebox { border: 1px solid black; }\n"
346 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
347 features.addPreambleSnippet("<style type=\"text/css\">\n"
348 "span.framebox { border: 1px solid black; }\n"
350 InsetMathNest::validate(features);
354 /////////////////////////////////////////////////////////////////////
358 /////////////////////////////////////////////////////////////////////
360 InsetMathBoxed::InsetMathBoxed(Buffer * buf)
361 : InsetMathNest(buf, 1)
365 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
367 cell(0).metrics(mi, dim);
368 metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
372 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
374 Dimension const dim = dimension(*pi.base.bv);
375 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
376 dim.width() - 2, dim.height() - 2, Color_foreground);
377 cell(0).draw(pi, x + 3, y);
378 setPosCache(pi, x, y);
382 void InsetMathBoxed::write(WriteStream & os) const
384 ModeSpecifier specifier(os, MATH_MODE);
385 os << "\\boxed{" << cell(0) << '}';
389 void InsetMathBoxed::normalize(NormalStream & os) const
391 os << "[boxed " << cell(0) << ']';
395 void InsetMathBoxed::infoize(odocstream & os) const
401 void InsetMathBoxed::mathmlize(MathStream & ms) const
403 ms << MTag("mstyle", "class='boxed'")
409 void InsetMathBoxed::htmlize(HtmlStream & ms) const
411 ms << MTag("span", "class='boxed'")
417 void InsetMathBoxed::validate(LaTeXFeatures & features) const
419 features.require("amsmath");
422 // It'd be better to be able to get this from an InsetLayout, but at present
423 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
424 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
425 features.addPreambleSnippet("<style type=\"text/css\">\n"
426 "mstyle.boxed { border: 1px solid black; }\n"
428 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
429 features.addPreambleSnippet("<style type=\"text/css\">\n"
430 "span.boxed { border: 1px solid black; }\n"
433 InsetMathNest::validate(features);