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 = (kind_ == UNITFRAC) ? mi.base.font.changeShape(UP_SHAPE)
187 Changer dummy2 = mi.base.changeScript();
188 cell(0).metrics(mi, dim0);
189 cell(1).metrics(mi, dim1);
190 dim.wid += dim0.wid + dim1.wid + 5;
191 dim.asc = max(dim2.asc, dim0.height() + 5);
192 dim.des = max(dim2.des, dim1.height() - 5);
205 // \tfrac is always in text size
206 (kind_ == TFRAC) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
207 // \cfrac and \dfrac are always in display size
209 || kind_ == CFRACLEFT
210 || kind_ == CFRACRIGHT
211 || kind_ == DFRAC) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
213 mi.base.changeFrac();
214 cell(0).metrics(mi, dim0);
215 cell(1).metrics(mi, dim1);
216 dim.wid = max(dim0.wid, dim1.wid) + 2;
217 dim.asc = dim0.height() + 2 + 5;
218 dim.des = dim1.height() + 2 - 5;
221 metricsMarkers(mi, dim);
225 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
227 setPosCache(pi, x, y);
228 Dimension const dim = dimension(*pi.base.bv);
229 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
232 // \unitone, \unittwo
235 // is there an extra cell holding the value being given a dimension?
236 // (this is \unittwo)
238 cell(0).draw(pi, x + 1, y);
242 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
243 cell(unit_cell).draw(pi, xx + 1, y);
249 // \unitfrac, \unitfracthree, \nicefrac
251 // is there an extra cell holding the value being given a dimension?
252 // (this is \unitfracthree)
253 if (kind_ == UNITFRAC && nargs() == 3) {
254 cell(2).draw(pi, x + 1, y);
255 xx += cell(2).dimension(*pi.base.bv).wid + 4;
257 Changer dummy = (kind_ == UNITFRAC) ? pi.base.font.changeShape(UP_SHAPE)
261 // * the solidus should be \kern-2mu/\kern-1mu.
262 // * the vertical offset of the first cell should be such that the
263 // top of M in the first cell matches the one of the
265 Changer dummy2 = pi.base.changeScript();
266 cell(0).draw(pi, xx + 2, y - dim0.des - 5);
267 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
268 cell(1).draw(pi, xx + dim0.wid + 5, y + dim1.asc / 2);
270 pi.pain.line(xx + dim0.wid + 1, y + dim.des - 2,
271 xx + dim0.wid + 6, y - dim.asc + 2,
272 pi.base.font.color());
285 // \tfrac is always in text size
286 (kind_ == TFRAC) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
287 // \cfrac and \dfrac are always in display size
289 || kind_ == CFRACLEFT
290 || kind_ == CFRACRIGHT
291 || kind_ == DFRAC) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
293 pi.base.changeFrac();
294 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
295 int m = x + dim.wid / 2;
298 (kind_ == CFRACLEFT) ? x + 2 :
300 (kind_ == CFRACRIGHT) ? x + dim.wid - dim0.wid - 2 :
303 // FIXME: vertical offset should be based on ex
304 //int dy = theFontMetrics(pi.base.font).ex() / 2;
305 cell(0).draw(pi, xx, y - dim0.des - 2 - 5);
307 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
310 pi.pain.line(x + 1, y - 5,
311 x + dim.wid - 2, y - 5, pi.base.font.color());
314 drawMarkers(pi, x, y);
318 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
320 Dimension dim0, dim1;
321 cell(0).metricsT(mi, dim0);
322 cell(1).metricsT(mi, dim1);
323 dim.wid = max(dim0.width(), dim1.wid);
324 dim.asc = dim0.height() + 1;
325 dim.des = dim1.height();
329 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
334 int m = x + dim.width() / 2;
335 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
336 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
337 // ASCII art: ignore niceties
338 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
339 pain.horizontalLine(x, y, dim.width());
344 void InsetMathFrac::write(WriteStream & os) const
346 MathEnsurer ensurer(os);
349 // \\atop is only for compatibility, \\binom is the
351 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
354 // \\over is only for compatibility, normalize this to \\frac
355 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
364 InsetMathNest::write(os);
366 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
370 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
372 os << "\\unit{" << cell(0) << '}';
375 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
378 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
384 docstring InsetMathFrac::name() const
388 return from_ascii("frac");
392 return from_ascii("cfrac");
394 return from_ascii("dfrac");
396 return from_ascii("tfrac");
398 return from_ascii("over");
400 return from_ascii("nicefrac");
402 return from_ascii("unitfrac");
404 return from_ascii("unit");
406 return from_ascii("atop");
408 // shut up stupid compiler
413 bool InsetMathFrac::extraBraces() const
415 return kind_ == ATOP || kind_ == OVER;
419 void InsetMathFrac::maple(MapleStream & os) const
422 // Someone who knows about maple should fix this.
423 LASSERT(false, return);
425 os << '(' << cell(0) << ")/(" << cell(1) << ')';
429 void InsetMathFrac::mathematica(MathematicaStream & os) const
432 // Someone who knows about mathematica should fix this.
433 LASSERT(false, return);
435 os << '(' << cell(0) << ")/(" << cell(1) << ')';
439 void InsetMathFrac::octave(OctaveStream & os) const
442 // Someone who knows about octave should fix this.
443 LASSERT(false, return);
445 os << '(' << cell(0) << ")/(" << cell(1) << ')';
449 void InsetMathFrac::mathmlize(MathStream & os) const
453 os << MTag("mfrac", "linethickeness='0'")
454 << MTag("mrow") << cell(0) << ETag("mrow")
455 << MTag("mrow") << cell(1) << ETag("mrow")
459 // we do not presently distinguish these
468 << MTag("mrow") << cell(0) << ETag("mrow")
469 << MTag("mrow") << cell(1) << ETag("mrow")
474 os << MTag("mfrac", "bevelled='true'")
475 << MTag("mrow") << cell(0) << ETag("mrow")
476 << MTag("mrow") << cell(1) << ETag("mrow")
483 os << MTag("mfrac", "bevelled='true'")
484 << MTag("mrow") << cell(0) << ETag("mrow")
485 << MTag("mrow") << cell(1) << ETag("mrow")
490 // FIXME This is not right, because we still output mi, etc,
491 // when we output the cell. So we need to prevent that somehow.
494 << MTag("mstyle mathvariant='normal'")
498 os << MTag("mstyle mathvariant='normal'")
505 void InsetMathFrac::htmlize(HtmlStream & os) const
509 os << MTag("span", "class='frac'")
510 << MTag("span", "class='numer'") << cell(0) << ETag("span")
511 << MTag("span", "class='numer'") << cell(1) << ETag("span")
515 // we do not presently distinguish these
523 os << MTag("span", "class='frac'")
524 << MTag("span", "class='numer'") << cell(0) << ETag("span")
525 << MTag("span", "class='denom'") << cell(1) << ETag("span")
530 os << cell(0) << '/' << cell(1);
535 os << cell(2) << ' ';
536 os << cell(0) << '/' << cell(1);
540 // FIXME This is not right, because we still output i, etc,
541 // when we output the cell. So we need to prevent that somehow.
555 void InsetMathFrac::validate(LaTeXFeatures & features) const
557 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
558 features.require("units");
559 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
560 || kind_ == DFRAC || kind_ == TFRAC)
561 features.require("amsmath");
562 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
563 // CSS adapted from eLyXer
564 features.addCSSSnippet(
565 "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
566 "span.numer{display: block;}\n"
567 "span.denom{display: block; border-top: thin solid #000040;}");
568 InsetMathNest::validate(features);
572 /////////////////////////////////////////////////////////////////////
576 /////////////////////////////////////////////////////////////////////
579 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
580 : InsetMathFracBase(buf), kind_(kind)
584 Inset * InsetMathBinom::clone() const
586 return new InsetMathBinom(*this);
590 int InsetMathBinom::dw(int height) const
601 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
603 Dimension dim0, dim1;
605 (kind_ == DBINOM) ? mi.base.font.changeStyle(LM_ST_DISPLAY) :
606 (kind_ == TBINOM) ? mi.base.font.changeStyle(LM_ST_SCRIPT) :
607 mi.base.changeFrac();
608 cell(0).metrics(mi, dim0);
609 cell(1).metrics(mi, dim1);
610 dim.asc = dim0.height() + 4 + 5;
611 dim.des = dim1.height() + 4 - 5;
612 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
613 metricsMarkers2(mi, dim);
617 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
619 Dimension const dim = dimension(*pi.base.bv);
620 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
621 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
622 // define the binom brackets
623 docstring const bra = kind_ == BRACE ? from_ascii("{") :
624 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
625 docstring const ket = kind_ == BRACE ? from_ascii("}") :
626 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
628 int m = x + dim.width() / 2;
631 (kind_ == DBINOM) ? pi.base.font.changeStyle(LM_ST_DISPLAY) :
632 (kind_ == TBINOM) ? pi.base.font.changeStyle(LM_ST_SCRIPT) :
633 pi.base.changeFrac();
634 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
635 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
637 // draw the brackets and the marker
638 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
640 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
641 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
642 drawMarkers2(pi, x, y);
646 bool InsetMathBinom::extraBraces() const
648 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
652 void InsetMathBinom::write(WriteStream & os) const
654 MathEnsurer ensurer(os);
657 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
660 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
663 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
666 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
669 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
672 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
678 void InsetMathBinom::normalize(NormalStream & os) const
680 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
684 void InsetMathBinom::mathmlize(MathStream & os) const
705 os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
706 << "<mfrac linethickness='0'>"
707 << cell(0) << cell(1)
709 << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
713 void InsetMathBinom::htmlize(HtmlStream & os) const
734 os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
735 << MTag("span", "class='binom'") << '\n'
736 << MTag("span") << cell(0) << ETag("span") << '\n'
737 << MTag("span") << cell(1) << ETag("span") << '\n'
738 << ETag("span") << '\n'
739 << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
743 void InsetMathBinom::validate(LaTeXFeatures & features) const
745 if (features.runparams().isLaTeX()) {
747 features.require("binom");
748 if (kind_ == DBINOM || kind_ == TBINOM)
749 features.require("amsmath");
750 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
751 features.addCSSSnippet(
752 "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
753 "span.binom span{display: block;}\n"
754 "span.binomdelim{font-size: 2em;}");
755 InsetMathNest::validate(features);