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
9 * Full author contact details are available in file CREDITS.
14 #include "InsetMathFrac.h"
17 #include "LaTeXFeatures.h"
19 #include "MathStream.h"
20 #include "MathSupport.h"
21 #include "MetricsInfo.h"
22 #include "TextPainter.h"
24 #include "frontends/Painter.h"
30 /////////////////////////////////////////////////////////////////////
34 /////////////////////////////////////////////////////////////////////
37 InsetMathFracBase::InsetMathFracBase(idx_type ncells)
38 : InsetMathNest(ncells)
42 bool InsetMathFracBase::idxUpDown(Cursor & cur, bool up) const
44 InsetMath::idx_type target = !up; // up ? 0 : 1, since upper cell has idx 0
45 if (cur.idx() == target)
48 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
54 /////////////////////////////////////////////////////////////////////
58 /////////////////////////////////////////////////////////////////////
61 InsetMathFrac::InsetMathFrac(Kind kind, InsetMath::idx_type ncells)
62 : InsetMathFracBase(ncells), kind_(kind)
66 Inset * InsetMathFrac::clone() const
68 return new InsetMathFrac(*this);
72 InsetMathFrac * InsetMathFrac::asFracInset()
74 return kind_ == ATOP ? 0 : this;
78 InsetMathFrac const * InsetMathFrac::asFracInset() const
80 return kind_ == ATOP ? 0 : this;
84 bool InsetMathFrac::idxForward(Cursor & cur) const
86 InsetMath::idx_type target = 0;
87 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
90 else if (nargs() == 2)
94 if (cur.idx() == target)
97 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
102 bool InsetMathFrac::idxBackward(Cursor & cur) const
104 InsetMath::idx_type target = 0;
105 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
108 else if (nargs() == 2)
112 if (cur.idx() == target)
115 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
120 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
122 Dimension dim0, dim1, dim2;
124 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
126 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
127 cell(0).metrics(mi, dim0);
128 dim.wid = dim0.width()+ 3;
131 } else if (nargs() == 2) {
132 cell(0).metrics(mi, dim0);
133 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
134 cell(1).metrics(mi, dim1);
135 dim.wid = dim0.width() + dim1.wid + 5;
136 dim.asc = max(dim0.asc, dim1.asc);
137 dim.des = max(dim0.des, dim1.des);
139 cell(2).metrics(mi, dim2);
140 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
141 FracChanger dummy(mi.base);
142 cell(0).metrics(mi, dim0);
143 cell(1).metrics(mi, dim1);
144 dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
145 dim.asc = max(dim2.asc, dim0.height() + 5);
146 dim.des = max(dim2.des, dim1.height() - 5);
149 FracChanger dummy(mi.base);
150 cell(0).metrics(mi, dim0);
151 cell(1).metrics(mi, dim1);
153 cell(2).metrics(mi, dim2);
155 if (kind_ == NICEFRAC) {
156 dim.wid = dim0.width() + dim1.wid + 5;
157 dim.asc = dim0.height() + 5;
158 dim.des = dim1.height() - 5;
159 } else if (kind_ == UNITFRAC) {
160 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
161 dim.wid = dim0.width() + dim1.wid + 5;
162 dim.asc = dim0.height() + 5;
163 dim.des = dim1.height() - 5;
165 dim.wid = max(dim0.width(), dim1.wid) + 2;
166 dim.asc = dim0.height() + 2 + 5;
167 dim.des = dim1.height() + 2 - 5;
174 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
176 setPosCache(pi, x, y);
177 Dimension const dim = dimension(*pi.base.bv);
178 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
179 int m = x + dim.wid / 2;
180 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
182 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
183 cell(0).draw(pi, x + 1, y);
184 } else if (nargs() == 2) {
185 cell(0).draw(pi, x + 1, y);
186 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
187 cell(1).draw(pi, x + dim0.width() + 5, y);
189 cell(2).draw(pi, x + 1, y);
190 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
191 FracChanger dummy(pi.base);
192 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
193 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
194 int xx = x + dim2.wid + 5;
195 cell(0).draw(pi, xx + 2,
197 cell(1).draw(pi, xx + dim0.width() + 5,
201 FracChanger dummy(pi.base);
202 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
203 if (kind_ == NICEFRAC) {
204 cell(0).draw(pi, x + 2,
206 cell(1).draw(pi, x + dim0.width() + 5,
208 } else if (kind_ == UNITFRAC) {
209 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
210 cell(0).draw(pi, x + 2,
212 cell(1).draw(pi, x + dim0.width() + 5,
215 // Classical fraction
216 cell(0).draw(pi, m - dim0.width() / 2,
217 y - dim0.des - 2 - 5);
218 cell(1).draw(pi, m - dim1.wid / 2,
219 y + dim1.asc + 2 - 5);
222 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
226 xx += cell(2).dimension(*pi.base.bv).wid + 5;
228 pi.pain.line(xx + dim0.wid,
231 y - dim.asc + 2, Color_math);
233 if (kind_ == FRAC || kind_ == OVER)
234 pi.pain.line(x + 1, y - 5,
235 x + dim.wid - 2, y - 5, Color_math);
236 drawMarkers(pi, x, y);
240 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
242 Dimension dim0, dim1;
243 cell(0).metricsT(mi, dim0);
244 cell(1).metricsT(mi, dim1);
245 dim.wid = max(dim0.width(), dim1.wid);
246 dim.asc = dim0.height() + 1;
247 dim.des = dim1.height();
251 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
256 int m = x + dim.width() / 2;
257 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
258 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
259 // ASCII art: ignore niceties
260 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
261 pain.horizontalLine(x, y, dim.width());
266 void InsetMathFrac::write(WriteStream & os) const
270 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
273 // \\over is only for compatibility, normalize this to \\frac
274 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
280 InsetMathNest::write(os);
282 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
286 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
288 os << "\\unit{" << cell(0) << '}';
294 docstring InsetMathFrac::name() const
298 return from_ascii("frac");
300 return from_ascii("over");
302 return from_ascii("nicefrac");
304 return from_ascii("unitfrac");
306 return from_ascii("unit");
308 return from_ascii("atop");
310 // shut up stupid compiler
315 bool InsetMathFrac::extraBraces() const
317 return kind_ == ATOP || kind_ == OVER;
321 void InsetMathFrac::maple(MapleStream & os) const
323 os << '(' << cell(0) << ")/(" << cell(1) << ')';
327 void InsetMathFrac::mathematica(MathematicaStream & os) const
329 os << '(' << cell(0) << ")/(" << cell(1) << ')';
333 void InsetMathFrac::octave(OctaveStream & os) const
335 os << '(' << cell(0) << ")/(" << cell(1) << ')';
339 void InsetMathFrac::mathmlize(MathStream & os) const
341 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
345 void InsetMathFrac::validate(LaTeXFeatures & features) const
347 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
348 features.require("units");
349 InsetMathNest::validate(features);
353 /////////////////////////////////////////////////////////////////////
357 /////////////////////////////////////////////////////////////////////
360 Inset * InsetMathDFrac::clone() const
362 return new InsetMathDFrac(*this);
366 void InsetMathDFrac::metrics(MetricsInfo & mi, Dimension & dim) const
368 Dimension dim0, dim1;
369 cell(0).metrics(mi, dim0);
370 cell(1).metrics(mi, dim1);
371 dim.wid = max(dim0.wid, dim1.wid) + 2;
372 dim.asc = dim0.height() + 2 + 5;
373 dim.des = dim1.height() + 2 - 5;
377 void InsetMathDFrac::draw(PainterInfo & pi, int x, int y) const
379 Dimension const dim = dimension(*pi.base.bv);
380 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
381 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
382 int m = x + dim.wid / 2;
383 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
384 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
385 pi.pain.line(x + 1, y - 5, x + dim.wid - 2, y - 5, Color_math);
386 setPosCache(pi, x, y);
390 docstring InsetMathDFrac::name() const
392 return from_ascii("dfrac");
396 void InsetMathDFrac::mathmlize(MathStream & os) const
398 os << MTag("mdfrac") << cell(0) << cell(1) << ETag("mdfrac");
402 void InsetMathDFrac::validate(LaTeXFeatures & features) const
404 features.require("amsmath");
405 InsetMathNest::validate(features);
409 /////////////////////////////////////////////////////////////////////
413 /////////////////////////////////////////////////////////////////////
416 Inset * InsetMathTFrac::clone() const
418 return new InsetMathTFrac(*this);
422 void InsetMathTFrac::metrics(MetricsInfo & mi, Dimension & dim) const
424 StyleChanger dummy(mi.base, LM_ST_SCRIPT);
426 cell(0).metrics(mi, dim0);
428 cell(1).metrics(mi, dim1);
429 dim.wid = max(dim0.width(), dim1.width()) + 2;
430 dim.asc = dim0.height() + 2 + 5;
431 dim.des = dim1.height() + 2 - 5;
435 void InsetMathTFrac::draw(PainterInfo & pi, int x, int y) const
437 StyleChanger dummy(pi.base, LM_ST_SCRIPT);
438 Dimension const dim = dimension(*pi.base.bv);
439 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
440 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
441 int m = x + dim.wid / 2;
442 cell(0).draw(pi, m - dim0.width() / 2, y - dim0.descent() - 2 - 5);
443 cell(1).draw(pi, m - dim1.width() / 2, y + dim1.ascent() + 2 - 5);
444 pi.pain.line(x + 1, y - 5, x + dim.wid - 2, y - 5, Color_math);
445 setPosCache(pi, x, y);
449 docstring InsetMathTFrac::name() const
451 return from_ascii("tfrac");
455 void InsetMathTFrac::mathmlize(MathStream & os) const
457 os << MTag("mtfrac") << cell(0) << cell(1) << ETag("mtfrac");
461 void InsetMathTFrac::validate(LaTeXFeatures & features) const
463 features.require("amsmath");
464 InsetMathNest::validate(features);
468 /////////////////////////////////////////////////////////////////////
472 /////////////////////////////////////////////////////////////////////
475 InsetMathBinom::InsetMathBinom(bool choose)
480 Inset * InsetMathBinom::clone() const
482 return new InsetMathBinom(*this);
486 int InsetMathBinom::dw(int height) const
497 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
499 FracChanger dummy(mi.base);
500 Dimension dim0, dim1;
501 cell(0).metrics(mi, dim0);
502 cell(1).metrics(mi, dim1);
503 dim.asc = dim0.height() + 4 + 5;
504 dim.des = dim1.height() + 4 - 5;
505 dim.wid = max(dim0.width(), dim1.wid) + 2 * dw(dim.height()) + 4;
506 metricsMarkers2(dim);
510 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
512 Dimension const dim = dimension(*pi.base.bv);
513 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
514 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
515 int m = x + dim.width() / 2;
516 FracChanger dummy(pi.base);
517 cell(0).draw(pi, m - dim0.width() / 2, y - dim0.des - 3 - 5);
518 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
519 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()), dim.height(), from_ascii("("));
520 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()), y - dim.ascent(),
521 dw(dim.height()), dim.height(), from_ascii(")"));
522 drawMarkers2(pi, x, y);
526 bool InsetMathBinom::extraBraces() const
532 void InsetMathBinom::write(WriteStream & os) const
535 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
537 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
541 void InsetMathBinom::normalize(NormalStream & os) const
543 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
547 /////////////////////////////////////////////////////////////////////
551 /////////////////////////////////////////////////////////////////////
553 Inset * InsetMathDBinom::clone() const
555 return new InsetMathDBinom(*this);
559 int InsetMathDBinom::dw(int height) const
570 void InsetMathDBinom::metrics(MetricsInfo & mi, Dimension & dim) const
572 Dimension dim0, dim1;
573 cell(0).metrics(mi, dim0);
574 cell(1).metrics(mi, dim1);
575 dim.asc = dim0.height() + 4 + 5;
576 dim.des = dim1.height() + 4 - 5;
577 dim.wid = max(dim0.width(), dim1.wid) + 2 * dw(dim.height()) + 4;
578 metricsMarkers2(dim);
582 void InsetMathDBinom::draw(PainterInfo & pi, int x, int y) const
584 Dimension const dim = dimension(*pi.base.bv);
585 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
586 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
587 int m = x + dim.width() / 2;
588 cell(0).draw(pi, m - dim0.width() / 2, y - dim0.des - 3 - 5);
589 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
590 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()), dim.height(), from_ascii("("));
591 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()), y - dim.ascent(),
592 dw(dim.height()), dim.height(), from_ascii(")"));
593 drawMarkers2(pi, x, y);
597 docstring InsetMathDBinom::name() const
599 return from_ascii("dbinom");
602 void InsetMathDBinom::mathmlize(MathStream & os) const
604 os << MTag("mdbinom") << cell(0) << cell(1) << ETag("mdbinom");
607 void InsetMathDBinom::validate(LaTeXFeatures & features) const
609 features.require("amsmath");
610 InsetMathNest::validate(features);
614 /////////////////////////////////////////////////////////////////////
618 /////////////////////////////////////////////////////////////////////
620 Inset * InsetMathTBinom::clone() const
622 return new InsetMathTBinom(*this);
626 int InsetMathTBinom::dw(int height) const
637 void InsetMathTBinom::metrics(MetricsInfo & mi, Dimension & dim) const
639 StyleChanger dummy(mi.base, LM_ST_SCRIPT);
640 Dimension dim0, dim1;
641 cell(0).metrics(mi, dim0);
642 cell(1).metrics(mi, dim1);
643 dim.asc = dim0.height() + 4 + 5;
644 dim.des = dim1.height() + 4 - 5;
645 dim.wid = max(dim0.width(), dim1.wid) + 2 * dw(dim.height()) + 4;
646 metricsMarkers2(dim);
650 void InsetMathTBinom::draw(PainterInfo & pi, int x, int y) const
652 StyleChanger dummy(pi.base, LM_ST_SCRIPT);
653 Dimension const dim = dimension(*pi.base.bv);
654 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
655 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
656 int m = x + dim.width() / 2;
657 cell(0).draw(pi, m - dim0.width() / 2, y - dim0.des - 3 - 5);
658 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
659 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()), dim.height(), from_ascii("("));
660 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()), y - dim.ascent(),
661 dw(dim.height()), dim.height(), from_ascii(")"));
662 drawMarkers2(pi, x, y);
666 docstring InsetMathTBinom::name() const
668 return from_ascii("tbinom");
671 void InsetMathTBinom::mathmlize(MathStream & os) const
673 os << MTag("mtbinom") << cell(0) << cell(1) << ETag("mtbinom");
676 void InsetMathTBinom::validate(LaTeXFeatures & features) const
678 features.require("amsmath");
679 InsetMathNest::validate(features);