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 "MathParser.h"
19 #include "MathStream.h"
20 #include "MathSupport.h"
23 #include "LaTeXFeatures.h"
24 #include "MetricsInfo.h"
25 #include "TextPainter.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
30 #include "support/lassert.h"
37 /////////////////////////////////////////////////////////////////////
41 /////////////////////////////////////////////////////////////////////
44 InsetMathFracBase::InsetMathFracBase(Buffer * buf, idx_type ncells)
45 : InsetMathNest(buf, ncells)
49 bool InsetMathFracBase::idxUpDown(Cursor & cur, bool up) const
51 // If we only have one cell, target = 0, otherwise
52 // target = up ? 0 : 1, since upper cell has idx 0
53 InsetMath::idx_type target = nargs() > 1 ? !up : 0;
54 if (cur.idx() == target)
57 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
63 /////////////////////////////////////////////////////////////////////
67 /////////////////////////////////////////////////////////////////////
70 InsetMathFrac::InsetMathFrac(Buffer * buf, Kind kind, InsetMath::idx_type ncells)
71 : InsetMathFracBase(buf, ncells), kind_(kind)
75 Inset * InsetMathFrac::clone() const
77 return new InsetMathFrac(*this);
81 InsetMathFrac * InsetMathFrac::asFracInset()
83 return kind_ == ATOP ? 0 : this;
87 InsetMathFrac const * InsetMathFrac::asFracInset() const
89 return kind_ == ATOP ? 0 : this;
93 bool InsetMathFrac::idxForward(Cursor & cur) const
95 InsetMath::idx_type target = 0;
96 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
99 else if (nargs() == 2)
103 if (cur.idx() == target)
106 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
111 bool InsetMathFrac::idxBackward(Cursor & cur) const
113 InsetMath::idx_type target = 0;
114 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
117 else if (nargs() == 2)
121 if (cur.idx() == target)
124 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
129 MathClass InsetMathFrac::mathClass() const
131 // Generalized fractions are of inner class (see The TeXbook, p. 292)
132 // But stuff from the unit/nicefrac packages are not real fractions.
133 MathClass mc = MC_ORD;
157 // align frac to minus character
158 int dy_for_frac(MetricsBase & mb)
160 Changer dummy = mb.changeFontSet("mathnormal");
161 return theFontMetrics(mb.font).ascent('-') - 1;
165 // align the top of M in the cell with the top of M in the surrounding font
166 int dy_for_nicefrac(MetricsBase & mb)
168 // this is according to nicefrac.sty
169 int big_m = theFontMetrics(mb.font).ascent('M');
170 Changer dummy = mb.changeScript();
171 int small_m = theFontMetrics(mb.font).ascent('M');
172 return big_m - small_m;
176 // symbol for nicefrac solidus
177 latexkeys const * slash_symbol()
179 return in_word_set(from_ascii("slash"));
186 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
188 Dimension dim0, dim1, dim2;
189 Changer dummy3 = mi.base.changeEnsureMath();
193 // \unitone, \unittwo
196 // is there an extra cell holding the value being given a dimension?
197 // (this is \unittwo)
199 cell(0).metrics(mi, dim1);
200 dim.wid += dim1.wid + 4;
203 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
204 cell(unit_cell).metrics(mi, dim0);
205 dim.wid += dim0.width() + 1;
206 dim.asc = max(dim0.asc, dim1.asc);
207 dim.des = max(dim0.des, dim1.des);
213 // \unitfrac, \unitfracthree, \nicefrac
217 int const dy = dy_for_nicefrac(mi.base);
218 // is there an extra cell holding the value being given a dimension?
219 // (this is \unitfracthree)
220 if (kind_ == UNITFRAC && nargs() == 3) {
221 cell(2).metrics(mi, dim2);
222 dim.wid += dim2.wid + 4;
226 Changer dummy = (kind_ == UNITFRAC) ? mi.base.font.changeShape(UP_SHAPE)
228 Changer dummy2 = mi.base.changeScript();
229 if (latexkeys const * slash = slash_symbol()) {
231 mathedSymbolDim(mi.base, dimslash, slash);
232 dim.wid += dimslash.wid - mathed_mu(mi.base.font, 3.0);
233 dim.asc = max(dim.asc, dimslash.asc);
234 dim.des = max(dim.des, dimslash.des);
236 cell(0).metrics(mi, dim0);
237 cell(1).metrics(mi, dim1);
238 dim.wid += dim0.wid + dim1.wid + 2;
239 dim.asc = max(max(dim.asc, dim0.asc + dy), dim1.asc);
240 dim.des = max(max(dim.des, dim0.des - dy), dim1.des);
252 int const dy = dy_for_frac(mi.base);
254 // \tfrac is always in text size
255 (kind_ == TFRAC) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
256 // \cfrac and \dfrac are always in display size
258 || kind_ == CFRACLEFT
259 || kind_ == CFRACRIGHT
260 || kind_ == DFRAC) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
262 mi.base.changeFrac();
263 Changer dummy2 = mi.base.changeEnsureMath();
264 cell(0).metrics(mi, dim0);
265 cell(1).metrics(mi, dim1);
266 dim.wid = max(dim0.wid, dim1.wid) + 2;
267 dim.asc = dim0.height() + dy/2 + dy;
268 int const t = mi.base.solidLineThickness();
269 dim.des = max(0, dim1.height() + dy/2 - dy + t);
272 metricsMarkers(mi, dim);
276 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
278 setPosCache(pi, x, y);
279 Changer dummy3 = pi.base.changeEnsureMath();
280 Dimension const dim = dimension(*pi.base.bv);
281 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
284 // \unitone, \unittwo
287 // is there an extra cell holding the value being given a dimension?
288 // (this is \unittwo)
290 cell(0).draw(pi, x + 1, y);
294 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
295 cell(unit_cell).draw(pi, xx + 1, y);
301 // \unitfrac, \unitfracthree, \nicefrac
303 int const dy = dy_for_nicefrac(pi.base);
304 // is there an extra cell holding the value being given a dimension?
305 // (this is \unitfracthree)
306 if (kind_ == UNITFRAC && nargs() == 3) {
307 cell(2).draw(pi, x + 1, y);
308 xx += cell(2).dimension(*pi.base.bv).wid + 4;
310 Changer dummy = (kind_ == UNITFRAC) ? pi.base.font.changeShape(UP_SHAPE)
313 Changer dummy2 = pi.base.changeScript();
314 cell(0).draw(pi, xx + 2, y - dy);
315 // reference LaTeX code from nicefrac.sty:
316 // \mkern-2mu/\mkern-1mu
317 if (latexkeys const * slash = slash_symbol()) {
318 int mkern = mathed_mu(pi.base.font, 2.0);
319 mathedSymbolDraw(pi, xx + 2 + dim0.wid - mkern, y, slash);
321 mathedSymbolDim(pi.base, dimslash, slash);
322 xx += dimslash.wid - mathed_mu(pi.base.font, 3.0);
324 cell(1).draw(pi, xx + 2 + dim0.wid, y);
336 int const dy = dy_for_frac(pi.base);
338 // \tfrac is always in text size
339 (kind_ == TFRAC) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
340 // \cfrac and \dfrac are always in display size
342 || kind_ == CFRACLEFT
343 || kind_ == CFRACRIGHT
344 || kind_ == DFRAC) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
346 pi.base.changeFrac();
347 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
348 int const m = x + dim.wid / 2;
351 (kind_ == CFRACLEFT) ? x + 2 :
353 (kind_ == CFRACRIGHT) ? x + dim.wid - dim0.wid - 2 :
356 int const t = pi.base.solidLineThickness();
357 // take dy/2 for the spacing around the horizontal line. This is
358 // arbitrary. In LaTeX it is more complicated to ensure that displayed
359 // fractions line up next to each other.
360 // For more accurate implementation refer to the TeXbook, Appendix G,
362 cell(0).draw(pi, xx, y - dim0.des - dy/2 - dy);
364 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + dy/2 - dy + t);
367 pi.pain.line(x + 1, y - dy, x + dim.wid - 2, y - dy,
368 pi.base.font.color(), pi.pain.line_solid, t);
371 drawMarkers(pi, x, y);
375 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
377 Dimension dim0, dim1;
378 cell(0).metricsT(mi, dim0);
379 cell(1).metricsT(mi, dim1);
380 dim.wid = max(dim0.width(), dim1.wid);
381 dim.asc = dim0.height() + 1;
382 dim.des = dim1.height();
386 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
391 int m = x + dim.width() / 2;
392 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
393 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
394 // ASCII art: ignore niceties
395 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
396 pain.horizontalLine(x, y, dim.width());
401 void InsetMathFrac::write(WriteStream & os) const
403 MathEnsurer ensurer(os);
406 // \\atop is only for compatibility, \\binom is the
408 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
411 // \\over is only for compatibility, normalize this to \\frac
412 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
421 InsetMathNest::write(os);
423 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
427 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
429 os << "\\unit{" << cell(0) << '}';
432 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
435 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
441 docstring InsetMathFrac::name() const
445 return from_ascii("frac");
449 return from_ascii("cfrac");
451 return from_ascii("dfrac");
453 return from_ascii("tfrac");
455 return from_ascii("over");
457 return from_ascii("nicefrac");
459 return from_ascii("unitfrac");
461 return from_ascii("unit");
463 return from_ascii("atop");
465 // shut up stupid compiler
470 bool InsetMathFrac::extraBraces() const
472 return kind_ == ATOP || kind_ == OVER;
476 void InsetMathFrac::maple(MapleStream & os) const
479 // Someone who knows about maple should fix this.
480 LASSERT(false, return);
482 os << '(' << cell(0) << ")/(" << cell(1) << ')';
486 void InsetMathFrac::mathematica(MathematicaStream & os) const
489 // Someone who knows about mathematica should fix this.
490 LASSERT(false, return);
492 os << '(' << cell(0) << ")/(" << cell(1) << ')';
496 void InsetMathFrac::octave(OctaveStream & os) const
499 // Someone who knows about octave should fix this.
500 LASSERT(false, return);
502 os << '(' << cell(0) << ")/(" << cell(1) << ')';
506 void InsetMathFrac::mathmlize(MathStream & os) const
510 os << MTag("mfrac", "linethickeness='0'")
511 << MTag("mrow") << cell(0) << ETag("mrow")
512 << MTag("mrow") << cell(1) << ETag("mrow")
516 // we do not presently distinguish these
525 << MTag("mrow") << cell(0) << ETag("mrow")
526 << MTag("mrow") << cell(1) << ETag("mrow")
531 os << MTag("mfrac", "bevelled='true'")
532 << MTag("mrow") << cell(0) << ETag("mrow")
533 << MTag("mrow") << cell(1) << ETag("mrow")
540 os << MTag("mfrac", "bevelled='true'")
541 << MTag("mrow") << cell(0) << ETag("mrow")
542 << MTag("mrow") << cell(1) << ETag("mrow")
547 // FIXME This is not right, because we still output mi, etc,
548 // when we output the cell. So we need to prevent that somehow.
551 << MTag("mstyle mathvariant='normal'")
555 os << MTag("mstyle mathvariant='normal'")
562 void InsetMathFrac::htmlize(HtmlStream & os) const
566 os << MTag("span", "class='frac'")
567 << MTag("span", "class='numer'") << cell(0) << ETag("span")
568 << MTag("span", "class='numer'") << cell(1) << ETag("span")
572 // we do not presently distinguish these
580 os << MTag("span", "class='frac'")
581 << MTag("span", "class='numer'") << cell(0) << ETag("span")
582 << MTag("span", "class='denom'") << cell(1) << ETag("span")
587 os << cell(0) << '/' << cell(1);
592 os << cell(2) << ' ';
593 os << cell(0) << '/' << cell(1);
597 // FIXME This is not right, because we still output i, etc,
598 // when we output the cell. So we need to prevent that somehow.
612 void InsetMathFrac::validate(LaTeXFeatures & features) const
614 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
615 features.require("units");
616 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
617 || kind_ == DFRAC || kind_ == TFRAC)
618 features.require("amsmath");
619 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
620 // CSS adapted from eLyXer
621 features.addCSSSnippet(
622 "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
623 "span.numer{display: block;}\n"
624 "span.denom{display: block; border-top: thin solid #000040;}");
625 InsetMathNest::validate(features);
629 /////////////////////////////////////////////////////////////////////
633 /////////////////////////////////////////////////////////////////////
636 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
637 : InsetMathFracBase(buf), kind_(kind)
641 Inset * InsetMathBinom::clone() const
643 return new InsetMathBinom(*this);
647 int InsetMathBinom::dw(int height) const
658 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
660 Changer dummy2 = mi.base.changeEnsureMath();
661 Dimension dim0, dim1;
662 int const dy = dy_for_frac(mi.base);
664 (kind_ == DBINOM) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
665 (kind_ == TBINOM) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
666 mi.base.changeFrac();
667 cell(0).metrics(mi, dim0);
668 cell(1).metrics(mi, dim1);
669 dim.asc = dim0.height() + 1 + dy/2 + dy;
670 dim.des = max(0, dim1.height() + 1 + dy/2 - dy);
671 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
672 metricsMarkers2(mi, dim);
676 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
678 Changer dummy2 = pi.base.changeEnsureMath();
679 Dimension const dim = dimension(*pi.base.bv);
680 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
681 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
682 int const dy = dy_for_frac(pi.base);
683 // define the binom brackets
684 docstring const bra = kind_ == BRACE ? from_ascii("{") :
685 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
686 docstring const ket = kind_ == BRACE ? from_ascii("}") :
687 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
689 int m = x + dim.width() / 2;
692 (kind_ == DBINOM) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
693 (kind_ == TBINOM) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
694 pi.base.changeFrac();
695 // take dy both for the vertical alignment and for the spacing between
697 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - dy/2 - dy);
698 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + dy/2 - dy);
700 // draw the brackets and the marker
701 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
703 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
704 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
705 drawMarkers2(pi, x, y);
709 bool InsetMathBinom::extraBraces() const
711 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
715 void InsetMathBinom::write(WriteStream & os) const
717 MathEnsurer ensurer(os);
720 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
723 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
726 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
729 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
732 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
735 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
741 void InsetMathBinom::normalize(NormalStream & os) const
743 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
747 void InsetMathBinom::mathmlize(MathStream & os) const
768 os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
769 << "<mfrac linethickness='0'>"
770 << cell(0) << cell(1)
772 << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
776 void InsetMathBinom::htmlize(HtmlStream & os) const
797 os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
798 << MTag("span", "class='binom'") << '\n'
799 << MTag("span") << cell(0) << ETag("span") << '\n'
800 << MTag("span") << cell(1) << ETag("span") << '\n'
801 << ETag("span") << '\n'
802 << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
806 void InsetMathBinom::validate(LaTeXFeatures & features) const
808 if (features.runparams().isLaTeX()) {
810 features.require("binom");
811 if (kind_ == DBINOM || kind_ == TBINOM)
812 features.require("amsmath");
813 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
814 features.addCSSSnippet(
815 "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
816 "span.binom span{display: block;}\n"
817 "span.binomdelim{font-size: 2em;}");
818 InsetMathNest::validate(features);