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_ == UNITFRAC3)
54 else if (kind_ == UNIT)
58 if (cur.idx() == target)
61 cur.pos() = cell(target).x2pos(cur.x_target());
66 bool InsetMathFrac::idxLeft(Cursor & cur) const
68 InsetMath::idx_type target;
69 if (kind_ == UNITFRAC3)
71 else if (kind_ == UNIT)
75 if (cur.idx() == target)
78 cur.pos() = cell(target).x2pos(cur.x_target());
83 bool InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
87 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
89 dim.wid = cell(0).width() + cell(1).width() + 5;
90 dim.asc = std::max(cell(0).ascent(), cell(1).ascent());
91 dim.des = std::max(cell(0).descent(), cell(1).descent());
92 } else if (kind_ == UNITFRAC3) {
94 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
95 FracChanger dummy(mi.base);
98 dim.wid = cell(0).width() + cell(1).width() + cell(2).width() + 10;
99 dim.asc = std::max(cell(2).ascent(), cell(0).height() + 5);
100 dim.des = std::max(cell(2).descent(), cell(1).height() - 5);
102 FracChanger dummy(mi.base);
105 if (kind_ == NICEFRAC) {
106 dim.wid = cell(0).width() + cell(1).width() + 5;
107 dim.asc = cell(0).height() + 5;
108 dim.des = cell(1).height() - 5;
109 } else if (kind_ == UNITFRAC) {
110 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
111 dim.wid = cell(0).width() + cell(1).width() + 5;
112 dim.asc = cell(0).height() + 5;
113 dim.des = cell(1).height() - 5;
115 dim.wid = std::max(cell(0).width(), cell(1).width()) + 2;
116 dim.asc = cell(0).height() + 2 + 5;
117 dim.des = cell(1).height() + 2 - 5;
128 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
130 setPosCache(pi, x, y);
131 int m = x + dim_.wid / 2;
133 cell(0).draw(pi, x + 1, y);
134 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
135 cell(1).draw(pi, x + cell(0).width() + 5, y);
136 } else if (kind_ == UNITFRAC3) {
137 cell(2).draw(pi, x + 1, y);
138 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
139 FracChanger dummy(pi.base);
140 int xx = x + cell(2).width() + 5;
141 cell(0).draw(pi, xx + 2,
142 y - cell(0).descent() - 5);
143 cell(1).draw(pi, xx + cell(0).width() + 5,
144 y + cell(1).ascent() / 2);
146 FracChanger dummy(pi.base);
147 if (kind_ == NICEFRAC) {
148 cell(0).draw(pi, x + 2,
149 y - cell(0).descent() - 5);
150 cell(1).draw(pi, x + cell(0).width() + 5,
151 y + cell(1).ascent() / 2);
152 } else if (kind_ == UNITFRAC) {
153 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
154 cell(0).draw(pi, x + 2,
155 y - cell(0).descent() - 5);
156 cell(1).draw(pi, x + cell(0).width() + 5,
157 y + cell(1).ascent() / 2);
159 // Classical fraction
160 cell(0).draw(pi, m - cell(0).width() / 2,
161 y - cell(0).descent() - 2 - 5);
162 cell(1).draw(pi, m - cell(1).width() / 2,
163 y + cell(1).ascent() + 2 - 5);
166 if (kind_ == NICEFRAC || kind_ == UNITFRAC || kind_ == UNITFRAC3) {
169 if (kind_ == UNITFRAC3)
170 xx += cell(2).width() + 5;
171 pi.pain.line(xx + cell(0).width(),
173 xx + cell(0).width() + 5,
174 y - dim_.asc + 2, Color::math);
176 if (kind_ == FRAC || kind_ == OVER)
177 pi.pain.line(x + 1, y - 5,
178 x + dim_.wid - 2, y - 5, Color::math);
179 drawMarkers(pi, x, y);
183 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
185 cell(0).metricsT(mi, dim);
186 cell(1).metricsT(mi, dim);
187 dim.wid = std::max(cell(0).width(), cell(1).width());
188 dim.asc = cell(0).height() + 1;
189 dim.des = cell(1).height();
194 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
196 int m = x + dim_.width() / 2;
197 cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
198 cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
199 // ASCII art: ignore niceties
200 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
201 pain.horizontalLine(x, y, dim_.width());
205 void InsetMathFrac::write(WriteStream & os) const
209 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
212 // \\over is only for compatibility, normalize this to \\frac
213 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
218 InsetMathNest::write(os);
221 os << "\\unit[" << cell(0) << "]{" << cell(1) << '}';
224 os << "\\unitfrac[" << cell(2) << "]{" << cell(0) << "}{" << cell(1) << '}';
230 docstring InsetMathFrac::name() const
234 return from_ascii("frac");
236 return from_ascii("over");
238 return from_ascii("nicefrac");
240 return from_ascii("unitfrac");
242 return from_ascii("unitfracthree");
244 return from_ascii("unit");
246 return from_ascii("atop");
248 // shut up stupid compiler
253 bool InsetMathFrac::extraBraces() const
255 return kind_ == ATOP || kind_ == OVER;
259 void InsetMathFrac::maple(MapleStream & os) const
261 os << '(' << cell(0) << ")/(" << cell(1) << ')';
265 void InsetMathFrac::mathematica(MathematicaStream & os) const
267 os << '(' << cell(0) << ")/(" << cell(1) << ')';
271 void InsetMathFrac::octave(OctaveStream & os) const
273 os << '(' << cell(0) << ")/(" << cell(1) << ')';
277 void InsetMathFrac::mathmlize(MathStream & os) const
279 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
283 void InsetMathFrac::validate(LaTeXFeatures & features) const
285 if (kind_ == NICEFRAC || kind_ == UNITFRAC
286 || kind_ == UNIT || kind_ == UNITFRAC3)
287 features.require("units");
288 InsetMathNest::validate(features);