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);
118 cell(2).metrics(mi, dim2);
120 if (kind_ == NICEFRAC) {
121 dim.wid = dim0.width() + dim1.wid + 5;
122 dim.asc = dim0.height() + 5;
123 dim.des = dim1.height() - 5;
124 } else if (kind_ == UNITFRAC) {
125 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
126 dim.wid = dim0.width() + dim1.wid + 5;
127 dim.asc = dim0.height() + 5;
128 dim.des = dim1.height() - 5;
130 dim.wid = std::max(dim0.width(), dim1.wid) + 2;
131 dim.asc = dim0.height() + 2 + 5;
132 dim.des = dim1.height() + 2 - 5;
136 // Cache the inset dimension.
137 setDimCache(mi, dim);
141 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
143 setPosCache(pi, x, y);
144 Dimension const dim = dimension(*pi.base.bv);
145 Dimension const dim0 = cell(0).dimension(*pi.base.bv);
146 int m = x + dim.wid / 2;
147 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
149 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
150 cell(0).draw(pi, x + 1, y);
151 } else if (nargs() == 2) {
152 cell(0).draw(pi, x + 1, y);
153 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
154 cell(1).draw(pi, x + dim0.width() + 5, y);
156 cell(2).draw(pi, x + 1, y);
157 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
158 FracChanger dummy(pi.base);
159 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
160 Dimension const dim2 = cell(2).dimension(*pi.base.bv);
161 int xx = x + dim2.wid + 5;
162 cell(0).draw(pi, xx + 2,
164 cell(1).draw(pi, xx + dim0.width() + 5,
168 FracChanger dummy(pi.base);
169 Dimension const dim1 = cell(1).dimension(*pi.base.bv);
170 if (kind_ == NICEFRAC) {
171 cell(0).draw(pi, x + 2,
173 cell(1).draw(pi, x + dim0.width() + 5,
175 } else if (kind_ == UNITFRAC) {
176 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
177 cell(0).draw(pi, x + 2,
179 cell(1).draw(pi, x + dim0.width() + 5,
182 // Classical fraction
183 cell(0).draw(pi, m - dim0.width() / 2,
184 y - dim0.des - 2 - 5);
185 cell(1).draw(pi, m - dim1.wid / 2,
186 y + dim1.asc + 2 - 5);
189 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
193 xx += cell(2).dimension(*pi.base.bv).wid + 5;
195 pi.pain.line(xx + dim0.wid,
198 y - dim.asc + 2, Color::math);
200 if (kind_ == FRAC || kind_ == OVER)
201 pi.pain.line(x + 1, y - 5,
202 x + dim.wid - 2, y - 5, Color::math);
203 drawMarkers(pi, x, y);
207 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
209 Dimension dim0, dim1;
210 cell(0).metricsT(mi, dim0);
211 cell(1).metricsT(mi, dim1);
212 dim.wid = std::max(dim0.width(), dim1.wid);
213 dim.asc = dim0.height() + 1;
214 dim.des = dim1.height();
218 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
223 int m = x + dim.width() / 2;
224 cell(0).drawT(pain, m - dim0.width() / 2, y - dim0.des - 1);
225 cell(1).drawT(pain, m - dim1.wid / 2, y + dim1.asc);
226 // ASCII art: ignore niceties
227 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
228 pain.horizontalLine(x, y, dim.width());
233 void InsetMathFrac::write(WriteStream & os) const
237 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
240 // \\over is only for compatibility, normalize this to \\frac
241 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
247 InsetMathNest::write(os);
249 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
253 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
255 os << "\\unit{" << cell(0) << '}';
261 docstring InsetMathFrac::name() const
265 return from_ascii("frac");
267 return from_ascii("over");
269 return from_ascii("nicefrac");
271 return from_ascii("unitfrac");
273 return from_ascii("unit");
275 return from_ascii("atop");
277 // shut up stupid compiler
282 bool InsetMathFrac::extraBraces() const
284 return kind_ == ATOP || kind_ == OVER;
288 void InsetMathFrac::maple(MapleStream & os) const
290 os << '(' << cell(0) << ")/(" << cell(1) << ')';
294 void InsetMathFrac::mathematica(MathematicaStream & os) const
296 os << '(' << cell(0) << ")/(" << cell(1) << ')';
300 void InsetMathFrac::octave(OctaveStream & os) const
302 os << '(' << cell(0) << ")/(" << cell(1) << ')';
306 void InsetMathFrac::mathmlize(MathStream & os) const
308 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
312 void InsetMathFrac::validate(LaTeXFeatures & features) const
314 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
315 features.require("units");
316 InsetMathNest::validate(features);