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 static std::map<string, string> latex_to_html_entities = {
88 {"xleftarrow", "←"},
89 {"xrightarrow", "→"},
90 {"xhookleftarrow", "↩"},
91 {"xhookrightarrow", "↪"},
92 {"xLeftarrow", "⇐"},
93 {"xRightarrow", "⇒"},
94 {"xleftrightarrow", "↔"},
95 {"xLeftrightarrow", "⇔"},
96 {"xleftharpoondown", "↽"},
97 {"xleftharpoonup", "↼"},
98 {"xleftrightharpoons", "⇋"},
99 {"xrightharpoondown", "⇁"},
100 {"xrightharpoonup", "⇀"},
101 {"xrightleftharpoons", "⇌"},
102 {"xmapsto", "↦"},
106 static std::map<string, string> latex_to_xml_entities = {
107 {"xleftarrow", "←"},
108 {"xrightarrow", "→"},
109 {"xhookleftarrow", "↩"},
110 {"xhookrightarrow", "↪"},
111 {"xLeftarrow", "⇐"},
112 {"xRightarrow", "⇒"},
113 {"xleftrightarrow", "↔"},
114 {"xLeftrightarrow", "⇔"},
115 {"xleftharpoondown", "↽"},
116 {"xleftharpoonup", "↼"},
117 {"xleftrightharpoons", "⇋"},
118 {"xrightharpoondown", "⇁"},
119 {"xrightharpoonup", "⇀"},
120 {"xrightleftharpoons", "⇌"},
121 {"xmapsto", "↦"},
125 void InsetMathXArrow::mathmlize(MathMLStream & ms) const
129 if (!ms.xmlMode()) { // Use HTML entities.
130 auto mapping = latex_to_html_entities.find(to_ascii(name_));
131 if (mapping != latex_to_html_entities.end()) {
132 arrow = mapping->second;
134 lyxerr << "mathmlize conversion for '" << name_ << "' not implemented" << endl;
135 LASSERT(false, arrow = latex_to_html_entities["xrightarrow"]);
137 } else { // Use XML entities.
138 auto mapping = latex_to_xml_entities.find(to_ascii(name_));
139 if (mapping != latex_to_xml_entities.end()) {
140 arrow = mapping->second;
142 lyxerr << "mathmlize XML conversion for '" << name_ << "' not implemented" << endl;
143 LASSERT(false, arrow = latex_to_xml_entities["xrightarrow"]);
147 ms << "<" << from_ascii(ms.namespacedTag("munderover")) << " accent='false' accentunder='false'>"
148 << MTagInline("mo") << from_ascii(arrow) << ETagInline("mo")
149 << cell(1) << cell(0)
150 << "</" << from_ascii(ms.namespacedTag("munderover"))<< ">";
154 void InsetMathXArrow::htmlize(HtmlStream & os) const
158 auto mapping = latex_to_html_entities.find(to_ascii(name_));
159 if (mapping != latex_to_html_entities.end()) {
160 arrow = mapping->second;
162 lyxerr << "htmlize conversion for '" << name_ << "' not implemented" << endl;
163 LASSERT(false, arrow = latex_to_html_entities["xrightarrow"]);
166 os << MTag("span", "class='xarrow'")
167 << MTag("span", "class='xatop'") << cell(0) << ETag("span")
168 << MTag("span", "class='xabottom'") << from_ascii(arrow) << ETag("span")
173 void InsetMathXArrow::validate(LaTeXFeatures & features) const
175 if (name_ == "xleftarrow" || name_ == "xrightarrow")
176 features.require("amsmath");
178 features.require("mathtools");
179 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
180 // CSS adapted from eLyXer
181 features.addCSSSnippet(
182 "span.xarrow{display: inline-block; vertical-align: middle; text-align:center;}\n"
183 "span.xatop{display: block;}\n"
184 "span.xabottom{display: block;}");
185 InsetMathNest::validate(features);