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"
21 #include "frontends/Painter.h"
26 InsetMathFrac::InsetMathFrac(Kind kind, InsetMath::idx_type ncells)
27 : InsetMathFracBase(ncells), kind_(kind)
31 Inset * InsetMathFrac::clone() const
33 return new InsetMathFrac(*this);
37 InsetMathFrac * InsetMathFrac::asFracInset()
39 return kind_ == ATOP ? 0 : this;
43 InsetMathFrac const * InsetMathFrac::asFracInset() const
45 return kind_ == ATOP ? 0 : this;
49 bool InsetMathFrac::idxRight(Cursor & cur) const
51 InsetMath::idx_type target;
52 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
55 else if (nargs() == 2)
59 if (cur.idx() == target)
62 cur.pos() = cell(target).x2pos(cur.x_target());
67 bool InsetMathFrac::idxLeft(Cursor & cur) const
69 InsetMath::idx_type target;
70 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
73 else if (nargs() == 2)
77 if (cur.idx() == target)
80 cur.pos() = cell(target).x2pos(cur.x_target());
85 void InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
87 Dimension dim0, dim1, dim2;
89 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
91 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
92 cell(0).metrics(mi, dim0);
93 dim.wid = dim0.width()+ 3;
96 } else if (nargs() == 2) {
97 cell(0).metrics(mi, dim0);
98 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
99 cell(1).metrics(mi, dim1);
100 dim.wid = dim0.width() + dim1.wid + 5;
101 dim.asc = std::max(dim0.asc, dim1.asc);
102 dim.des = std::max(dim0.des, dim1.des);
104 cell(2).metrics(mi, dim2);
105 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
106 FracChanger dummy(mi.base);
107 cell(0).metrics(mi, dim0);
108 cell(1).metrics(mi, dim1);
109 dim.wid = dim0.width() + dim1.wid + dim2.wid + 10;
110 dim.asc = std::max(dim2.asc, dim0.height() + 5);
111 dim.des = std::max(dim2.des, dim1.height() - 5);
114 FracChanger dummy(mi.base);
115 cell(0).metrics(mi, dim0);
116 cell(1).metrics(mi, dim1);
117 if (kind_ == NICEFRAC) {
118 dim.wid = dim0.width() + dim1.wid + 5;
119 dim.asc = dim0.height() + 5;
120 dim.des = dim1.height() - 5;
121 } else if (kind_ == UNITFRAC) {
122 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
123 dim.wid = dim0.width() + dim1.wid + 5;
124 dim.asc = dim0.height() + 5;
125 dim.des = dim1.height() - 5;
127 dim.wid = std::max(dim0.width(), dim1.wid) + 2;
128 dim.asc = dim0.height() + 2 + 5;
129 dim.des = dim1.height() + 2 - 5;
133 // Cache the inset dimension.
134 setDimCache(mi, dim);
138 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
140 setPosCache(pi, x, y);
141 Dimension const dim = dimension(*pi.base.bv);
142 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
143 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
144 Dimension const dim2 = 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 int xx = x + dim2.wid + 5;
159 cell(0).draw(pi, xx + 2,
161 cell(1).draw(pi, xx + dim0.width() + 5,
165 FracChanger dummy(pi.base);
166 if (kind_ == NICEFRAC) {
167 cell(0).draw(pi, x + 2,
169 cell(1).draw(pi, x + dim0.width() + 5,
171 } else if (kind_ == UNITFRAC) {
172 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
173 cell(0).draw(pi, x + 2,
175 cell(1).draw(pi, x + dim0.width() + 5,
178 // Classical fraction
179 cell(0).draw(pi, m - dim0.width() / 2,
180 y - dim0.des - 2 - 5);
181 cell(1).draw(pi, m - dim1.wid / 2,
182 y + dim1.asc + 2 - 5);
185 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
190 pi.pain.line(xx + dim0.wid,
193 y - dim.asc + 2, Color::math);
195 if (kind_ == FRAC || kind_ == OVER)
196 pi.pain.line(x + 1, y - 5,
197 x + dim.wid - 2, y - 5, Color::math);
198 drawMarkers(pi, x, y);
202 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
204 Dimension dim0, dim1;
205 cell(0).metricsT(mi, dim0);
206 cell(1).metricsT(mi, dim1);
207 dim.wid = std::max(dim0.width(), dim1.wid);
208 dim.asc = dim0.height() + 1;
209 dim.des = dim1.height();
213 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
218 int m = x + dim.width() / 2;
219 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
220 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
221 // ASCII art: ignore niceties
222 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
223 pain.horizontalLine(x, y, dim.width());
228 void InsetMathFrac::write(WriteStream & os) const
232 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
235 // \\over is only for compatibility, normalize this to \\frac
236 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
242 InsetMathNest::write(os);
244 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
248 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
250 os << "\\unit{" << cell(0) << '}';
256 docstring InsetMathFrac::name() const
260 return from_ascii("frac");
262 return from_ascii("over");
264 return from_ascii("nicefrac");
266 return from_ascii("unitfrac");
268 return from_ascii("unit");
270 return from_ascii("atop");
272 // shut up stupid compiler
277 bool InsetMathFrac::extraBraces() const
279 return kind_ == ATOP || kind_ == OVER;
283 void InsetMathFrac::maple(MapleStream & os) const
285 os << '(' << cell(0) << ")/(" << cell(1) << ')';
289 void InsetMathFrac::mathematica(MathematicaStream & os) const
291 os << '(' << cell(0) << ")/(" << cell(1) << ')';
295 void InsetMathFrac::octave(OctaveStream & os) const
297 os << '(' << cell(0) << ")/(" << cell(1) << ')';
301 void InsetMathFrac::mathmlize(MathStream & os) const
303 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
307 void InsetMathFrac::validate(LaTeXFeatures & features) const
309 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
310 features.require("units");
311 InsetMathNest::validate(features);