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 MathEnsurer ensurer(os);
63 os << "\\left" << convertDelimToLatexName(left_) << cell(0)
64 << "\\right" << convertDelimToLatexName(right_);
68 void InsetMathDelim::normalize(NormalStream & os) const
70 os << "[delim " << convertDelimToLatexName(left_) << ' '
71 << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
75 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
78 cell(0).metrics(mi, dim0);
79 Dimension t = theFontMetrics(mi.base.font).dimension('I');
80 int h0 = (t.asc + t.des) / 2;
81 int a0 = max(dim0.asc, t.asc) - h0;
82 int d0 = max(dim0.des, t.des) + h0;
83 dw_ = dim0.height() / 5;
88 dim.wid = dim0.width() + 2 * dw_ + 8;
89 dim.asc = max(a0, d0) + h0;
90 dim.des = max(a0, d0) - h0;
94 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
96 Dimension const dim = dimension(*pi.base.bv);
97 int const b = y - dim.asc;
98 cell(0).draw(pi, x + dw_ + 4, y);
99 mathed_draw_deco(pi, x + 4, b, dw_, dim.height(), left_);
100 mathed_draw_deco(pi, x + dim.width() - dw_ - 4,
101 b, dw_, dim.height(), right_);
102 setPosCache(pi, x, y);
106 bool InsetMathDelim::isParenthesis() const
108 return left_ == "(" && right_ == ")";
112 bool InsetMathDelim::isBrackets() const
114 return left_ == "[" && right_ == "]";
118 bool InsetMathDelim::isAbs() const
120 return left_ == "|" && right_ == "|";
124 void InsetMathDelim::maple(MapleStream & os) const
127 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
128 os << "linalg[det](" << cell(0) << ')';
130 os << "abs(" << cell(0) << ')';
133 os << left_ << cell(0) << right_;
137 void InsetMathDelim::maxima(MaximaStream & os) const
140 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
141 os << "determinant(" << cell(0) << ')';
143 os << "abs(" << cell(0) << ')';
146 os << left_ << cell(0) << right_;
150 void InsetMathDelim::mathematica(MathematicaStream & os) const
153 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
154 os << "Det" << cell(0) << ']';
156 os << "Abs[" << cell(0) << ']';
159 os << left_ << cell(0) << right_;
163 void InsetMathDelim::mathmlize(MathStream & os) const
165 os << "<fenced open=\"" << left_ << "\" close=\""
166 << right_ << "\">" << cell(0) << "</fenced>";
170 void InsetMathDelim::octave(OctaveStream & os) const
173 os << "det(" << cell(0) << ')';
175 os << left_ << cell(0) << right_;