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 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
89 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
91 dim.wid = cell(0).width()+ 3;
92 dim.asc = cell(0).ascent();
93 dim.des = cell(0).descent();
94 } else if (nargs() == 2) {
96 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
98 dim.wid = cell(0).width() + cell(1).width() + 5;
99 dim.asc = std::max(cell(0).ascent(), cell(1).ascent());
100 dim.des = std::max(cell(0).descent(), cell(1).descent());
103 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
104 FracChanger dummy(mi.base);
107 dim.wid = cell(0).width() + cell(1).width() + cell(2).width() + 10;
108 dim.asc = std::max(cell(2).ascent(), cell(0).height() + 5);
109 dim.des = std::max(cell(2).descent(), cell(1).height() - 5);
112 FracChanger dummy(mi.base);
115 if (kind_ == NICEFRAC) {
116 dim.wid = cell(0).width() + cell(1).width() + 5;
117 dim.asc = cell(0).height() + 5;
118 dim.des = cell(1).height() - 5;
119 } else if (kind_ == UNITFRAC) {
120 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
121 dim.wid = cell(0).width() + cell(1).width() + 5;
122 dim.asc = cell(0).height() + 5;
123 dim.des = cell(1).height() - 5;
125 dim.wid = std::max(cell(0).width(), cell(1).width()) + 2;
126 dim.asc = cell(0).height() + 2 + 5;
127 dim.des = cell(1).height() + 2 - 5;
131 // Cache the inset dimension.
132 setDimCache(mi, dim);
136 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
138 setPosCache(pi, x, y);
139 Dimension const dim = dimension(*pi.base.bv);
140 int m = x + dim.wid / 2;
141 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
143 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
144 cell(0).draw(pi, x + 1, y);
145 } else if (nargs() == 2) {
146 cell(0).draw(pi, x + 1, y);
147 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
148 cell(1).draw(pi, x + cell(0).width() + 5, y);
150 cell(2).draw(pi, x + 1, y);
151 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
152 FracChanger dummy(pi.base);
153 int xx = x + cell(2).width() + 5;
154 cell(0).draw(pi, xx + 2,
155 y - cell(0).descent() - 5);
156 cell(1).draw(pi, xx + cell(0).width() + 5,
157 y + cell(1).ascent() / 2);
160 FracChanger dummy(pi.base);
161 if (kind_ == NICEFRAC) {
162 cell(0).draw(pi, x + 2,
163 y - cell(0).descent() - 5);
164 cell(1).draw(pi, x + cell(0).width() + 5,
165 y + cell(1).ascent() / 2);
166 } else if (kind_ == UNITFRAC) {
167 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
168 cell(0).draw(pi, x + 2,
169 y - cell(0).descent() - 5);
170 cell(1).draw(pi, x + cell(0).width() + 5,
171 y + cell(1).ascent() / 2);
173 // Classical fraction
174 cell(0).draw(pi, m - cell(0).width() / 2,
175 y - cell(0).descent() - 2 - 5);
176 cell(1).draw(pi, m - cell(1).width() / 2,
177 y + cell(1).ascent() + 2 - 5);
180 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
184 xx += cell(2).width() + 5;
185 pi.pain.line(xx + cell(0).width(),
187 xx + cell(0).width() + 5,
188 y - dim.asc + 2, Color::math);
190 if (kind_ == FRAC || kind_ == OVER)
191 pi.pain.line(x + 1, y - 5,
192 x + dim.wid - 2, y - 5, Color::math);
193 drawMarkers(pi, x, y);
197 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
199 cell(0).metricsT(mi, dim);
200 cell(1).metricsT(mi, dim);
201 dim.wid = std::max(cell(0).width(), cell(1).width());
202 dim.asc = cell(0).height() + 1;
203 dim.des = cell(1).height();
207 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
211 int m = x + dim.width() / 2;
212 cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
213 cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
214 // ASCII art: ignore niceties
215 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
216 pain.horizontalLine(x, y, dim.width());
220 void InsetMathFrac::write(WriteStream & os) const
224 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
227 // \\over is only for compatibility, normalize this to \\frac
228 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
234 InsetMathNest::write(os);
236 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
240 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
242 os << "\\unit{" << cell(0) << '}';
248 docstring InsetMathFrac::name() const
252 return from_ascii("frac");
254 return from_ascii("over");
256 return from_ascii("nicefrac");
258 return from_ascii("unitfrac");
260 return from_ascii("unit");
262 return from_ascii("atop");
264 // shut up stupid compiler
269 bool InsetMathFrac::extraBraces() const
271 return kind_ == ATOP || kind_ == OVER;
275 void InsetMathFrac::maple(MapleStream & os) const
277 os << '(' << cell(0) << ")/(" << cell(1) << ')';
281 void InsetMathFrac::mathematica(MathematicaStream & os) const
283 os << '(' << cell(0) << ")/(" << cell(1) << ')';
287 void InsetMathFrac::octave(OctaveStream & os) const
289 os << '(' << cell(0) << ")/(" << cell(1) << ')';
293 void InsetMathFrac::mathmlize(MathStream & os) const
295 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
299 void InsetMathFrac::validate(LaTeXFeatures & features) const
301 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
302 features.require("units");
303 InsetMathNest::validate(features);