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"
29 using namespace lyx::support;
33 /////////////////////////////////////////////////////////////////////
37 /////////////////////////////////////////////////////////////////////
39 InsetMathBox::InsetMathBox(Buffer * buf, docstring const & name)
40 : InsetMathNest(buf, 1), name_(name)
44 void InsetMathBox::write(WriteStream & os) const
46 ModeSpecifier specifier(os, TEXT_MODE);
47 os << '\\' << name_ << '{' << cell(0) << '}';
51 void InsetMathBox::normalize(NormalStream & os) const
53 os << '[' << name_ << ' ';
54 //text_->write(buffer(), os);
59 void InsetMathBox::mathmlize(MathStream & ms) const
62 // Need to do something special for tags here.
63 // Probably will have to involve deferring them, which
64 // means returning something from this routine.
65 SetMode textmode(ms, true);
66 ms << MTag("mstyle", "class='mathbox'")
72 void InsetMathBox::htmlize(HtmlStream & ms) const
74 SetHTMLMode textmode(ms, true);
75 ms << MTag("span", "class='mathbox'")
81 void InsetMathBox::metrics(MetricsInfo & mi, Dimension & dim) const
83 FontSetChanger dummy(mi.base, "textnormal");
84 cell(0).metrics(mi, dim);
89 void InsetMathBox::draw(PainterInfo & pi, int x, int y) const
91 FontSetChanger dummy(pi.base, "textnormal");
92 cell(0).draw(pi, x, y);
93 drawMarkers(pi, x, y);
97 void InsetMathBox::infoize(odocstream & os) const
99 os << bformat(_("Box: %1$s"), name_);
103 void InsetMathBox::validate(LaTeXFeatures & features) const
106 // It'd be better to be able to get this from an InsetLayout, but at present
107 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
108 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
109 features.addCSSSnippet("mstyle.mathbox { font-style: normal; }");
110 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
111 features.addCSSSnippet("span.mathbox { font-style: normal; }");
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.addCSSSnippet(
196 "mstyle.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }");
197 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
198 features.addCSSSnippet(
199 "span.fbox { border: 1px solid black; font-style: normal; padding: 0.5ex; }");
201 cell(0).validate(features);
202 InsetMathNest::validate(features);
207 /////////////////////////////////////////////////////////////////////
211 /////////////////////////////////////////////////////////////////////
214 InsetMathMakebox::InsetMathMakebox(Buffer * buf, bool framebox)
215 : InsetMathNest(buf, 3), framebox_(framebox)
219 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
221 FontSetChanger dummy(mi.base, "textnormal");
224 static docstring bracket = from_ascii("[");
225 metricsStrRedBlack(mi, wdim, bracket);
231 cell(0).metrics(mi, dim0);
232 cell(1).metrics(mi, dim1);
233 cell(2).metrics(mi, dim2);
235 dim.wid = w + dim0.wid + w + w + dim1.wid + w + 2 + dim2.wid;
236 dim.asc = std::max(std::max(wdim.asc, dim0.asc), std::max(dim1.asc, dim2.asc));
237 dim.des = std::max(std::max(wdim.des, dim0.des), std::max(dim1.des, dim2.des));
252 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
254 drawMarkers(pi, x, y);
256 FontSetChanger dummy(pi.base, "textnormal");
257 BufferView const & bv = *pi.base.bv;
258 int w = mathed_char_width(pi.base.font, '[');
261 Dimension const dim = dimension(*pi.base.bv);
262 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
263 dim.width() - 2, dim.height() - 2, Color_foreground);
267 drawStrBlack(pi, x, y, from_ascii("["));
269 cell(0).draw(pi, x, y);
270 x += cell(0).dimension(bv).wid;
271 drawStrBlack(pi, x, y, from_ascii("]"));
274 drawStrBlack(pi, x, y, from_ascii("["));
276 cell(1).draw(pi, x, y);
277 x += cell(1).dimension(bv).wid;
278 drawStrBlack(pi, x, y, from_ascii("]"));
281 cell(2).draw(pi, x, y);
285 void InsetMathMakebox::write(WriteStream & os) const
287 ModeSpecifier specifier(os, TEXT_MODE);
288 os << (framebox_ ? "\\framebox" : "\\makebox");
289 if (!cell(0).empty() || !os.latex()) {
290 os << '[' << cell(0) << ']';
291 if (!cell(1).empty() || !os.latex())
292 os << '[' << cell(1) << ']';
294 os << '{' << cell(2) << '}';
298 void InsetMathMakebox::normalize(NormalStream & os) const
300 os << (framebox_ ? "[framebox " : "[makebox ")
301 << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
305 void InsetMathMakebox::infoize(odocstream & os) const
307 os << (framebox_ ? "Framebox" : "Makebox")
308 << " (width: " << cell(0)
309 << " pos: " << cell(1) << ")";
313 void InsetMathMakebox::mathmlize(MathStream & ms) const
315 // FIXME We could do something with the other arguments.
316 std::string const cssclass = framebox_ ? "framebox" : "makebox";
317 SetMode textmode(ms, true);
318 ms << MTag("mstyle", "class='" + cssclass + "'")
324 void InsetMathMakebox::htmlize(HtmlStream & ms) const
326 // FIXME We could do something with the other arguments.
327 SetHTMLMode textmode(ms, true);
328 std::string const cssclass = framebox_ ? "framebox" : "makebox";
329 ms << MTag("span", "class='" + cssclass + "'")
335 void InsetMathMakebox::validate(LaTeXFeatures & features) const
338 // It'd be better to be able to get this from an InsetLayout, but at present
339 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
340 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
341 features.addCSSSnippet("mstyle.framebox { border: 1px solid black; }");
342 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
343 features.addCSSSnippet("span.framebox { border: 1px solid black; }");
344 InsetMathNest::validate(features);
348 /////////////////////////////////////////////////////////////////////
352 /////////////////////////////////////////////////////////////////////
354 InsetMathBoxed::InsetMathBoxed(Buffer * buf)
355 : InsetMathNest(buf, 1)
359 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
361 cell(0).metrics(mi, dim);
362 metricsMarkers2(dim, 3); // 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);
372 setPosCache(pi, x, y);
376 void InsetMathBoxed::write(WriteStream & os) const
378 ModeSpecifier specifier(os, MATH_MODE);
379 os << "\\boxed{" << cell(0) << '}';
383 void InsetMathBoxed::normalize(NormalStream & os) const
385 os << "[boxed " << cell(0) << ']';
389 void InsetMathBoxed::infoize(odocstream & os) const
395 void InsetMathBoxed::mathmlize(MathStream & ms) const
397 ms << MTag("mstyle", "class='boxed'")
403 void InsetMathBoxed::htmlize(HtmlStream & ms) const
405 ms << MTag("span", "class='boxed'")
411 void InsetMathBoxed::validate(LaTeXFeatures & features) const
413 features.require("amsmath");
416 // It'd be better to be able to get this from an InsetLayout, but at present
417 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
418 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
419 features.addCSSSnippet("mstyle.boxed { border: 1px solid black; }");
420 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
421 features.addCSSSnippet("span.boxed { border: 1px solid black; }");
423 InsetMathNest::validate(features);