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"
26 #include "support/lstrings.h"
32 using namespace frontend;
34 InsetMathRoot::InsetMathRoot(Buffer * buf)
35 : InsetMathNest(buf, 2)
39 Inset * InsetMathRoot::clone() const
41 return new InsetMathRoot(*this);
45 void mathed_root_metrics(MetricsInfo & mi, MathData const & nucleus,
46 MathData const * root, Dimension & dim)
48 Changer dummy = mi.base.changeEnsureMath();
51 Changer script = mi.base.font.changeStyle(SCRIPTSCRIPT_STYLE);
52 // make sure that the dim is high enough for any character
53 root->metrics(mi, dimr, false);
57 nucleus.metrics(mi, dimn);
59 // Some room for the decoration
60 // The width of left decoration was 9 pixels with a 10em font
61 int const w = 9 * mathed_font_em(mi.base.font) / 10;
62 /* See rule 11 in Appendix G of Rhe TeXbook for the computation of the spacing
64 * FIXME more work is needed to implement properly rule 11.
65 * * Ideally, we should use sqrt glyphs from the math fonts. Note
66 that then we would get rule thickness from there.
67 * * The positioning of the root MathData is arbitrary. It should
68 * follow the definition of \root...\of... in The Texbook in
69 * Apprendix B page 360.
72 int const t = mi.base.solidLineThickness();
73 int const x_height = mathed_font_x_height(mi.base.font);
74 int const phi = (mi.base.font.style() == DISPLAY_STYLE) ? x_height : t;
75 // first part is the spacing, second part is the line width
76 // itself, and last one is the spacing above.
77 int const space_above = (t + phi / 4) + t + t;
78 int const a = dimn.ascent();
79 int const d = dimn.descent();
80 // Not sure what the 1 stands for, it is needed to have some spacing at small sizes.
81 dim.asc = max(dimr.ascent() + (d - a) / 2, a + space_above) + 1;
82 dim.des = max(dimr.descent() - (d - a) / 2, d);
83 dim.wid = max(dimr.width() + 3 * w / 8, w) + dimn.width();
87 void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
89 mathed_root_metrics(mi, cell(0), &cell(1), dim);
93 void mathed_draw_root(PainterInfo & pi, int x, int y, MathData const & nucleus,
94 MathData const * root, Dimension const & dim)
96 Changer dummy = pi.base.changeEnsureMath();
97 // The width of left decoration was 9 pixels with a 10em font
98 int const w = 9 * mathed_font_em(pi.base.font) / 10;
99 // the height of the hook was 5 with a 10em font
100 int const h = 5 * mathed_font_em(pi.base.font) / 10;
101 int const a = dim.ascent();
102 int const d = dim.descent();
103 int const t = pi.base.solidLineThickness();
104 Dimension const & dimn = nucleus.dimension(*pi.base.bv);
105 // the width of the left part of the root
106 int const wl = dim.width() - dimn.width();
109 Changer script = pi.base.font.changeStyle(SCRIPTSCRIPT_STYLE);
110 Dimension const & dimr = root->dimension(*pi.base.bv);
111 int const root_offset = wl - 3 * w / 8 - dimr.width();
112 root->draw(pi, x + root_offset, y + (d - a)/2);
115 nucleus.draw(pi, x + wl, y);
118 pi.pain.line(x + dim.width(), y - a + 2 * t,
119 x + wl, y - a + 2 * t, pi.base.font.color(),
120 Painter::line_solid, t);
121 xp[0] = x + wl; yp[0] = y - a + 2 * t + 1;
122 xp[1] = x + wl - w / 2; yp[1] = y + d;
123 xp[2] = x + wl - w + h / 4; yp[2] = y + d - h;
124 xp[3] = x + wl - w; yp[3] = y + d - h + h / 4;
125 pi.pain.lines(xp, yp, 4, pi.base.font.color(),
126 Painter::fill_none, Painter::line_solid, t);
130 void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
132 mathed_draw_root(pi, x, y, cell(0), &cell(1), dimension(*pi.base.bv));
136 void InsetMathRoot::write(TeXMathStream & os) const
138 MathEnsurer ensurer(os);
139 if (os.latex() && !cell(1).empty() && !cell(1).front()->asBraceInset()
140 && support::contains(asString(cell(1)), '['))
141 os << "\\sqrt[{" << cell(1) << "}]{" << cell(0) << '}';
143 os << "\\sqrt[" << cell(1) << "]{" << cell(0) << '}';
147 void InsetMathRoot::normalize(NormalStream & os) const
149 os << "[root " << cell(1) << ' ' << cell(0) << ']';
153 bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
155 idx_type const target = up; //up ? 1 : 0;
156 if (cur.idx() == target)
159 cur.pos() = up ? cur.lastpos() : 0;
164 bool InsetMathRoot::idxForward(Cursor & cur) const
166 // nucleus is 0 and is on the right
176 bool InsetMathRoot::idxBackward(Cursor & cur) const
178 // nucleus is 0 and is on the right
183 cur.pos() = cur.lastpos();
188 bool InsetMathRoot::idxFirst(Cursor & cur) const
190 LASSERT(&cur.inset() == this, return false);
197 bool InsetMathRoot::idxLast(Cursor & cur) const
199 LASSERT(&cur.inset() == this, return false);
201 cur.pos() = cur.lastpos();
206 void InsetMathRoot::maple(MapleStream & os) const
208 os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
212 void InsetMathRoot::mathematica(MathematicaStream & os) const
214 os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
218 void InsetMathRoot::octave(OctaveStream & os) const
220 os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
224 void InsetMathRoot::mathmlize(MathMLStream & ms) const
226 ms << MTag("mroot") << cell(0) << cell(1) << ETag("mroot");
230 void InsetMathRoot::htmlize(HtmlStream & os) const
232 os << MTag("span", "class='root'")
233 << MTag("sup") << cell(1) << ETag("sup")
234 << from_ascii("√")
235 << MTag("span", "class='rootof'") << cell(0) << ETag("span")
240 void InsetMathRoot::validate(LaTeXFeatures & features) const
242 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
243 features.addCSSSnippet(
244 "span.rootof{border-top: thin solid black;}\n"
245 "span.root sup{font-size: 75%;}");
246 InsetMathNest::validate(features);