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, "class='mathbox'");
62 void InsetMathBox::htmlize(HtmlStream & ms) const
64 SetHTMLMode textmode(ms, true);
69 void InsetMathBox::metrics(MetricsInfo & mi, Dimension & dim) const
71 FontSetChanger dummy(mi.base, "textnormal");
72 cell(0).metrics(mi, dim);
77 void InsetMathBox::draw(PainterInfo & pi, int x, int y) const
79 FontSetChanger dummy(pi.base, "textnormal");
80 cell(0).draw(pi, x, y);
81 drawMarkers(pi, x, y);
85 void InsetMathBox::infoize(odocstream & os) const
87 os << "Box: " << name_;
91 void InsetMathBox::validate(LaTeXFeatures & features) const
93 if (name_ == "tag" || name_ == "tag*")
94 features.require("amsmath");
96 cell(0).validate(features);
101 /////////////////////////////////////////////////////////////////////
105 /////////////////////////////////////////////////////////////////////
108 InsetMathFBox::InsetMathFBox(Buffer * buf)
109 : InsetMathNest(buf, 1)
113 void InsetMathFBox::metrics(MetricsInfo & mi, Dimension & dim) const
115 FontSetChanger dummy(mi.base, "textnormal");
116 cell(0).metrics(mi, dim);
117 metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
121 void InsetMathFBox::draw(PainterInfo & pi, int x, int y) const
123 Dimension const dim = dimension(*pi.base.bv);
124 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
125 dim.width() - 2, dim.height() - 2, Color_foreground);
126 FontSetChanger dummy(pi.base, "textnormal");
127 cell(0).draw(pi, x + 3, y);
128 setPosCache(pi, x, y);
132 void InsetMathFBox::write(WriteStream & os) const
134 ModeSpecifier specifier(os, TEXT_MODE);
135 os << "\\fbox{" << cell(0) << '}';
139 void InsetMathFBox::normalize(NormalStream & os) const
141 os << "[fbox " << cell(0) << ']';
145 void InsetMathFBox::mathmlize(MathStream & ms) const
147 SetMode textmode(ms, true, "class='fbox'");
152 void InsetMathFBox::htmlize(HtmlStream & ms) const
154 SetHTMLMode textmode(ms, true, "class='fbox'");
159 void InsetMathFBox::infoize(odocstream & os) const
165 void InsetMathFBox::validate(LaTeXFeatures & features) const
168 // It'd be better to be able to get this from an InsetLayout, but at present
169 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
170 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
171 features.addPreambleSnippet("<style type=\"text/css\">\n"
172 "mtext.fbox { border: 1px solid black; }\n"
174 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
175 features.addPreambleSnippet("<style type=\"text/css\">\n"
176 "span.fbox { border: 1px solid black; }\n"
178 InsetMathNest::validate(features);
183 /////////////////////////////////////////////////////////////////////
187 /////////////////////////////////////////////////////////////////////
190 InsetMathMakebox::InsetMathMakebox(Buffer * buf, bool framebox)
191 : InsetMathNest(buf, 3), framebox_(framebox)
195 void InsetMathMakebox::metrics(MetricsInfo & mi, Dimension & dim) const
197 FontSetChanger dummy(mi.base, "textnormal");
200 static docstring bracket = from_ascii("[");
201 mathed_string_dim(mi.base.font, bracket, wdim);
207 cell(0).metrics(mi, dim0);
208 cell(1).metrics(mi, dim1);
209 cell(2).metrics(mi, dim2);
211 dim.wid = w + dim0.wid + w + w + dim1.wid + w + 2 + dim2.wid;
212 dim.asc = std::max(std::max(wdim.asc, dim0.asc), std::max(dim1.asc, dim2.asc));
213 dim.des = std::max(std::max(wdim.des, dim0.des), std::max(dim1.des, dim2.des));
228 void InsetMathMakebox::draw(PainterInfo & pi, int x, int y) const
230 drawMarkers(pi, x, y);
232 FontSetChanger dummy(pi.base, "textnormal");
233 BufferView const & bv = *pi.base.bv;
234 int w = mathed_char_width(pi.base.font, '[');
237 Dimension const dim = dimension(*pi.base.bv);
238 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
239 dim.width() - 2, dim.height() - 2, Color_foreground);
243 drawStrBlack(pi, x, y, from_ascii("["));
245 cell(0).draw(pi, x, y);
246 x += cell(0).dimension(bv).wid;
247 drawStrBlack(pi, x, y, from_ascii("]"));
250 drawStrBlack(pi, x, y, from_ascii("["));
252 cell(1).draw(pi, x, y);
253 x += cell(1).dimension(bv).wid;
254 drawStrBlack(pi, x, y, from_ascii("]"));
257 cell(2).draw(pi, x, y);
261 void InsetMathMakebox::write(WriteStream & os) const
263 ModeSpecifier specifier(os, TEXT_MODE);
264 os << (framebox_ ? "\\framebox" : "\\makebox");
265 if (cell(0).size() || !os.latex()) {
266 os << '[' << cell(0) << ']';
267 if (cell(1).size() || !os.latex())
268 os << '[' << cell(1) << ']';
270 os << '{' << cell(2) << '}';
274 void InsetMathMakebox::normalize(NormalStream & os) const
276 os << (framebox_ ? "[framebox " : "[makebox ")
277 << cell(0) << ' ' << cell(1) << ' ' << cell(2) << ']';
281 void InsetMathMakebox::infoize(odocstream & os) const
283 os << (framebox_ ? "Framebox" : "Makebox")
284 << " (width: " << cell(0)
285 << " pos: " << cell(1) << ")";
289 void InsetMathMakebox::mathmlize(MathStream & ms) const
291 // FIXME We could do something with the other arguments.
292 std::string const cssclass = framebox_ ? "framebox" : "makebox";
293 SetMode textmode(ms, true, "class='" + cssclass + "'");
298 void InsetMathMakebox::htmlize(HtmlStream & ms) const
300 // FIXME We could do something with the other arguments.
301 std::string const cssclass = framebox_ ? "framebox" : "makebox";
302 SetHTMLMode textmode(ms, true, "class='" + cssclass + "'");
307 void InsetMathMakebox::validate(LaTeXFeatures & features) const
310 // It'd be better to be able to get this from an InsetLayout, but at present
311 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
312 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
313 features.addPreambleSnippet("<style type=\"text/css\">\n"
314 "span.framebox { border: 1px solid black; }\n"
316 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
317 features.addPreambleSnippet("<style type=\"text/css\">\n"
318 "span.framebox { border: 1px solid black; }\n"
320 InsetMathNest::validate(features);
324 /////////////////////////////////////////////////////////////////////
328 /////////////////////////////////////////////////////////////////////
330 InsetMathBoxed::InsetMathBoxed(Buffer * buf)
331 : InsetMathNest(buf, 1)
335 void InsetMathBoxed::metrics(MetricsInfo & mi, Dimension & dim) const
337 cell(0).metrics(mi, dim);
338 metricsMarkers2(dim, 3); // 1 pixel space, 1 frame, 1 space
342 void InsetMathBoxed::draw(PainterInfo & pi, int x, int y) const
344 Dimension const dim = dimension(*pi.base.bv);
345 pi.pain.rectangle(x + 1, y - dim.ascent() + 1,
346 dim.width() - 2, dim.height() - 2, Color_foreground);
347 cell(0).draw(pi, x + 3, y);
348 setPosCache(pi, x, y);
352 void InsetMathBoxed::write(WriteStream & os) const
354 ModeSpecifier specifier(os, MATH_MODE);
355 os << "\\boxed{" << cell(0) << '}';
359 void InsetMathBoxed::normalize(NormalStream & os) const
361 os << "[boxed " << cell(0) << ']';
365 void InsetMathBoxed::infoize(odocstream & os) const
371 void InsetMathBoxed::mathmlize(MathStream & ms) const
373 SetMode mathmode(ms, false, "class='boxed'");
378 void InsetMathBoxed::htmlize(HtmlStream & ms) const
380 SetHTMLMode mathmode(ms, false, "class='boxed'");
385 void InsetMathBoxed::validate(LaTeXFeatures & features) const
387 features.require("amsmath");
390 // It'd be better to be able to get this from an InsetLayout, but at present
391 // InsetLayouts do not seem really to work for things that aren't InsetTexts.
392 if (features.runparams().math_flavor == OutputParams::MathAsMathML)
393 features.addPreambleSnippet("<style type=\"text/css\">\n"
394 "mstyle.boxed { border: 1px solid black; }\n"
396 else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
397 features.addPreambleSnippet("<style type=\"text/css\">\n"
398 "span.boxed { border: 1px solid black; }\n"
401 InsetMathNest::validate(features);