2 * \file InsetMathFracBase.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
10 * Full author contact details are available in file CREDITS.
15 #include "InsetMathFrac.h"
18 #include "LaTeXFeatures.h"
20 #include "MathStream.h"
21 #include "MathSupport.h"
22 #include "MetricsInfo.h"
23 #include "TextPainter.h"
25 #include "support/lassert.h"
26 #include "frontends/Painter.h"
32 /////////////////////////////////////////////////////////////////////
36 /////////////////////////////////////////////////////////////////////
39 InsetMathFracBase::InsetMathFracBase(Buffer * buf, idx_type ncells)
40 : InsetMathNest(buf, ncells)
44 bool InsetMathFracBase::idxUpDown(Cursor & cur, bool up) const
46 // If we only have one cell, target = 0, otherwise
47 // target = up ? 0 : 1, since upper cell has idx 0
48 InsetMath::idx_type target = nargs() > 1 ? !up : 0;
49 if (cur.idx() == target)
52 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
58 /////////////////////////////////////////////////////////////////////
62 /////////////////////////////////////////////////////////////////////
65 InsetMathFrac::InsetMathFrac(Buffer * buf, Kind kind, InsetMath::idx_type ncells)
66 : InsetMathFracBase(buf, ncells), kind_(kind)
70 Inset * InsetMathFrac::clone() const
72 return new InsetMathFrac(*this);
76 InsetMathFrac * InsetMathFrac::asFracInset()
78 return kind_ == ATOP ? 0 : this;
82 InsetMathFrac const * InsetMathFrac::asFracInset() const
84 return kind_ == ATOP ? 0 : this;
88 bool InsetMathFrac::idxForward(Cursor & cur) const
90 InsetMath::idx_type target = 0;
91 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
94 else if (nargs() == 2)
98 if (cur.idx() == target)
101 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
106 bool InsetMathFrac::idxBackward(Cursor & cur) const
108 InsetMath::idx_type target = 0;
109 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
112 else if (nargs() == 2)
116 if (cur.idx() == target)
119 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
124 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
126 Dimension dim0, dim1, dim2;
128 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
130 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
131 cell(0).metrics(mi, dim0);
132 dim.wid = dim0.width()+ 3;
135 } else if (nargs() == 2) {
136 cell(0).metrics(mi, dim0);
137 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
138 cell(1).metrics(mi, dim1);
139 dim.wid = dim0.width() + dim1.wid + 5;
140 dim.asc = max(dim0.asc, dim1.asc);
141 dim.des = max(dim0.des, dim1.des);
143 cell(2).metrics(mi, dim2);
144 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
145 FracChanger dummy(mi.base);
146 cell(0).metrics(mi, dim0);
147 cell(1).metrics(mi, dim1);
148 dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
149 dim.asc = max(dim2.asc, dim0.height() + 5);
150 dim.des = max(dim2.des, dim1.height() - 5);
153 // general cell metrics used for \frac
154 FracChanger dummy(mi.base);
155 cell(0).metrics(mi, dim0);
156 cell(1).metrics(mi, dim1);
158 cell(2).metrics(mi, dim2);
159 // metrics for special fraction types
160 if (kind_ == NICEFRAC) {
161 dim.wid = dim0.width() + dim1.wid + 5;
162 dim.asc = dim0.height() + 5;
163 dim.des = dim1.height() - 5;
164 } else if (kind_ == UNITFRAC) {
165 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
166 dim.wid = dim0.width() + dim1.wid + 5;
167 dim.asc = dim0.height() + 5;
168 dim.des = dim1.height() - 5;
170 if (kind_ == CFRAC || kind_ == CFRACLEFT
171 || kind_ == CFRACRIGHT || kind_ == DFRAC) {
172 // \cfrac and \dfrac are always in display size
173 StyleChanger dummy2(mi.base, LM_ST_DISPLAY);
174 cell(0).metrics(mi, dim0);
175 cell(1).metrics(mi, dim1);
176 } else if (kind_ == TFRAC) {
177 // tfrac is in always in text size
178 StyleChanger dummy2(mi.base, LM_ST_SCRIPT);
179 cell(0).metrics(mi, dim0);
180 cell(1).metrics(mi, dim1);
182 dim.wid = max(dim0.wid, dim1.wid) + 2;
183 dim.asc = dim0.height() + 2 + 5;
184 dim.des = dim1.height() + 2 - 5;
191 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
193 setPosCache(pi, x, y);
194 Dimension const dim = dimension(*pi.base.bv);
195 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
196 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
198 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
199 cell(0).draw(pi, x + 1, y);
200 } else if (nargs() == 2) {
201 cell(0).draw(pi, x + 1, y);
202 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
203 cell(1).draw(pi, x + dim0.width() + 5, y);
205 cell(2).draw(pi, x + 1, y);
206 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
207 FracChanger dummy(pi.base);
208 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
209 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
210 int xx = x + dim2.wid + 5;
211 cell(0).draw(pi, xx + 2,
213 cell(1).draw(pi, xx + dim0.width() + 5,
217 FracChanger dummy(pi.base);
218 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
219 int m = x + dim.wid / 2;
220 if (kind_ == NICEFRAC) {
221 cell(0).draw(pi, x + 2,
223 cell(1).draw(pi, x + dim0.width() + 5,
225 } else if (kind_ == UNITFRAC) {
226 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
227 cell(0).draw(pi, x + 2, y - dim0.des - 5);
228 cell(1).draw(pi, x + dim0.width() + 5, y + dim1.asc / 2);
229 } else if (kind_ == FRAC || kind_ == ATOP || kind_ == OVER) {
230 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
231 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
232 } else if (kind_ == TFRAC) {
233 // tfrac is in always in text size
234 StyleChanger dummy2(pi.base, LM_ST_SCRIPT);
235 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
236 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
238 // \cfrac and \dfrac are always in display size
239 StyleChanger dummy2(pi.base, LM_ST_DISPLAY);
240 if (kind_ == CFRAC || kind_ == DFRAC)
241 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
242 else if (kind_ == CFRACLEFT)
243 cell(0).draw(pi, x + 2, y - dim0.des - 2 - 5);
244 else if (kind_ == CFRACRIGHT)
245 cell(0).draw(pi, x + dim.wid - dim0.wid - 2,
246 y - dim0.des - 2 - 5);
247 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
250 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
254 xx += cell(2).dimension(*pi.base.bv).wid + 5;
255 pi.pain.line(xx + dim0.wid,
258 y - dim.asc + 2, pi.base.font.color());
260 if (kind_ == FRAC || kind_ == CFRAC || kind_ == CFRACLEFT
261 || kind_ == CFRACRIGHT || kind_ == DFRAC
262 || kind_ == TFRAC || kind_ == OVER)
263 pi.pain.line(x + 1, y - 5,
264 x + dim.wid - 2, y - 5, pi.base.font.color());
265 drawMarkers(pi, x, y);
269 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
271 Dimension dim0, dim1;
272 cell(0).metricsT(mi, dim0);
273 cell(1).metricsT(mi, dim1);
274 dim.wid = max(dim0.width(), dim1.wid);
275 dim.asc = dim0.height() + 1;
276 dim.des = dim1.height();
280 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
285 int m = x + dim.width() / 2;
286 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
287 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
288 // ASCII art: ignore niceties
289 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
290 pain.horizontalLine(x, y, dim.width());
295 void InsetMathFrac::write(WriteStream & os) const
297 MathEnsurer ensurer(os);
300 // \\atop is only for compatibility, \\binom is the
302 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
305 // \\over is only for compatibility, normalize this to \\frac
306 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
315 InsetMathNest::write(os);
317 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
321 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
323 os << "\\unit{" << cell(0) << '}';
326 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
329 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
335 docstring InsetMathFrac::name() const
339 return from_ascii("frac");
343 return from_ascii("cfrac");
345 return from_ascii("dfrac");
347 return from_ascii("tfrac");
349 return from_ascii("over");
351 return from_ascii("nicefrac");
353 return from_ascii("unitfrac");
355 return from_ascii("unit");
357 return from_ascii("atop");
359 // shut up stupid compiler
364 bool InsetMathFrac::extraBraces() const
366 return kind_ == ATOP || kind_ == OVER;
370 void InsetMathFrac::maple(MapleStream & os) const
373 // Someone who knows about maple should fix this.
374 LASSERT(false, return);
376 os << '(' << cell(0) << ")/(" << cell(1) << ')';
380 void InsetMathFrac::mathematica(MathematicaStream & os) const
383 // Someone who knows about mathematica should fix this.
384 LASSERT(false, return);
386 os << '(' << cell(0) << ")/(" << cell(1) << ')';
390 void InsetMathFrac::octave(OctaveStream & os) const
393 // Someone who knows about octave should fix this.
394 LASSERT(false, return);
396 os << '(' << cell(0) << ")/(" << cell(1) << ')';
400 void InsetMathFrac::mathmlize(MathStream & os) const
404 os << MTag("mfrac", "linethickeness='0'")
405 << MTag("mrow") << cell(0) << ETag("mrow")
406 << MTag("mrow") << cell(1) << ETag("mrow")
410 // we do not presently distinguish these
419 << MTag("mrow") << cell(0) << ETag("mrow")
420 << MTag("mrow") << cell(1) << ETag("mrow")
425 os << MTag("mfrac", "bevelled='true'")
426 << MTag("mrow") << cell(0) << ETag("mrow")
427 << MTag("mrow") << cell(1) << ETag("mrow")
434 os << MTag("mfrac", "bevelled='true'")
435 << MTag("mrow") << cell(0) << ETag("mrow")
436 << MTag("mrow") << cell(1) << ETag("mrow")
441 // FIXME This is not right, because we still output mi, etc,
442 // when we output the cell. So we need to prevent that somehow.
445 << MTag("mstyle mathvariant='normal'")
449 os << MTag("mstyle mathvariant='normal'")
456 void InsetMathFrac::htmlize(HtmlStream & os) const
460 os << MTag("span", "class='frac'")
461 << MTag("span", "class='numer'") << cell(0) << ETag("span")
462 << MTag("span", "class='numer'") << cell(1) << ETag("span")
466 // we do not presently distinguish these
474 os << MTag("span", "class='frac'")
475 << MTag("span", "class='numer'") << cell(0) << ETag("span")
476 << MTag("span", "class='denom'") << cell(1) << ETag("span")
481 os << cell(0) << '/' << cell(1);
486 os << cell(2) << ' ';
487 os << cell(0) << '/' << cell(1);
491 // FIXME This is not right, because we still output i, etc,
492 // when we output the cell. So we need to prevent that somehow.
506 void InsetMathFrac::validate(LaTeXFeatures & features) const
508 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
509 features.require("units");
510 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
511 || kind_ == DFRAC || kind_ == TFRAC)
512 features.require("amsmath");
513 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
514 // CSS adapted from eLyXer
515 features.addCSSSnippet(
516 "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
517 "span.numer{display: block;}\n"
518 "span.denom{display: block; border-top: thin solid #000040;}");
519 InsetMathNest::validate(features);
523 /////////////////////////////////////////////////////////////////////
527 /////////////////////////////////////////////////////////////////////
530 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
531 : InsetMathFracBase(buf), kind_(kind)
535 Inset * InsetMathBinom::clone() const
537 return new InsetMathBinom(*this);
541 int InsetMathBinom::dw(int height) const
552 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
554 Dimension dim0, dim1;
556 // FIXME: for an unknown reason the cells must be set directly
557 // after the StyleChanger and cannot be set after the if case
558 if (kind_ == DBINOM) {
559 StyleChanger dummy(mi.base, LM_ST_DISPLAY);
560 cell(0).metrics(mi, dim0);
561 cell(1).metrics(mi, dim1);
562 } else if (kind_ == TBINOM) {
563 StyleChanger dummy(mi.base, LM_ST_SCRIPT);
564 cell(0).metrics(mi, dim0);
565 cell(1).metrics(mi, dim1);
567 FracChanger dummy(mi.base);
568 cell(0).metrics(mi, dim0);
569 cell(1).metrics(mi, dim1);
571 dim.asc = dim0.height() + 4 + 5;
572 dim.des = dim1.height() + 4 - 5;
573 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
574 metricsMarkers2(dim);
578 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
580 Dimension const dim = dimension(*pi.base.bv);
581 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
582 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
583 // define the binom brackets
584 docstring const bra = kind_ == BRACE ? from_ascii("{") :
585 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
586 docstring const ket = kind_ == BRACE ? from_ascii("}") :
587 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
589 int m = x + dim.width() / 2;
590 // FIXME: for an unknown reason the cells must be drawn directly
591 // after the StyleChanger and cannot be drawn after the if case
592 if (kind_ == DBINOM) {
593 StyleChanger dummy(pi.base, LM_ST_DISPLAY);
594 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
595 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
596 } else if (kind_ == TBINOM) {
597 StyleChanger dummy(pi.base, LM_ST_SCRIPT);
598 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
599 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
601 FracChanger dummy2(pi.base);
602 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
603 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
605 // draw the brackets and the marker
606 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
608 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
609 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
610 drawMarkers2(pi, x, y);
614 bool InsetMathBinom::extraBraces() const
616 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
620 void InsetMathBinom::write(WriteStream & os) const
622 MathEnsurer ensurer(os);
625 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
628 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
631 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
634 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
637 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
640 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
646 void InsetMathBinom::normalize(NormalStream & os) const
648 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
652 void InsetMathBinom::mathmlize(MathStream & os) const
673 os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
674 << "<mfrac linethickness='0'>"
675 << cell(0) << cell(1)
677 << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
681 void InsetMathBinom::htmlize(HtmlStream & os) const
702 os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
703 << MTag("span", "class='binom'") << '\n'
704 << MTag("span") << cell(0) << ETag("span") << '\n'
705 << MTag("span") << cell(1) << ETag("span") << '\n'
706 << ETag("span") << '\n'
707 << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
711 void InsetMathBinom::validate(LaTeXFeatures & features) const
713 if (features.runparams().isLaTeX()) {
715 features.require("binom");
716 if (kind_ == DBINOM || kind_ == TBINOM)
717 features.require("amsmath");
718 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
719 features.addCSSSnippet(
720 "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
721 "span.binom span{display: block;}\n"
722 "span.binomdelim{font-size: 2em;}");
723 InsetMathNest::validate(features);