2 * \file InsetMathRoot.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 "InsetMathRoot.h"
17 #include "MathStream.h"
18 #include "MathSupport.h"
21 #include "LaTeXFeatures.h"
22 #include "MetricsInfo.h"
24 #include "frontends/Painter.h"
32 InsetMathRoot::InsetMathRoot(Buffer * buf)
33 : InsetMathNest(buf, 2)
37 Inset * InsetMathRoot::clone() const
39 return new InsetMathRoot(*this);
43 void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
45 Changer dummy = mi.base.changeEnsureMath();
48 Changer script = mi.base.font.changeStyle(LM_ST_SCRIPTSCRIPT);
49 cell(0).metrics(mi, dim0);
50 // make sure that the dim is high enough for any character
52 math_font_max_dim(mi.base.font, fontDim.asc, fontDim.des);
57 cell(1).metrics(mi, dim1);
58 // make sure that the dim is high enough for any character
60 math_font_max_dim(mi.base.font, fontDim.asc, fontDim.des);
63 dim.asc = max(dim0.ascent() + 5, dim1.ascent()) + 1;
64 dim.des = max(dim0.descent() - 5, dim1.descent());
65 dim.wid = dim0.width() + dim1.width() + 4;
69 void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
71 Changer dummy = pi.base.changeEnsureMath();
72 Dimension const dim = dimension(*pi.base.bv);
73 int const a = dim.ascent();
74 int const d = dim.descent();
75 Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
76 int const w = dim0.width();
79 Changer script = pi.base.font.changeStyle(LM_ST_SCRIPTSCRIPT);
80 cell(0).draw(pi, x, y + (d - a)/2 - dim0.descent());
83 cell(1).draw(pi, x + w + 4, y);
86 pi.pain.line(x + dim.width(), y - a + 1,
87 x + w + 4, y - a + 1, pi.base.font.color());
88 xp[0] = x + w + 4; yp[0] = y - a + 1;
89 xp[1] = x + w; yp[1] = y + d;
90 xp[2] = x + w - 2; yp[2] = y + (d - a)/2 + 2;
91 xp[3] = x + w - 5; yp[3] = y + (d - a)/2 + 4;
92 pi.pain.lines(xp, yp, 4, pi.base.font.color());
96 void InsetMathRoot::write(WriteStream & os) const
98 MathEnsurer ensurer(os);
99 os << "\\sqrt[" << cell(0) << "]{" << cell(1) << '}';
103 void InsetMathRoot::normalize(NormalStream & os) const
105 os << "[root " << cell(0) << ' ' << cell(1) << ']';
109 bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
111 Cursor::idx_type const target = up ? 0 : 1;
112 if (cur.idx() == target)
115 cur.pos() = up ? cur.lastpos() : 0;
120 void InsetMathRoot::maple(MapleStream & os) const
122 os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
126 void InsetMathRoot::mathematica(MathematicaStream & os) const
128 os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
132 void InsetMathRoot::octave(OctaveStream & os) const
134 os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
138 void InsetMathRoot::mathmlize(MathStream & os) const
140 os << MTag("mroot") << cell(1) << cell(0) << ETag("mroot");
144 void InsetMathRoot::htmlize(HtmlStream & os) const
146 os << MTag("span", "class='root'")
147 << MTag("sup") << cell(0) << ETag("sup")
148 << from_ascii("√")
149 << MTag("span", "class='rootof'") << cell(1) << ETag("span")
154 void InsetMathRoot::validate(LaTeXFeatures & features) const
156 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
157 features.addCSSSnippet(
158 "span.rootof{border-top: thin solid black;}\n"
159 "span.root sup{font-size: 75%;}");
160 InsetMathNest::validate(features);