]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathRoot.cpp
Move several common types to support/types.h
[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
27 using namespace std;
28
29 namespace lyx {
30
31 using namespace frontend;
32
33 InsetMathRoot::InsetMathRoot(Buffer * buf)
34         : InsetMathNest(buf, 2)
35 {}
36
37
38 Inset * InsetMathRoot::clone() const
39 {
40         return new InsetMathRoot(*this);
41 }
42
43
44 void mathed_root_metrics(MetricsInfo & mi, MathData const & nucleus,
45                          MathData const * root, Dimension & dim)
46 {
47         Changer dummy = mi.base.changeEnsureMath();
48         Dimension dimr;
49         if (root) {
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);
53         }
54
55         Dimension dimn;
56         nucleus.metrics(mi, dimn);
57
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
62          * above nucleus.
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.
69          *
70          */
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();
83 }
84
85
86 void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
87 {
88         mathed_root_metrics(mi, cell(0), &cell(1), dim);
89 }
90
91
92 void mathed_draw_root(PainterInfo & pi, int x, int y, MathData const & nucleus,
93                       MathData const * root, Dimension const & dim)
94 {
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();
106         // the "exponent"
107         if (root) {
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);
112         }
113         // the "base"
114         nucleus.draw(pi, x + wl, y);
115         int xp[4];
116         int yp[4];
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);
126 }
127
128
129 void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
130 {
131         mathed_draw_root(pi, x, y, cell(0), &cell(1), dimension(*pi.base.bv));
132 }
133
134
135 void InsetMathRoot::write(WriteStream & os) const
136 {
137         MathEnsurer ensurer(os);
138         os << "\\sqrt[" << cell(1) << "]{" << cell(0) << '}';
139 }
140
141
142 void InsetMathRoot::normalize(NormalStream & os) const
143 {
144         os << "[root " << cell(1) << ' ' << cell(0) << ']';
145 }
146
147
148 bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
149 {
150         idx_type const target = up; //up ? 1 : 0;
151         if (cur.idx() == target)
152                 return false;
153         cur.idx() = target;
154         cur.pos() = up ? cur.lastpos() : 0;
155         return true;
156 }
157
158
159 bool InsetMathRoot::idxForward(Cursor & cur) const
160 {
161         // nucleus is 0 and is on the right
162         if (cur.idx() == 0)
163                 return false;
164
165         cur.idx() = 0;
166         cur.pos() = 0;
167         return true;
168 }
169
170
171 bool InsetMathRoot::idxBackward(Cursor & cur) const
172 {
173         // nucleus is 0 and is on the right
174         if (cur.idx() == 1)
175                 return false;
176
177         cur.idx() = 1;
178         cur.pos() = cur.lastpos();
179         return true;
180 }
181
182
183 bool InsetMathRoot::idxFirst(Cursor & cur) const
184 {
185         LASSERT(&cur.inset() == this, return false);
186         cur.idx() = 1;
187         cur.pos() = 0;
188         return true;
189 }
190
191
192 bool InsetMathRoot::idxLast(Cursor & cur) const
193 {
194         LASSERT(&cur.inset() == this, return false);
195         cur.idx() = 0;
196         cur.pos() = cur.lastpos();
197         return true;
198 }
199
200
201 void InsetMathRoot::maple(MapleStream & os) const
202 {
203         os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
204 }
205
206
207 void InsetMathRoot::mathematica(MathematicaStream & os) const
208 {
209         os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
210 }
211
212
213 void InsetMathRoot::octave(OctaveStream & os) const
214 {
215         os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
216 }
217
218
219 void InsetMathRoot::mathmlize(MathStream & ms) const
220 {
221         ms << MTag("mroot") << cell(0) << cell(1) << ETag("mroot");
222 }
223
224
225 void InsetMathRoot::htmlize(HtmlStream & os) const
226 {
227         os << MTag("span", "class='root'")
228            << MTag("sup") << cell(1) << ETag("sup")
229            << from_ascii("&radic;")
230            << MTag("span", "class='rootof'")    << cell(0) << ETag("span")
231                  << ETag("span");
232 }
233
234
235 void InsetMathRoot::validate(LaTeXFeatures & features) const
236 {
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);
242 }
243
244 } // namespace lyx