]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathRoot.cpp
Fix #10863 compiler warnings.
[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
26 using namespace std;
27
28 namespace lyx {
29
30 using namespace frontend;
31
32 InsetMathRoot::InsetMathRoot(Buffer * buf)
33         : InsetMathNest(buf, 2)
34 {}
35
36
37 Inset * InsetMathRoot::clone() const
38 {
39         return new InsetMathRoot(*this);
40 }
41
42
43 void mathed_root_metrics(MetricsInfo & mi, MathData const & nucleus,
44                          MathData const * root, Dimension & dim)
45 {
46         Changer dummy = mi.base.changeEnsureMath();
47         Dimension dimr;
48         if (root) {
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
52                 Dimension fontDim;
53                 math_font_max_dim(mi.base.font, fontDim.asc, fontDim.des);
54                 dimr += fontDim;
55         }
56
57         Dimension dimn;
58         nucleus.metrics(mi, dimn);
59         // make sure that the dim is high enough for any character
60         // Dimension fontDim;
61         // math_font_max_dim(mi.base.font, fontDim.asc, fontDim.des);
62         // dimn += fontDim;
63
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
68          * above nucleus.
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.
75          *
76          */
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();
89 }
90
91
92 void InsetMathRoot::metrics(MetricsInfo & mi, Dimension & dim) const
93 {
94         mathed_root_metrics(mi, cell(1), &cell(0), dim);
95 }
96
97
98 void mathed_draw_root(PainterInfo & pi, int x, int y, MathData const & nucleus,
99                       MathData const * root, Dimension const & dim)
100 {
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();
112         // the "exponent"
113         if (root) {
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);
118         }
119         // the "base"
120         nucleus.draw(pi, x + wl, y);
121         int xp[4];
122         int yp[4];
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);
132 }
133
134
135 void InsetMathRoot::draw(PainterInfo & pi, int x, int y) const
136 {
137         mathed_draw_root(pi, x, y, cell(1), &cell(0), dimension(*pi.base.bv));
138 }
139
140
141 void InsetMathRoot::write(WriteStream & os) const
142 {
143         MathEnsurer ensurer(os);
144         os << "\\sqrt[" << cell(0) << "]{" << cell(1) << '}';
145 }
146
147
148 void InsetMathRoot::normalize(NormalStream & os) const
149 {
150         os << "[root " << cell(0) << ' ' << cell(1) << ']';
151 }
152
153
154 bool InsetMathRoot::idxUpDown(Cursor & cur, bool up) const
155 {
156         Cursor::idx_type const target = up ? 0 : 1;
157         if (cur.idx() == target)
158                 return false;
159         cur.idx() = target;
160         cur.pos() = up ? cur.lastpos() : 0;
161         return true;
162 }
163
164
165 void InsetMathRoot::maple(MapleStream & os) const
166 {
167         os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
168 }
169
170
171 void InsetMathRoot::mathematica(MathematicaStream & os) const
172 {
173         os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
174 }
175
176
177 void InsetMathRoot::octave(OctaveStream & os) const
178 {
179         os << '(' << cell(1) << ")^(1/(" << cell(0) <<"))";
180 }
181
182
183 void InsetMathRoot::mathmlize(MathStream & os) const
184 {
185         os << MTag("mroot") << cell(1) << cell(0) << ETag("mroot");
186 }
187
188
189 void InsetMathRoot::htmlize(HtmlStream & os) const
190 {
191         os << MTag("span", "class='root'")
192            << MTag("sup") << cell(0) << ETag("sup")
193            << from_ascii("&radic;")
194            << MTag("span", "class='rootof'")    << cell(1) << ETag("span")
195                  << ETag("span");
196 }
197
198
199 void InsetMathRoot::validate(LaTeXFeatures & features) const
200 {
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);
206 }
207
208 } // namespace lyx