2 * \file InsetMathXArrow.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "InsetMathXArrow.h"
16 #include "MathStream.h"
17 #include "MathSupport.h"
19 #include "LaTeXFeatures.h"
20 #include "MetricsInfo.h"
22 #include "support/debug.h"
23 #include "support/lassert.h"
32 InsetMathXArrow::InsetMathXArrow(Buffer * buf, docstring const & name)
33 : InsetMathFracBase(buf), name_(name)
37 Inset * InsetMathXArrow::clone() const
39 return new InsetMathXArrow(*this);
43 void InsetMathXArrow::metrics(MetricsInfo & mi, Dimension & dim) const
45 Changer dummy2 = mi.base.changeEnsureMath();
46 Changer dummy = mi.base.changeScript();
48 cell(0).metrics(mi, dim0);
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();
57 void InsetMathXArrow::draw(PainterInfo & pi, int x, int y) const
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_);
71 void InsetMathXArrow::write(TeXMathStream & os) const
73 MathEnsurer ensurer(os);
76 os << '[' << cell(1) << ']';
77 os << '{' << cell(0) << '}';
81 void InsetMathXArrow::normalize(NormalStream & os) const
83 os << "[xarrow " << name_ << ' ' << cell(0) << ' ' << cell(1) << ']';
87 void InsetMathXArrow::mathmlize(MathMLStream & ms) const
91 if (!ms.xmlMode()) { // Use HTML entities.
92 if (name_ == "xleftarrow")
94 else if (name_ == "xrightarrow")
96 else if (name_ == "xhookleftarrow")
98 else if (name_ == "xhookrightarrow")
100 else if (name_ == "xLeftarrow")
102 else if (name_ == "xRightarrow")
104 else if (name_ == "xleftrightarrow")
105 arrow = "↔";
106 else if (name_ == "xLeftrightarrow")
107 arrow = "⇔";
108 else if (name_ == "xleftharpoondown")
109 arrow = "↽";
110 else if (name_ == "xleftharpoonup")
111 arrow = "↼";
112 else if (name_ == "xleftrightharpoons")
113 arrow = "⇋";
114 else if (name_ == "xrightharpoondown")
115 arrow = "⇁";
116 else if (name_ == "xrightharpoonup")
117 arrow = "⇀";
118 else if (name_ == "xrightleftharpoons")
119 arrow = "⇌";
120 else if (name_ == "xmapsto")
123 lyxerr << "mathmlize conversion for '" << name_ << "' not implemented" << endl;
124 LASSERT(false, arrow = "→");
126 } else { // Use XML entities.
127 if (name_ == "xleftarrow")
129 else if (name_ == "xrightarrow")
131 else if (name_ == "xhookleftarrow")
133 else if (name_ == "xhookrightarrow")
135 else if (name_ == "xLeftarrow")
137 else if (name_ == "xRightarrow")
139 else if (name_ == "xleftrightarrow")
141 else if (name_ == "xLeftrightarrow")
143 else if (name_ == "xleftharpoondown")
145 else if (name_ == "xleftharpoonup")
147 else if (name_ == "xleftrightharpoons")
149 else if (name_ == "xrightharpoondown")
151 else if (name_ == "xrightharpoonup")
153 else if (name_ == "xrightleftharpoons")
155 else if (name_ == "xmapsto")
158 lyxerr << "mathmlize XML conversion for '" << name_ << "' not implemented" << endl;
159 LASSERT(false, arrow = "→");
163 ms << "<" << from_ascii(ms.namespacedTag("munderover")) << " accent='false' accentunder='false'>"
164 << arrow << cell(1) << cell(0)
165 << "</" << from_ascii(ms.namespacedTag("munderover"))<< ">";
169 void InsetMathXArrow::htmlize(HtmlStream & os) const
173 if (name_ == "xleftarrow")
175 else if (name_ == "xrightarrow")
177 else if (name_ == "xhookleftarrow")
179 else if (name_ == "xhookrightarrow")
181 else if (name_ == "xLeftarrow")
183 else if (name_ == "xRightarrow")
185 else if (name_ == "xleftrightarrow")
186 arrow = "↔";
187 else if (name_ == "xLeftrightarrow")
188 arrow = "⇔";
189 else if (name_ == "xleftharpoondown")
190 arrow = "↽";
191 else if (name_ == "xleftharpoonup")
192 arrow = "↼";
193 else if (name_ == "xleftrightharpoons")
194 arrow = "⇋";
195 else if (name_ == "xrightharpoondown")
196 arrow = "⇁";
197 else if (name_ == "xrightharpoonup")
198 arrow = "⇀";
199 else if (name_ == "xrightleftharpoons")
200 arrow = "⇌";
201 else if (name_ == "xmapsto")
204 lyxerr << "htmlize conversion for '" << name_ << "' not implemented" << endl;
205 LASSERT(false, arrow = "→");
207 os << MTag("span", "class='xarrow'")
208 << MTag("span", "class='xatop'") << cell(0) << ETag("span")
209 << MTag("span", "class='xabottom'") << arrow << ETag("span")
214 void InsetMathXArrow::validate(LaTeXFeatures & features) const
216 if (name_ == "xleftarrow" || name_ == "xrightarrow")
217 features.require("amsmath");
219 features.require("mathtools");
220 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
221 // CSS adapted from eLyXer
222 features.addCSSSnippet(
223 "span.xarrow{display: inline-block; vertical-align: middle; text-align:center;}\n"
224 "span.xatop{display: block;}\n"
225 "span.xabottom{display: block;}");
226 InsetMathNest::validate(features);