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 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
128 Dimension dim0, dim1, dim2;
130 // This could be simplified, including avoiding useless recalculation of
132 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
134 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
135 cell(0).metrics(mi, dim0);
136 dim.wid = dim0.width()+ 3;
139 } else if (nargs() == 2) {
140 cell(0).metrics(mi, dim0);
141 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
142 cell(1).metrics(mi, dim1);
143 dim.wid = dim0.width() + dim1.wid + 5;
144 dim.asc = max(dim0.asc, dim1.asc);
145 dim.des = max(dim0.des, dim1.des);
147 cell(2).metrics(mi, dim2);
148 Changer dummy = mi.base.font.changeShape(UP_SHAPE);
149 Changer dummy2 = mi.base.changeFrac();
150 cell(0).metrics(mi, dim0);
151 cell(1).metrics(mi, dim1);
152 dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
153 dim.asc = max(dim2.asc, dim0.height() + 5);
154 dim.des = max(dim2.des, dim1.height() - 5);
157 // general cell metrics used for \frac
158 Changer dummy = mi.base.changeFrac();
159 cell(0).metrics(mi, dim0);
160 cell(1).metrics(mi, dim1);
162 cell(2).metrics(mi, dim2);
163 // metrics for special fraction types
164 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
165 Changer dummy2 = mi.base.font.changeShape(UP_SHAPE, kind_ == UNITFRAC);
166 dim.wid = dim0.width() + dim1.wid + 5;
167 dim.asc = dim0.height() + 5;
168 dim.des = dim1.height() - 5;
170 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
171 || kind_ == DFRAC || kind_ == TFRAC) {
172 // \cfrac and \dfrac are always in display size
173 // \tfrac is in always in text size
174 Changer dummy2 = mi.base.changeStyle((kind_ == TFRAC)
177 cell(0).metrics(mi, dim0);
178 cell(1).metrics(mi, dim1);
180 dim.wid = max(dim0.wid, dim1.wid) + 2;
181 dim.asc = dim0.height() + 2 + 5;
182 dim.des = dim1.height() + 2 - 5;
189 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
191 setPosCache(pi, x, y);
192 Dimension const dim = dimension(*pi.base.bv);
193 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
194 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
196 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
197 cell(0).draw(pi, x + 1, y);
198 } else if (nargs() == 2) {
199 cell(0).draw(pi, x + 1, y);
200 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
201 cell(1).draw(pi, x + dim0.width() + 5, y);
203 cell(2).draw(pi, x + 1, y);
204 Changer dummy = pi.base.font.changeShape(UP_SHAPE);
205 Changer dummy2 = pi.base.changeFrac();
206 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
207 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
208 int xx = x + dim2.wid + 5;
209 cell(0).draw(pi, xx + 2,
211 cell(1).draw(pi, xx + dim0.width() + 5,
215 Changer dummy = pi.base.changeFrac();
216 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
217 int m = x + dim.wid / 2;
218 if (kind_ == NICEFRAC) {
219 cell(0).draw(pi, x + 2,
221 cell(1).draw(pi, x + dim0.width() + 5,
223 } else if (kind_ == UNITFRAC) {
224 Changer dummy2 = pi.base.font.changeShape(UP_SHAPE);
225 cell(0).draw(pi, x + 2, y - dim0.des - 5);
226 cell(1).draw(pi, x + dim0.width() + 5, y + dim1.asc / 2);
227 } else if (kind_ == FRAC || kind_ == ATOP || kind_ == OVER
229 // tfrac is in always in text size
230 Changer dummy2 = pi.base.changeStyle(LM_ST_SCRIPT, kind_ == TFRAC);
231 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
232 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
234 // \cfrac and \dfrac are always in display size
235 Changer dummy2 = pi.base.changeStyle(LM_ST_DISPLAY);
236 if (kind_ == CFRAC || kind_ == DFRAC)
237 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
238 else if (kind_ == CFRACLEFT)
239 cell(0).draw(pi, x + 2, y - dim0.des - 2 - 5);
240 else if (kind_ == CFRACRIGHT)
241 cell(0).draw(pi, x + dim.wid - dim0.wid - 2,
242 y - dim0.des - 2 - 5);
243 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
246 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
250 xx += cell(2).dimension(*pi.base.bv).wid + 5;
251 pi.pain.line(xx + dim0.wid,
254 y - dim.asc + 2, pi.base.font.color());
256 if (kind_ == FRAC || kind_ == CFRAC || kind_ == CFRACLEFT
257 || kind_ == CFRACRIGHT || kind_ == DFRAC
258 || kind_ == TFRAC || kind_ == OVER)
259 pi.pain.line(x + 1, y - 5,
260 x + dim.wid - 2, y - 5, pi.base.font.color());
261 drawMarkers(pi, x, y);
265 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
267 Dimension dim0, dim1;
268 cell(0).metricsT(mi, dim0);
269 cell(1).metricsT(mi, dim1);
270 dim.wid = max(dim0.width(), dim1.wid);
271 dim.asc = dim0.height() + 1;
272 dim.des = dim1.height();
276 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
281 int m = x + dim.width() / 2;
282 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
283 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
284 // ASCII art: ignore niceties
285 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
286 pain.horizontalLine(x, y, dim.width());
291 void InsetMathFrac::write(WriteStream & os) const
293 MathEnsurer ensurer(os);
296 // \\atop is only for compatibility, \\binom is the
298 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
301 // \\over is only for compatibility, normalize this to \\frac
302 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
311 InsetMathNest::write(os);
313 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
317 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
319 os << "\\unit{" << cell(0) << '}';
322 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
325 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
331 docstring InsetMathFrac::name() const
335 return from_ascii("frac");
339 return from_ascii("cfrac");
341 return from_ascii("dfrac");
343 return from_ascii("tfrac");
345 return from_ascii("over");
347 return from_ascii("nicefrac");
349 return from_ascii("unitfrac");
351 return from_ascii("unit");
353 return from_ascii("atop");
355 // shut up stupid compiler
360 bool InsetMathFrac::extraBraces() const
362 return kind_ == ATOP || kind_ == OVER;
366 void InsetMathFrac::maple(MapleStream & os) const
369 // Someone who knows about maple should fix this.
370 LASSERT(false, return);
372 os << '(' << cell(0) << ")/(" << cell(1) << ')';
376 void InsetMathFrac::mathematica(MathematicaStream & os) const
379 // Someone who knows about mathematica should fix this.
380 LASSERT(false, return);
382 os << '(' << cell(0) << ")/(" << cell(1) << ')';
386 void InsetMathFrac::octave(OctaveStream & os) const
389 // Someone who knows about octave should fix this.
390 LASSERT(false, return);
392 os << '(' << cell(0) << ")/(" << cell(1) << ')';
396 void InsetMathFrac::mathmlize(MathStream & os) const
400 os << MTag("mfrac", "linethickeness='0'")
401 << MTag("mrow") << cell(0) << ETag("mrow")
402 << MTag("mrow") << cell(1) << ETag("mrow")
406 // we do not presently distinguish these
415 << MTag("mrow") << cell(0) << ETag("mrow")
416 << MTag("mrow") << cell(1) << ETag("mrow")
421 os << MTag("mfrac", "bevelled='true'")
422 << MTag("mrow") << cell(0) << ETag("mrow")
423 << MTag("mrow") << cell(1) << ETag("mrow")
430 os << MTag("mfrac", "bevelled='true'")
431 << MTag("mrow") << cell(0) << ETag("mrow")
432 << MTag("mrow") << cell(1) << ETag("mrow")
437 // FIXME This is not right, because we still output mi, etc,
438 // when we output the cell. So we need to prevent that somehow.
441 << MTag("mstyle mathvariant='normal'")
445 os << MTag("mstyle mathvariant='normal'")
452 void InsetMathFrac::htmlize(HtmlStream & os) const
456 os << MTag("span", "class='frac'")
457 << MTag("span", "class='numer'") << cell(0) << ETag("span")
458 << MTag("span", "class='numer'") << cell(1) << ETag("span")
462 // we do not presently distinguish these
470 os << MTag("span", "class='frac'")
471 << MTag("span", "class='numer'") << cell(0) << ETag("span")
472 << MTag("span", "class='denom'") << cell(1) << ETag("span")
477 os << cell(0) << '/' << cell(1);
482 os << cell(2) << ' ';
483 os << cell(0) << '/' << cell(1);
487 // FIXME This is not right, because we still output i, etc,
488 // when we output the cell. So we need to prevent that somehow.
502 void InsetMathFrac::validate(LaTeXFeatures & features) const
504 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
505 features.require("units");
506 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
507 || kind_ == DFRAC || kind_ == TFRAC)
508 features.require("amsmath");
509 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
510 // CSS adapted from eLyXer
511 features.addCSSSnippet(
512 "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
513 "span.numer{display: block;}\n"
514 "span.denom{display: block; border-top: thin solid #000040;}");
515 InsetMathNest::validate(features);
519 /////////////////////////////////////////////////////////////////////
523 /////////////////////////////////////////////////////////////////////
526 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
527 : InsetMathFracBase(buf), kind_(kind)
531 Inset * InsetMathBinom::clone() const
533 return new InsetMathBinom(*this);
537 int InsetMathBinom::dw(int height) const
548 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
550 Dimension dim0, dim1;
552 (kind_ == DBINOM) ? mi.base.changeStyle(LM_ST_DISPLAY) :
553 (kind_ == TBINOM) ? mi.base.changeStyle(LM_ST_SCRIPT) :
554 mi.base.changeFrac();
555 cell(0).metrics(mi, dim0);
556 cell(1).metrics(mi, dim1);
557 dim.asc = dim0.height() + 4 + 5;
558 dim.des = dim1.height() + 4 - 5;
559 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
560 metricsMarkers2(dim);
564 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
566 Dimension const dim = dimension(*pi.base.bv);
567 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
568 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
569 // define the binom brackets
570 docstring const bra = kind_ == BRACE ? from_ascii("{") :
571 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
572 docstring const ket = kind_ == BRACE ? from_ascii("}") :
573 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
575 int m = x + dim.width() / 2;
578 (kind_ == DBINOM) ? pi.base.changeStyle(LM_ST_DISPLAY) :
579 (kind_ == TBINOM) ? pi.base.changeStyle(LM_ST_SCRIPT) :
580 pi.base.changeFrac();
581 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
582 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
584 // draw the brackets and the marker
585 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
587 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
588 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
589 drawMarkers2(pi, x, y);
593 bool InsetMathBinom::extraBraces() const
595 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
599 void InsetMathBinom::write(WriteStream & os) const
601 MathEnsurer ensurer(os);
604 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
607 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
610 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
613 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
616 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
619 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
625 void InsetMathBinom::normalize(NormalStream & os) const
627 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
631 void InsetMathBinom::mathmlize(MathStream & os) const
652 os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
653 << "<mfrac linethickness='0'>"
654 << cell(0) << cell(1)
656 << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
660 void InsetMathBinom::htmlize(HtmlStream & os) const
681 os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
682 << MTag("span", "class='binom'") << '\n'
683 << MTag("span") << cell(0) << ETag("span") << '\n'
684 << MTag("span") << cell(1) << ETag("span") << '\n'
685 << ETag("span") << '\n'
686 << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
690 void InsetMathBinom::validate(LaTeXFeatures & features) const
692 if (features.runparams().isLaTeX()) {
694 features.require("binom");
695 if (kind_ == DBINOM || kind_ == TBINOM)
696 features.require("amsmath");
697 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
698 features.addCSSSnippet(
699 "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
700 "span.binom span{display: block;}\n"
701 "span.binomdelim{font-size: 2em;}");
702 InsetMathNest::validate(features);