]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathRoot.cpp
LyXHTML: switch the doctype to (X)HTML5 and only output XML entities.
[lyx.git] / src / mathed / InsetMathRoot.cpp
1 /**
2  * \file InsetMathRoot.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetMathRoot.h"
15
16 #include "MathStream.h"
17 #include "MathSupport.h"
18
19 #include "Cursor.h"
20 #include "LaTeXFeatures.h"
21 #include "MetricsInfo.h"
22
23 #include "frontends/Painter.h"
24
25 #include "support/lassert.h"
26 #include "support/lstrings.h"
27
28 using namespace std;
29
30 namespace lyx {
31
32 using namespace frontend;
33
34 InsetMathRoot::InsetMathRoot(Buffer * buf)
35         : InsetMathNest(buf, 2)
36 {}
37
38
39 Inset * InsetMathRoot::clone() const
40 {
41         return new InsetMathRoot(*this);
42 }
43
44
45 void mathed_root_metrics(MetricsInfo & mi, MathData const & nucleus,
46                          MathData const * root, Dimension & dim)
47 {
48         Changer dummy = mi.base.changeEnsureMath();
49         Dimension dimr;
50         if (root) {
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);
54         }
55
56         Dimension dimn;
57         nucleus.metrics(mi, dimn);
58
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
63          * above nucleus.
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.
70          *
71          */
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();
84 }
85
86
87 void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
88 {
89         mathed_root_metrics(mi, cell(0), &cell(1), dim);
90 }
91
92
93 void mathed_draw_root(PainterInfo & pi, int x, int y, MathData const & nucleus,
94                       MathData const * root, Dimension const & dim)
95 {
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();
107         // the "exponent"
108         if (root) {
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);
113         }
114         // the "base"
115         nucleus.draw(pi, x + wl, y);
116         int xp[4];
117         int yp[4];
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);
127 }
128
129
130 void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
131 {
132         mathed_draw_root(pi, x, y, cell(0), &cell(1), dimension(*pi.base.bv));
133 }
134
135
136 void InsetMathRoot::write(TeXMathStream & os) const
137 {
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) << '}';
142         else
143                 os << "\\sqrt[" << cell(1) << "]{" << cell(0) << '}';
144 }
145
146
147 void InsetMathRoot::normalize(NormalStream & os) const
148 {
149         os << "[root " << cell(1) << ' ' << cell(0) << ']';
150 }
151
152
153 bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
154 {
155         idx_type const target = up; //up ? 1 : 0;
156         if (cur.idx() == target)
157                 return false;
158         cur.idx() = target;
159         cur.pos() = up ? cur.lastpos() : 0;
160         return true;
161 }
162
163
164 bool InsetMathRoot::idxForward(Cursor & cur) const
165 {
166         // nucleus is 0 and is on the right
167         if (cur.idx() == 0)
168                 return false;
169
170         cur.idx() = 0;
171         cur.pos() = 0;
172         return true;
173 }
174
175
176 bool InsetMathRoot::idxBackward(Cursor & cur) const
177 {
178         // nucleus is 0 and is on the right
179         if (cur.idx() == 1)
180                 return false;
181
182         cur.idx() = 1;
183         cur.pos() = cur.lastpos();
184         return true;
185 }
186
187
188 bool InsetMathRoot::idxFirst(Cursor & cur) const
189 {
190         LASSERT(&cur.inset() == this, return false);
191         cur.idx() = 1;
192         cur.pos() = 0;
193         return true;
194 }
195
196
197 bool InsetMathRoot::idxLast(Cursor & cur) const
198 {
199         LASSERT(&cur.inset() == this, return false);
200         cur.idx() = 0;
201         cur.pos() = cur.lastpos();
202         return true;
203 }
204
205
206 void InsetMathRoot::maple(MapleStream & os) const
207 {
208         os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
209 }
210
211
212 void InsetMathRoot::mathematica(MathematicaStream & os) const
213 {
214         os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
215 }
216
217
218 void InsetMathRoot::octave(OctaveStream & os) const
219 {
220         os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
221 }
222
223
224 void InsetMathRoot::mathmlize(MathMLStream & ms) const
225 {
226         ms << MTag("mroot") << cell(0) << cell(1) << ETag("mroot");
227 }
228
229
230 void InsetMathRoot::htmlize(HtmlStream & os) const
231 {
232         os << MTag("span", "class='root'")
233            << MTag("sup") << cell(1) << ETag("sup")
234            << from_ascii("&#8730;")
235            << MTag("span", "class='rootof'")    << cell(0) << ETag("span")
236                  << ETag("span");
237 }
238
239
240 void InsetMathRoot::validate(LaTeXFeatures & features) const
241 {
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);
247 }
248
249 } // namespace lyx