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, Color_math);
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, Color_math);
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 void InsetMathFrac::maple(MapleStream & os) const
371 os << '(' << cell(0) << ")/(" << cell(1) << ')';
375 void InsetMathFrac::mathematica(MathematicaStream & os) const
377 os << '(' << cell(0) << ")/(" << cell(1) << ')';
381 void InsetMathFrac::octave(OctaveStream & os) const
383 os << '(' << cell(0) << ")/(" << cell(1) << ')';
387 void InsetMathFrac::mathmlize(MathStream & os) const
390 << MTag("mrow") << cell(0) << ETag("mrow")
391 << MTag("mrow") << cell(1) << ETag("mrow")
396 void InsetMathFrac::validate(LaTeXFeatures & features) const
398 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
399 features.require("units");
400 if (kind_ == CFRAC || kind_ == CFRACLEFT || kind_ == CFRACRIGHT
401 || kind_ == DFRAC || kind_ == TFRAC)
402 features.require("amsmath");
403 InsetMathNest::validate(features);
407 /////////////////////////////////////////////////////////////////////
411 /////////////////////////////////////////////////////////////////////
414 InsetMathBinom::InsetMathBinom(Buffer * buf, Kind kind)
415 : InsetMathFracBase(buf), kind_(kind)
419 Inset * InsetMathBinom::clone() const
421 return new InsetMathBinom(*this);
425 int InsetMathBinom::dw(int height) const
436 void InsetMathBinom::metrics(MetricsInfo & mi, Dimension & dim) const
438 Dimension dim0, dim1;
440 // FIXME: for an unknown reason the cells must be set directly
441 // after the StyleChanger and cannot be set after the if case
442 if (kind_ == DBINOM) {
443 StyleChanger dummy(mi.base, LM_ST_DISPLAY);
444 cell(0).metrics(mi, dim0);
445 cell(1).metrics(mi, dim1);
446 } else if (kind_ == TBINOM) {
447 StyleChanger dummy(mi.base, LM_ST_SCRIPT);
448 cell(0).metrics(mi, dim0);
449 cell(1).metrics(mi, dim1);
451 FracChanger dummy(mi.base);
452 cell(0).metrics(mi, dim0);
453 cell(1).metrics(mi, dim1);
455 dim.asc = dim0.height() + 4 + 5;
456 dim.des = dim1.height() + 4 - 5;
457 dim.wid = max(dim0.wid, dim1.wid) + 2 * dw(dim.height()) + 4;
458 metricsMarkers2(dim);
462 void InsetMathBinom::draw(PainterInfo & pi, int x, int y) const
464 Dimension const dim = dimension(*pi.base.bv);
465 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
466 Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
467 // define the binom brackets
468 docstring const bra = kind_ == BRACE ? from_ascii("{") :
469 kind_ == BRACK ? from_ascii("[") : from_ascii("(");
470 docstring const ket = kind_ == BRACE ? from_ascii("}") :
471 kind_ == BRACK ? from_ascii("]") : from_ascii(")");
473 int m = x + dim.width() / 2;
474 // FIXME: for an unknown reason the cells must be drawn directly
475 // after the StyleChanger and cannot be drawn after the if case
476 if (kind_ == DBINOM) {
477 StyleChanger dummy(pi.base, LM_ST_DISPLAY);
478 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
479 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
480 } else if (kind_ == TBINOM) {
481 StyleChanger dummy(pi.base, LM_ST_SCRIPT);
482 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
483 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
485 FracChanger dummy2(pi.base);
486 cell(0).draw(pi, m - dim0.wid / 2, y - dim0.des - 3 - 5);
487 cell(1).draw(pi, m - dim1.wid / 2, y + dim1.asc + 3 - 5);
489 // draw the brackets and the marker
490 mathed_draw_deco(pi, x, y - dim.ascent(), dw(dim.height()),
492 mathed_draw_deco(pi, x + dim.width() - dw(dim.height()),
493 y - dim.ascent(), dw(dim.height()), dim.height(), ket);
494 drawMarkers2(pi, x, y);
498 bool InsetMathBinom::extraBraces() const
500 return kind_ == CHOOSE || kind_ == BRACE || kind_ == BRACK;
504 void InsetMathBinom::write(WriteStream & os) const
506 MathEnsurer ensurer(os);
509 os << "\\binom{" << cell(0) << "}{" << cell(1) << '}';
512 os << "\\dbinom{" << cell(0) << "}{" << cell(1) << '}';
515 os << "\\tbinom{" << cell(0) << "}{" << cell(1) << '}';
518 os << '{' << cell(0) << " \\choose " << cell(1) << '}';
521 os << '{' << cell(0) << " \\brace " << cell(1) << '}';
524 os << '{' << cell(0) << " \\brack " << cell(1) << '}';
530 void InsetMathBinom::normalize(NormalStream & os) const
532 os << "[binom " << cell(0) << ' ' << cell(1) << ']';
536 void InsetMathBinom::mathmlize(MathStream & os) const
557 os << "<mo fence='true' stretchy='true' form='prefix'>" << ldelim << "</mo>"
558 << "<mfrac linethickness='0'>"
559 << cell(0) << cell(1)
561 << "<mo fence='true' stretchy='true' form='postfix'>" << rdelim << "</mo>";
565 void InsetMathBinom::validate(LaTeXFeatures & features) const
568 features.require("binom");
569 if (kind_ == DBINOM || kind_ == TBINOM)
570 features.require("amsmath");
571 InsetMathNest::validate(features);