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"
20 #include "frontends/Painter.h"
25 InsetMathFrac::InsetMathFrac(Kind kind)
30 Inset * InsetMathFrac::clone() const
32 return new InsetMathFrac(*this);
36 InsetMathFrac * InsetMathFrac::asFracInset()
38 return kind_ == ATOP ? 0 : this;
42 InsetMathFrac const * InsetMathFrac::asFracInset() const
44 return kind_ == ATOP ? 0 : this;
48 bool InsetMathFrac::metrics(MetricsInfo & mi, Dimension & dim) const
50 FracChanger dummy(mi.base);
53 if (kind_ == NICEFRAC) {
54 dim.wid = cell(0).width() + cell(1).width() + 5;
55 dim.asc = cell(0).height() + 5;
56 dim.des = cell(1).height() - 5;
57 } else if (kind_ == UNITFRAC) {
58 ShapeChanger dummy2(mi.base.font, Font::UP_SHAPE);
59 dim.wid = cell(0).width() + cell(1).width() + 5;
60 dim.asc = cell(0).height() + 5;
61 dim.des = cell(1).height() - 5;
63 dim.wid = std::max(cell(0).width(), cell(1).width()) + 2;
64 dim.asc = cell(0).height() + 2 + 5;
65 dim.des = cell(1).height() + 2 - 5;
75 void InsetMathFrac::draw(PainterInfo & pi, int x, int y) const
77 setPosCache(pi, x, y);
78 int m = x + dim_.wid / 2;
79 FracChanger dummy(pi.base);
80 if (kind_ == NICEFRAC) {
81 cell(0).draw(pi, x + 2,
82 y - cell(0).descent() - 5);
83 cell(1).draw(pi, x + cell(0).width() + 5,
84 y + cell(1).ascent() / 2);
85 } else if (kind_ == UNITFRAC) {
86 ShapeChanger dummy2(pi.base.font, Font::UP_SHAPE);
87 cell(0).draw(pi, x + 2,
88 y - cell(0).descent() - 5);
89 cell(1).draw(pi, x + cell(0).width() + 5,
90 y + cell(1).ascent() / 2);
92 cell(0).draw(pi, m - cell(0).width() / 2,
93 y - cell(0).descent() - 2 - 5);
94 cell(1).draw(pi, m - cell(1).width() / 2,
95 y + cell(1).ascent() + 2 - 5);
97 if (kind_ == NICEFRAC || kind_ == UNITFRAC) {
98 pi.pain.line(x + cell(0).width(),
100 x + cell(0).width() + 5,
101 y - dim_.asc + 2, Color::math);
103 if (kind_ == FRAC || kind_ == OVER)
104 pi.pain.line(x + 1, y - 5,
105 x + dim_.wid - 2, y - 5, Color::math);
106 drawMarkers(pi, x, y);
110 void InsetMathFrac::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
112 cell(0).metricsT(mi, dim);
113 cell(1).metricsT(mi, dim);
114 dim.wid = std::max(cell(0).width(), cell(1).width());
115 dim.asc = cell(0).height() + 1;
116 dim.des = cell(1).height();
121 void InsetMathFrac::drawT(TextPainter & pain, int x, int y) const
123 int m = x + dim_.width() / 2;
124 cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
125 cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
126 // ASCII art: ignore niceties
127 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC || kind_ == UNITFRAC)
128 pain.horizontalLine(x, y, dim_.width());
132 void InsetMathFrac::write(WriteStream & os) const
136 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
139 // \\over is only for compatibility, normalize this to \\frac
140 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
145 InsetMathNest::write(os);
151 docstring InsetMathFrac::name() const
155 return from_ascii("frac");
157 return from_ascii("over");
159 return from_ascii("nicefrac");
161 return from_ascii("unitfrac");
163 return from_ascii("atop");
165 // shut up stupid compiler
170 bool InsetMathFrac::extraBraces() const
172 return kind_ == ATOP || kind_ == OVER;
176 void InsetMathFrac::maple(MapleStream & os) const
178 os << '(' << cell(0) << ")/(" << cell(1) << ')';
182 void InsetMathFrac::mathematica(MathematicaStream & os) const
184 os << '(' << cell(0) << ")/(" << cell(1) << ')';
188 void InsetMathFrac::octave(OctaveStream & os) const
190 os << '(' << cell(0) << ")/(" << cell(1) << ')';
194 void InsetMathFrac::mathmlize(MathStream & os) const
196 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
200 void InsetMathFrac::validate(LaTeXFeatures & features) const
202 if (kind_ == NICEFRAC || kind_ == UNITFRAC)
203 features.require("units");
204 InsetMathNest::validate(features);