]> git.lyx.org Git - lyx.git/blob - src/mathed/math_deliminset.C
5882a0cd0528f135211911a929115ce9a5db1ae9
[lyx.git] / src / mathed / math_deliminset.C
1 #include <config.h>
2
3 #ifdef __GNUG__
4 #pragma implementation
5 #endif
6
7 #include "math_deliminset.h"
8 #include "math_parser.h"
9 #include "math_support.h"
10 #include "math_mathmlstream.h"
11 #include "math_streamstr.h"
12 #include "math_extern.h"
13
14
15 using std::max;
16
17 namespace {
18
19 string convertDelimToLatexName(string const & name)
20 {
21         if (name == "(")
22                 return name;
23         if (name == "[")
24                 return name;
25         if (name == ".")
26                 return name;
27         if (name == ")")
28                 return name;
29         if (name == "]")
30                 return name;
31         if (name == "/")
32                 return name;
33         if (name == "|")
34                 return name;
35         return "\\" + name + " ";
36 }
37
38 }
39
40
41
42 MathDelimInset::MathDelimInset(string const & l, string const & r)
43         : MathNestInset(1), left_(l), right_(r)
44 {}
45
46
47 MathDelimInset::MathDelimInset
48                 (string const & l, string const & r, MathArray const & ar)
49         : MathNestInset(1), left_(l), right_(r)
50 {
51         cell(0) = ar;
52 }
53
54
55 MathInset * MathDelimInset::clone() const
56 {
57         return new MathDelimInset(*this);
58 }
59
60
61 void MathDelimInset::write(WriteStream & os) const
62 {
63         os << "\\left" << convertDelimToLatexName(left_) << cell(0)
64            << "\\right" << convertDelimToLatexName(right_);
65 }
66
67
68 void MathDelimInset::normalize(NormalStream & os) const
69 {
70         os << "[delim " << convertDelimToLatexName(left_) << ' '
71            << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
72 }
73
74
75 int MathDelimInset::dw() const
76 {
77         int w = height() / 5;
78         if (w > 8)
79                 w = 8;
80         if (w < 4)
81                 w = 4;
82         return w;
83 }
84
85
86 void MathDelimInset::metrics(MathMetricsInfo & mi) const
87 {
88         xcell(0).metrics(mi);
89         int a, d, w;
90         mathed_char_dim(mi.base.font, 'I', a, d, w);
91         int h0   = (a + d) / 2;
92         int a0   = max(xcell(0).ascent(), a)   - h0;
93         int d0   = max(xcell(0).descent(), d)  + h0;
94         ascent_  = max(a0, d0) + h0;
95         descent_ = max(a0, d0) - h0;
96         width_   = xcell(0).width() + 2 * dw() + 8;
97 }
98
99
100 void MathDelimInset::draw(MathPainterInfo & pi, int x, int y) const
101 {
102         int const w = dw();
103         int const b = y - ascent_;
104         xcell(0).draw(pi, x + w + 4, y);
105         mathed_draw_deco(pi, x + 4, b, w, height(), left_);
106         mathed_draw_deco(pi, x + width() - w - 4, b, w, height(), right_);
107 }
108
109
110 bool MathDelimInset::isParanthesis() const
111 {
112         return left_ == "(" && right_ == ")";
113 }
114
115
116 bool MathDelimInset::isBrackets() const
117 {
118         return left_ == "[" && right_ == "]";
119 }
120
121
122 bool MathDelimInset::isAbs() const
123 {
124         return left_ == "|" && right_ == "|";
125 }
126
127
128 void MathDelimInset::maplize(MapleStream & os) const
129 {
130         if (isAbs()) {
131                 bool mat =
132                         cell(0).size() == 1 && cell(0).begin()->nucleus()
133                                         && cell(0).begin()->nucleus()->asMatrixInset();
134                 if (mat)
135                         os << "linalg[det](" << cell(0) << ")";
136                 else
137                         os << "abs(" << cell(0) << ")";
138         }
139         else
140                 os << left_ << cell(0) << right_;
141 }
142
143
144 void MathDelimInset::mathmlize(MathMLStream & os) const
145 {
146         os << "<fenced open=\"" << left_ << "\" close=\""
147                 << right_ << "\">" << cell(0) << "</fenced>";
148 }
149
150
151 void MathDelimInset::octavize(OctaveStream & os) const
152 {
153         if (isAbs())
154                 os << "det(" << cell(0) << ")";
155         else
156                 os << left_ << cell(0) << right_;
157 }