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"
31 /////////////////////////////////////////////////////////////////////
35 /////////////////////////////////////////////////////////////////////
38 InsetMathFracBase::InsetMathFracBase(Buffer * buf, idx_type ncells)
39 : InsetMathNest(buf, ncells)
43 bool InsetMathFracBase::idxUpDown(Cursor & cur, bool up) const
45 // If we only have one cell, target = 0, otherwise
46 // target = up ? 0 : 1, since upper cell has idx 0
47 InsetMath::idx_type target = nargs() > 1 ? !up : 0;
48 if (cur.idx() == target)
51 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
57 /////////////////////////////////////////////////////////////////////
61 /////////////////////////////////////////////////////////////////////
64 InsetMathFrac::InsetMathFrac(Buffer * buf, Kind kind, InsetMath::idx_type ncells)
65 : InsetMathFracBase(buf, ncells), kind_(kind)
69 Inset * InsetMathFrac::clone() const
71 return new InsetMathFrac(*this);
75 InsetMathFrac * InsetMathFrac::asFracInset()
77 return kind_ == ATOP ? 0 : this;
81 InsetMathFrac const * InsetMathFrac::asFracInset() const
83 return kind_ == ATOP ? 0 : this;
87 bool InsetMathFrac::idxForward(Cursor & cur) const
89 InsetMath::idx_type target = 0;
90 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
93 else if (nargs() == 2)
97 if (cur.idx() == target)
100 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
105 bool InsetMathFrac::idxBackward(Cursor & cur) const
107 InsetMath::idx_type target = 0;
108 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
111 else if (nargs() == 2)
115 if (cur.idx() == target)
118 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
123 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
125 Dimension dim0, dim1, dim2;
127 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
129 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
130 cell(0).metrics(mi, dim0);
131 dim.wid = dim0.width()+ 3;
134 } else if (nargs() == 2) {
135 cell(0).metrics(mi, dim0);
136 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
137 cell(1).metrics(mi, dim1);
138 dim.wid = dim0.width() + dim1.wid + 5;
139 dim.asc = max(dim0.asc, dim1.asc);
140 dim.des = max(dim0.des, dim1.des);
142 cell(2).metrics(mi, dim2);
143 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
144 FracChanger dummy(mi.base);
145 cell(0).metrics(mi, dim0);
146 cell(1).metrics(mi, dim1);
147 dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
148 dim.asc = max(dim2.asc, dim0.height() + 5);
149 dim.des = max(dim2.des, dim1.height() - 5);
152 // general cell metrics used for \frac
153 FracChanger dummy(mi.base);
154 cell(0).metrics(mi, dim0);
155 cell(1).metrics(mi, dim1);
157 cell(2).metrics(mi, dim2);
158 // metrics for special fraction types
159 if (kind_ == NICEFRAC) {
160 dim.wid = dim0.width() + dim1.wid + 5;
161 dim.asc = dim0.height() + 5;
162 dim.des = dim1.height() - 5;
163 } else if (kind_ == UNITFRAC) {
164 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
165 dim.wid = dim0.width() + dim1.wid + 5;
166 dim.asc = dim0.height() + 5;
167 dim.des = dim1.height() - 5;
169 if (kind_ == CFRAC || kind_ == CFRACLEFT
170 || kind_ == CFRACRIGHT || kind_ == DFRAC) {
171 // \cfrac and \dfrac are always in display size
172 StyleChanger dummy2(mi.base, LM_ST_DISPLAY);
173 cell(0).metrics(mi, dim0);
174 cell(1).metrics(mi, dim1);
175 } else if (kind_ == TFRAC) {
176 // tfrac is in always in text size
177 StyleChanger dummy2(mi.base, LM_ST_SCRIPT);
178 cell(0).metrics(mi, dim0);
179 cell(1).metrics(mi, dim1);
181 dim.wid = max(dim0.wid, dim1.wid) + 2;
182 dim.asc = dim0.height() + 2 + 5;
183 dim.des = dim1.height() + 2 - 5;
190 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
192 setPosCache(pi, x, y);
193 Dimension const dim = dimension(*pi.base.bv);
194 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
195 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
197 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
198 cell(0).draw(pi, x + 1, y);
199 } else if (nargs() == 2) {
200 cell(0).draw(pi, x + 1, y);
201 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
202 cell(1).draw(pi, x + dim0.width() + 5, y);
204 cell(2).draw(pi, x + 1, y);
205 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
206 FracChanger dummy(pi.base);
207 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
208 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
209 int xx = x + dim2.wid + 5;
210 cell(0).draw(pi, xx + 2,
212 cell(1).draw(pi, xx + dim0.width() + 5,
216 FracChanger dummy(pi.base);
217 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
218 int m = x + dim.wid / 2;
219 if (kind_ == NICEFRAC) {
220 cell(0).draw(pi, x + 2,
222 cell(1).draw(pi, x + dim0.width() + 5,
224 } else if (kind_ == UNITFRAC) {
225 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
226 cell(0).draw(pi, x + 2, y - dim0.des - 5);
227 cell(1).draw(pi, x + dim0.width() + 5, y + dim1.asc / 2);
228 } else if (kind_ == FRAC || kind_ == ATOP || kind_ == OVER) {
229 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
230 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
231 } else if (kind_ == TFRAC) {
232 // tfrac is in always in text size
233 StyleChanger dummy2(pi.base, LM_ST_SCRIPT);
234 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
235 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
237 // \cfrac and \dfrac are always in display size
238 StyleChanger dummy2(pi.base, LM_ST_DISPLAY);
239 if (kind_ == CFRAC || kind_ == DFRAC)
240 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
241 else if (kind_ == CFRACLEFT)
242 cell(0).draw(pi, x + 2, y - dim0.des - 2 - 5);
243 else if (kind_ == CFRACRIGHT)
244 cell(0).draw(pi, x + dim.wid - dim0.wid - 2,
245 y - dim0.des - 2 - 5);
246 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
249 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
253 xx += cell(2).dimension(*pi.base.bv).wid + 5;
254 pi.pain.line(xx + dim0.wid,
257 y - dim.asc + 2, pi.base.font.color());
259 if (kind_ == FRAC || kind_ == CFRAC || kind_ == CFRACLEFT
260 || kind_ == CFRACRIGHT || kind_ == DFRAC
261 || kind_ == TFRAC || kind_ == OVER)
262 pi.pain.line(x + 1, y - 5,
263 x + dim.wid - 2, y - 5, pi.base.font.color());
264 drawMarkers(pi, x, y);
268 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
270 Dimension dim0, dim1;
271 cell(0).metricsT(mi, dim0);
272 cell(1).metricsT(mi, dim1);
273 dim.wid = max(dim0.width(), dim1.wid);
274 dim.asc = dim0.height() + 1;
275 dim.des = dim1.height();
279 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
284 int m = x + dim.width() / 2;
285 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
286 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
287 // ASCII art: ignore niceties
288 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
289 pain.horizontalLine(x, y, dim.width());
294 void InsetMathFrac::write(WriteStream & os) const
296 MathEnsurer ensurer(os);
299 // \\atop is only for compatibility, \\binom is the
301 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
304 // \\over is only for compatibility, normalize this to \\frac
305 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
314 InsetMathNest::write(os);
316 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
320 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
322 os << "\\unit{" << cell(0) << '}';
325 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
328 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
334 docstring InsetMathFrac::name() const
338 return from_ascii("frac");
342 return from_ascii("cfrac");
344 return from_ascii("dfrac");
346 return from_ascii("tfrac");
348 return from_ascii("over");
350 return from_ascii("nicefrac");
352 return from_ascii("unitfrac");
354 return from_ascii("unit");
356 return from_ascii("atop");
358 // shut up stupid compiler
363 bool InsetMathFrac::extraBraces() const
365 return kind_ == ATOP || kind_ == OVER;
369 // FIXME This will crash on unitone and is wrong in other cases.
370 void InsetMathFrac::maple(MapleStream & os) const
372 os << '(' << cell(0) << ")/(" << cell(1) << ')';
376 // FIXME This will crash on unitone and is wrong in other cases.
377 void InsetMathFrac::mathematica(MathematicaStream & os) const
379 os << '(' << cell(0) << ")/(" << cell(1) << ')';
383 // FIXME This will crash on unitone and is wrong in other cases.
384 void InsetMathFrac::octave(OctaveStream & os) const
386 os << '(' << cell(0) << ")/(" << cell(1) << ')';
390 void InsetMathFrac::mathmlize(MathStream & os) const
394 os << MTag("mfrac", "linethickeness='0'")
395 << MTag("mrow") << cell(0) << ETag("mrow")
396 << MTag("mrow") << cell(1) << ETag("mrow")
400 // we do not presently distinguish these
409 << MTag("mrow") << cell(0) << ETag("mrow")
410 << MTag("mrow") << cell(1) << ETag("mrow")
415 os << MTag("mfrac", "bevelled='true'")
416 << MTag("mrow") << cell(0) << ETag("mrow")
417 << MTag("mrow") << cell(1) << ETag("mrow")
424 os << MTag("mfrac", "bevelled='true'")
425 << MTag("mrow") << cell(0) << ETag("mrow")
426 << MTag("mrow") << cell(1) << ETag("mrow")
431 // FIXME This is not right, because we still output mi, etc,
432 // when we output the cell. So we need to prevent that somehow.
435 << MTag("mstyle mathvariant='normal'")
439 os << MTag("mstyle mathvariant='normal'")
446 void InsetMathFrac::htmlize(HtmlStream & os) const
450 os << MTag("span", "class='frac'")
451 << MTag("span", "class='numer'") << cell(0) << ETag("span")
452 << MTag("span", "class='numer'") << cell(1) << ETag("span")
456 // we do not presently distinguish these
464 os << MTag("span", "class='frac'")
465 << MTag("span", "class='numer'") << cell(0) << ETag("span")
466 << MTag("span", "class='denom'") << cell(1) << ETag("span")
471 os << cell(0) << '/' << cell(1);
476 os << cell(2) << ' ';
477 os << cell(0) << '/' << cell(1);
481 // FIXME This is not right, because we still output i, etc,
482 // when we output the cell. So we need to prevent that somehow.
496 void InsetMathFrac::validate(LaTeXFeatures & features) const
498 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
499 features.require("units");
500 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
501 || kind_ == DFRAC || kind_ == TFRAC)
502 features.require("amsmath");
503 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
504 // CSS adapted from eLyXer
505 features.addPreambleSnippet("<style type=\"text/css\">\n"
506 "span.frac{display: inline-block; vertical-align: middle; text-align:center;}\n"
507 "span.numer{display: block;}\n"
508 "span.denom{display: block; border-top: thin solid #000040;}\n"
510 InsetMathNest::validate(features);
514 /////////////////////////////////////////////////////////////////////
518 /////////////////////////////////////////////////////////////////////
521 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
522 : InsetMathFracBase(buf), kind_(kind)
526 Inset * InsetMathBinom::clone() const
528 return new InsetMathBinom(*this);
532 int InsetMathBinom::dw(int height) const
543 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
545 Dimension dim0, dim1;
547 // FIXME: for an unknown reason the cells must be set directly
548 // after the StyleChanger and cannot be set after the if case
549 if (kind_ == DBINOM) {
550 StyleChanger dummy(mi.base, LM_ST_DISPLAY);
551 cell(0).metrics(mi, dim0);
552 cell(1).metrics(mi, dim1);
553 } else if (kind_ == TBINOM) {
554 StyleChanger dummy(mi.base, LM_ST_SCRIPT);
555 cell(0).metrics(mi, dim0);
556 cell(1).metrics(mi, dim1);
558 FracChanger dummy(mi.base);
559 cell(0).metrics(mi, dim0);
560 cell(1).metrics(mi, dim1);
562 dim.asc = dim0.height() + 4 + 5;
563 dim.des = dim1.height() + 4 - 5;
564 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
565 metricsMarkers2(dim);
569 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
571 Dimension const dim = dimension(*pi.base.bv);
572 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
573 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
574 // define the binom brackets
575 docstring const bra = kind_ == BRACE ? from_ascii("{") :
576 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
577 docstring const ket = kind_ == BRACE ? from_ascii("}") :
578 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
580 int m = x + dim.width() / 2;
581 // FIXME: for an unknown reason the cells must be drawn directly
582 // after the StyleChanger and cannot be drawn after the if case
583 if (kind_ == DBINOM) {
584 StyleChanger dummy(pi.base, LM_ST_DISPLAY);
585 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
586 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
587 } else if (kind_ == TBINOM) {
588 StyleChanger dummy(pi.base, LM_ST_SCRIPT);
589 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
590 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
592 FracChanger dummy2(pi.base);
593 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
594 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
596 // draw the brackets and the marker
597 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
599 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
600 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
601 drawMarkers2(pi, x, y);
605 bool InsetMathBinom::extraBraces() const
607 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
611 void InsetMathBinom::write(WriteStream & os) const
613 MathEnsurer ensurer(os);
616 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
619 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
622 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
625 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
628 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
631 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
637 void InsetMathBinom::normalize(NormalStream & os) const
639 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
643 void InsetMathBinom::mathmlize(MathStream & os) const
664 os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
665 << "<mfrac linethickness='0'>"
666 << cell(0) << cell(1)
668 << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
672 void InsetMathBinom::htmlize(HtmlStream & os) const
693 os << MTag("span", "class='binomdelim'") << ldelim << ETag("span") << '\n'
694 << MTag("span", "class='binom'") << '\n'
695 << MTag("span") << cell(0) << ETag("span") << '\n'
696 << MTag("span") << cell(1) << ETag("span") << '\n'
697 << ETag("span") << '\n'
698 << MTag("span", "class='binomdelim'") << rdelim << ETag("span") << '\n';
702 void InsetMathBinom::validate(LaTeXFeatures & features) const
704 if (features.runparams().isLaTeX()) {
706 features.require("binom");
707 if (kind_ == DBINOM || kind_ == TBINOM)
708 features.require("amsmath");
709 } else if (features.runparams().math_flavor == OutputParams::MathAsHTML)
710 features.addPreambleSnippet("<style type=\"text/css\">\n"
711 "span.binom{display: inline-block; vertical-align: bottom; text-align:center;}\n"
712 "span.binom span{display: block;}\n"
713 "span.binomdelim{font-size: 2em;}\n"
715 InsetMathNest::validate(features);