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;
155 // This could be simplified, including avoiding useless recalculation of
157 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
159 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
160 cell(0).metrics(mi, dim0);
161 dim.wid = dim0.width()+ 3;
164 } else if (nargs() == 2) {
165 cell(0).metrics(mi, dim0);
166 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
167 cell(1).metrics(mi, dim1);
168 dim.wid = dim0.width() + dim1.wid + 5;
169 dim.asc = max(dim0.asc, dim1.asc);
170 dim.des = max(dim0.des, dim1.des);
172 cell(2).metrics(mi, dim2);
173 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
174 Changer dummy2 = mi.base.changeFrac();
175 cell(0).metrics(mi, dim0);
176 cell(1).metrics(mi, dim1);
177 dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
178 dim.asc = max(dim2.asc, dim0.height() + 5);
179 dim.des = max(dim2.des, dim1.height() - 5);
182 // general cell metrics used for \frac
183 Changer dummy = mi.base.changeFrac();
184 cell(0).metrics(mi, dim0);
185 cell(1).metrics(mi, dim1);
187 cell(2).metrics(mi, dim2);
188 // metrics for special fraction types
189 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
190 Changer dummy2 = mi.base.font.changeShape(UP_SHAPE, kind_ == UNITFRAC);
191 dim.wid = dim0.width() + dim1.wid + 5;
192 dim.asc = dim0.height() + 5;
193 dim.des = dim1.height() - 5;
195 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
196 || kind_ == DFRAC || kind_ == TFRAC) {
197 // \cfrac and \dfrac are always in display size
198 // \tfrac is in always in text size
199 Changer dummy2 = mi.base.changeStyle((kind_ == TFRAC)
202 cell(0).metrics(mi, dim0);
203 cell(1).metrics(mi, dim1);
205 dim.wid = max(dim0.wid, dim1.wid) + 2;
206 dim.asc = dim0.height() + 2 + 5;
207 dim.des = dim1.height() + 2 - 5;
214 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
216 setPosCache(pi, x, y);
217 Dimension const dim = dimension(*pi.base.bv);
218 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
219 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
221 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
222 cell(0).draw(pi, x + 1, y);
223 } else if (nargs() == 2) {
224 cell(0).draw(pi, x + 1, y);
225 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
226 cell(1).draw(pi, x + dim0.width() + 5, y);
228 cell(2).draw(pi, x + 1, y);
229 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
230 Changer dummy2 = pi.base.changeFrac();
231 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
232 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
233 int xx = x + dim2.wid + 5;
234 cell(0).draw(pi, xx + 2,
236 cell(1).draw(pi, xx + dim0.width() + 5,
240 Changer dummy = pi.base.changeFrac();
241 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
242 int m = x + dim.wid / 2;
243 if (kind_ == NICEFRAC) {
244 cell(0).draw(pi, x + 2,
246 cell(1).draw(pi, x + dim0.width() + 5,
248 } else if (kind_ == UNITFRAC) {
249 Changer dummy2 = pi.base.font.changeShape(UP_SHAPE);
250 cell(0).draw(pi, x + 2, y - dim0.des - 5);
251 cell(1).draw(pi, x + dim0.width() + 5, y + dim1.asc / 2);
252 } else if (kind_ == FRAC || kind_ == ATOP || kind_ == OVER
254 // tfrac is in always in text size
255 Changer dummy2 = pi.base.changeStyle(LM_ST_SCRIPT, kind_ == TFRAC);
256 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
257 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
259 // \cfrac and \dfrac are always in display size
260 Changer dummy2 = pi.base.changeStyle(LM_ST_DISPLAY);
261 if (kind_ == CFRAC || kind_ == DFRAC)
262 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
263 else if (kind_ == CFRACLEFT)
264 cell(0).draw(pi, x + 2, y - dim0.des - 2 - 5);
265 else if (kind_ == CFRACRIGHT)
266 cell(0).draw(pi, x + dim.wid - dim0.wid - 2,
267 y - dim0.des - 2 - 5);
268 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
271 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
275 xx += cell(2).dimension(*pi.base.bv).wid + 5;
276 pi.pain.line(xx + dim0.wid,
279 y - dim.asc + 2, pi.base.font.color());
281 if (kind_ == FRAC || kind_ == CFRAC || kind_ == CFRACLEFT
282 || kind_ == CFRACRIGHT || kind_ == DFRAC
283 || kind_ == TFRAC || kind_ == OVER)
284 pi.pain.line(x + 1, y - 5,
285 x + dim.wid - 2, y - 5, pi.base.font.color());
286 drawMarkers(pi, x, y);
290 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
292 Dimension dim0, dim1;
293 cell(0).metricsT(mi, dim0);
294 cell(1).metricsT(mi, dim1);
295 dim.wid = max(dim0.width(), dim1.wid);
296 dim.asc = dim0.height() + 1;
297 dim.des = dim1.height();
301 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
306 int m = x + dim.width() / 2;
307 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
308 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
309 // ASCII art: ignore niceties
310 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
311 pain.horizontalLine(x, y, dim.width());
316 void InsetMathFrac::write(WriteStream & os) const
318 MathEnsurer ensurer(os);
321 // \\atop is only for compatibility, \\binom is the
323 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
326 // \\over is only for compatibility, normalize this to \\frac
327 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
336 InsetMathNest::write(os);
338 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
342 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
344 os << "\\unit{" << cell(0) << '}';
347 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
350 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
356 docstring InsetMathFrac::name() const
360 return from_ascii("frac");
364 return from_ascii("cfrac");
366 return from_ascii("dfrac");
368 return from_ascii("tfrac");
370 return from_ascii("over");
372 return from_ascii("nicefrac");
374 return from_ascii("unitfrac");
376 return from_ascii("unit");
378 return from_ascii("atop");
380 // shut up stupid compiler
385 bool InsetMathFrac::extraBraces() const
387 return kind_ == ATOP || kind_ == OVER;
391 void InsetMathFrac::maple(MapleStream & os) const
394 // Someone who knows about maple should fix this.
395 LASSERT(false, return);
397 os << '(' << cell(0) << ")/(" << cell(1) << ')';
401 void InsetMathFrac::mathematica(MathematicaStream & os) const
404 // Someone who knows about mathematica should fix this.
405 LASSERT(false, return);
407 os << '(' << cell(0) << ")/(" << cell(1) << ')';
411 void InsetMathFrac::octave(OctaveStream & os) const
414 // Someone who knows about octave should fix this.
415 LASSERT(false, return);
417 os << '(' << cell(0) << ")/(" << cell(1) << ')';
421 void InsetMathFrac::mathmlize(MathStream & os) const
425 os << MTag("mfrac", "linethickeness='0'")
426 << MTag("mrow") << cell(0) << ETag("mrow")
427 << MTag("mrow") << cell(1) << ETag("mrow")
431 // we do not presently distinguish these
440 << MTag("mrow") << cell(0) << ETag("mrow")
441 << MTag("mrow") << cell(1) << ETag("mrow")
446 os << MTag("mfrac", "bevelled='true'")
447 << MTag("mrow") << cell(0) << ETag("mrow")
448 << MTag("mrow") << cell(1) << ETag("mrow")
455 os << MTag("mfrac", "bevelled='true'")
456 << MTag("mrow") << cell(0) << ETag("mrow")
457 << MTag("mrow") << cell(1) << ETag("mrow")
462 // FIXME This is not right, because we still output mi, etc,
463 // when we output the cell. So we need to prevent that somehow.
466 << MTag("mstyle mathvariant='normal'")
470 os << MTag("mstyle mathvariant='normal'")
477 void InsetMathFrac::htmlize(HtmlStream & os) const
481 os << MTag("span", "class='frac'")
482 << MTag("span", "class='numer'") << cell(0) << ETag("span")
483 << MTag("span", "class='numer'") << cell(1) << ETag("span")
487 // we do not presently distinguish these
495 os << MTag("span", "class='frac'")
496 << MTag("span", "class='numer'") << cell(0) << ETag("span")
497 << MTag("span", "class='denom'") << cell(1) << ETag("span")
502 os << cell(0) << '/' << cell(1);
507 os << cell(2) << ' ';
508 os << cell(0) << '/' << cell(1);
512 // FIXME This is not right, because we still output i, etc,
513 // when we output the cell. So we need to prevent that somehow.
527 void InsetMathFrac::validate(LaTeXFeatures & features) const
529 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
530 features.require("units");
531 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
532 || kind_ == DFRAC || kind_ == TFRAC)
533 features.require("amsmath");
534 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
535 // CSS adapted from eLyXer
536 features.addCSSSnippet(
537 "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
538 "span.numer{display: block;}\n"
539 "span.denom{display: block; border-top: thin solid #000040;}");
540 InsetMathNest::validate(features);
544 /////////////////////////////////////////////////////////////////////
548 /////////////////////////////////////////////////////////////////////
551 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
552 : InsetMathFracBase(buf), kind_(kind)
556 Inset * InsetMathBinom::clone() const
558 return new InsetMathBinom(*this);
562 int InsetMathBinom::dw(int height) const
573 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
575 Dimension dim0, dim1;
577 (kind_ == DBINOM) ? mi.base.changeStyle(LM_ST_DISPLAY) :
578 (kind_ == TBINOM) ? mi.base.changeStyle(LM_ST_SCRIPT) :
579 mi.base.changeFrac();
580 cell(0).metrics(mi, dim0);
581 cell(1).metrics(mi, dim1);
582 dim.asc = dim0.height() + 4 + 5;
583 dim.des = dim1.height() + 4 - 5;
584 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
585 metricsMarkers2(dim);
589 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
591 Dimension const dim = dimension(*pi.base.bv);
592 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
593 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
594 // define the binom brackets
595 docstring const bra = kind_ == BRACE ? from_ascii("{") :
596 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
597 docstring const ket = kind_ == BRACE ? from_ascii("}") :
598 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
600 int m = x + dim.width() / 2;
603 (kind_ == DBINOM) ? pi.base.changeStyle(LM_ST_DISPLAY) :
604 (kind_ == TBINOM) ? pi.base.changeStyle(LM_ST_SCRIPT) :
605 pi.base.changeFrac();
606 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
607 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
609 // draw the brackets and the marker
610 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
612 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
613 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
614 drawMarkers2(pi, x, y);
618 bool InsetMathBinom::extraBraces() const
620 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
624 void InsetMathBinom::write(WriteStream & os) const
626 MathEnsurer ensurer(os);
629 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
632 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
635 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
638 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
641 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
644 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
650 void InsetMathBinom::normalize(NormalStream & os) const
652 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
656 void InsetMathBinom::mathmlize(MathStream & os) const
677 os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
678 << "<mfrac linethickness='0'>"
679 << cell(0) << cell(1)
681 << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
685 void InsetMathBinom::htmlize(HtmlStream & os) const
706 os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
707 << MTag("span", "class='binom'") << '\n'
708 << MTag("span") << cell(0) << ETag("span") << '\n'
709 << MTag("span") << cell(1) << ETag("span") << '\n'
710 << ETag("span") << '\n'
711 << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
715 void InsetMathBinom::validate(LaTeXFeatures & features) const
717 if (features.runparams().isLaTeX()) {
719 features.require("binom");
720 if (kind_ == DBINOM || kind_ == TBINOM)
721 features.require("amsmath");
722 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
723 features.addCSSSnippet(
724 "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
725 "span.binom span{display: block;}\n"
726 "span.binomdelim{font-size: 2em;}");
727 InsetMathNest::validate(features);