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 "MathStream.h"
18 #include "MathSupport.h"
19 #include "MetricsInfo.h"
21 #include "support/docstring.h"
23 #include "frontends/FontMetrics.h"
29 static docstring convertDelimToLatexName(docstring const & name)
31 if (name.size() == 1) {
32 char_type const c = name[0];
33 if (c == '<' || c == '(' || c == '[' || c == '.'
34 || c == '>' || c == ')' || c == ']' || c == '/' || c == '|')
37 return '\\' + name + ' ';
41 InsetMathDelim::InsetMathDelim(docstring const & l, docstring const & r)
42 : InsetMathNest(1), left_(l), right_(r)
46 InsetMathDelim::InsetMathDelim
47 (docstring const & l, docstring const & r, MathData const & ar)
48 : InsetMathNest(1), left_(l), right_(r)
54 Inset * InsetMathDelim::clone() const
56 return new InsetMathDelim(*this);
60 void InsetMathDelim::write(WriteStream & os) const
62 bool brace = os.pendingBrace();
63 os.pendingBrace(false);
64 if (os.latex() && os.textMode()) {
65 os << "\\ensuremath{";
70 os << "\\left" << convertDelimToLatexName(left_) << cell(0)
71 << "\\right" << convertDelimToLatexName(right_);
73 os.pendingBrace(brace);
77 void InsetMathDelim::normalize(NormalStream & os) const
79 os << "[delim " << convertDelimToLatexName(left_) << ' '
80 << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
84 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
87 cell(0).metrics(mi, dim0);
88 Dimension t = theFontMetrics(mi.base.font).dimension('I');
89 int h0 = (t.asc + t.des) / 2;
90 int a0 = max(dim0.asc, t.asc) - h0;
91 int d0 = max(dim0.des, t.des) + h0;
92 dw_ = dim0.height() / 5;
97 dim.wid = dim0.width() + 2 * dw_ + 8;
98 dim.asc = max(a0, d0) + h0;
99 dim.des = max(a0, d0) - h0;
103 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
105 Dimension const dim = dimension(*pi.base.bv);
106 int const b = y - dim.asc;
107 cell(0).draw(pi, x + dw_ + 4, y);
108 mathed_draw_deco(pi, x + 4, b, dw_, dim.height(), left_);
109 mathed_draw_deco(pi, x + dim.width() - dw_ - 4,
110 b, dw_, dim.height(), right_);
111 setPosCache(pi, x, y);
115 bool InsetMathDelim::isParenthesis() const
117 return left_ == "(" && right_ == ")";
121 bool InsetMathDelim::isBrackets() const
123 return left_ == "[" && right_ == "]";
127 bool InsetMathDelim::isAbs() const
129 return left_ == "|" && right_ == "|";
133 void InsetMathDelim::maple(MapleStream & os) const
136 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
137 os << "linalg[det](" << cell(0) << ')';
139 os << "abs(" << cell(0) << ')';
142 os << left_ << cell(0) << right_;
146 void InsetMathDelim::maxima(MaximaStream & os) const
149 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
150 os << "determinant(" << cell(0) << ')';
152 os << "abs(" << cell(0) << ')';
155 os << left_ << cell(0) << right_;
159 void InsetMathDelim::mathematica(MathematicaStream & os) const
162 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
163 os << "Det" << cell(0) << ']';
165 os << "Abs[" << cell(0) << ']';
168 os << left_ << cell(0) << right_;
172 void InsetMathDelim::mathmlize(MathStream & os) const
174 os << "<fenced open=\"" << left_ << "\" close=\""
175 << right_ << "\">" << cell(0) << "</fenced>";
179 void InsetMathDelim::octave(OctaveStream & os) const
182 os << "det(" << cell(0) << ')';
184 os << left_ << cell(0) << right_;