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"
30 using namespace frontend;
32 InsetMathRoot::InsetMathRoot(Buffer * buf)
33 : InsetMathNest(buf, 2)
37 Inset * InsetMathRoot::clone() const
39 return new InsetMathRoot(*this);
43 void mathed_root_metrics(MetricsInfo & mi, MathData const & nucleus,
44 MathData const * root, Dimension & dim)
46 Changer dummy = mi.base.changeEnsureMath();
49 Changer script = mi.base.font.changeStyle(LM_ST_SCRIPTSCRIPT);
50 root->metrics(mi, dimr);
51 // make sure that the dim is high enough for any character
53 math_font_max_dim(mi.base.font, fontDim.asc, fontDim.des);
58 nucleus.metrics(mi, dimn);
59 // make sure that the dim is high enough for any character
61 // math_font_max_dim(mi.base.font, fontDim.asc, fontDim.des);
64 // Some room for the decoration
65 // The width of left decoration was 9 pixels with a 10em font
66 int const w = 9 * mathed_font_em(mi.base.font) / 10;
67 /* See rule 11 in Appendix G of Rhe TeXbook for the computation of the spacing
69 * FIXME more work is needed to implement properly rule 11.
70 * * Ideally, we should use sqrt glyphs from the math fonts. Note
71 that then we would get rule thickness from there.
72 * * The positioning of the root MathData is arbitrary. It should
73 * follow the definition of \root...\of... in The Texbook in
74 * Apprendix B page 360.
77 int const t = mi.base.solidLineThickness();
78 int const x_height = mathed_font_x_height(mi.base.font);
79 int const phi = (mi.base.font.style() == LM_ST_DISPLAY) ? x_height : t;
80 // first part is the spacing, second part is the line width
81 // itself, and last one is the spacing above.
82 int const space_above = (t + phi / 4) + t + t;
83 int const a = dimn.ascent();
84 int const d = dimn.descent();
85 // Not sure what the 1 stands for, it is needed to have some spacing at small sizes.
86 dim.asc = max(dimr.ascent() + (d - a) / 2, a + space_above) + 1;
87 dim.des = max(dimr.descent() - (d - a) / 2, d);
88 dim.wid = max(dimr.width() + 3 * w / 8, w) + dimn.width();
92 void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
94 mathed_root_metrics(mi, cell(1), &cell(0), dim);
98 void mathed_draw_root(PainterInfo & pi, int x, int y, MathData const & nucleus,
99 MathData const * root, Dimension const & dim)
101 Changer dummy = pi.base.changeEnsureMath();
102 // The width of left decoration was 9 pixels with a 10em font
103 int const w = 9 * mathed_font_em(pi.base.font) / 10;
104 // the height of the hook was 5 with a 10em font
105 int const h = 5 * mathed_font_em(pi.base.font) / 10;
106 int const a = dim.ascent();
107 int const d = dim.descent();
108 int const t = pi.base.solidLineThickness();
109 Dimension const dimn = nucleus.dimension(*pi.base.bv);
110 // the width of the left part of the root
111 int const wl = dim.width() - dimn.width();
114 Changer script = pi.base.font.changeStyle(LM_ST_SCRIPTSCRIPT);
115 Dimension const dimr = root->dimension(*pi.base.bv);
116 int const root_offset = wl - 3 * w / 8 - dimr.width();
117 root->draw(pi, x + root_offset, y + (d - a)/2);
120 nucleus.draw(pi, x + wl, y);
123 pi.pain.line(x + dim.width(), y - a + 2 * t,
124 x + wl, y - a + 2 * t, pi.base.font.color(),
125 Painter::line_solid, t);
126 xp[0] = x + wl; yp[0] = y - a + 2 * t + 1;
127 xp[1] = x + wl - w / 2; yp[1] = y + d;
128 xp[2] = x + wl - w + h / 4; yp[2] = y + d - h;
129 xp[3] = x + wl - w; yp[3] = y + d - h + h / 4;
130 pi.pain.lines(xp, yp, 4, pi.base.font.color(),
131 Painter::fill_none, Painter::line_solid, t);
135 void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
137 mathed_draw_root(pi, x, y, cell(1), &cell(0), dimension(*pi.base.bv));
141 void InsetMathRoot::write(WriteStream & os) const
143 MathEnsurer ensurer(os);
144 os << "\\sqrt[" << cell(0) << "]{" << cell(1) << '}';
148 void InsetMathRoot::normalize(NormalStream & os) const
150 os << "[root " << cell(0) << ' ' << cell(1) << ']';
154 bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
156 Cursor::idx_type const target = up ? 0 : 1;
157 if (cur.idx() == target)
160 cur.pos() = up ? cur.lastpos() : 0;
165 void InsetMathRoot::maple(MapleStream & os) const
167 os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
171 void InsetMathRoot::mathematica(MathematicaStream & os) const
173 os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
177 void InsetMathRoot::octave(OctaveStream & os) const
179 os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
183 void InsetMathRoot::mathmlize(MathStream & os) const
185 os << MTag("mroot") << cell(1) << cell(0) << ETag("mroot");
189 void InsetMathRoot::htmlize(HtmlStream & os) const
191 os << MTag("span", "class='root'")
192 << MTag("sup") << cell(0) << ETag("sup")
193 << from_ascii("√")
194 << MTag("span", "class='rootof'") << cell(1) << ETag("span")
199 void InsetMathRoot::validate(LaTeXFeatures & features) const
201 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
202 features.addCSSSnippet(
203 "span.rootof{border-top: thin solid black;}\n"
204 "span.root sup{font-size: 75%;}");
205 InsetMathNest::validate(features);