]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathDelim.cpp
Store ASCII data in std::string
[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 using namespace std;
29
30 namespace lyx {
31
32 static docstring convertDelimToLatexName(docstring const & name)
33 {
34         if (name.size() == 1) {
35                 char_type const c = name[0];
36                 if (c == '<' || c == '(' || c == '[' || c == '.'
37                     || c == '>' || c == ')' || c == ']' || c == '/' || c == '|')
38                         return name;
39         }
40         return '\\' + name + ' ';
41 }
42
43
44 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l,
45                 docstring const & r)
46         : InsetMathNest(buf, 1), left_(l), right_(r)
47 {}
48
49
50 InsetMathDelim::InsetMathDelim(Buffer * buf, docstring const & l, docstring const & r,
51         MathData const & ar)
52         : InsetMathNest(buf, 1), left_(l), right_(r)
53 {
54         cell(0) = ar;
55 }
56
57
58 Inset * InsetMathDelim::clone() const
59 {
60         return new InsetMathDelim(*this);
61 }
62
63
64 void InsetMathDelim::validate(LaTeXFeatures & features) const
65 {
66         InsetMathNest::validate(features);
67         // The delimiters may be used without \left or \right as well.
68         // Therefore they are listed in lib/symbols, and if they have
69         // requirements, we need to add them here.
70         MathWordList const & words = mathedWordList();
71         MathWordList::const_iterator it = words.find(left_);
72         if (it != words.end())
73         {
74                 string const req = it->second.requires;
75                 if (!req.empty())
76                         features.require(req);
77         }
78         it = words.find(right_);
79         if (it != words.end())
80         {
81                 string const req = it->second.requires;
82                 if (!req.empty())
83                         features.require(req);
84         }
85 }
86
87
88 void InsetMathDelim::write(WriteStream & os) const
89 {
90         MathEnsurer ensurer(os);
91         os << "\\left" << convertDelimToLatexName(left_) << cell(0)
92            << "\\right" << convertDelimToLatexName(right_);
93 }
94
95
96 void InsetMathDelim::normalize(NormalStream & os) const
97 {
98         os << "[delim " << convertDelimToLatexName(left_) << ' '
99            << convertDelimToLatexName(right_) << ' ' << cell(0) << ']';
100 }
101
102
103 void InsetMathDelim::metrics(MetricsInfo & mi, Dimension & dim) const
104 {
105         Dimension dim0;
106         cell(0).metrics(mi, dim0);
107         Dimension t = theFontMetrics(mi.base.font).dimension('I');
108         int h0 = (t.asc + t.des) / 2;
109         int a0 = max(dim0.asc, t.asc)   - h0;
110         int d0 = max(dim0.des, t.des)  + h0;
111         dw_ = dim0.height() / 5;
112         if (dw_ > 8)
113                 dw_ = 8;
114         if (dw_ < 4)
115                 dw_ = 4;
116         dim.wid = dim0.width() + 2 * dw_ + 8;
117         dim.asc = max(a0, d0) + h0;
118         dim.des = max(a0, d0) - h0;
119 }
120
121
122 void InsetMathDelim::draw(PainterInfo & pi, int x, int y) const
123 {
124         Dimension const dim = dimension(*pi.base.bv);
125         int const b = y - dim.asc;
126         cell(0).draw(pi, x + dw_ + 4, y);
127         mathed_draw_deco(pi, x + 4, b, dw_, dim.height(), left_);
128         mathed_draw_deco(pi, x + dim.width() - dw_ - 4,
129                 b, dw_, dim.height(), right_);
130         setPosCache(pi, x, y);
131 }
132
133
134 bool InsetMathDelim::isParenthesis() const
135 {
136         return left_ == "(" && right_ == ")";
137 }
138
139
140 bool InsetMathDelim::isBrackets() const
141 {
142         return left_ == "[" && right_ == "]";
143 }
144
145
146 bool InsetMathDelim::isAbs() const
147 {
148         return left_ == "|" && right_ == "|";
149 }
150
151
152 void InsetMathDelim::maple(MapleStream & os) const
153 {
154         if (isAbs()) {
155                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
156                         os << "linalg[det](" << cell(0) << ')';
157                 else
158                         os << "abs(" << cell(0) << ')';
159         }
160         else
161                 os << left_ << cell(0) << right_;
162 }
163
164
165 void InsetMathDelim::maxima(MaximaStream & os) const
166 {
167         if (isAbs()) {
168                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
169                         os << "determinant(" << cell(0) << ')';
170                 else
171                         os << "abs(" << cell(0) << ')';
172         }
173         else
174                 os << left_ << cell(0) << right_;
175 }
176
177
178 void InsetMathDelim::mathematica(MathematicaStream & os) const
179 {
180         if (isAbs()) {
181                 if (cell(0).size() == 1 && cell(0).front()->asMatrixInset())
182                         os << "Det" << cell(0) << ']';
183                 else
184                         os << "Abs[" << cell(0) << ']';
185         }
186         else
187                 os << left_ << cell(0) << right_;
188 }
189
190
191 void InsetMathDelim::mathmlize(MathStream & os) const
192 {
193         os << "<mo form='prefix' fence='true' stretchy='true' symmetric='true'>" << left_ << "</mo>"
194                 << cell(0) << "<mo form='postfix' fence='true' stretchy='true' symmetric='true'>" << right_ << "</mo>";
195 }
196
197
198 void InsetMathDelim::htmlize(HtmlStream & os) const
199 {
200         os << left_ << cell(0) << right_;
201 }
202
203
204 void InsetMathDelim::octave(OctaveStream & os) const
205 {
206         if (isAbs())
207                 os << "det(" << cell(0) << ')';
208         else
209                 os << left_ << cell(0) << right_;
210 }
211
212
213 } // namespace lyx