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 "MathExtern.h"
21 #include "MathStream.h"
22 #include "MathSupport.h"
23 #include "MetricsInfo.h"
24 #include "TextPainter.h"
26 #include "frontends/Painter.h"
32 /////////////////////////////////////////////////////////////////////
36 /////////////////////////////////////////////////////////////////////
39 InsetMathFracBase::InsetMathFracBase(Buffer * buf, idx_type ncells)
40 : InsetMathNest(buf, ncells)
44 bool InsetMathFracBase::idxUpDown(Cursor & cur, bool up) const
46 // If we only have one cell, target = 0, otherwise
47 // target = up ? 0 : 1, since upper cell has idx 0
48 InsetMath::idx_type target = nargs() > 1 ? !up : 0;
49 if (cur.idx() == target)
52 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
58 /////////////////////////////////////////////////////////////////////
62 /////////////////////////////////////////////////////////////////////
65 InsetMathFrac::InsetMathFrac(Buffer * buf, Kind kind, InsetMath::idx_type ncells)
66 : InsetMathFracBase(buf, ncells), kind_(kind)
70 Inset * InsetMathFrac::clone() const
72 return new InsetMathFrac(*this);
76 InsetMathFrac * InsetMathFrac::asFracInset()
78 return kind_ == ATOP ? 0 : this;
82 InsetMathFrac const * InsetMathFrac::asFracInset() const
84 return kind_ == ATOP ? 0 : this;
88 bool InsetMathFrac::idxForward(Cursor & cur) const
90 InsetMath::idx_type target = 0;
91 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
94 else if (nargs() == 2)
98 if (cur.idx() == target)
101 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
106 bool InsetMathFrac::idxBackward(Cursor & cur) const
108 InsetMath::idx_type target = 0;
109 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
112 else if (nargs() == 2)
116 if (cur.idx() == target)
119 cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
124 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
126 Dimension dim0, dim1, dim2;
128 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
130 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
131 cell(0).metrics(mi, dim0);
132 dim.wid = dim0.width()+ 3;
135 } else if (nargs() == 2) {
136 cell(0).metrics(mi, dim0);
137 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
138 cell(1).metrics(mi, dim1);
139 dim.wid = dim0.width() + dim1.wid + 5;
140 dim.asc = max(dim0.asc, dim1.asc);
141 dim.des = max(dim0.des, dim1.des);
143 cell(2).metrics(mi, dim2);
144 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
145 FracChanger dummy(mi.base);
146 cell(0).metrics(mi, dim0);
147 cell(1).metrics(mi, dim1);
148 dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
149 dim.asc = max(dim2.asc, dim0.height() + 5);
150 dim.des = max(dim2.des, dim1.height() - 5);
153 // general cell metrics used for \frac
154 FracChanger dummy(mi.base);
155 cell(0).metrics(mi, dim0);
156 cell(1).metrics(mi, dim1);
158 cell(2).metrics(mi, dim2);
159 // metrics for special fraction types
160 if (kind_ == NICEFRAC) {
161 dim.wid = dim0.width() + dim1.wid + 5;
162 dim.asc = dim0.height() + 5;
163 dim.des = dim1.height() - 5;
164 } else if (kind_ == UNITFRAC) {
165 ShapeChanger dummy2(mi.base.font, UP_SHAPE);
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
171 || kind_ == CFRACRIGHT || kind_ == DFRAC) {
172 // \cfrac and \dfrac are always in display size
173 StyleChanger dummy2(mi.base, LM_ST_DISPLAY);
174 cell(0).metrics(mi, dim0);
175 cell(1).metrics(mi, dim1);
176 } else if (kind_ == TFRAC) {
177 // tfrac is in always in text size
178 StyleChanger dummy2(mi.base, LM_ST_SCRIPT);
179 cell(0).metrics(mi, dim0);
180 cell(1).metrics(mi, dim1);
182 dim.wid = max(dim0.wid, dim1.wid) + 2;
183 dim.asc = dim0.height() + 2 + 5;
184 dim.des = dim1.height() + 2 - 5;
191 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
193 setPosCache(pi, x, y);
194 Dimension const dim = dimension(*pi.base.bv);
195 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
196 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
198 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
199 cell(0).draw(pi, x + 1, y);
200 } else if (nargs() == 2) {
201 cell(0).draw(pi, x + 1, y);
202 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
203 cell(1).draw(pi, x + dim0.width() + 5, y);
205 cell(2).draw(pi, x + 1, y);
206 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
207 FracChanger dummy(pi.base);
208 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
209 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
210 int xx = x + dim2.wid + 5;
211 cell(0).draw(pi, xx + 2,
213 cell(1).draw(pi, xx + dim0.width() + 5,
217 FracChanger dummy(pi.base);
218 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
219 int m = x + dim.wid / 2;
220 if (kind_ == NICEFRAC) {
221 cell(0).draw(pi, x + 2,
223 cell(1).draw(pi, x + dim0.width() + 5,
225 } else if (kind_ == UNITFRAC) {
226 ShapeChanger dummy2(pi.base.font, UP_SHAPE);
227 cell(0).draw(pi, x + 2, y - dim0.des - 5);
228 cell(1).draw(pi, x + dim0.width() + 5, y + dim1.asc / 2);
229 } else if (kind_ == FRAC || kind_ == ATOP || kind_ == OVER) {
230 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
231 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
232 } else if (kind_ == TFRAC) {
233 // tfrac is in always in text size
234 StyleChanger dummy2(pi.base, LM_ST_SCRIPT);
235 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
236 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
238 // \cfrac and \dfrac are always in display size
239 StyleChanger dummy2(pi.base, LM_ST_DISPLAY);
240 if (kind_ == CFRAC || kind_ == DFRAC)
241 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 2 - 5);
242 else if (kind_ == CFRACLEFT)
243 cell(0).draw(pi, x + 2, y - dim0.des - 2 - 5);
244 else if (kind_ == CFRACRIGHT)
245 cell(0).draw(pi, x + dim.wid - dim0.wid - 2,
246 y - dim0.des - 2 - 5);
247 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 2 - 5);
250 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
254 xx += cell(2).dimension(*pi.base.bv).wid + 5;
255 pi.pain.line(xx + dim0.wid,
258 y - dim.asc + 2, Color_math);
260 if (kind_ == FRAC || kind_ == CFRAC || kind_ == CFRACLEFT
261 || kind_ == CFRACRIGHT || kind_ == DFRAC
262 || kind_ == TFRAC || kind_ == OVER)
263 pi.pain.line(x + 1, y - 5,
264 x + dim.wid - 2, y - 5, Color_math);
265 drawMarkers(pi, x, y);
269 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
271 Dimension dim0, dim1;
272 cell(0).metricsT(mi, dim0);
273 cell(1).metricsT(mi, dim1);
274 dim.wid = max(dim0.width(), dim1.wid);
275 dim.asc = dim0.height() + 1;
276 dim.des = dim1.height();
280 void InsetMathFrac::drawT(TextPainter & /*pain*/, int /*x*/, int /*y*/) const
285 int m = x + dim.width() / 2;
286 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
287 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
288 // ASCII art: ignore niceties
289 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
290 pain.horizontalLine(x, y, dim.width());
295 void InsetMathFrac::write(WriteStream & os) const
297 MathEnsurer ensurer(os);
300 // \\atop is only for compatibility, \\binom is the
302 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
305 // \\over is only for compatibility, normalize this to \\frac
306 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
315 InsetMathNest::write(os);
317 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
321 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
323 os << "\\unit{" << cell(0) << '}';
326 os << "\\cfrac[l]{" << cell(0) << "}{" << cell(1) << '}';
329 os << "\\cfrac[r]{" << cell(0) << "}{" << cell(1) << '}';
335 docstring InsetMathFrac::name() const
339 return from_ascii("frac");
343 return from_ascii("cfrac");
345 return from_ascii("dfrac");
347 return from_ascii("tfrac");
349 return from_ascii("over");
351 return from_ascii("nicefrac");
353 return from_ascii("unitfrac");
355 return from_ascii("unit");
357 return from_ascii("atop");
359 // shut up stupid compiler
364 bool InsetMathFrac::extraBraces() const
366 return kind_ == ATOP || kind_ == OVER;
370 void InsetMathFrac::maple(MapleStream & os) const
372 os << '(' << cell(0) << ")/(" << cell(1) << ')';
376 void InsetMathFrac::mathematica(MathematicaStream & os) const
378 os << '(' << cell(0) << ")/(" << cell(1) << ')';
382 void InsetMathFrac::octave(OctaveStream & os) const
384 os << '(' << cell(0) << ")/(" << cell(1) << ')';
388 docstring InsetMathFrac::mathmlize(MathStream & os) const
390 os << MTag("mfrac") << MTag("mrow");
391 docstring rv = lyx::mathmlize(cell(0), os);
392 os << ETag("mrow") << MTag("mrow");
393 rv += lyx::mathmlize(cell(1), os);
394 os << ETag("mrow") << ETag("mfrac");
399 void InsetMathFrac::validate(LaTeXFeatures & features) const
401 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
402 features.require("units");
403 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
404 || kind_ == DFRAC || kind_ == TFRAC)
405 features.require("amsmath");
406 InsetMathNest::validate(features);
410 /////////////////////////////////////////////////////////////////////
414 /////////////////////////////////////////////////////////////////////
417 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
418 : InsetMathFracBase(buf), kind_(kind)
422 Inset * InsetMathBinom::clone() const
424 return new InsetMathBinom(*this);
428 int InsetMathBinom::dw(int height) const
439 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
441 Dimension dim0, dim1;
443 // FIXME: for an unknown reason the cells must be set directly
444 // after the StyleChanger and cannot be set after the if case
445 if (kind_ == DBINOM) {
446 StyleChanger dummy(mi.base, LM_ST_DISPLAY);
447 cell(0).metrics(mi, dim0);
448 cell(1).metrics(mi, dim1);
449 } else if (kind_ == TBINOM) {
450 StyleChanger dummy(mi.base, LM_ST_SCRIPT);
451 cell(0).metrics(mi, dim0);
452 cell(1).metrics(mi, dim1);
454 FracChanger dummy(mi.base);
455 cell(0).metrics(mi, dim0);
456 cell(1).metrics(mi, dim1);
458 dim.asc = dim0.height() + 4 + 5;
459 dim.des = dim1.height() + 4 - 5;
460 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
461 metricsMarkers2(dim);
465 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
467 Dimension const dim = dimension(*pi.base.bv);
468 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
469 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
470 // define the binom brackets
471 docstring const bra = kind_ == BRACE ? from_ascii("{") :
472 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
473 docstring const ket = kind_ == BRACE ? from_ascii("}") :
474 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
476 int m = x + dim.width() / 2;
477 // FIXME: for an unknown reason the cells must be drawn directly
478 // after the StyleChanger and cannot be drawn after the if case
479 if (kind_ == DBINOM) {
480 StyleChanger dummy(pi.base, LM_ST_DISPLAY);
481 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
482 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
483 } else if (kind_ == TBINOM) {
484 StyleChanger dummy(pi.base, LM_ST_SCRIPT);
485 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
486 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
488 FracChanger dummy2(pi.base);
489 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
490 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
492 // draw the brackets and the marker
493 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
495 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
496 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
497 drawMarkers2(pi, x, y);
501 bool InsetMathBinom::extraBraces() const
503 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
507 void InsetMathBinom::write(WriteStream & os) const
509 MathEnsurer ensurer(os);
512 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
515 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
518 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
521 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
524 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
527 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
533 void InsetMathBinom::normalize(NormalStream & os) const
535 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
539 docstring InsetMathBinom::mathmlize(MathStream & os) const
541 // FIXME This all needs fixing
544 os << MTag("mbinom") << cell(0) << cell(1) << ETag("mbinom");
547 os << MTag("mtbinom") << cell(0) << cell(1) << ETag("mtbinom");
551 os << MTag("mdbinom") << cell(0) << cell(1) << ETag("mdbinom");
558 void InsetMathBinom::validate(LaTeXFeatures & features) const
561 features.require("binom");
562 if (kind_ == DBINOM || kind_ == TBINOM)
563 features.require("amsmath");
564 InsetMathNest::validate(features);