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/FontMetrics.h"
26 #include "frontends/Painter.h"
28 #include "support/lassert.h"
35 /////////////////////////////////////////////////////////////////////
39 /////////////////////////////////////////////////////////////////////
42 InsetMathFracBase::InsetMathFracBase(Buffer * buf, idx_type ncells)
43 : InsetMathNest(buf, ncells)
47 bool InsetMathFracBase::idxUpDown(Cursor & cur, bool up) const
49 // If we only have one cell, target = 0, otherwise
50 // target = up ? 0 : 1, since upper cell has idx 0
51 InsetMath::idx_type target = nargs() > 1 ? !up : 0;
52 if (cur.idx() == target)
55 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
61 /////////////////////////////////////////////////////////////////////
65 /////////////////////////////////////////////////////////////////////
68 InsetMathFrac::InsetMathFrac(Buffer * buf, Kind kind, InsetMath::idx_type ncells)
69 : InsetMathFracBase(buf, ncells), kind_(kind)
73 Inset * InsetMathFrac::clone() const
75 return new InsetMathFrac(*this);
79 InsetMathFrac * InsetMathFrac::asFracInset()
81 return kind_ == ATOP ? 0 : this;
85 InsetMathFrac const * InsetMathFrac::asFracInset() const
87 return kind_ == ATOP ? 0 : this;
91 bool InsetMathFrac::idxForward(Cursor & cur) const
93 InsetMath::idx_type target = 0;
94 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
97 else if (nargs() == 2)
101 if (cur.idx() == target)
104 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
109 bool InsetMathFrac::idxBackward(Cursor & cur) const
111 InsetMath::idx_type target = 0;
112 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
115 else if (nargs() == 2)
119 if (cur.idx() == target)
122 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
127 MathClass InsetMathFrac::mathClass() const
129 // Generalized fractions are of inner class (see The TeXbook, p. 292)
130 // But stuff from the unit/nicefrac packages are not real fractions.
131 MathClass mc = MC_ORD;
155 // align frac to minus character
156 int dy_for_frac(MetricsBase & mb)
158 Changer dummy = mb.changeFontSet("mathnormal");
159 return theFontMetrics(mb.font).ascent('-') - 1;
163 // align the top of M in the cell with the top of M in the surrounding font
164 int dy_for_nicefrac(MetricsBase & mb)
166 // this is according to nicefrac.sty
167 int big_m = theFontMetrics(mb.font).ascent('M');
168 Changer dummy = mb.changeScript();
169 int small_m = theFontMetrics(mb.font).ascent('M');
170 return big_m - small_m;
177 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
179 Dimension dim0, dim1, dim2;
183 // \unitone, \unittwo
186 // is there an extra cell holding the value being given a dimension?
187 // (this is \unittwo)
189 cell(0).metrics(mi, dim1);
190 dim.wid += dim1.wid + 4;
193 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
194 cell(unit_cell).metrics(mi, dim0);
195 dim.wid += dim0.width() + 1;
196 dim.asc = max(dim0.asc, dim1.asc);
197 dim.des = max(dim0.des, dim1.des);
203 // \unitfrac, \unitfracthree, \nicefrac
205 int const dy = dy_for_nicefrac(mi.base);
206 // is there an extra cell holding the value being given a dimension?
207 // (this is \unitfracthree)
208 if (kind_ == UNITFRAC && nargs() == 3) {
209 cell(2).metrics(mi, dim2);
210 dim.wid += dim2.wid + 4;
212 Changer dummy = (kind_ == UNITFRAC) ? mi.base.font.changeShape(UP_SHAPE)
214 Changer dummy2 = mi.base.changeScript();
215 cell(0).metrics(mi, dim0);
216 cell(1).metrics(mi, dim1);
217 dim.wid += dim0.wid + dim1.wid + 5;
218 dim.asc = max(max(dim2.asc, dim0.asc + dy), dim1.asc);
219 dim.des = max(max(dim2.des, dim0.des - dy), dim1.des);
231 int const dy = dy_for_frac(mi.base);
233 // \tfrac is always in text size
234 (kind_ == TFRAC) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
235 // \cfrac and \dfrac are always in display size
237 || kind_ == CFRACLEFT
238 || kind_ == CFRACRIGHT
239 || kind_ == DFRAC) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
241 mi.base.changeFrac();
242 cell(0).metrics(mi, dim0);
243 cell(1).metrics(mi, dim1);
244 dim.wid = max(dim0.wid, dim1.wid) + 2;
245 dim.asc = dim0.height() + dy/2 + dy;
246 dim.des = max(0, dim1.height() + dy/2 - dy);
249 metricsMarkers(mi, dim);
253 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
255 setPosCache(pi, x, y);
256 Dimension const dim = dimension(*pi.base.bv);
257 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
260 // \unitone, \unittwo
263 // is there an extra cell holding the value being given a dimension?
264 // (this is \unittwo)
266 cell(0).draw(pi, x + 1, y);
270 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
271 cell(unit_cell).draw(pi, xx + 1, y);
277 // \unitfrac, \unitfracthree, \nicefrac
279 int const dy = dy_for_nicefrac(pi.base);
280 // is there an extra cell holding the value being given a dimension?
281 // (this is \unitfracthree)
282 if (kind_ == UNITFRAC && nargs() == 3) {
283 cell(2).draw(pi, x + 1, y);
284 xx += cell(2).dimension(*pi.base.bv).wid + 4;
286 Changer dummy = (kind_ == UNITFRAC) ? pi.base.font.changeShape(UP_SHAPE)
290 // * the solidus should be \kern-2mu/\kern-1mu.
291 Changer dummy2 = pi.base.changeScript();
292 cell(0).draw(pi, xx + 2, y - dy);
293 cell(1).draw(pi, xx + dim0.wid + 5, y);
295 pi.pain.line(xx + dim0.wid + 1, y + dim.des - 2,
296 xx + dim0.wid + 6, y - dim.asc + 2,
297 pi.base.font.color(), pi.pain.line_solid,
298 pi.base.solidLineThickness());
310 int const dy = dy_for_frac(pi.base);
312 // \tfrac is always in text size
313 (kind_ == TFRAC) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
314 // \cfrac and \dfrac are always in display size
316 || kind_ == CFRACLEFT
317 || kind_ == CFRACRIGHT
318 || kind_ == DFRAC) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
320 pi.base.changeFrac();
321 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
322 int m = x + dim.wid / 2;
325 (kind_ == CFRACLEFT) ? x + 2 :
327 (kind_ == CFRACRIGHT) ? x + dim.wid - dim0.wid - 2 :
330 // take dy/2 for the spacing around the horizontal line. This is
331 // arbitrary. In LaTeX it is more complicated to ensure that displayed
332 // fractions line up next to each other.
333 cell(0).draw(pi, xx, y - dim0.des - dy/2 - dy);
335 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + dy/2 - dy);
338 pi.pain.line(x + 1, y - dy,
339 x + dim.wid - 2, y - dy,
340 pi.base.font.color(), pi.pain.line_solid,
341 pi.base.solidLineThickness());
344 drawMarkers(pi, x, y);
348 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
350 Dimension dim0, dim1;
351 cell(0).metricsT(mi, dim0);
352 cell(1).metricsT(mi, dim1);
353 dim.wid = max(dim0.width(), dim1.wid);
354 dim.asc = dim0.height() + 1;
355 dim.des = dim1.height();
359 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
364 int m = x + dim.width() / 2;
365 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
366 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
367 // ASCII art: ignore niceties
368 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
369 pain.horizontalLine(x, y, dim.width());
374 void InsetMathFrac::write(WriteStream & os) const
376 MathEnsurer ensurer(os);
379 // \\atop is only for compatibility, \\binom is the
381 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
384 // \\over is only for compatibility, normalize this to \\frac
385 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
394 InsetMathNest::write(os);
396 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
400 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
402 os << "\\unit{" << cell(0) << '}';
405 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
408 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
414 docstring InsetMathFrac::name() const
418 return from_ascii("frac");
422 return from_ascii("cfrac");
424 return from_ascii("dfrac");
426 return from_ascii("tfrac");
428 return from_ascii("over");
430 return from_ascii("nicefrac");
432 return from_ascii("unitfrac");
434 return from_ascii("unit");
436 return from_ascii("atop");
438 // shut up stupid compiler
443 bool InsetMathFrac::extraBraces() const
445 return kind_ == ATOP || kind_ == OVER;
449 void InsetMathFrac::maple(MapleStream & os) const
452 // Someone who knows about maple should fix this.
453 LASSERT(false, return);
455 os << '(' << cell(0) << ")/(" << cell(1) << ')';
459 void InsetMathFrac::mathematica(MathematicaStream & os) const
462 // Someone who knows about mathematica should fix this.
463 LASSERT(false, return);
465 os << '(' << cell(0) << ")/(" << cell(1) << ')';
469 void InsetMathFrac::octave(OctaveStream & os) const
472 // Someone who knows about octave should fix this.
473 LASSERT(false, return);
475 os << '(' << cell(0) << ")/(" << cell(1) << ')';
479 void InsetMathFrac::mathmlize(MathStream & os) const
483 os << MTag("mfrac", "linethickeness='0'")
484 << MTag("mrow") << cell(0) << ETag("mrow")
485 << MTag("mrow") << cell(1) << ETag("mrow")
489 // we do not presently distinguish these
498 << MTag("mrow") << cell(0) << ETag("mrow")
499 << MTag("mrow") << cell(1) << ETag("mrow")
504 os << MTag("mfrac", "bevelled='true'")
505 << MTag("mrow") << cell(0) << ETag("mrow")
506 << MTag("mrow") << cell(1) << ETag("mrow")
513 os << MTag("mfrac", "bevelled='true'")
514 << MTag("mrow") << cell(0) << ETag("mrow")
515 << MTag("mrow") << cell(1) << ETag("mrow")
520 // FIXME This is not right, because we still output mi, etc,
521 // when we output the cell. So we need to prevent that somehow.
524 << MTag("mstyle mathvariant='normal'")
528 os << MTag("mstyle mathvariant='normal'")
535 void InsetMathFrac::htmlize(HtmlStream & os) const
539 os << MTag("span", "class='frac'")
540 << MTag("span", "class='numer'") << cell(0) << ETag("span")
541 << MTag("span", "class='numer'") << cell(1) << ETag("span")
545 // we do not presently distinguish these
553 os << MTag("span", "class='frac'")
554 << MTag("span", "class='numer'") << cell(0) << ETag("span")
555 << MTag("span", "class='denom'") << cell(1) << ETag("span")
560 os << cell(0) << '/' << cell(1);
565 os << cell(2) << ' ';
566 os << cell(0) << '/' << cell(1);
570 // FIXME This is not right, because we still output i, etc,
571 // when we output the cell. So we need to prevent that somehow.
585 void InsetMathFrac::validate(LaTeXFeatures & features) const
587 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
588 features.require("units");
589 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
590 || kind_ == DFRAC || kind_ == TFRAC)
591 features.require("amsmath");
592 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
593 // CSS adapted from eLyXer
594 features.addCSSSnippet(
595 "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
596 "span.numer{display: block;}\n"
597 "span.denom{display: block; border-top: thin solid #000040;}");
598 InsetMathNest::validate(features);
602 /////////////////////////////////////////////////////////////////////
606 /////////////////////////////////////////////////////////////////////
609 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
610 : InsetMathFracBase(buf), kind_(kind)
614 Inset * InsetMathBinom::clone() const
616 return new InsetMathBinom(*this);
620 int InsetMathBinom::dw(int height) const
631 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
633 Dimension dim0, dim1;
634 int const dy = dy_for_frac(mi.base);
636 (kind_ == DBINOM) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
637 (kind_ == TBINOM) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
638 mi.base.changeFrac();
639 cell(0).metrics(mi, dim0);
640 cell(1).metrics(mi, dim1);
641 dim.asc = dim0.height() + 1 + dy/2 + dy;
642 dim.des = max(0, dim1.height() + 1 + dy/2 - dy);
643 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
644 metricsMarkers2(mi, dim);
648 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
650 Dimension const dim = dimension(*pi.base.bv);
651 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
652 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
653 int const dy = dy_for_frac(pi.base);
654 // define the binom brackets
655 docstring const bra = kind_ == BRACE ? from_ascii("{") :
656 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
657 docstring const ket = kind_ == BRACE ? from_ascii("}") :
658 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
660 int m = x + dim.width() / 2;
663 (kind_ == DBINOM) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
664 (kind_ == TBINOM) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
665 pi.base.changeFrac();
666 // take dy both for the vertical alignment and for the spacing between
668 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - dy/2 - dy);
669 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + dy/2 - dy);
671 // draw the brackets and the marker
672 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
674 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
675 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
676 drawMarkers2(pi, x, y);
680 bool InsetMathBinom::extraBraces() const
682 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
686 void InsetMathBinom::write(WriteStream & os) const
688 MathEnsurer ensurer(os);
691 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
694 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
697 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
700 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
703 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
706 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
712 void InsetMathBinom::normalize(NormalStream & os) const
714 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
718 void InsetMathBinom::mathmlize(MathStream & os) const
739 os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
740 << "<mfrac linethickness='0'>"
741 << cell(0) << cell(1)
743 << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
747 void InsetMathBinom::htmlize(HtmlStream & os) const
768 os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
769 << MTag("span", "class='binom'") << '\n'
770 << MTag("span") << cell(0) << ETag("span") << '\n'
771 << MTag("span") << cell(1) << ETag("span") << '\n'
772 << ETag("span") << '\n'
773 << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
777 void InsetMathBinom::validate(LaTeXFeatures & features) const
779 if (features.runparams().isLaTeX()) {
781 features.require("binom");
782 if (kind_ == DBINOM || kind_ == TBINOM)
783 features.require("amsmath");
784 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
785 features.addCSSSnippet(
786 "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
787 "span.binom span{display: block;}\n"
788 "span.binomdelim{font-size: 2em;}");
789 InsetMathNest::validate(features);