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"
32 static docstring convertDelimToLatexName(docstring const & name)
34 if (name.size() == 1) {
35 char_type const c = name[0];
36 if (c == '<' || c == '(' || c == '[' || c == '.'
37 || c == '>' || c == ')' || c == ']' || c == '/' || c == '|')
40 return '\\' + name + ' ';
44 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l,
46 : InsetMathNest(buf, 1), left_(l), right_(r)
50 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l, docstring const & r,
52 : InsetMathNest(buf, 1), left_(l), right_(r)
58 Inset * InsetMathDelim::clone() const
60 return new InsetMathDelim(*this);
64 void InsetMathDelim::validate(LaTeXFeatures & features) const
66 InsetMathNest::validate(features);
67 // The delimiters may be used without \left or \right as well.
68 // Therefore they are listed in lib/symbols, and if they have
69 // requirements, we need to add them here.
70 MathWordList const & words = mathedWordList();
71 MathWordList::const_iterator it = words.find(left_);
72 if (it != words.end())
74 docstring const req = it->second.requires;
76 features.require(to_ascii(req));
78 it = words.find(right_);
79 if (it != words.end())
81 docstring const req = it->second.requires;
83 features.require(to_ascii(req));
88 void InsetMathDelim::write(WriteStream & os) const
90 MathEnsurer ensurer(os);
91 os << "\\left" << convertDelimToLatexName(left_) << cell(0)
92 << "\\right" << convertDelimToLatexName(right_);
96 void InsetMathDelim::normalize(NormalStream & os) const
98 os << "[delim " << convertDelimToLatexName(left_) << ' '
99 << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
103 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
106 cell(0).metrics(mi, dim0);
107 Dimension t = theFontMetrics(mi.base.font).dimension('I');
108 int h0 = (t.asc + t.des) / 2;
109 int a0 = max(dim0.asc, t.asc) - h0;
110 int d0 = max(dim0.des, t.des) + h0;
111 dw_ = dim0.height() / 5;
116 dim.wid = dim0.width() + 2 * dw_ + 8;
117 dim.asc = max(a0, d0) + h0;
118 dim.des = max(a0, d0) - h0;
122 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
124 Dimension const dim = dimension(*pi.base.bv);
125 int const b = y - dim.asc;
126 cell(0).draw(pi, x + dw_ + 4, y);
127 mathed_draw_deco(pi, x + 4, b, dw_, dim.height(), left_);
128 mathed_draw_deco(pi, x + dim.width() - dw_ - 4,
129 b, dw_, dim.height(), right_);
130 setPosCache(pi, x, y);
134 bool InsetMathDelim::isParenthesis() const
136 return left_ == "(" && right_ == ")";
140 bool InsetMathDelim::isBrackets() const
142 return left_ == "[" && right_ == "]";
146 bool InsetMathDelim::isAbs() const
148 return left_ == "|" && right_ == "|";
152 void InsetMathDelim::maple(MapleStream & os) const
155 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
156 os << "linalg[det](" << cell(0) << ')';
158 os << "abs(" << cell(0) << ')';
161 os << left_ << cell(0) << right_;
165 void InsetMathDelim::maxima(MaximaStream & os) const
168 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
169 os << "determinant(" << cell(0) << ')';
171 os << "abs(" << cell(0) << ')';
174 os << left_ << cell(0) << right_;
178 void InsetMathDelim::mathematica(MathematicaStream & os) const
181 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
182 os << "Det" << cell(0) << ']';
184 os << "Abs[" << cell(0) << ']';
187 os << left_ << cell(0) << right_;
191 void InsetMathDelim::mathmlize(MathStream & os) const
193 os << "<mo form='prefix' fence='true' stretchy='true' symmetric='true'>" << left_ << "</mo>"
194 << cell(0) << "<mo form='postfix' fence='true' stretchy='true' symmetric='true'>" << right_ << "</mo>";
198 void InsetMathDelim::htmlize(HtmlStream & os) const
200 os << left_ << cell(0) << right_;
204 void InsetMathDelim::octave(OctaveStream & os) const
207 os << "det(" << cell(0) << ')';
209 os << left_ << cell(0) << right_;