]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDelim.cpp
g-brief loads babel internally. So don't load it ourselves.
[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), is_extracted_(false)
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), is_extracted_(false)
55 {
56         cell(0) = ar;
57 }
58
59
60 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l, docstring const & r,
61                                MathData const & ar, bool const is_extracted)
62                 : InsetMathNest(buf, 1), left_(l), right_(r), dw_(0), is_extracted_(is_extracted)
63 {
64         cell(0) = ar;
65 }
66
67
68 Inset * InsetMathDelim::clone() const
69 {
70         return new InsetMathDelim(*this);
71 }
72
73
74 void InsetMathDelim::validate(LaTeXFeatures & features) const
75 {
76         InsetMathNest::validate(features);
77         // The delimiters may be used without \left or \right as well.
78         // Therefore they are listed in lib/symbols, and if they have
79         // requirements, we need to add them here.
80         validate_math_word(features, left_);
81         validate_math_word(features, right_);
82 }
83
84
85 void InsetMathDelim::write(TeXMathStream & os) const
86 {
87         MathEnsurer ensurer(os);
88         os << "\\left" << convertDelimToLatexName(left_) << cell(0)
89            << "\\right" << convertDelimToLatexName(right_);
90 }
91
92
93 void InsetMathDelim::normalize(NormalStream & os) const
94 {
95         os << "[delim " << convertDelimToLatexName(left_) << ' '
96            << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
97 }
98
99
100 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
101 {
102         Changer dummy = mi.base.changeEnsureMath();
103         Dimension dim0;
104         cell(0).metrics(mi, dim0, false);
105         Dimension t = theFontMetrics(mi.base.font).dimension('I');
106         int h0 = (t.asc + t.des) / 2;
107         int a0 = max(dim0.asc, t.asc)   - h0;
108         int d0 = max(dim0.des, t.des)  + h0;
109         dw_ = dim0.height() / 5;
110         if (dw_ > 8)
111                 dw_ = 8;
112         if (dw_ < 4)
113                 dw_ = 4;
114         dim.wid = dim0.width() + 2 * dw_ + 2 * mathed_thinmuskip(mi.base.font);
115         dim.asc = max(a0, d0) + h0;
116         dim.des = max(a0, d0) - h0;
117 }
118
119
120 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
121 {
122         Changer dummy = pi.base.changeEnsureMath();
123         Dimension const dim = dimension(*pi.base.bv);
124         int const b = y - dim.asc;
125         int const skip = mathed_thinmuskip(pi.base.font);
126         cell(0).draw(pi, x + dw_ + skip, y);
127         mathed_draw_deco(pi, x + skip / 2, b, dw_, dim.height(), left_);
128         mathed_draw_deco(pi, x + dim.width() - dw_ - skip / 2,
129                 b, dw_, dim.height(), right_);
130 }
131
132
133 bool InsetMathDelim::isParenthesis() const
134 {
135         return left_ == "(" && right_ == ")";
136 }
137
138
139 bool InsetMathDelim::isBrackets() const
140 {
141         return left_ == "[" && right_ == "]";
142 }
143
144
145 bool InsetMathDelim::isAbs() const
146 {
147         return left_ == "|" && right_ == "|";
148 }
149
150
151 void InsetMathDelim::maple(MapleStream & os) const
152 {
153         if (isAbs()) {
154                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
155                         os << "linalg[det](" << cell(0) << ')';
156                 else
157                         os << "abs(" << cell(0) << ')';
158         }
159         else
160                 os << left_ << cell(0) << right_;
161 }
162
163
164 void InsetMathDelim::maxima(MaximaStream & os) const
165 {
166         if (isAbs()) {
167                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
168                         os << "determinant(" << cell(0) << ')';
169                 else
170                         os << "abs(" << cell(0) << ')';
171         }
172         else
173                 os << left_ << cell(0) << right_;
174 }
175
176
177 void InsetMathDelim::mathematica(MathematicaStream & os) const
178 {
179         if (isAbs()) {
180                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
181                         os << "Det[" << cell(0) << ']';
182                 else
183                         os << "Abs[" << cell(0) << ']';
184         }
185         else
186                 os << left_ << cell(0) << right_;
187 }
188
189
190 void InsetMathDelim::mathmlize(MathMLStream & ms) const
191 {
192         // Ignore the delimiter if: it is empty or only a space (one character).
193         const std::string attr = is_extracted_ ? "stretchy='false'" : "";
194         if (!left_.empty() && ((left_.size() == 1 && left_[0] != ' ') || left_.size() > 1)) {
195             ms << MTag("mrow")
196                << MTagInline("mo", attr)
197                << convertDelimToXMLEscape(left_)
198                << ETagInline("mo");
199         }
200         ms << cell(0);
201         if (!right_.empty() && ((right_.size() == 1 && right_[0] != ' ') || right_.size() > 1)) {
202             ms << MTagInline("mo", attr)
203                << convertDelimToXMLEscape(right_)
204                << ETagInline("mo")
205                << ETag("mrow");
206         }
207 }
208
209
210 void InsetMathDelim::htmlize(HtmlStream & os) const
211 {
212         os << convertDelimToXMLEscape(left_)
213            << cell(0)
214            << convertDelimToXMLEscape(right_);
215 }
216
217
218 void InsetMathDelim::octave(OctaveStream & os) const
219 {
220         if (isAbs())
221                 os << "det(" << cell(0) << ')';
222         else
223                 os << left_ << cell(0) << right_;
224 }
225
226
227 } // namespace lyx