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 = 0;
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 = 0;
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 int m = x + dim.wid / 2;
144 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
146 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
147 cell(0).draw(pi, x + 1, y);
148 } else if (nargs() == 2) {
149 cell(0).draw(pi, x + 1, y);
150 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
151 cell(1).draw(pi, x + dim0.width() + 5, y);
153 cell(2).draw(pi, x + 1, y);
154 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
155 FracChanger dummy(pi.base);
156 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
157 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
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 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
167 if (kind_ == NICEFRAC) {
168 cell(0).draw(pi, x + 2,
170 cell(1).draw(pi, x + dim0.width() + 5,
172 } else if (kind_ == UNITFRAC) {
173 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
174 cell(0).draw(pi, x + 2,
176 cell(1).draw(pi, x + dim0.width() + 5,
179 // Classical fraction
180 cell(0).draw(pi, m - dim0.width() / 2,
181 y - dim0.des - 2 - 5);
182 cell(1).draw(pi, m - dim1.wid / 2,
183 y + dim1.asc + 2 - 5);
186 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
189 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
192 pi.pain.line(xx + dim0.wid,
195 y - dim.asc + 2, Color::math);
197 if (kind_ == FRAC || kind_ == OVER)
198 pi.pain.line(x + 1, y - 5,
199 x + dim.wid - 2, y - 5, Color::math);
200 drawMarkers(pi, x, y);
204 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
206 Dimension dim0, dim1;
207 cell(0).metricsT(mi, dim0);
208 cell(1).metricsT(mi, dim1);
209 dim.wid = std::max(dim0.width(), dim1.wid);
210 dim.asc = dim0.height() + 1;
211 dim.des = dim1.height();
215 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
220 int m = x + dim.width() / 2;
221 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
222 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
223 // ASCII art: ignore niceties
224 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
225 pain.horizontalLine(x, y, dim.width());
230 void InsetMathFrac::write(WriteStream & os) const
234 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
237 // \\over is only for compatibility, normalize this to \\frac
238 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
244 InsetMathNest::write(os);
246 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
250 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
252 os << "\\unit{" << cell(0) << '}';
258 docstring InsetMathFrac::name() const
262 return from_ascii("frac");
264 return from_ascii("over");
266 return from_ascii("nicefrac");
268 return from_ascii("unitfrac");
270 return from_ascii("unit");
272 return from_ascii("atop");
274 // shut up stupid compiler
279 bool InsetMathFrac::extraBraces() const
281 return kind_ == ATOP || kind_ == OVER;
285 void InsetMathFrac::maple(MapleStream & os) const
287 os << '(' << cell(0) << ")/(" << cell(1) << ')';
291 void InsetMathFrac::mathematica(MathematicaStream & os) const
293 os << '(' << cell(0) << ")/(" << cell(1) << ')';
297 void InsetMathFrac::octave(OctaveStream & os) const
299 os << '(' << cell(0) << ")/(" << cell(1) << ')';
303 void InsetMathFrac::mathmlize(MathStream & os) const
305 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
309 void InsetMathFrac::validate(LaTeXFeatures & features) const
311 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
312 features.require("units");
313 InsetMathNest::validate(features);