]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathXArrow.cpp
MathML: allow XArrow to stretch.
[features.git] / src / mathed / InsetMathXArrow.cpp
1 /**
2  * \file InsetMathXArrow.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 "InsetMathXArrow.h"
14
15 #include "MathData.h"
16 #include "MathStream.h"
17 #include "MathSupport.h"
18
19 #include "LaTeXFeatures.h"
20 #include "MetricsInfo.h"
21
22 #include "support/debug.h"
23 #include "support/lassert.h"
24
25 #include <algorithm>
26
27 using namespace std;
28
29 namespace lyx {
30
31
32 InsetMathXArrow::InsetMathXArrow(Buffer * buf, docstring const & name)
33         : InsetMathFracBase(buf), name_(name)
34 {}
35
36
37 Inset * InsetMathXArrow::clone() const
38 {
39         return new InsetMathXArrow(*this);
40 }
41
42
43 void InsetMathXArrow::metrics(MetricsInfo & mi, Dimension & dim) const
44 {
45         Changer dummy2 = mi.base.changeEnsureMath();
46         Changer dummy = mi.base.changeScript();
47         Dimension dim0;
48         cell(0).metrics(mi, dim0);
49         Dimension dim1;
50         cell(1).metrics(mi, dim1);
51         dim.wid = max(dim0.width(), dim1.width()) + 10;
52         dim.asc = dim0.height() + 10;
53         dim.des = dim1.height();
54 }
55
56
57 void InsetMathXArrow::draw(PainterInfo & pi, int x, int y) const
58 {
59         Changer dummy2 = pi.base.changeEnsureMath();
60         Changer dummy = pi.base.changeScript();
61         Dimension const dim = dimension(*pi.base.bv);
62         Dimension const & dim0 = cell(0).dimension(*pi.base.bv);
63         // center the cells with the decoration
64         cell(0).draw(pi, x + dim.width()/2 - dim0.width()/2, y - 10);
65         Dimension const & dim1 = cell(1).dimension(*pi.base.bv);
66         cell(1).draw(pi, x + dim.width()/2 - dim1.width()/2, y + dim1.height());
67         mathed_draw_deco(pi, x, y - 7, dim.wid, 5, name_);
68 }
69
70
71 void InsetMathXArrow::write(TeXMathStream & os) const
72 {
73         MathEnsurer ensurer(os);
74         os << '\\' << name_;
75         if (!cell(1).empty())
76                 os << '[' << cell(1) << ']';
77         os << '{' << cell(0) << '}';
78 }
79
80
81 void InsetMathXArrow::normalize(NormalStream & os) const
82 {
83         os << "[xarrow " << name_ << ' ' <<  cell(0) << ' ' << cell(1) << ']';
84 }
85
86
87 void InsetMathXArrow::mathmlize(MathMLStream & ms) const
88 {
89         char const * arrow;
90
91         if (!ms.xmlMode()) { // Use HTML entities.
92                 if (name_ == "xleftarrow")
93                         arrow = "&larr;";
94                 else if (name_ == "xrightarrow")
95                         arrow = "&rarr;";
96                 else if (name_ == "xhookleftarrow")
97                         arrow = "&larrhk;";
98                 else if (name_ == "xhookrightarrow")
99                         arrow = "&rarrhk;";
100                 else if (name_ == "xLeftarrow")
101                         arrow = "&lArr;";
102                 else if (name_ == "xRightarrow")
103                         arrow = "&rArr;";
104                 else if (name_ == "xleftrightarrow")
105                         arrow = "&leftrightarrow;";
106                 else if (name_ == "xLeftrightarrow")
107                         arrow = "&Leftrightarrow;";
108                 else if (name_ == "xleftharpoondown")
109                         arrow = "&leftharpoondown;";
110                 else if (name_ == "xleftharpoonup")
111                         arrow = "&leftharpoonup;";
112                 else if (name_ == "xleftrightharpoons")
113                         arrow = "&leftrightharpoons;";
114                 else if (name_ == "xrightharpoondown")
115                         arrow = "&rightharpoondown;";
116                 else if (name_ == "xrightharpoonup")
117                         arrow = "&rightharpoonup;";
118                 else if (name_ == "xrightleftharpoons")
119                         arrow = "&rightleftharpoons;";
120                 else if (name_ == "xmapsto")
121                         arrow = "&mapsto;";
122                 else {
123                         lyxerr << "mathmlize conversion for '" << name_ << "' not implemented" << endl;
124                         LASSERT(false, arrow = "&rarr;");
125                 }
126         } else { // Use XML entities.
127                 if (name_ == "xleftarrow")
128                         arrow = "&#x2190;";
129                 else if (name_ == "xrightarrow")
130                         arrow = "&#x2192;";
131                 else if (name_ == "xhookleftarrow")
132                         arrow = "&#x21a9;";
133                 else if (name_ == "xhookrightarrow")
134                         arrow = "&#x21aa;";
135                 else if (name_ == "xLeftarrow")
136                         arrow = "&#x21d0;";
137                 else if (name_ == "xRightarrow")
138                         arrow = "&#x21d2;";
139                 else if (name_ == "xleftrightarrow")
140                         arrow = "&#x2194;";
141                 else if (name_ == "xLeftrightarrow")
142                         arrow = "&#x21d4;";
143                 else if (name_ == "xleftharpoondown")
144                         arrow = "&#x21bd;";
145                 else if (name_ == "xleftharpoonup")
146                         arrow = "&#x21bc;";
147                 else if (name_ == "xleftrightharpoons")
148                         arrow = "&#x21cb;";
149                 else if (name_ == "xrightharpoondown")
150                         arrow = "&#x21c1;";
151                 else if (name_ == "xrightharpoonup")
152                         arrow = "&#x21c0;";
153                 else if (name_ == "xrightleftharpoons")
154                         arrow = "&#x21cc;";
155                 else if (name_ == "xmapsto")
156                         arrow = "&#x21a6;";
157                 else {
158                         lyxerr << "mathmlize XML conversion for '" << name_ << "' not implemented" << endl;
159                         LASSERT(false, arrow = "&#x2192;");
160                 }
161         }
162
163         ms << "<" << from_ascii(ms.namespacedTag("munderover")) << " accent='false' accentunder='false'>"
164            << MTagInline("mo") << arrow << ETagInline("mo")
165            << cell(1) << cell(0)
166            << "</" << from_ascii(ms.namespacedTag("munderover"))<< ">";
167 }
168
169
170 void InsetMathXArrow::htmlize(HtmlStream & os) const
171 {
172         char const * arrow;
173
174         if (name_ == "xleftarrow")
175                 arrow = "&larr;";
176         else if (name_ == "xrightarrow")
177                 arrow = "&rarr;";
178         else if (name_ == "xhookleftarrow")
179                 arrow = "&larrhk;";
180         else if (name_ == "xhookrightarrow")
181                 arrow = "&rarrhk;";
182         else if (name_ == "xLeftarrow")
183                 arrow = "&lArr;";
184         else if (name_ == "xRightarrow")
185                 arrow = "&rArr;";
186         else if (name_ == "xleftrightarrow")
187                 arrow = "&leftrightarrow;";
188         else if (name_ == "xLeftrightarrow")
189                 arrow = "&Leftrightarrow;";
190         else if (name_ == "xleftharpoondown")
191                 arrow = "&leftharpoondown;";
192         else if (name_ == "xleftharpoonup")
193                 arrow = "&leftharpoonup;";
194         else if (name_ == "xleftrightharpoons")
195                 arrow = "&leftrightharpoons;";
196         else if (name_ == "xrightharpoondown")
197                 arrow = "&rightharpoondown;";
198         else if (name_ == "xrightharpoonup")
199                 arrow = "&rightharpoonup;";
200         else if (name_ == "xrightleftharpoons")
201                 arrow = "&rightleftharpoons;";
202         else if (name_ == "xmapsto")
203                 arrow = "&mapsto;";
204         else {
205                 lyxerr << "htmlize conversion for '" << name_ << "' not implemented" << endl;
206                 LASSERT(false, arrow = "&rarr;");
207         }
208         os << MTag("span", "class='xarrow'")
209                  << MTag("span", "class='xatop'") << cell(0) << ETag("span")
210                  << MTag("span", "class='xabottom'") << arrow << ETag("span")
211                  << ETag("span");
212 }
213
214
215 void InsetMathXArrow::validate(LaTeXFeatures & features) const
216 {
217         if (name_ == "xleftarrow" || name_ == "xrightarrow")
218                 features.require("amsmath");
219         else
220                 features.require("mathtools");
221         if (features.runparams().math_flavor == OutputParams::MathAsHTML)
222                 // CSS adapted from eLyXer
223                 features.addCSSSnippet(
224                         "span.xarrow{display: inline-block; vertical-align: middle; text-align:center;}\n"
225                         "span.xatop{display: block;}\n"
226                         "span.xabottom{display: block;}");
227         InsetMathNest::validate(features);
228 }
229
230
231 } // namespace lyx