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_xml_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 docstring map_latex_to(docstring latex)
108 auto mapping = latex_to_xml_entities.find(to_ascii(latex));
109 if (mapping != latex_to_xml_entities.end()) {
110 return from_ascii(mapping->second);
112 lyxerr << "mathmlize conversion for '" << latex << "' not implemented" << endl;
113 LASSERT(false, return from_ascii(latex_to_xml_entities["xrightarrow"]));
119 void InsetMathXArrow::mathmlize(MathMLStream & ms) const
121 docstring arrow = map_latex_to(name_);
122 ms << MTag("munderover", "accent='false' accentunder='false'")
123 << MTagInline("mo") << arrow << ETagInline("mo")
124 << cell(1) << cell(0)
125 << ETag("munderover");
129 void InsetMathXArrow::htmlize(HtmlStream & os) const
131 docstring arrow = map_latex_to(name_);
132 os << MTag("span", "class='xarrow'")
133 << MTag("span", "class='xatop'") << cell(0) << ETag("span")
134 << MTag("span", "class='xabottom'") << arrow << ETag("span")
139 void InsetMathXArrow::validate(LaTeXFeatures & features) const
141 if (name_ == "xleftarrow" || name_ == "xrightarrow")
142 features.require("amsmath");
144 features.require("mathtools");
146 if (features.runparams().math_flavor == OutputParams::MathAsHTML)
147 // CSS adapted from eLyXer
148 features.addCSSSnippet(
149 "span.xarrow{display: inline-block; vertical-align: middle; text-align:center;}\n"
150 "span.xatop{display: block;}\n"
151 "span.xabottom{display: block;}");
152 InsetMathNest::validate(features);