2 * \file InsetMathDelim.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
9 * Full author contact details are available in file CREDITS.
14 #include "InsetMathDelim.h"
17 #include "MathFactory.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "MetricsInfo.h"
22 #include "LaTeXFeatures.h"
24 #include "support/docstring.h"
26 #include "frontends/FontMetrics.h"
34 static docstring convertDelimToLatexName(docstring const & name)
36 if (name.size() == 1) {
37 char_type const c = name[0];
38 if (c == '<' || c == '(' || c == '[' || c == '.'
39 || c == '>' || c == ')' || c == ']' || c == '/' || c == '|')
42 return '\\' + name + ' ';
46 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l,
48 : InsetMathNest(buf, 1), left_(l), right_(r), dw_(0)
52 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l, docstring const & r,
54 : InsetMathNest(buf, 1), left_(l), right_(r), dw_(0)
60 Inset * InsetMathDelim::clone() const
62 return new InsetMathDelim(*this);
66 void InsetMathDelim::validate(LaTeXFeatures & features) const
68 InsetMathNest::validate(features);
69 // The delimiters may be used without \left or \right as well.
70 // Therefore they are listed in lib/symbols, and if they have
71 // requirements, we need to add them here.
72 validate_math_word(features, left_);
73 validate_math_word(features, right_);
77 void InsetMathDelim::write(WriteStream & os) const
79 MathEnsurer ensurer(os);
80 os << "\\left" << convertDelimToLatexName(left_) << cell(0)
81 << "\\right" << convertDelimToLatexName(right_);
85 void InsetMathDelim::normalize(NormalStream & os) const
87 os << "[delim " << convertDelimToLatexName(left_) << ' '
88 << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
92 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
94 Changer dummy = mi.base.changeEnsureMath();
96 cell(0).metrics(mi, dim0, false);
97 Dimension t = theFontMetrics(mi.base.font).dimension('I');
98 int h0 = (t.asc + t.des) / 2;
99 int a0 = max(dim0.asc, t.asc) - h0;
100 int d0 = max(dim0.des, t.des) + h0;
101 dw_ = dim0.height() / 5;
106 dim.wid = dim0.width() + 2 * dw_ + 2 * mathed_thinmuskip(mi.base.font);
107 dim.asc = max(a0, d0) + h0;
108 dim.des = max(a0, d0) - h0;
112 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
114 Changer dummy = pi.base.changeEnsureMath();
115 Dimension const dim = dimension(*pi.base.bv);
116 int const b = y - dim.asc;
117 int const skip = mathed_thinmuskip(pi.base.font);
118 cell(0).draw(pi, x + dw_ + skip, y);
119 mathed_draw_deco(pi, x + skip / 2, b, dw_, dim.height(), left_);
120 mathed_draw_deco(pi, x + dim.width() - dw_ - skip / 2,
121 b, dw_, dim.height(), right_);
125 bool InsetMathDelim::isParenthesis() const
127 return left_ == "(" && right_ == ")";
131 bool InsetMathDelim::isBrackets() const
133 return left_ == "[" && right_ == "]";
137 bool InsetMathDelim::isAbs() const
139 return left_ == "|" && right_ == "|";
143 void InsetMathDelim::maple(MapleStream & os) const
146 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
147 os << "linalg[det](" << cell(0) << ')';
149 os << "abs(" << cell(0) << ')';
152 os << left_ << cell(0) << right_;
156 void InsetMathDelim::maxima(MaximaStream & os) const
159 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
160 os << "determinant(" << cell(0) << ')';
162 os << "abs(" << cell(0) << ')';
165 os << left_ << cell(0) << right_;
169 void InsetMathDelim::mathematica(MathematicaStream & os) const
172 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
173 os << "Det[" << cell(0) << ']';
175 os << "Abs[" << cell(0) << ']';
178 os << left_ << cell(0) << right_;
182 void InsetMathDelim::mathmlize(MathStream & ms) const
184 ms << "<" << from_ascii(ms.namespacedTag("mo")) << " form='prefix' fence='true' stretchy='true' symmetric='true'>"
185 << "<" << from_ascii(ms.namespacedTag("mrow")) << ">"
186 << convertDelimToXMLEscape(left_, ms.xmlMode())
187 << "</" << from_ascii(ms.namespacedTag("mrow")) << ">"
188 << "</" << from_ascii(ms.namespacedTag("mo")) << ">"
192 << "<" << from_ascii(ms.namespacedTag("mo")) << " form='postfix' fence='true' stretchy='true' symmetric='true'>"
193 << "<" << from_ascii(ms.namespacedTag("mrow")) << ">"
194 << convertDelimToXMLEscape(right_, ms.xmlMode())
195 << "</" << from_ascii(ms.namespacedTag("mrow")) << ">"
196 << "</" << from_ascii(ms.namespacedTag("mo")) << ">"
201 void InsetMathDelim::htmlize(HtmlStream & os) const
203 os << convertDelimToXMLEscape(left_, false)
205 << convertDelimToXMLEscape(right_, false);
209 void InsetMathDelim::octave(OctaveStream & os) const
212 os << "det(" << cell(0) << ')';
214 os << left_ << cell(0) << right_;