]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDelim.cpp
Fix #7549, due to a problem in exporting to plaintext a
[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 "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(Buffer * buf, docstring const & l,
42                 docstring const & r)
43         : InsetMathNest(buf, 1), left_(l), right_(r)
44 {}
45
46
47 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l, docstring const & r,
48         MathData const & ar)
49         : InsetMathNest(buf, 1), left_(l), right_(r)
50 {
51         cell(0) = ar;
52 }
53
54
55 Inset * InsetMathDelim::clone() const
56 {
57         return new InsetMathDelim(*this);
58 }
59
60
61 void InsetMathDelim::write(WriteStream & os) const
62 {
63         MathEnsurer ensurer(os);
64         os << "\\left" << convertDelimToLatexName(left_) << cell(0)
65            << "\\right" << convertDelimToLatexName(right_);
66 }
67
68
69 void InsetMathDelim::normalize(NormalStream & os) const
70 {
71         os << "[delim " << convertDelimToLatexName(left_) << ' '
72            << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
73 }
74
75
76 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
77 {
78         Dimension dim0;
79         cell(0).metrics(mi, dim0);
80         Dimension t = theFontMetrics(mi.base.font).dimension('I');
81         int h0 = (t.asc + t.des) / 2;
82         int a0 = max(dim0.asc, t.asc)   - h0;
83         int d0 = max(dim0.des, t.des)  + h0;
84         dw_ = dim0.height() / 5;
85         if (dw_ > 8)
86                 dw_ = 8;
87         if (dw_ < 4)
88                 dw_ = 4;
89         dim.wid = dim0.width() + 2 * dw_ + 8;
90         dim.asc = max(a0, d0) + h0;
91         dim.des = max(a0, d0) - h0;
92 }
93
94
95 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
96 {
97         Dimension const dim = dimension(*pi.base.bv);
98         int const b = y - dim.asc;
99         cell(0).draw(pi, x + dw_ + 4, y);
100         mathed_draw_deco(pi, x + 4, b, dw_, dim.height(), left_);
101         mathed_draw_deco(pi, x + dim.width() - dw_ - 4,
102                 b, dw_, dim.height(), right_);
103         setPosCache(pi, x, y);
104 }
105
106
107 bool InsetMathDelim::isParenthesis() const
108 {
109         return left_ == "(" && right_ == ")";
110 }
111
112
113 bool InsetMathDelim::isBrackets() const
114 {
115         return left_ == "[" && right_ == "]";
116 }
117
118
119 bool InsetMathDelim::isAbs() const
120 {
121         return left_ == "|" && right_ == "|";
122 }
123
124
125 void InsetMathDelim::maple(MapleStream & os) const
126 {
127         if (isAbs()) {
128                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
129                         os << "linalg[det](" << cell(0) << ')';
130                 else
131                         os << "abs(" << cell(0) << ')';
132         }
133         else
134                 os << left_ << cell(0) << right_;
135 }
136
137
138 void InsetMathDelim::maxima(MaximaStream & os) const
139 {
140         if (isAbs()) {
141                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
142                         os << "determinant(" << cell(0) << ')';
143                 else
144                         os << "abs(" << cell(0) << ')';
145         }
146         else
147                 os << left_ << cell(0) << right_;
148 }
149
150
151 void InsetMathDelim::mathematica(MathematicaStream & os) const
152 {
153         if (isAbs()) {
154                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
155                         os << "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::mathmlize(MathStream & os) const
165 {
166         os << "<mo form='prefix' fence='true' stretchy='true' symmetric='true'>" << left_ << "</mo>"
167                 << cell(0) << "<mo form='postfix' fence='true' stretchy='true' symmetric='true'>" << right_ << "</mo>";
168 }
169
170
171 void InsetMathDelim::htmlize(HtmlStream & os) const
172 {
173         os << left_ << cell(0) << right_;
174 }
175
176
177 void InsetMathDelim::octave(OctaveStream & os) const
178 {
179         if (isAbs())
180                 os << "det(" << cell(0) << ')';
181         else
182                 os << left_ << cell(0) << right_;
183 }
184
185
186 } // namespace lyx