]> git.lyx.org Git - features.git/blob - src/mathed/InsetMathDelim.cpp
9cf6414e294257b03909933ecd287209d6fc10cd
[features.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 "MathStream.h"
18 #include "MathSupport.h"
19 #include "MetricsInfo.h"
20
21 #include "support/docstring.h"
22
23 #include "frontends/FontMetrics.h"
24
25 using namespace std;
26
27 namespace lyx {
28
29 static docstring convertDelimToLatexName(docstring const & name)
30 {
31         if (name.size() == 1) {
32                 char_type const c = name[0];
33                 if (c == '<' || c == '(' || c == '[' || c == '.'
34                     || c == '>' || c == ')' || c == ']' || c == '/' || c == '|')
35                         return name;
36         }
37         return '\\' + name + ' ';
38 }
39
40
41 InsetMathDelim::InsetMathDelim(docstring const & l, docstring const & r)
42         : InsetMathNest(1), left_(l), right_(r)
43 {}
44
45
46 InsetMathDelim::InsetMathDelim
47                 (docstring const & l, docstring const & r, MathData const & ar)
48         : InsetMathNest(1), left_(l), right_(r)
49 {
50         cell(0) = ar;
51 }
52
53
54 Inset * InsetMathDelim::clone() const
55 {
56         return new InsetMathDelim(*this);
57 }
58
59
60 void InsetMathDelim::write(WriteStream & os) const
61 {
62         bool brace = os.pendingBrace();
63         os.pendingBrace(false);
64         if (os.latex() && os.textMode()) {
65                 os << "\\ensuremath{";
66                 os.textMode(false);
67                 brace = true;
68         }
69
70         os << "\\left" << convertDelimToLatexName(left_) << cell(0)
71            << "\\right" << convertDelimToLatexName(right_);
72
73         os.pendingBrace(brace);
74 }
75
76
77 void InsetMathDelim::normalize(NormalStream & os) const
78 {
79         os << "[delim " << convertDelimToLatexName(left_) << ' '
80            << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
81 }
82
83
84 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
85 {
86         Dimension dim0;
87         cell(0).metrics(mi, dim0);
88         Dimension t = theFontMetrics(mi.base.font).dimension('I');
89         int h0 = (t.asc + t.des) / 2;
90         int a0 = max(dim0.asc, t.asc)   - h0;
91         int d0 = max(dim0.des, t.des)  + h0;
92         dw_ = dim0.height() / 5;
93         if (dw_ > 8)
94                 dw_ = 8;
95         if (dw_ < 4)
96                 dw_ = 4;
97         dim.wid = dim0.width() + 2 * dw_ + 8;
98         dim.asc = max(a0, d0) + h0;
99         dim.des = max(a0, d0) - h0;
100 }
101
102
103 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
104 {
105         Dimension const dim = dimension(*pi.base.bv);
106         int const b = y - dim.asc;
107         cell(0).draw(pi, x + dw_ + 4, y);
108         mathed_draw_deco(pi, x + 4, b, dw_, dim.height(), left_);
109         mathed_draw_deco(pi, x + dim.width() - dw_ - 4,
110                 b, dw_, dim.height(), right_);
111         setPosCache(pi, x, y);
112 }
113
114
115 bool InsetMathDelim::isParenthesis() const
116 {
117         return left_ == "(" && right_ == ")";
118 }
119
120
121 bool InsetMathDelim::isBrackets() const
122 {
123         return left_ == "[" && right_ == "]";
124 }
125
126
127 bool InsetMathDelim::isAbs() const
128 {
129         return left_ == "|" && right_ == "|";
130 }
131
132
133 void InsetMathDelim::maple(MapleStream & os) const
134 {
135         if (isAbs()) {
136                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
137                         os << "linalg[det](" << cell(0) << ')';
138                 else
139                         os << "abs(" << cell(0) << ')';
140         }
141         else
142                 os << left_ << cell(0) << right_;
143 }
144
145
146 void InsetMathDelim::maxima(MaximaStream & os) const
147 {
148         if (isAbs()) {
149                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
150                         os << "determinant(" << cell(0) << ')';
151                 else
152                         os << "abs(" << cell(0) << ')';
153         }
154         else
155                 os << left_ << cell(0) << right_;
156 }
157
158
159 void InsetMathDelim::mathematica(MathematicaStream & os) const
160 {
161         if (isAbs()) {
162                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
163                         os << "Det" << cell(0) << ']';
164                 else
165                         os << "Abs[" << cell(0) << ']';
166         }
167         else
168                 os << left_ << cell(0) << right_;
169 }
170
171
172 void InsetMathDelim::mathmlize(MathStream & os) const
173 {
174         os << "<fenced open=\"" << left_ << "\" close=\""
175                 << right_ << "\">" << cell(0) << "</fenced>";
176 }
177
178
179 void InsetMathDelim::octave(OctaveStream & os) const
180 {
181         if (isAbs())
182                 os << "det(" << cell(0) << ')';
183         else
184                 os << left_ << cell(0) << right_;
185 }
186
187
188 } // namespace lyx