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() + 2 + dy;
246 dim.des = max(0, dim1.height() + 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());
309 int const dy = dy_for_frac(pi.base);
311 // \tfrac is always in text size
312 (kind_ == TFRAC) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
313 // \cfrac and \dfrac are always in display size
315 || kind_ == CFRACLEFT
316 || kind_ == CFRACRIGHT
317 || kind_ == DFRAC) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
319 pi.base.changeFrac();
320 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
321 int m = x + dim.wid / 2;
324 (kind_ == CFRACLEFT) ? x + 2 :
326 (kind_ == CFRACRIGHT) ? x + dim.wid - dim0.wid - 2 :
329 cell(0).draw(pi, xx, y - dim0.des - 2 - dy);
331 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - dy);
334 pi.pain.line(x + 1, y - dy,
335 x + dim.wid - 2, y - dy, pi.base.font.color());
338 drawMarkers(pi, x, y);
342 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
344 Dimension dim0, dim1;
345 cell(0).metricsT(mi, dim0);
346 cell(1).metricsT(mi, dim1);
347 dim.wid = max(dim0.width(), dim1.wid);
348 dim.asc = dim0.height() + 1;
349 dim.des = dim1.height();
353 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
358 int m = x + dim.width() / 2;
359 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
360 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
361 // ASCII art: ignore niceties
362 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
363 pain.horizontalLine(x, y, dim.width());
368 void InsetMathFrac::write(WriteStream & os) const
370 MathEnsurer ensurer(os);
373 // \\atop is only for compatibility, \\binom is the
375 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
378 // \\over is only for compatibility, normalize this to \\frac
379 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
388 InsetMathNest::write(os);
390 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
394 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
396 os << "\\unit{" << cell(0) << '}';
399 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
402 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
408 docstring InsetMathFrac::name() const
412 return from_ascii("frac");
416 return from_ascii("cfrac");
418 return from_ascii("dfrac");
420 return from_ascii("tfrac");
422 return from_ascii("over");
424 return from_ascii("nicefrac");
426 return from_ascii("unitfrac");
428 return from_ascii("unit");
430 return from_ascii("atop");
432 // shut up stupid compiler
437 bool InsetMathFrac::extraBraces() const
439 return kind_ == ATOP || kind_ == OVER;
443 void InsetMathFrac::maple(MapleStream & os) const
446 // Someone who knows about maple should fix this.
447 LASSERT(false, return);
449 os << '(' << cell(0) << ")/(" << cell(1) << ')';
453 void InsetMathFrac::mathematica(MathematicaStream & os) const
456 // Someone who knows about mathematica should fix this.
457 LASSERT(false, return);
459 os << '(' << cell(0) << ")/(" << cell(1) << ')';
463 void InsetMathFrac::octave(OctaveStream & os) const
466 // Someone who knows about octave should fix this.
467 LASSERT(false, return);
469 os << '(' << cell(0) << ")/(" << cell(1) << ')';
473 void InsetMathFrac::mathmlize(MathStream & os) const
477 os << MTag("mfrac", "linethickeness='0'")
478 << MTag("mrow") << cell(0) << ETag("mrow")
479 << MTag("mrow") << cell(1) << ETag("mrow")
483 // we do not presently distinguish these
492 << MTag("mrow") << cell(0) << ETag("mrow")
493 << MTag("mrow") << cell(1) << ETag("mrow")
498 os << MTag("mfrac", "bevelled='true'")
499 << MTag("mrow") << cell(0) << ETag("mrow")
500 << MTag("mrow") << cell(1) << ETag("mrow")
507 os << MTag("mfrac", "bevelled='true'")
508 << MTag("mrow") << cell(0) << ETag("mrow")
509 << MTag("mrow") << cell(1) << ETag("mrow")
514 // FIXME This is not right, because we still output mi, etc,
515 // when we output the cell. So we need to prevent that somehow.
518 << MTag("mstyle mathvariant='normal'")
522 os << MTag("mstyle mathvariant='normal'")
529 void InsetMathFrac::htmlize(HtmlStream & os) const
533 os << MTag("span", "class='frac'")
534 << MTag("span", "class='numer'") << cell(0) << ETag("span")
535 << MTag("span", "class='numer'") << cell(1) << ETag("span")
539 // we do not presently distinguish these
547 os << MTag("span", "class='frac'")
548 << MTag("span", "class='numer'") << cell(0) << ETag("span")
549 << MTag("span", "class='denom'") << cell(1) << ETag("span")
554 os << cell(0) << '/' << cell(1);
559 os << cell(2) << ' ';
560 os << cell(0) << '/' << cell(1);
564 // FIXME This is not right, because we still output i, etc,
565 // when we output the cell. So we need to prevent that somehow.
579 void InsetMathFrac::validate(LaTeXFeatures & features) const
581 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
582 features.require("units");
583 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
584 || kind_ == DFRAC || kind_ == TFRAC)
585 features.require("amsmath");
586 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
587 // CSS adapted from eLyXer
588 features.addCSSSnippet(
589 "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
590 "span.numer{display: block;}\n"
591 "span.denom{display: block; border-top: thin solid #000040;}");
592 InsetMathNest::validate(features);
596 /////////////////////////////////////////////////////////////////////
600 /////////////////////////////////////////////////////////////////////
603 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
604 : InsetMathFracBase(buf), kind_(kind)
608 Inset * InsetMathBinom::clone() const
610 return new InsetMathBinom(*this);
614 int InsetMathBinom::dw(int height) const
625 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
627 Dimension dim0, dim1;
628 int const dy = dy_for_frac(mi.base);
630 (kind_ == DBINOM) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
631 (kind_ == TBINOM) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
632 mi.base.changeFrac();
633 cell(0).metrics(mi, dim0);
634 cell(1).metrics(mi, dim1);
635 dim.asc = dim0.height() + 4 + dy;
636 dim.des = max(0, dim1.height() + 4 - dy);
637 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
638 metricsMarkers2(mi, dim);
642 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
644 Dimension const dim = dimension(*pi.base.bv);
645 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
646 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
647 int const dy = dy_for_frac(pi.base);
648 // define the binom brackets
649 docstring const bra = kind_ == BRACE ? from_ascii("{") :
650 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
651 docstring const ket = kind_ == BRACE ? from_ascii("}") :
652 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
654 int m = x + dim.width() / 2;
657 (kind_ == DBINOM) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
658 (kind_ == TBINOM) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
659 pi.base.changeFrac();
660 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - dy);
661 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - dy);
663 // draw the brackets and the marker
664 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
666 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
667 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
668 drawMarkers2(pi, x, y);
672 bool InsetMathBinom::extraBraces() const
674 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
678 void InsetMathBinom::write(WriteStream & os) const
680 MathEnsurer ensurer(os);
683 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
686 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
689 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
692 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
695 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
698 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
704 void InsetMathBinom::normalize(NormalStream & os) const
706 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
710 void InsetMathBinom::mathmlize(MathStream & os) const
731 os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
732 << "<mfrac linethickness='0'>"
733 << cell(0) << cell(1)
735 << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
739 void InsetMathBinom::htmlize(HtmlStream & os) const
760 os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
761 << MTag("span", "class='binom'") << '\n'
762 << MTag("span") << cell(0) << ETag("span") << '\n'
763 << MTag("span") << cell(1) << ETag("span") << '\n'
764 << ETag("span") << '\n'
765 << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
769 void InsetMathBinom::validate(LaTeXFeatures & features) const
771 if (features.runparams().isLaTeX()) {
773 features.require("binom");
774 if (kind_ == DBINOM || kind_ == TBINOM)
775 features.require("amsmath");
776 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
777 features.addCSSSnippet(
778 "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
779 "span.binom span{display: block;}\n"
780 "span.binomdelim{font-size: 2em;}");
781 InsetMathNest::validate(features);