]> git.lyx.org Git - lyx.git/blob - src/mathed/math_deliminset.C
architectural changes to tex2lyx
[lyx.git] / src / mathed / math_deliminset.C
1 #include <config.h>
2
3 #include "math_deliminset.h"
4 #include "math_parser.h"
5 #include "math_support.h"
6 #include "math_mathmlstream.h"
7 #include "math_streamstr.h"
8 #include "math_extern.h"
9
10
11 using std::max;
12 using std::auto_ptr;
13
14 namespace {
15
16 string convertDelimToLatexName(string const & name)
17 {
18         if (name == "<")
19                 return name;
20         if (name == "(")
21                 return name;
22         if (name == "[")
23                 return name;
24         if (name == ".")
25                 return name;
26         if (name == ">")
27                 return name;
28         if (name == ")")
29                 return name;
30         if (name == "]")
31                 return name;
32         if (name == "/")
33                 return name;
34         if (name == "|")
35                 return name;
36         return '\\' + name + ' ';
37 }
38
39 }
40
41
42
43 MathDelimInset::MathDelimInset(string const & l, string const & r)
44         : MathNestInset(1), left_(l), right_(r)
45 {}
46
47
48 MathDelimInset::MathDelimInset
49                 (string const & l, string const & r, MathArray const & ar)
50         : MathNestInset(1), left_(l), right_(r)
51 {
52         cell(0) = ar;
53 }
54
55
56 auto_ptr<InsetBase> MathDelimInset::clone() const
57 {
58         return auto_ptr<InsetBase>(new MathDelimInset(*this));
59 }
60
61
62 void MathDelimInset::write(WriteStream & os) const
63 {
64         os << "\\left" << convertDelimToLatexName(left_) << cell(0)
65            << "\\right" << convertDelimToLatexName(right_);
66 }
67
68
69 void MathDelimInset::normalize(NormalStream & os) const
70 {
71         os << "[delim " << convertDelimToLatexName(left_) << ' '
72            << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
73 }
74
75
76 void MathDelimInset::metrics(MetricsInfo & mi, Dimension & dim) const
77 {
78         cell(0).metrics(mi);
79         Dimension t;
80         mathed_char_dim(mi.base.font, 'I', t);
81         int h0 = (t.asc + t.des) / 2;
82         int a0 = max(cell(0).ascent(), t.asc)   - h0;
83         int d0 = max(cell(0).descent(), t.des)  + h0;
84         dw_ = cell(0).height() / 5;
85         if (dw_ > 8)
86                 dw_ = 8;
87         if (dw_ < 4)
88                 dw_ = 4;
89         dim_.wid = cell(0).width() + 2 * dw_ + 8;
90         dim_.asc = max(a0, d0) + h0;
91         dim_.des = max(a0, d0) - h0;
92         dim = dim_;
93 }
94
95
96 void MathDelimInset::draw(PainterInfo & pi, int x, int y) const
97 {
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 }
104
105
106 bool MathDelimInset::isParanthesis() const
107 {
108         return left_ == "(" && right_ == ")";
109 }
110
111
112 bool MathDelimInset::isBrackets() const
113 {
114         return left_ == "[" && right_ == "]";
115 }
116
117
118 bool MathDelimInset::isAbs() const
119 {
120         return left_ == "|" && right_ == "|";
121 }
122
123
124 void MathDelimInset::maple(MapleStream & os) const
125 {
126         if (isAbs()) {
127                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
128                         os << "linalg[det](" << cell(0) << ')';
129                 else
130                         os << "abs(" << cell(0) << ')';
131         }
132         else
133                 os << left_ << cell(0) << right_;
134 }
135
136 void MathDelimInset::maxima(MaximaStream & os) const
137 {
138         if (isAbs()) {
139                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
140                         os << "determinant(" << cell(0) << ')';
141                 else
142                         os << "abs(" << cell(0) << ')';
143         }
144         else
145                 os << left_ << cell(0) << right_;
146 }
147
148
149 void MathDelimInset::mathematica(MathematicaStream & os) const
150 {
151         if (isAbs()) {
152                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
153                         os << "Det" << cell(0) << ']';
154                 else
155                         os << "Abs[" << cell(0) << ']';
156         }
157         else
158                 os << left_ << cell(0) << right_;
159 }
160
161
162 void MathDelimInset::mathmlize(MathMLStream & os) const
163 {
164         os << "<fenced open=\"" << left_ << "\" close=\""
165                 << right_ << "\">" << cell(0) << "</fenced>";
166 }
167
168
169 void MathDelimInset::octave(OctaveStream & os) const
170 {
171         if (isAbs())
172                 os << "det(" << cell(0) << ')';
173         else
174                 os << left_ << cell(0) << right_;
175 }