2 * \file InsetMathFrac.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"
16 #include "MathStream.h"
17 #include "TextPainter.h"
18 #include "LaTeXFeatures.h"
20 #include "frontends/Painter.h"
25 InsetMathFrac::InsetMathFrac(Kind kind, InsetMath::idx_type ncells)
26 : InsetMathFracBase(ncells), kind_(kind)
30 Inset * InsetMathFrac::clone() const
32 return new InsetMathFrac(*this);
36 InsetMathFrac * InsetMathFrac::asFracInset()
38 return kind_ == ATOP ? 0 : this;
42 InsetMathFrac const * InsetMathFrac::asFracInset() const
44 return kind_ == ATOP ? 0 : this;
48 bool InsetMathFrac::idxRight(Cursor & cur) const
50 InsetMath::idx_type target = 0;
51 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
54 else if (nargs() == 2)
58 if (cur.idx() == target)
61 cur.pos() = cell(target).x2pos(cur.x_target());
66 bool InsetMathFrac::idxLeft(Cursor & cur) const
68 InsetMath::idx_type target = 0;
69 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
72 else if (nargs() == 2)
76 if (cur.idx() == target)
79 cur.pos() = cell(target).x2pos(cur.x_target());
84 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
86 Dimension dim0, dim1, dim2;
88 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
90 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
91 cell(0).metrics(mi, dim0);
92 dim.wid = dim0.width()+ 3;
95 } else if (nargs() == 2) {
96 cell(0).metrics(mi, dim0);
97 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
98 cell(1).metrics(mi, dim1);
99 dim.wid = dim0.width() + dim1.wid + 5;
100 dim.asc = std::max(dim0.asc, dim1.asc);
101 dim.des = std::max(dim0.des, dim1.des);
103 cell(2).metrics(mi, dim2);
104 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
105 FracChanger dummy(mi.base);
106 cell(0).metrics(mi, dim0);
107 cell(1).metrics(mi, dim1);
108 dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
109 dim.asc = std::max(dim2.asc, dim0.height() + 5);
110 dim.des = std::max(dim2.des, dim1.height() - 5);
113 FracChanger dummy(mi.base);
114 cell(0).metrics(mi, dim0);
115 cell(1).metrics(mi, dim1);
117 cell(2).metrics(mi, dim2);
119 if (kind_ == NICEFRAC) {
120 dim.wid = dim0.width() + dim1.wid + 5;
121 dim.asc = dim0.height() + 5;
122 dim.des = dim1.height() - 5;
123 } else if (kind_ == UNITFRAC) {
124 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
125 dim.wid = dim0.width() + dim1.wid + 5;
126 dim.asc = dim0.height() + 5;
127 dim.des = dim1.height() - 5;
129 dim.wid = std::max(dim0.width(), dim1.wid) + 2;
130 dim.asc = dim0.height() + 2 + 5;
131 dim.des = dim1.height() + 2 - 5;
135 // Cache the inset dimension.
136 setDimCache(mi, dim);
140 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
142 setPosCache(pi, x, y);
143 Dimension const dim = dimension(*pi.base.bv);
144 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
145 int m = x + dim.wid / 2;
146 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
148 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
149 cell(0).draw(pi, x + 1, y);
150 } else if (nargs() == 2) {
151 cell(0).draw(pi, x + 1, y);
152 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
153 cell(1).draw(pi, x + dim0.width() + 5, y);
155 cell(2).draw(pi, x + 1, y);
156 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
157 FracChanger dummy(pi.base);
158 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
159 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
160 int xx = x + dim2.wid + 5;
161 cell(0).draw(pi, xx + 2,
163 cell(1).draw(pi, xx + dim0.width() + 5,
167 FracChanger dummy(pi.base);
168 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
169 if (kind_ == NICEFRAC) {
170 cell(0).draw(pi, x + 2,
172 cell(1).draw(pi, x + dim0.width() + 5,
174 } else if (kind_ == UNITFRAC) {
175 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
176 cell(0).draw(pi, x + 2,
178 cell(1).draw(pi, x + dim0.width() + 5,
181 // Classical fraction
182 cell(0).draw(pi, m - dim0.width() / 2,
183 y - dim0.des - 2 - 5);
184 cell(1).draw(pi, m - dim1.wid / 2,
185 y + dim1.asc + 2 - 5);
188 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
192 xx += cell(2).dimension(*pi.base.bv).wid + 5;
194 pi.pain.line(xx + dim0.wid,
197 y - dim.asc + 2, Color_math);
199 if (kind_ == FRAC || kind_ == OVER)
200 pi.pain.line(x + 1, y - 5,
201 x + dim.wid - 2, y - 5, Color_math);
202 drawMarkers(pi, x, y);
206 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
208 Dimension dim0, dim1;
209 cell(0).metricsT(mi, dim0);
210 cell(1).metricsT(mi, dim1);
211 dim.wid = std::max(dim0.width(), dim1.wid);
212 dim.asc = dim0.height() + 1;
213 dim.des = dim1.height();
217 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
222 int m = x + dim.width() / 2;
223 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
224 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
225 // ASCII art: ignore niceties
226 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
227 pain.horizontalLine(x, y, dim.width());
232 void InsetMathFrac::write(WriteStream & os) const
236 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
239 // \\over is only for compatibility, normalize this to \\frac
240 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
246 InsetMathNest::write(os);
248 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
252 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
254 os << "\\unit{" << cell(0) << '}';
260 docstring InsetMathFrac::name() const
264 return from_ascii("frac");
266 return from_ascii("over");
268 return from_ascii("nicefrac");
270 return from_ascii("unitfrac");
272 return from_ascii("unit");
274 return from_ascii("atop");
276 // shut up stupid compiler
281 bool InsetMathFrac::extraBraces() const
283 return kind_ == ATOP || kind_ == OVER;
287 void InsetMathFrac::maple(MapleStream & os) const
289 os << '(' << cell(0) << ")/(" << cell(1) << ')';
293 void InsetMathFrac::mathematica(MathematicaStream & os) const
295 os << '(' << cell(0) << ")/(" << cell(1) << ')';
299 void InsetMathFrac::octave(OctaveStream & os) const
301 os << '(' << cell(0) << ")/(" << cell(1) << ')';
305 void InsetMathFrac::mathmlize(MathStream & os) const
307 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
311 void InsetMathFrac::validate(LaTeXFeatures & features) const
313 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
314 features.require("units");
315 InsetMathNest::validate(features);