]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathStackrel.cpp
Accept \kern and \mkern for math kerning
[lyx.git] / src / mathed / InsetMathStackrel.cpp
1 /**
2  * \file InsetMathStackrel.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathStackrel.h"
14
15 #include "MathData.h"
16 #include "MathStream.h"
17
18 #include "Cursor.h"
19 #include "LaTeXFeatures.h"
20 #include "MetricsInfo.h"
21
22 using namespace std;
23
24 namespace lyx {
25
26 InsetMathStackrel::InsetMathStackrel(Buffer * buf, bool sub)
27         : InsetMathFracBase(buf, sub ? 3 : 2)
28 {}
29
30
31 Inset * InsetMathStackrel::clone() const
32 {
33         return new InsetMathStackrel(*this);
34 }
35
36
37 bool InsetMathStackrel::idxUpDown(Cursor & cur, bool up) const
38 {
39         if (up) {
40                 if (cur.idx() == 0)
41                         return false;
42         } else {
43                 if (cur.idx() + 1 ==  nargs())
44                         return false;
45         }
46         InsetMath::idx_type target = up ? cur.idx() - 1 : cur.idx() + 1;
47         if (cur.idx() == target)
48                 return false;
49         cur.idx() = target;
50         cur.pos() = cell(target).x2pos(&cur.bv(), cur.x_target());
51         return true;
52 }
53
54
55 MathClass InsetMathStackrel::mathClass() const
56 {
57         // FIXME: update this when/if \stackbin is supported
58         return MC_REL;
59 }
60
61
62 void InsetMathStackrel::metrics(MetricsInfo & mi, Dimension & dim) const
63 {
64         Changer dummy2 = mi.base.changeEnsureMath();
65         Dimension dim1;
66         cell(1).metrics(mi, dim1);
67         Changer dummy = mi.base.changeFrac();
68         Dimension dim0;
69         cell(0).metrics(mi, dim0);
70         if (nargs() > 2) {
71                 Dimension dim2;
72                 cell(2).metrics(mi, dim2);
73                 dim.wid = max(max(dim0.width(), dim1.width()), dim2.width()) + 4;
74                 dim.asc = dim1.ascent() + dim0.height() + 4;
75                 dim.des = dim1.descent() + dim2.height() + dim2.descent() + 1;
76         } else {
77                 dim.wid = max(dim0.width(), dim1.width()) + 4;
78                 dim.asc = dim1.ascent() + dim0.height() + 4;
79                 dim.des = dim1.descent();
80         }
81         metricsMarkers(mi, dim);
82 }
83
84
85 void InsetMathStackrel::draw(PainterInfo & pi, int x, int y) const
86 {
87         Changer dummy2 = pi.base.changeEnsureMath();
88         Dimension const dim = dimension(*pi.base.bv);
89         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
90         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
91         int m  = x + dim.width() / 2;
92         int yo = y - dim1.ascent() - dim0.descent() - 1;
93         cell(1).draw(pi, m - dim1.width() / 2, y);
94         Changer dummy = pi.base.changeFrac();
95         cell(0).draw(pi, m - dim0.width() / 2, yo);
96         if (nargs() > 2) {
97                 Dimension const & dim2 = cell(2).dimension(*pi.base.bv);
98                 int y2 = y + dim1.descent() + dim2.ascent() + 1;
99                 cell(2).draw(pi, m - dim2.width() / 2, y2);
100         }
101         drawMarkers(pi, x, y);
102 }
103
104
105 void InsetMathStackrel::write(WriteStream & os) const
106 {
107         MathEnsurer ensurer(os);
108         os << "\\stackrel";
109         if (nargs() > 2)
110                 os << '[' << cell(2) << ']';
111         os << '{' << cell(0) << "}{" << cell(1) << '}';
112 }
113
114
115 void InsetMathStackrel::normalize(NormalStream & os) const
116 {
117         os << "[stackrel " << cell(0) << ' ' << cell(1);
118         if (nargs() > 2)
119                 os << ' ' << cell(2);
120         os << ']';
121 }
122
123
124 void InsetMathStackrel::mathmlize(MathStream & ms) const
125 {
126         if (nargs() > 2)
127                 ms << "<munderover>" << cell(1) << cell(2) << cell(0) << "</munderover>";
128         else
129                 ms << "<mover accent='false'>" << cell(1) << cell(0) << "</mover>";
130 }
131
132
133 void InsetMathStackrel::htmlize(HtmlStream & os) const
134 {
135         if (nargs() > 2) {
136                 os << MTag("span", "class='underoverset'")
137                    << MTag("span", "class='top'") << cell(0) << ETag("span")
138                    << MTag("span") << cell(1) << ETag("span")
139                    << MTag("span", "class='bottom'") << cell(2) << ETag("span");
140         } else {
141                 // at the moment, this is exactly the same as overset
142                 os << MTag("span", "class='overset'")
143                    << MTag("span", "class='top'") << cell(0) << ETag("span")
144                    << MTag("span") << cell(1) << ETag("span");
145         }
146         os << ETag("span");
147 }
148
149
150 void InsetMathStackrel::validate(LaTeXFeatures & features) const
151 {
152         if (features.runparams().math_flavor == OutputParams::MathAsHTML) {
153                 if (nargs() > 2) {
154                         // FIXME: "vertical-align: middle" works only if the
155                         // height of sub and super script is approximately equal.
156                         features.addCSSSnippet(
157                                 "span.underoverset{display: inline-block; vertical-align: middle; text-align:center;}\n"
158                                 "span.underoverset span {display: block;}\n"
159                                 "span.bottom{font-size: 66%;}\n"
160                                 "span.top{font-size: 66%;}");
161                 } else {
162                         // from overset
163                         features.addCSSSnippet(
164                                 "span.overset{display: inline-block; vertical-align: bottom; text-align:center;}\n"
165                                 "span.overset span {display: block;}\n"
166                                 "span.top{font-size: 66%;}");
167                 }
168         }
169         if (nargs() > 2)
170                 features.require("stackrel");
171
172         InsetMathNest::validate(features);
173 }
174
175 } // namespace lyx