2 * \file math_fracinset.C
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 "math_fracinset.h"
15 #include "math_data.h"
16 #include "math_mathmlstream.h"
17 #include "textpainter.h"
18 #include "LaTeXFeatures.h"
20 #include "frontends/Painter.h"
28 MathFracInset::MathFracInset(Kind kind)
33 auto_ptr<InsetBase> MathFracInset::doClone() const
35 return auto_ptr<InsetBase>(new MathFracInset(*this));
39 MathFracInset * MathFracInset::asFracInset()
41 return kind_ == ATOP ? 0 : this;
45 MathFracInset const * MathFracInset::asFracInset() const
47 return kind_ == ATOP ? 0 : this;
51 void MathFracInset::metrics(MetricsInfo & mi, Dimension & dim) const
53 FracChanger dummy(mi.base);
56 if (kind_ == NICEFRAC) {
57 dim.wid = cell(0).width() + cell(1).width() + 5;
58 dim.asc = cell(0).height() + 5;
59 dim.des = cell(1).height() - 5;
61 dim.wid = max(cell(0).width(), cell(1).width()) + 2;
62 dim.asc = cell(0).height() + 2 + 5;
63 dim.des = cell(1).height() + 2 - 5;
70 void MathFracInset::draw(PainterInfo & pi, int x, int y) const
72 setPosCache(pi, x, y);
73 int m = x + dim_.wid / 2;
74 FracChanger dummy(pi.base);
75 if (kind_ == NICEFRAC) {
76 cell(0).draw(pi, x + 2,
77 y - cell(0).descent() - 5);
78 cell(1).draw(pi, x + cell(0).width() + 5,
79 y + cell(1).ascent() / 2);
80 pi.pain.line(x + cell(0).width(),
82 x + cell(0).width() + 5,
83 y - dim_.asc + 2, LColor::math);
85 cell(0).draw(pi, m - cell(0).width() / 2,
86 y - cell(0).descent() - 2 - 5);
87 cell(1).draw(pi, m - cell(1).width() / 2,
88 y + cell(1).ascent() + 2 - 5);
90 if (kind_ == FRAC || kind_ == OVER)
91 pi.pain.line(x + 1, y - 5,
92 x + dim_.wid - 2, y - 5, LColor::math);
93 drawMarkers(pi, x, y);
97 void MathFracInset::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
99 cell(0).metricsT(mi, dim);
100 cell(1).metricsT(mi, dim);
101 dim.wid = max(cell(0).width(), cell(1).width());
102 dim.asc = cell(0).height() + 1;
103 dim.des = cell(1).height();
108 void MathFracInset::drawT(TextPainter & pain, int x, int y) const
110 int m = x + dim_.width() / 2;
111 cell(0).drawT(pain, m - cell(0).width() / 2, y - cell(0).descent() - 1);
112 cell(1).drawT(pain, m - cell(1).width() / 2, y + cell(1).ascent());
113 // ASCII art: ignore niceties
114 if (kind_ == FRAC || kind_ == OVER || kind_ == NICEFRAC)
115 pain.horizontalLine(x, y, dim_.width());
119 void MathFracInset::write(WriteStream & os) const
123 os << '{' << cell(0) << "\\atop " << cell(1) << '}';
126 // \\over is only for compatibility, normalize this to \\frac
127 os << "\\frac{" << cell(0) << "}{" << cell(1) << '}';
131 MathNestInset::write(os);
137 string MathFracInset::name() const
149 // shut up stupid compiler
154 bool MathFracInset::extraBraces() const
156 return kind_ == ATOP || kind_ == OVER;
160 void MathFracInset::maple(MapleStream & os) const
162 os << '(' << cell(0) << ")/(" << cell(1) << ')';
166 void MathFracInset::mathematica(MathematicaStream & os) const
168 os << '(' << cell(0) << ")/(" << cell(1) << ')';
172 void MathFracInset::octave(OctaveStream & os) const
174 os << '(' << cell(0) << ")/(" << cell(1) << ')';
178 void MathFracInset::mathmlize(MathMLStream & os) const
180 os << MTag("mfrac") << cell(0) << cell(1) << ETag("mfrac");
184 void MathFracInset::validate(LaTeXFeatures & features) const
186 if (kind_ == NICEFRAC)
187 features.require("nicefrac");
188 MathNestInset::validate(features);