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"
16 #include "MathStream.h"
17 #include "MathSupport.h"
20 #include "LaTeXFeatures.h"
21 #include "MetricsInfo.h"
23 #include "frontends/Painter.h"
25 #include "support/lassert.h"
31 using namespace frontend;
33 InsetMathRoot::InsetMathRoot(Buffer * buf)
34 : InsetMathNest(buf, 2)
38 Inset * InsetMathRoot::clone() const
40 return new InsetMathRoot(*this);
44 void mathed_root_metrics(MetricsInfo & mi, MathData const & nucleus,
45 MathData const * root, Dimension & dim)
47 Changer dummy = mi.base.changeEnsureMath();
50 Changer script = mi.base.font.changeStyle(SCRIPTSCRIPT_STYLE);
51 // make sure that the dim is high enough for any character
52 root->metrics(mi, dimr, false);
56 nucleus.metrics(mi, dimn);
58 // Some room for the decoration
59 // The width of left decoration was 9 pixels with a 10em font
60 int const w = 9 * mathed_font_em(mi.base.font) / 10;
61 /* See rule 11 in Appendix G of Rhe TeXbook for the computation of the spacing
63 * FIXME more work is needed to implement properly rule 11.
64 * * Ideally, we should use sqrt glyphs from the math fonts. Note
65 that then we would get rule thickness from there.
66 * * The positioning of the root MathData is arbitrary. It should
67 * follow the definition of \root...\of... in The Texbook in
68 * Apprendix B page 360.
71 int const t = mi.base.solidLineThickness();
72 int const x_height = mathed_font_x_height(mi.base.font);
73 int const phi = (mi.base.font.style() == DISPLAY_STYLE) ? x_height : t;
74 // first part is the spacing, second part is the line width
75 // itself, and last one is the spacing above.
76 int const space_above = (t + phi / 4) + t + t;
77 int const a = dimn.ascent();
78 int const d = dimn.descent();
79 // Not sure what the 1 stands for, it is needed to have some spacing at small sizes.
80 dim.asc = max(dimr.ascent() + (d - a) / 2, a + space_above) + 1;
81 dim.des = max(dimr.descent() - (d - a) / 2, d);
82 dim.wid = max(dimr.width() + 3 * w / 8, w) + dimn.width();
86 void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
88 mathed_root_metrics(mi, cell(0), &cell(1), dim);
92 void mathed_draw_root(PainterInfo & pi, int x, int y, MathData const & nucleus,
93 MathData const * root, Dimension const & dim)
95 Changer dummy = pi.base.changeEnsureMath();
96 // The width of left decoration was 9 pixels with a 10em font
97 int const w = 9 * mathed_font_em(pi.base.font) / 10;
98 // the height of the hook was 5 with a 10em font
99 int const h = 5 * mathed_font_em(pi.base.font) / 10;
100 int const a = dim.ascent();
101 int const d = dim.descent();
102 int const t = pi.base.solidLineThickness();
103 Dimension const & dimn = nucleus.dimension(*pi.base.bv);
104 // the width of the left part of the root
105 int const wl = dim.width() - dimn.width();
108 Changer script = pi.base.font.changeStyle(SCRIPTSCRIPT_STYLE);
109 Dimension const & dimr = root->dimension(*pi.base.bv);
110 int const root_offset = wl - 3 * w / 8 - dimr.width();
111 root->draw(pi, x + root_offset, y + (d - a)/2);
114 nucleus.draw(pi, x + wl, y);
117 pi.pain.line(x + dim.width(), y - a + 2 * t,
118 x + wl, y - a + 2 * t, pi.base.font.color(),
119 Painter::line_solid, t);
120 xp[0] = x + wl; yp[0] = y - a + 2 * t + 1;
121 xp[1] = x + wl - w / 2; yp[1] = y + d;
122 xp[2] = x + wl - w + h / 4; yp[2] = y + d - h;
123 xp[3] = x + wl - w; yp[3] = y + d - h + h / 4;
124 pi.pain.lines(xp, yp, 4, pi.base.font.color(),
125 Painter::fill_none, Painter::line_solid, t);
129 void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
131 mathed_draw_root(pi, x, y, cell(0), &cell(1), dimension(*pi.base.bv));
135 void InsetMathRoot::write(TeXMathStream & os) const
137 MathEnsurer ensurer(os);
138 os << "\\sqrt[" << cell(1) << "]{" << cell(0) << '}';
142 void InsetMathRoot::normalize(NormalStream & os) const
144 os << "[root " << cell(1) << ' ' << cell(0) << ']';
148 bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
150 idx_type const target = up; //up ? 1 : 0;
151 if (cur.idx() == target)
154 cur.pos() = up ? cur.lastpos() : 0;
159 bool InsetMathRoot::idxForward(Cursor & cur) const
161 // nucleus is 0 and is on the right
171 bool InsetMathRoot::idxBackward(Cursor & cur) const
173 // nucleus is 0 and is on the right
178 cur.pos() = cur.lastpos();
183 bool InsetMathRoot::idxFirst(Cursor & cur) const
185 LASSERT(&cur.inset() == this, return false);
192 bool InsetMathRoot::idxLast(Cursor & cur) const
194 LASSERT(&cur.inset() == this, return false);
196 cur.pos() = cur.lastpos();
201 void InsetMathRoot::maple(MapleStream & os) const
203 os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
207 void InsetMathRoot::mathematica(MathematicaStream & os) const
209 os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
213 void InsetMathRoot::octave(OctaveStream & os) const
215 os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
219 void InsetMathRoot::mathmlize(MathMLStream & ms) const
221 ms << MTag("mroot") << cell(0) << cell(1) << ETag("mroot");
225 void InsetMathRoot::htmlize(HtmlStream & os) const
227 os << MTag("span", "class='root'")
228 << MTag("sup") << cell(1) << ETag("sup")
229 << from_ascii("√")
230 << MTag("span", "class='rootof'") << cell(0) << ETag("span")
235 void InsetMathRoot::validate(LaTeXFeatures & features) const
237 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
238 features.addCSSSnippet(
239 "span.rootof{border-top: thin solid black;}\n"
240 "span.root sup{font-size: 75%;}");
241 InsetMathNest::validate(features);