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;
135 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
137 setPosCache(pi, x, y);
138 int m = x + dim_.wid / 2;
139 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
141 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
142 cell(0).draw(pi, x + 1, y);
143 } else if (nargs() == 2) {
144 cell(0).draw(pi, x + 1, y);
145 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
146 cell(1).draw(pi, x + cell(0).width() + 5, y);
148 cell(2).draw(pi, x + 1, y);
149 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
150 FracChanger dummy(pi.base);
151 int xx = x + cell(2).width() + 5;
152 cell(0).draw(pi, xx + 2,
153 y - cell(0).descent() - 5);
154 cell(1).draw(pi, xx + cell(0).width() + 5,
155 y + cell(1).ascent() / 2);
158 FracChanger dummy(pi.base);
159 if (kind_ == NICEFRAC) {
160 cell(0).draw(pi, x + 2,
161 y - cell(0).descent() - 5);
162 cell(1).draw(pi, x + cell(0).width() + 5,
163 y + cell(1).ascent() / 2);
164 } else if (kind_ == UNITFRAC) {
165 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
166 cell(0).draw(pi, x + 2,
167 y - cell(0).descent() - 5);
168 cell(1).draw(pi, x + cell(0).width() + 5,
169 y + cell(1).ascent() / 2);
171 // Classical fraction
172 cell(0).draw(pi, m - cell(0).width() / 2,
173 y - cell(0).descent() - 2 - 5);
174 cell(1).draw(pi, m - cell(1).width() / 2,
175 y + cell(1).ascent() + 2 - 5);
178 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
182 xx += cell(2).width() + 5;
183 pi.pain.line(xx + cell(0).width(),
185 xx + cell(0).width() + 5,
186 y - dim_.asc + 2, Color::math);
188 if (kind_ == FRAC || kind_ == OVER)
189 pi.pain.line(x + 1, y - 5,
190 x + dim_.wid - 2, y - 5, Color::math);
191 drawMarkers(pi, x, y);
195 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
197 cell(0).metricsT(mi, dim);
198 cell(1).metricsT(mi, dim);
199 dim.wid = std::max(cell(0).width(), cell(1).width());
200 dim.asc = cell(0).height() + 1;
201 dim.des = cell(1).height();
206 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
208 int m = x + dim_.width() / 2;
209 cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
210 cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
211 // ASCII art: ignore niceties
212 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
213 pain.horizontalLine(x, y, dim_.width());
217 void InsetMathFrac::write(WriteStream & os) const
221 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
224 // \\over is only for compatibility, normalize this to \\frac
225 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
231 InsetMathNest::write(os);
233 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
237 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
239 os << "\\unit{" << cell(0) << '}';
245 docstring InsetMathFrac::name() const
249 return from_ascii("frac");
251 return from_ascii("over");
253 return from_ascii("nicefrac");
255 return from_ascii("unitfrac");
257 return from_ascii("unit");
259 return from_ascii("atop");
261 // shut up stupid compiler
266 bool InsetMathFrac::extraBraces() const
268 return kind_ == ATOP || kind_ == OVER;
272 void InsetMathFrac::maple(MapleStream & os) const
274 os << '(' << cell(0) << ")/(" << cell(1) << ')';
278 void InsetMathFrac::mathematica(MathematicaStream & os) const
280 os << '(' << cell(0) << ")/(" << cell(1) << ')';
284 void InsetMathFrac::octave(OctaveStream & os) const
286 os << '(' << cell(0) << ")/(" << cell(1) << ')';
290 void InsetMathFrac::mathmlize(MathStream & os) const
292 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
296 void InsetMathFrac::validate(LaTeXFeatures & features) const
298 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNIT)
299 features.require("units");
300 InsetMathNest::validate(features);