]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDelim.cpp
Remove obsolete (and false) comment.
[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         validate_math_word(features, left_);
73         validate_math_word(features, right_);
74 }
75
76
77 void InsetMathDelim::write(WriteStream & os) const
78 {
79         MathEnsurer ensurer(os);
80         os << "\\left" << convertDelimToLatexName(left_) << cell(0)
81            << "\\right" << convertDelimToLatexName(right_);
82 }
83
84
85 void InsetMathDelim::normalize(NormalStream & os) const
86 {
87         os << "[delim " << convertDelimToLatexName(left_) << ' '
88            << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
89 }
90
91
92 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
93 {
94         Changer dummy = mi.base.changeEnsureMath();
95         Dimension dim0;
96         cell(0).metrics(mi, dim0, false);
97         Dimension t = theFontMetrics(mi.base.font).dimension('I');
98         int h0 = (t.asc + t.des) / 2;
99         int a0 = max(dim0.asc, t.asc)   - h0;
100         int d0 = max(dim0.des, t.des)  + h0;
101         dw_ = dim0.height() / 5;
102         if (dw_ > 8)
103                 dw_ = 8;
104         if (dw_ < 4)
105                 dw_ = 4;
106         dim.wid = dim0.width() + 2 * dw_ + 2 * mathed_thinmuskip(mi.base.font);
107         dim.asc = max(a0, d0) + h0;
108         dim.des = max(a0, d0) - h0;
109 }
110
111
112 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
113 {
114         Changer dummy = pi.base.changeEnsureMath();
115         Dimension const dim = dimension(*pi.base.bv);
116         int const b = y - dim.asc;
117         int const skip = mathed_thinmuskip(pi.base.font);
118         cell(0).draw(pi, x + dw_ + skip, y);
119         mathed_draw_deco(pi, x + skip / 2, b, dw_, dim.height(), left_);
120         mathed_draw_deco(pi, x + dim.width() - dw_ - skip / 2,
121                 b, dw_, dim.height(), right_);
122 }
123
124
125 bool InsetMathDelim::isParenthesis() const
126 {
127         return left_ == "(" && right_ == ")";
128 }
129
130
131 bool InsetMathDelim::isBrackets() const
132 {
133         return left_ == "[" && right_ == "]";
134 }
135
136
137 bool InsetMathDelim::isAbs() const
138 {
139         return left_ == "|" && right_ == "|";
140 }
141
142
143 void InsetMathDelim::maple(MapleStream & os) const
144 {
145         if (isAbs()) {
146                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
147                         os << "linalg[det](" << cell(0) << ')';
148                 else
149                         os << "abs(" << cell(0) << ')';
150         }
151         else
152                 os << left_ << cell(0) << right_;
153 }
154
155
156 void InsetMathDelim::maxima(MaximaStream & os) const
157 {
158         if (isAbs()) {
159                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
160                         os << "determinant(" << cell(0) << ')';
161                 else
162                         os << "abs(" << cell(0) << ')';
163         }
164         else
165                 os << left_ << cell(0) << right_;
166 }
167
168
169 void InsetMathDelim::mathematica(MathematicaStream & os) const
170 {
171         if (isAbs()) {
172                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
173                         os << "Det" << cell(0) << ']';
174                 else
175                         os << "Abs[" << cell(0) << ']';
176         }
177         else
178                 os << left_ << cell(0) << right_;
179 }
180
181
182 void InsetMathDelim::mathmlize(MathStream & os) const
183 {
184         os << "<mo form='prefix' fence='true' stretchy='true' symmetric='true'>"
185            << "<mrow>"
186            << convertDelimToXMLEscape(left_)
187            << "</mrow>"
188            << "</mo>\n"
189            << cell(0)
190            << "\n<mo form='postfix' fence='true' stretchy='true' symmetric='true'>"
191            << "<mrow>"
192            << convertDelimToXMLEscape(right_)
193            << "</mrow>"
194            << "</mo>\n";
195 }
196
197
198 void InsetMathDelim::htmlize(HtmlStream & os) const
199 {
200         os << convertDelimToXMLEscape(left_)
201            << cell(0)
202            << convertDelimToXMLEscape(right_);
203 }
204
205
206 void InsetMathDelim::octave(OctaveStream & os) const
207 {
208         if (isAbs())
209                 os << "det(" << cell(0) << ')';
210         else
211                 os << left_ << cell(0) << right_;
212 }
213
214
215 } // namespace lyx