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 "frontends/Painter.h"
27 #include "support/lassert.h"
34 /////////////////////////////////////////////////////////////////////
38 /////////////////////////////////////////////////////////////////////
41 InsetMathFracBase::InsetMathFracBase(Buffer * buf, idx_type ncells)
42 : InsetMathNest(buf, ncells)
46 bool InsetMathFracBase::idxUpDown(Cursor & cur, bool up) const
48 // If we only have one cell, target = 0, otherwise
49 // target = up ? 0 : 1, since upper cell has idx 0
50 InsetMath::idx_type target = nargs() > 1 ? !up : 0;
51 if (cur.idx() == target)
54 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
60 /////////////////////////////////////////////////////////////////////
64 /////////////////////////////////////////////////////////////////////
67 InsetMathFrac::InsetMathFrac(Buffer * buf, Kind kind, InsetMath::idx_type ncells)
68 : InsetMathFracBase(buf, ncells), kind_(kind)
72 Inset * InsetMathFrac::clone() const
74 return new InsetMathFrac(*this);
78 InsetMathFrac * InsetMathFrac::asFracInset()
80 return kind_ == ATOP ? 0 : this;
84 InsetMathFrac const * InsetMathFrac::asFracInset() const
86 return kind_ == ATOP ? 0 : this;
90 bool InsetMathFrac::idxForward(Cursor & cur) const
92 InsetMath::idx_type target = 0;
93 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
96 else if (nargs() == 2)
100 if (cur.idx() == target)
103 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
108 bool InsetMathFrac::idxBackward(Cursor & cur) const
110 InsetMath::idx_type target = 0;
111 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
114 else if (nargs() == 2)
118 if (cur.idx() == target)
121 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
126 MathClass InsetMathFrac::mathClass() const
128 // Generalized fractions are of inner class (see The TeXbook, p. 292)
129 // But stuff from the unit/nicefrac packages are not real fractions.
130 MathClass mc = MC_ORD;
151 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
153 Dimension dim0, dim1, dim2;
155 // This could be simplified, including avoiding useless recalculation of
157 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
159 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
160 cell(0).metrics(mi, dim0);
161 dim.wid = dim0.width()+ 3;
164 } else if (nargs() == 2) {
165 cell(0).metrics(mi, dim0);
166 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
167 cell(1).metrics(mi, dim1);
168 dim.wid = dim0.width() + dim1.wid + 5;
169 dim.asc = max(dim0.asc, dim1.asc);
170 dim.des = max(dim0.des, dim1.des);
172 cell(2).metrics(mi, dim2);
173 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
174 Changer dummy2 = mi.base.changeFrac();
175 cell(0).metrics(mi, dim0);
176 cell(1).metrics(mi, dim1);
177 dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
178 dim.asc = max(dim2.asc, dim0.height() + 5);
179 dim.des = max(dim2.des, dim1.height() - 5);
182 // general cell metrics used for \frac
183 Changer dummy = mi.base.changeFrac();
184 // FIXME: Exponential blowup
185 cell(0).metrics(mi, dim0);
186 cell(1).metrics(mi, dim1);
188 cell(2).metrics(mi, dim2);
189 // metrics for special fraction types
190 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
191 Changer dummy2 = mi.base.font.changeShape(UP_SHAPE, kind_ == UNITFRAC);
192 dim.wid = dim0.width() + dim1.wid + 5;
193 dim.asc = dim0.height() + 5;
194 dim.des = dim1.height() - 5;
196 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
197 || kind_ == DFRAC || kind_ == TFRAC) {
198 // \cfrac and \dfrac are always in display size
199 // \tfrac is in always in text size
200 Changer dummy2 = mi.base.font.changeStyle((kind_ == TFRAC)
203 cell(0).metrics(mi, dim0);
204 cell(1).metrics(mi, dim1);
206 dim.wid = max(dim0.wid, dim1.wid) + 2;
207 dim.asc = dim0.height() + 2 + 5;
208 dim.des = dim1.height() + 2 - 5;
211 metricsMarkers(mi, dim);
215 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
217 setPosCache(pi, x, y);
218 Dimension const dim = dimension(*pi.base.bv);
219 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
220 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
222 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
223 cell(0).draw(pi, x + 1, y);
224 } else if (nargs() == 2) {
225 cell(0).draw(pi, x + 1, y);
226 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
227 cell(1).draw(pi, x + dim0.width() + 5, y);
229 cell(2).draw(pi, x + 1, y);
230 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
231 Changer dummy2 = pi.base.changeFrac();
232 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
233 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
234 int xx = x + dim2.wid + 5;
235 cell(0).draw(pi, xx + 2,
237 cell(1).draw(pi, xx + dim0.width() + 5,
241 Changer dummy = pi.base.changeFrac();
242 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
243 int m = x + dim.wid / 2;
244 if (kind_ == NICEFRAC) {
245 cell(0).draw(pi, x + 2,
247 cell(1).draw(pi, x + dim0.width() + 5,
249 } else if (kind_ == UNITFRAC) {
250 Changer dummy2 = pi.base.font.changeShape(UP_SHAPE);
251 cell(0).draw(pi, x + 2, y - dim0.des - 5);
252 cell(1).draw(pi, x + dim0.width() + 5, y + dim1.asc / 2);
253 } else if (kind_ == FRAC || kind_ == ATOP || kind_ == OVER
255 // tfrac is in always in text size
256 Changer dummy2 = pi.base.font.changeStyle(LM_ST_SCRIPT,
258 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
259 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
261 // \cfrac and \dfrac are always in display size
262 Changer dummy2 = pi.base.font.changeStyle(LM_ST_DISPLAY);
263 if (kind_ == CFRAC || kind_ == DFRAC)
264 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
265 else if (kind_ == CFRACLEFT)
266 cell(0).draw(pi, x + 2, y - dim0.des - 2 - 5);
267 else if (kind_ == CFRACRIGHT)
268 cell(0).draw(pi, x + dim.wid - dim0.wid - 2,
269 y - dim0.des - 2 - 5);
270 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
273 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
277 xx += cell(2).dimension(*pi.base.bv).wid + 5;
278 pi.pain.line(xx + dim0.wid,
281 y - dim.asc + 2, pi.base.font.color());
283 if (kind_ == FRAC || kind_ == CFRAC || kind_ == CFRACLEFT
284 || kind_ == CFRACRIGHT || kind_ == DFRAC
285 || kind_ == TFRAC || kind_ == OVER)
286 pi.pain.line(x + 1, y - 5,
287 x + dim.wid - 2, y - 5, pi.base.font.color());
288 drawMarkers(pi, x, y);
292 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
294 Dimension dim0, dim1;
295 cell(0).metricsT(mi, dim0);
296 cell(1).metricsT(mi, dim1);
297 dim.wid = max(dim0.width(), dim1.wid);
298 dim.asc = dim0.height() + 1;
299 dim.des = dim1.height();
303 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
308 int m = x + dim.width() / 2;
309 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
310 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
311 // ASCII art: ignore niceties
312 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
313 pain.horizontalLine(x, y, dim.width());
318 void InsetMathFrac::write(WriteStream & os) const
320 MathEnsurer ensurer(os);
323 // \\atop is only for compatibility, \\binom is the
325 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
328 // \\over is only for compatibility, normalize this to \\frac
329 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
338 InsetMathNest::write(os);
340 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
344 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
346 os << "\\unit{" << cell(0) << '}';
349 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
352 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
358 docstring InsetMathFrac::name() const
362 return from_ascii("frac");
366 return from_ascii("cfrac");
368 return from_ascii("dfrac");
370 return from_ascii("tfrac");
372 return from_ascii("over");
374 return from_ascii("nicefrac");
376 return from_ascii("unitfrac");
378 return from_ascii("unit");
380 return from_ascii("atop");
382 // shut up stupid compiler
387 bool InsetMathFrac::extraBraces() const
389 return kind_ == ATOP || kind_ == OVER;
393 void InsetMathFrac::maple(MapleStream & os) const
396 // Someone who knows about maple should fix this.
397 LASSERT(false, return);
399 os << '(' << cell(0) << ")/(" << cell(1) << ')';
403 void InsetMathFrac::mathematica(MathematicaStream & os) const
406 // Someone who knows about mathematica should fix this.
407 LASSERT(false, return);
409 os << '(' << cell(0) << ")/(" << cell(1) << ')';
413 void InsetMathFrac::octave(OctaveStream & os) const
416 // Someone who knows about octave should fix this.
417 LASSERT(false, return);
419 os << '(' << cell(0) << ")/(" << cell(1) << ')';
423 void InsetMathFrac::mathmlize(MathStream & os) const
427 os << MTag("mfrac", "linethickeness='0'")
428 << MTag("mrow") << cell(0) << ETag("mrow")
429 << MTag("mrow") << cell(1) << ETag("mrow")
433 // we do not presently distinguish these
442 << MTag("mrow") << cell(0) << ETag("mrow")
443 << MTag("mrow") << cell(1) << ETag("mrow")
448 os << MTag("mfrac", "bevelled='true'")
449 << MTag("mrow") << cell(0) << ETag("mrow")
450 << MTag("mrow") << cell(1) << ETag("mrow")
457 os << MTag("mfrac", "bevelled='true'")
458 << MTag("mrow") << cell(0) << ETag("mrow")
459 << MTag("mrow") << cell(1) << ETag("mrow")
464 // FIXME This is not right, because we still output mi, etc,
465 // when we output the cell. So we need to prevent that somehow.
468 << MTag("mstyle mathvariant='normal'")
472 os << MTag("mstyle mathvariant='normal'")
479 void InsetMathFrac::htmlize(HtmlStream & os) const
483 os << MTag("span", "class='frac'")
484 << MTag("span", "class='numer'") << cell(0) << ETag("span")
485 << MTag("span", "class='numer'") << cell(1) << ETag("span")
489 // we do not presently distinguish these
497 os << MTag("span", "class='frac'")
498 << MTag("span", "class='numer'") << cell(0) << ETag("span")
499 << MTag("span", "class='denom'") << cell(1) << ETag("span")
504 os << cell(0) << '/' << cell(1);
509 os << cell(2) << ' ';
510 os << cell(0) << '/' << cell(1);
514 // FIXME This is not right, because we still output i, etc,
515 // when we output the cell. So we need to prevent that somehow.
529 void InsetMathFrac::validate(LaTeXFeatures & features) const
531 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
532 features.require("units");
533 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
534 || kind_ == DFRAC || kind_ == TFRAC)
535 features.require("amsmath");
536 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
537 // CSS adapted from eLyXer
538 features.addCSSSnippet(
539 "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
540 "span.numer{display: block;}\n"
541 "span.denom{display: block; border-top: thin solid #000040;}");
542 InsetMathNest::validate(features);
546 /////////////////////////////////////////////////////////////////////
550 /////////////////////////////////////////////////////////////////////
553 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
554 : InsetMathFracBase(buf), kind_(kind)
558 Inset * InsetMathBinom::clone() const
560 return new InsetMathBinom(*this);
564 int InsetMathBinom::dw(int height) const
575 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
577 Dimension dim0, dim1;
579 (kind_ == DBINOM) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
580 (kind_ == TBINOM) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
581 mi.base.changeFrac();
582 cell(0).metrics(mi, dim0);
583 cell(1).metrics(mi, dim1);
584 dim.asc = dim0.height() + 4 + 5;
585 dim.des = dim1.height() + 4 - 5;
586 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
587 metricsMarkers2(mi, dim);
591 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
593 Dimension const dim = dimension(*pi.base.bv);
594 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
595 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
596 // define the binom brackets
597 docstring const bra = kind_ == BRACE ? from_ascii("{") :
598 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
599 docstring const ket = kind_ == BRACE ? from_ascii("}") :
600 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
602 int m = x + dim.width() / 2;
605 (kind_ == DBINOM) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
606 (kind_ == TBINOM) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
607 pi.base.changeFrac();
608 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
609 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
611 // draw the brackets and the marker
612 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
614 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
615 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
616 drawMarkers2(pi, x, y);
620 bool InsetMathBinom::extraBraces() const
622 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
626 void InsetMathBinom::write(WriteStream & os) const
628 MathEnsurer ensurer(os);
631 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
634 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
637 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
640 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
643 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
646 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
652 void InsetMathBinom::normalize(NormalStream & os) const
654 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
658 void InsetMathBinom::mathmlize(MathStream & os) const
679 os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
680 << "<mfrac linethickness='0'>"
681 << cell(0) << cell(1)
683 << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
687 void InsetMathBinom::htmlize(HtmlStream & os) const
708 os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
709 << MTag("span", "class='binom'") << '\n'
710 << MTag("span") << cell(0) << ETag("span") << '\n'
711 << MTag("span") << cell(1) << ETag("span") << '\n'
712 << ETag("span") << '\n'
713 << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
717 void InsetMathBinom::validate(LaTeXFeatures & features) const
719 if (features.runparams().isLaTeX()) {
721 features.require("binom");
722 if (kind_ == DBINOM || kind_ == TBINOM)
723 features.require("amsmath");
724 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
725 features.addCSSSnippet(
726 "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
727 "span.binom span{display: block;}\n"
728 "span.binomdelim{font-size: 2em;}");
729 InsetMathNest::validate(features);