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 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, 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 ? nullptr : this;
87 InsetMathFrac const * InsetMathFrac::asFracInset() const
89 return kind_ == ATOP ? nullptr : this;
93 bool InsetMathFrac::idxForward(Cursor & cur) const
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
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;
158 // align the top of M in the cell with the top of M in the surrounding font
159 int dy_for_nicefrac(MetricsBase & mb)
161 // this is according to nicefrac.sty
162 int big_m = theFontMetrics(mb.font).ascent('M');
163 Changer dummy = mb.changeScript();
164 int small_m = theFontMetrics(mb.font).ascent('M');
165 return big_m - small_m;
169 // symbol for nicefrac solidus
170 latexkeys const * slash_symbol()
172 return in_word_set(from_ascii("slash"));
178 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
180 Dimension dim0, dim1, dim2;
181 Changer dummy3 = mi.base.changeEnsureMath();
185 // \unitone, \unittwo
188 // is there an extra cell holding the value being given a dimension?
189 // (this is \unittwo)
191 cell(0).metrics(mi, dim1);
192 dim.wid += dim1.wid + 4;
195 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
196 cell(unit_cell).metrics(mi, dim0);
197 dim.wid += dim0.width() + 1;
198 dim.asc = max(dim0.asc, dim1.asc);
199 dim.des = max(dim0.des, dim1.des);
205 // \unitfrac, \unitfracthree, \nicefrac
209 int const dy = dy_for_nicefrac(mi.base);
210 // is there an extra cell holding the value being given a dimension?
211 // (this is \unitfracthree)
212 if (kind_ == UNITFRAC && nargs() == 3) {
213 cell(2).metrics(mi, dim2);
214 dim.wid += dim2.wid + 4;
218 Changer dummy = (kind_ == UNITFRAC) ? mi.base.font.changeShape(UP_SHAPE)
220 Changer dummy2 = mi.base.changeScript();
221 if (latexkeys const * slash = slash_symbol()) {
223 mathedSymbolDim(mi.base, dimslash, slash);
224 dim.wid += dimslash.wid - mathed_mu(mi.base.font, 3.0);
225 dim.asc = max(dim.asc, dimslash.asc);
226 dim.des = max(dim.des, dimslash.des);
228 cell(0).metrics(mi, dim0);
229 cell(1).metrics(mi, dim1);
230 dim.wid += dim0.wid + dim1.wid + 2;
231 dim.asc = max(max(dim.asc, dim0.asc + dy), dim1.asc);
232 dim.des = max(max(dim.des, dim0.des - dy), dim1.des);
245 int const dy = axis_height(mi.base);
247 // \tfrac is always in text size
248 (kind_ == TFRAC) ? mi.base.font.changeStyle(SCRIPT_STYLE) :
249 // \cfrac and \dfrac are always in display size
251 || kind_ == CFRACLEFT
252 || kind_ == CFRACRIGHT
254 || kind_ == AASTEX_CASE) ? mi.base.font.changeStyle(DISPLAY_STYLE) :
256 mi.base.changeFrac();
257 Changer dummy2 = mi.base.changeEnsureMath();
258 cell(0).metrics(mi, dim0);
259 cell(1).metrics(mi, dim1);
260 dim.wid = max(dim0.wid, dim1.wid) + 2;
261 dim.asc = dim0.height() + dy/2 + dy;
262 int const t = mi.base.solidLineThickness();
263 dim.des = max(0, dim1.height() + dy/2 - dy + t);
269 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
271 Changer dummy3 = pi.base.changeEnsureMath();
272 Dimension const dim = dimension(*pi.base.bv);
273 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
276 // \unitone, \unittwo
279 // is there an extra cell holding the value being given a dimension?
280 // (this is \unittwo)
282 cell(0).draw(pi, x, y);
286 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
287 cell(unit_cell).draw(pi, xx, y);
293 // \unitfrac, \unitfracthree, \nicefrac
295 int const dy = dy_for_nicefrac(pi.base);
296 // is there an extra cell holding the value being given a dimension?
297 // (this is \unitfracthree)
298 if (kind_ == UNITFRAC && nargs() == 3) {
299 cell(2).draw(pi, x, y);
300 xx += cell(2).dimension(*pi.base.bv).wid + 4;
302 Changer dummy = (kind_ == UNITFRAC) ? pi.base.font.changeShape(UP_SHAPE)
305 Changer dummy2 = pi.base.changeScript();
306 cell(0).draw(pi, xx + 1, y - dy);
307 // reference LaTeX code from nicefrac.sty:
308 // \mkern-2mu/\mkern-1mu
309 if (latexkeys const * slash = slash_symbol()) {
310 int mkern = mathed_mu(pi.base.font, 2.0);
311 mathedSymbolDraw(pi, xx + 1 + dim0.wid - mkern, y, slash);
313 mathedSymbolDim(pi.base, dimslash, slash);
314 xx += dimslash.wid - mathed_mu(pi.base.font, 3.0);
316 cell(1).draw(pi, xx + 1 + dim0.wid, y);
329 int const dy = axis_height(pi.base);
331 // \tfrac is always in text size
332 (kind_ == TFRAC) ? pi.base.font.changeStyle(SCRIPT_STYLE) :
333 // \cfrac and \dfrac are always in display size
335 || kind_ == CFRACLEFT
336 || kind_ == CFRACRIGHT
338 || kind_ == AASTEX_CASE) ? pi.base.font.changeStyle(DISPLAY_STYLE) :
340 pi.base.changeFrac();
341 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
342 int const m = x + dim.wid / 2;
345 (kind_ == CFRACLEFT) ? x + 1 :
347 (kind_ == CFRACRIGHT) ? x + dim.wid - dim0.wid - 2 :
350 int const t = pi.base.solidLineThickness();
351 // take dy/2 for the spacing around the horizontal line. This is
352 // arbitrary. In LaTeX it is more complicated to ensure that displayed
353 // fractions line up next to each other.
354 // For more accurate implementation refer to the TeXbook, Appendix G,
356 cell(0).draw(pi, xx, y - dim0.des - dy/2 - dy);
358 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + dy/2 - dy + t);
361 pi.pain.line(x, y - dy, x + dim.wid, y - dy,
362 pi.base.font.color(), pi.pain.line_solid, t);
368 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
370 Dimension dim0, dim1;
371 cell(0).metricsT(mi, dim0);
372 cell(1).metricsT(mi, dim1);
373 dim.wid = max(dim0.width(), dim1.wid);
374 dim.asc = dim0.height() + 1;
375 dim.des = dim1.height();
379 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
384 int m = x + dim.width() / 2;
385 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
386 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
387 // ASCII art: ignore niceties
388 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
389 pain.horizontalLine(x, y, dim.width());
394 void InsetMathFrac::write(TeXMathStream & os) const
396 MathEnsurer ensurer(os);
399 // \\atop is only for compatibility, \\binom is the
401 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
404 // \\over is only for compatibility, normalize this to \\frac
405 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
414 InsetMathNest::write(os);
416 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
420 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
422 os << "\\unit{" << cell(0) << '}';
425 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
428 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
431 os << "\\case{" << cell(0) << "}{" << cell(1) << '}';
437 docstring InsetMathFrac::name() const
441 return from_ascii("frac");
445 return from_ascii("cfrac");
447 return from_ascii("dfrac");
449 return from_ascii("tfrac");
451 return from_ascii("over");
453 return from_ascii("nicefrac");
455 return from_ascii("unitfrac");
457 return from_ascii("unit");
459 return from_ascii("atop");
461 return from_ascii("case");
468 bool InsetMathFrac::extraBraces() const
470 return kind_ == ATOP || kind_ == OVER;
474 void InsetMathFrac::maple(MapleStream & os) const
477 // Someone who knows about maple should fix this.
478 LASSERT(false, return);
480 os << '(' << cell(0) << ")/(" << cell(1) << ')';
484 void InsetMathFrac::mathematica(MathematicaStream & os) const
487 // Someone who knows about mathematica should fix this.
488 LASSERT(false, return);
490 os << '(' << cell(0) << ")/(" << cell(1) << ')';
494 void InsetMathFrac::octave(OctaveStream & os) const
497 // Someone who knows about octave should fix this.
498 LASSERT(false, return);
500 os << '(' << cell(0) << ")/(" << cell(1) << ')';
504 void InsetMathFrac::mathmlize(MathMLStream & ms) const
508 ms << MTag("mfrac", "linethickness='0'")
509 << MTag("mrow") << cell(0) << ETag("mrow")
510 << MTag("mrow") << cell(1) << ETag("mrow")
514 // we do not presently distinguish these
530 ms << MTag("mfrac", "bevelled='true'")
539 ms << MTag("mfrac", "bevelled='true'")
546 // FIXME This is not right, because we still output mi, etc,
547 // when we output the cell. So we need to prevent that somehow.
550 << MTag("mstyle mathvariant='normal'")
554 ms << MTag("mstyle mathvariant='normal'")
561 void InsetMathFrac::htmlize(HtmlStream & os) const
565 os << MTag("span", "class='frac'")
566 << MTag("span", "class='numer'") << cell(0) << ETag("span")
567 << MTag("span", "class='numer'") << cell(1) << ETag("span")
571 // 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 (kind_ == AASTEX_CASE)
620 features.require("aastex_case");
622 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
623 // CSS adapted from eLyXer
624 features.addCSSSnippet(
625 "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
626 "span.numer{display: block;}\n"
627 "span.denom{display: block; border-top: thin solid #000040;}");
629 InsetMathNest::validate(features);
633 /////////////////////////////////////////////////////////////////////
637 /////////////////////////////////////////////////////////////////////
640 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
641 : InsetMathFracBase(buf), kind_(kind)
645 Inset * InsetMathBinom::clone() const
647 return new InsetMathBinom(*this);
651 int InsetMathBinom::dw(int height) const
662 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
664 Changer dummy2 = mi.base.changeEnsureMath();
665 Dimension dim0, dim1;
666 int const dy = axis_height(mi.base);
668 (kind_ == DBINOM) ? mi.base.font.changeStyle(DISPLAY_STYLE) :
669 (kind_ == TBINOM) ? mi.base.font.changeStyle(SCRIPT_STYLE) :
670 mi.base.changeFrac();
671 cell(0).metrics(mi, dim0);
672 cell(1).metrics(mi, dim1);
673 dim.asc = dim0.height() + 1 + dy/2 + dy;
674 dim.des = max(0, dim1.height() + 1 + dy/2 - dy);
675 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
679 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
681 Changer dummy2 = pi.base.changeEnsureMath();
682 Dimension const dim = dimension(*pi.base.bv);
683 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
684 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
685 int const dy = axis_height(pi.base);
686 // define the binom brackets
687 docstring const bra = kind_ == BRACE ? from_ascii("{") :
688 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
689 docstring const ket = kind_ == BRACE ? from_ascii("}") :
690 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
692 int m = x + dim.width() / 2;
695 (kind_ == DBINOM) ? pi.base.font.changeStyle(DISPLAY_STYLE) :
696 (kind_ == TBINOM) ? pi.base.font.changeStyle(SCRIPT_STYLE) :
697 pi.base.changeFrac();
698 // take dy both for the vertical alignment and for the spacing between
700 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - dy/2 - dy);
701 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + dy/2 - dy);
703 // draw the brackets and the marker
704 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
706 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
707 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
711 bool InsetMathBinom::extraBraces() const
713 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
717 void InsetMathBinom::write(TeXMathStream & os) const
719 MathEnsurer ensurer(os);
722 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
725 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
728 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
731 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
734 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
737 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
743 void InsetMathBinom::normalize(NormalStream & os) const
745 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
749 void InsetMathBinom::mathmlize(MathMLStream & ms) const
770 ms << "<" << from_ascii(ms.namespacedTag("mo")) << " fence='true' stretchy='true' form='prefix'>"
772 << "</" << from_ascii(ms.namespacedTag("mo")) << ">"
773 << "<" << from_ascii(ms.namespacedTag("mfrac")) << " linethickness='0'>"
774 << cell(0) << cell(1)
775 << "</" << from_ascii(ms.namespacedTag("mfrac")) << ">"
776 << "<" << from_ascii(ms.namespacedTag("mo")) << " fence='true' stretchy='true' form='postfix'>"
778 << "</" << from_ascii(ms.namespacedTag("mo")) << ">";
782 void InsetMathBinom::htmlize(HtmlStream & os) const
803 os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
804 << MTag("span", "class='binom'") << '\n'
805 << MTag("span") << cell(0) << ETag("span") << '\n'
806 << MTag("span") << cell(1) << ETag("span") << '\n'
807 << ETag("span") << '\n'
808 << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
812 void InsetMathBinom::validate(LaTeXFeatures & features) const
814 if (features.runparams().isLaTeX()) {
816 features.require("binom");
817 if (kind_ == DBINOM || kind_ == TBINOM)
818 features.require("amsmath");
819 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
820 features.addCSSSnippet(
821 "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
822 "span.binom span{display: block;}\n"
823 "span.binomdelim{font-size: 2em;}");
824 InsetMathNest::validate(features);