]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathRoot.cpp
Change cell numbers so that 0 is the main cell
[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(LM_ST_SCRIPTSCRIPT);
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() == LM_ST_DISPLAY) ? 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(LM_ST_SCRIPTSCRIPT);
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         Cursor::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 void InsetMathRoot::maple(MapleStream & os) const
160 {
161         os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
162 }
163
164
165 void InsetMathRoot::mathematica(MathematicaStream & os) const
166 {
167         os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
168 }
169
170
171 void InsetMathRoot::octave(OctaveStream & os) const
172 {
173         os << '(' << cell(0) << ")^(1/(" << cell(1) <<"))";
174 }
175
176
177 void InsetMathRoot::mathmlize(MathStream & os) const
178 {
179         os << MTag("mroot") << cell(0) << cell(1) << ETag("mroot");
180 }
181
182
183 void InsetMathRoot::htmlize(HtmlStream & os) const
184 {
185         os << MTag("span", "class='root'")
186            << MTag("sup") << cell(1) << ETag("sup")
187            << from_ascii("&radic;")
188            << MTag("span", "class='rootof'")    << cell(0) << ETag("span")
189                  << ETag("span");
190 }
191
192
193 void InsetMathRoot::validate(LaTeXFeatures & features) const
194 {
195         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
196                 features.addCSSSnippet(
197                         "span.rootof{border-top: thin solid black;}\n"
198                         "span.root sup{font-size: 75%;}");
199         InsetMathNest::validate(features);
200 }
201
202 } // namespace lyx