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 bool 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;
138 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
140 setPosCache(pi, x, y);
141 int m = x + dim_.wid / 2;
142 if (kind_ == UNIT || (kind_ == UNITFRAC && nargs() == 3)) {
144 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
145 cell(0).draw(pi, x + 1, y);
146 } else if (nargs() == 2) {
147 cell(0).draw(pi, x + 1, y);
148 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
149 cell(1).draw(pi, x + cell(0).width() + 5, y);
151 cell(2).draw(pi, x + 1, y);
152 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
153 FracChanger dummy(pi.base);
154 int xx = x + cell(2).width() + 5;
155 cell(0).draw(pi, xx + 2,
156 y - cell(0).descent() - 5);
157 cell(1).draw(pi, xx + cell(0).width() + 5,
158 y + cell(1).ascent() / 2);
161 FracChanger dummy(pi.base);
162 if (kind_ == NICEFRAC) {
163 cell(0).draw(pi, x + 2,
164 y - cell(0).descent() - 5);
165 cell(1).draw(pi, x + cell(0).width() + 5,
166 y + cell(1).ascent() / 2);
167 } else if (kind_ == UNITFRAC) {
168 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
169 cell(0).draw(pi, x + 2,
170 y - cell(0).descent() - 5);
171 cell(1).draw(pi, x + cell(0).width() + 5,
172 y + cell(1).ascent() / 2);
174 // Classical fraction
175 cell(0).draw(pi, m - cell(0).width() / 2,
176 y - cell(0).descent() - 2 - 5);
177 cell(1).draw(pi, m - cell(1).width() / 2,
178 y + cell(1).ascent() + 2 - 5);
181 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
185 xx += cell(2).width() + 5;
186 pi.pain.line(xx + cell(0).width(),
188 xx + cell(0).width() + 5,
189 y - dim_.asc + 2, Color::math);
191 if (kind_ == FRAC || kind_ == OVER)
192 pi.pain.line(x + 1, y - 5,
193 x + dim_.wid - 2, y - 5, Color::math);
194 drawMarkers(pi, x, y);
198 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
200 cell(0).metricsT(mi, dim);
201 cell(1).metricsT(mi, dim);
202 dim.wid = std::max(cell(0).width(), cell(1).width());
203 dim.asc = cell(0).height() + 1;
204 dim.des = cell(1).height();
209 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);