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;
157 // \unitone, \unittwo
160 // is there an extra cell holding the value being given a dimension?
161 // (this is \unittwo)
163 cell(0).metrics(mi, dim1);
164 dim.wid += dim1.wid + 4;
167 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
168 cell(unit_cell).metrics(mi, dim0);
169 dim.wid += dim0.width() + 1;
170 dim.asc = max(dim0.asc, dim1.asc);
171 dim.des = max(dim0.des, dim1.des);
177 // \unitfrac, \unitfracthree, \nicefrac
179 // is there an extra cell holding the value being given a dimension?
180 // (this is \unitfracthree)
181 if (kind_ == UNITFRAC && nargs() == 3) {
182 cell(2).metrics(mi, dim2);
183 dim.wid += dim2.wid + 4;
185 Changer dummy = mi.base.font.changeShape(UP_SHAPE, kind_ == UNITFRAC);
186 Changer dummy2 = mi.base.changeScript();
187 cell(0).metrics(mi, dim0);
188 cell(1).metrics(mi, dim1);
189 dim.wid += dim0.wid + dim1.wid + 5;
190 dim.asc = max(dim2.asc, dim0.height() + 5);
191 dim.des = max(dim2.des, dim1.height() - 5);
204 // \tfrac is always in text size
205 (kind_ == TFRAC) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
206 // \cfrac and \dfrac are always in display size
208 || kind_ == CFRACLEFT
209 || kind_ == CFRACRIGHT
210 || kind_ == DFRAC) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
212 mi.base.changeFrac();
213 cell(0).metrics(mi, dim0);
214 cell(1).metrics(mi, dim1);
215 dim.wid = max(dim0.wid, dim1.wid) + 2;
216 dim.asc = dim0.height() + 2 + 5;
217 dim.des = dim1.height() + 2 - 5;
220 metricsMarkers(mi, dim);
224 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
226 setPosCache(pi, x, y);
227 Dimension const dim = dimension(*pi.base.bv);
228 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
231 // \unitone, \unittwo
234 // is there an extra cell holding the value being given a dimension?
235 // (this is \unittwo)
237 cell(0).draw(pi, x + 1, y);
241 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
242 cell(unit_cell).draw(pi, xx + 1, y);
248 // \unitfrac, \unitfracthree, \nicefrac
250 // is there an extra cell holding the value being given a dimension?
251 // (this is \unitfracthree)
252 if (kind_ == UNITFRAC && nargs() == 3) {
253 cell(2).draw(pi, x + 1, y);
254 xx += cell(2).dimension(*pi.base.bv).wid + 4;
256 Changer dummy = pi.base.font.changeShape(UP_SHAPE, kind_ == UNITFRAC);
259 // * the solidus should be \kern-2mu/\kern-1mu.
260 // * the vertical offset of the first cell should be such that the
261 // top of M in the first cell matches the one of the
263 Changer dummy2 = pi.base.changeScript();
264 cell(0).draw(pi, xx + 2, y - dim0.des - 5);
265 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
266 cell(1).draw(pi, xx + dim0.wid + 5, y + dim1.asc / 2);
268 pi.pain.line(xx + dim0.wid + 1, y + dim.des - 2,
269 xx + dim0.wid + 6, y - dim.asc + 2,
270 pi.base.font.color());
283 // \tfrac is always in text size
284 (kind_ == TFRAC) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
285 // \cfrac and \dfrac are always in display size
287 || kind_ == CFRACLEFT
288 || kind_ == CFRACRIGHT
289 || kind_ == DFRAC) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
291 pi.base.changeFrac();
292 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
293 int m = x + dim.wid / 2;
296 (kind_ == CFRACLEFT) ? x + 2 :
298 (kind_ == CFRACRIGHT) ? x + dim.wid - dim0.wid - 2 :
301 // FIXME: vertical offset should be based on ex
302 //int dy = theFontMetrics(pi.base.font).ex() / 2;
303 cell(0).draw(pi, xx, y - dim0.des - 2 - 5);
305 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
308 pi.pain.line(x + 1, y - 5,
309 x + dim.wid - 2, y - 5, pi.base.font.color());
312 drawMarkers(pi, x, y);
316 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
318 Dimension dim0, dim1;
319 cell(0).metricsT(mi, dim0);
320 cell(1).metricsT(mi, dim1);
321 dim.wid = max(dim0.width(), dim1.wid);
322 dim.asc = dim0.height() + 1;
323 dim.des = dim1.height();
327 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
332 int m = x + dim.width() / 2;
333 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
334 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
335 // ASCII art: ignore niceties
336 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
337 pain.horizontalLine(x, y, dim.width());
342 void InsetMathFrac::write(WriteStream & os) const
344 MathEnsurer ensurer(os);
347 // \\atop is only for compatibility, \\binom is the
349 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
352 // \\over is only for compatibility, normalize this to \\frac
353 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
362 InsetMathNest::write(os);
364 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
368 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
370 os << "\\unit{" << cell(0) << '}';
373 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
376 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
382 docstring InsetMathFrac::name() const
386 return from_ascii("frac");
390 return from_ascii("cfrac");
392 return from_ascii("dfrac");
394 return from_ascii("tfrac");
396 return from_ascii("over");
398 return from_ascii("nicefrac");
400 return from_ascii("unitfrac");
402 return from_ascii("unit");
404 return from_ascii("atop");
406 // shut up stupid compiler
411 bool InsetMathFrac::extraBraces() const
413 return kind_ == ATOP || kind_ == OVER;
417 void InsetMathFrac::maple(MapleStream & os) const
420 // Someone who knows about maple should fix this.
421 LASSERT(false, return);
423 os << '(' << cell(0) << ")/(" << cell(1) << ')';
427 void InsetMathFrac::mathematica(MathematicaStream & os) const
430 // Someone who knows about mathematica should fix this.
431 LASSERT(false, return);
433 os << '(' << cell(0) << ")/(" << cell(1) << ')';
437 void InsetMathFrac::octave(OctaveStream & os) const
440 // Someone who knows about octave should fix this.
441 LASSERT(false, return);
443 os << '(' << cell(0) << ")/(" << cell(1) << ')';
447 void InsetMathFrac::mathmlize(MathStream & os) const
451 os << MTag("mfrac", "linethickeness='0'")
452 << MTag("mrow") << cell(0) << ETag("mrow")
453 << MTag("mrow") << cell(1) << ETag("mrow")
457 // we do not presently distinguish these
466 << MTag("mrow") << cell(0) << ETag("mrow")
467 << MTag("mrow") << cell(1) << ETag("mrow")
472 os << MTag("mfrac", "bevelled='true'")
473 << MTag("mrow") << cell(0) << ETag("mrow")
474 << MTag("mrow") << cell(1) << ETag("mrow")
481 os << MTag("mfrac", "bevelled='true'")
482 << MTag("mrow") << cell(0) << ETag("mrow")
483 << MTag("mrow") << cell(1) << ETag("mrow")
488 // FIXME This is not right, because we still output mi, etc,
489 // when we output the cell. So we need to prevent that somehow.
492 << MTag("mstyle mathvariant='normal'")
496 os << MTag("mstyle mathvariant='normal'")
503 void InsetMathFrac::htmlize(HtmlStream & os) const
507 os << MTag("span", "class='frac'")
508 << MTag("span", "class='numer'") << cell(0) << ETag("span")
509 << MTag("span", "class='numer'") << cell(1) << ETag("span")
513 // we do not presently distinguish these
521 os << MTag("span", "class='frac'")
522 << MTag("span", "class='numer'") << cell(0) << ETag("span")
523 << MTag("span", "class='denom'") << cell(1) << ETag("span")
528 os << cell(0) << '/' << cell(1);
533 os << cell(2) << ' ';
534 os << cell(0) << '/' << cell(1);
538 // FIXME This is not right, because we still output i, etc,
539 // when we output the cell. So we need to prevent that somehow.
553 void InsetMathFrac::validate(LaTeXFeatures & features) const
555 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
556 features.require("units");
557 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
558 || kind_ == DFRAC || kind_ == TFRAC)
559 features.require("amsmath");
560 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
561 // CSS adapted from eLyXer
562 features.addCSSSnippet(
563 "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
564 "span.numer{display: block;}\n"
565 "span.denom{display: block; border-top: thin solid #000040;}");
566 InsetMathNest::validate(features);
570 /////////////////////////////////////////////////////////////////////
574 /////////////////////////////////////////////////////////////////////
577 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
578 : InsetMathFracBase(buf), kind_(kind)
582 Inset * InsetMathBinom::clone() const
584 return new InsetMathBinom(*this);
588 int InsetMathBinom::dw(int height) const
599 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
601 Dimension dim0, dim1;
603 (kind_ == DBINOM) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
604 (kind_ == TBINOM) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
605 mi.base.changeFrac();
606 cell(0).metrics(mi, dim0);
607 cell(1).metrics(mi, dim1);
608 dim.asc = dim0.height() + 4 + 5;
609 dim.des = dim1.height() + 4 - 5;
610 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
611 metricsMarkers2(mi, dim);
615 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
617 Dimension const dim = dimension(*pi.base.bv);
618 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
619 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
620 // define the binom brackets
621 docstring const bra = kind_ == BRACE ? from_ascii("{") :
622 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
623 docstring const ket = kind_ == BRACE ? from_ascii("}") :
624 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
626 int m = x + dim.width() / 2;
629 (kind_ == DBINOM) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
630 (kind_ == TBINOM) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
631 pi.base.changeFrac();
632 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
633 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
635 // draw the brackets and the marker
636 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
638 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
639 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
640 drawMarkers2(pi, x, y);
644 bool InsetMathBinom::extraBraces() const
646 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
650 void InsetMathBinom::write(WriteStream & os) const
652 MathEnsurer ensurer(os);
655 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
658 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
661 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
664 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
667 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
670 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
676 void InsetMathBinom::normalize(NormalStream & os) const
678 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
682 void InsetMathBinom::mathmlize(MathStream & os) const
703 os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
704 << "<mfrac linethickness='0'>"
705 << cell(0) << cell(1)
707 << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
711 void InsetMathBinom::htmlize(HtmlStream & os) const
732 os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
733 << MTag("span", "class='binom'") << '\n'
734 << MTag("span") << cell(0) << ETag("span") << '\n'
735 << MTag("span") << cell(1) << ETag("span") << '\n'
736 << ETag("span") << '\n'
737 << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
741 void InsetMathBinom::validate(LaTeXFeatures & features) const
743 if (features.runparams().isLaTeX()) {
745 features.require("binom");
746 if (kind_ == DBINOM || kind_ == TBINOM)
747 features.require("amsmath");
748 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
749 features.addCSSSnippet(
750 "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
751 "span.binom span{display: block;}\n"
752 "span.binomdelim{font-size: 2em;}");
753 InsetMathNest::validate(features);