]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDelim.cpp
Simplify Changers interface
[lyx.git] / src / mathed / InsetMathDelim.cpp
1 /**
2  * \file InsetMathDelim.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author André Pönitz
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetMathDelim.h"
15
16 #include "MathData.h"
17 #include "MathFactory.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "MetricsInfo.h"
21
22 #include "LaTeXFeatures.h"
23
24 #include "support/docstring.h"
25
26 #include "frontends/FontMetrics.h"
27
28 #include <algorithm>
29
30 using namespace std;
31
32 namespace lyx {
33
34 static docstring convertDelimToLatexName(docstring const & name)
35 {
36         if (name.size() == 1) {
37                 char_type const c = name[0];
38                 if (c == '<' || c == '(' || c == '[' || c == '.'
39                     || c == '>' || c == ')' || c == ']' || c == '/' || c == '|')
40                         return name;
41         }
42         return '\\' + name + ' ';
43 }
44
45
46 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l,
47                 docstring const & r)
48         : InsetMathNest(buf, 1), left_(l), right_(r), dw_(0)
49 {}
50
51
52 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l, docstring const & r,
53         MathData const & ar)
54         : InsetMathNest(buf, 1), left_(l), right_(r), dw_(0)
55 {
56         cell(0) = ar;
57 }
58
59
60 Inset * InsetMathDelim::clone() const
61 {
62         return new InsetMathDelim(*this);
63 }
64
65
66 void InsetMathDelim::validate(LaTeXFeatures & features) const
67 {
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         MathWordList const & words = mathedWordList();
73         MathWordList::const_iterator it = words.find(left_);
74         if (it != words.end())
75         {
76                 string const req = it->second.requires;
77                 if (!req.empty())
78                         features.require(req);
79         }
80         it = words.find(right_);
81         if (it != words.end())
82         {
83                 string const req = it->second.requires;
84                 if (!req.empty())
85                         features.require(req);
86         }
87 }
88
89
90 void InsetMathDelim::write(WriteStream & os) const
91 {
92         MathEnsurer ensurer(os);
93         os << "\\left" << convertDelimToLatexName(left_) << cell(0)
94            << "\\right" << convertDelimToLatexName(right_);
95 }
96
97
98 void InsetMathDelim::normalize(NormalStream & os) const
99 {
100         os << "[delim " << convertDelimToLatexName(left_) << ' '
101            << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
102 }
103
104
105 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
106 {
107         Dimension dim0;
108         cell(0).metrics(mi, dim0);
109         Dimension t = theFontMetrics(mi.base.font).dimension('I');
110         int h0 = (t.asc + t.des) / 2;
111         int a0 = max(dim0.asc, t.asc)   - h0;
112         int d0 = max(dim0.des, t.des)  + h0;
113         dw_ = dim0.height() / 5;
114         if (dw_ > 8)
115                 dw_ = 8;
116         if (dw_ < 4)
117                 dw_ = 4;
118         dim.wid = dim0.width() + 2 * dw_;
119         dim.asc = max(a0, d0) + h0;
120         dim.des = max(a0, d0) - h0;
121 }
122
123
124 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
125 {
126         Dimension const dim = dimension(*pi.base.bv);
127         int const b = y - dim.asc;
128         cell(0).draw(pi, x + dw_, y);
129         mathed_draw_deco(pi, x, b, dw_, dim.height(), left_);
130         mathed_draw_deco(pi, x + dim.width() - dw_,
131                 b, dw_, dim.height(), right_);
132         setPosCache(pi, x, y);
133 }
134
135
136 bool InsetMathDelim::isParenthesis() const
137 {
138         return left_ == "(" && right_ == ")";
139 }
140
141
142 bool InsetMathDelim::isBrackets() const
143 {
144         return left_ == "[" && right_ == "]";
145 }
146
147
148 bool InsetMathDelim::isAbs() const
149 {
150         return left_ == "|" && right_ == "|";
151 }
152
153
154 void InsetMathDelim::maple(MapleStream & os) const
155 {
156         if (isAbs()) {
157                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
158                         os << "linalg[det](" << cell(0) << ')';
159                 else
160                         os << "abs(" << cell(0) << ')';
161         }
162         else
163                 os << left_ << cell(0) << right_;
164 }
165
166
167 void InsetMathDelim::maxima(MaximaStream & os) const
168 {
169         if (isAbs()) {
170                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
171                         os << "determinant(" << cell(0) << ')';
172                 else
173                         os << "abs(" << cell(0) << ')';
174         }
175         else
176                 os << left_ << cell(0) << right_;
177 }
178
179
180 void InsetMathDelim::mathematica(MathematicaStream & os) const
181 {
182         if (isAbs()) {
183                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
184                         os << "Det" << cell(0) << ']';
185                 else
186                         os << "Abs[" << cell(0) << ']';
187         }
188         else
189                 os << left_ << cell(0) << right_;
190 }
191
192
193 void InsetMathDelim::mathmlize(MathStream & os) const
194 {
195         os << "<mo form='prefix' fence='true' stretchy='true' symmetric='true'>"
196            << convertDelimToXMLEscape(left_) 
197            << "</mo>\n"
198            << cell(0) 
199            << "\n<mo form='postfix' fence='true' stretchy='true' symmetric='true'>" 
200            << convertDelimToXMLEscape(right_) 
201            << "</mo>\n";
202 }
203
204
205 void InsetMathDelim::htmlize(HtmlStream & os) const
206 {
207         os << convertDelimToXMLEscape(left_) 
208            << cell(0) 
209            << convertDelimToXMLEscape(right_);
210 }
211
212
213 void InsetMathDelim::octave(OctaveStream & os) const
214 {
215         if (isAbs())
216                 os << "det(" << cell(0) << ')';
217         else
218                 os << left_ << cell(0) << right_;
219 }
220
221
222 } // namespace lyx