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 os << "\\left" << convertDelimToLatexName(left_) << cell(0)
63 << "\\right" << convertDelimToLatexName(right_);
67 void InsetMathDelim::normalize(NormalStream & os) const
69 os << "[delim " << convertDelimToLatexName(left_) << ' '
70 << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
74 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
77 cell(0).metrics(mi, dim0);
78 Dimension t = theFontMetrics(mi.base.font).dimension('I');
79 int h0 = (t.asc + t.des) / 2;
80 int a0 = max(dim0.asc, t.asc) - h0;
81 int d0 = max(dim0.des, t.des) + h0;
82 dw_ = dim0.height() / 5;
87 dim.wid = dim0.width() + 2 * dw_ + 8;
88 dim.asc = max(a0, d0) + h0;
89 dim.des = max(a0, d0) - h0;
90 // Cache the inset dimension.
95 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
97 Dimension const dim = dimension(*pi.base.bv);
98 int const b = y - dim.asc;
99 cell(0).draw(pi, x + dw_ + 4, y);
100 mathed_draw_deco(pi, x + 4, b, dw_, dim.height(), left_);
101 mathed_draw_deco(pi, x + dim.width() - dw_ - 4,
102 b, dw_, dim.height(), right_);
103 setPosCache(pi, x, y);
107 bool InsetMathDelim::isParenthesis() const
109 return left_ == "(" && right_ == ")";
113 bool InsetMathDelim::isBrackets() const
115 return left_ == "[" && right_ == "]";
119 bool InsetMathDelim::isAbs() const
121 return left_ == "|" && right_ == "|";
125 void InsetMathDelim::maple(MapleStream & os) const
128 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
129 os << "linalg[det](" << cell(0) << ')';
131 os << "abs(" << cell(0) << ')';
134 os << left_ << cell(0) << right_;
138 void InsetMathDelim::maxima(MaximaStream & os) const
141 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
142 os << "determinant(" << cell(0) << ')';
144 os << "abs(" << cell(0) << ')';
147 os << left_ << cell(0) << right_;
151 void InsetMathDelim::mathematica(MathematicaStream & os) const
154 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
155 os << "Det" << cell(0) << ']';
157 os << "Abs[" << cell(0) << ']';
160 os << left_ << cell(0) << right_;
164 void InsetMathDelim::mathmlize(MathStream & os) const
166 os << "<fenced open=\"" << left_ << "\" close=\""
167 << right_ << "\">" << cell(0) << "</fenced>";
171 void InsetMathDelim::octave(OctaveStream & os) const
174 os << "det(" << cell(0) << ')';
176 os << left_ << cell(0) << right_;