]> git.lyx.org Git - features.git/blob - src/mathed/array.C
LyX Drinkers United: patch 2
[features.git] / src / mathed / array.C
1
2 #include <config.h>
3
4 #ifdef __GNUG__
5 #pragma implementation
6 #endif
7
8 #include "debug.h"
9 #include "array.h"
10 #include "math_inset.h"
11 #include "math_scriptinset.h"
12 #include "math_parser.h"
13 #include "mathed/support.h"
14
15 using namespace std;
16
17
18 MathArray::MathArray()
19 {}
20
21
22 MathArray::~MathArray()
23 {
24         for (int pos = 0; pos < size(); next(pos)) 
25                 if (MathIsInset(pos)) 
26                         delete GetInset(pos);
27 }
28
29
30 MathArray::MathArray(MathArray const & array)
31         : bf_(array.bf_)
32 {
33         for (int pos = 0; pos < size(); next(pos)) 
34                 if (isInset(pos)) 
35                         replace(pos, GetInset(pos)->Clone());
36 }
37
38
39 bool MathArray::next(int & pos) const
40 {
41         if (pos >= size() - 1)
42                 return false;
43
44         pos += item_size(pos);
45         return true;
46 }
47
48
49 bool MathArray::prev(int & pos) const
50 {
51         if (pos == 0)
52                 return false;
53
54         pos -= item_size(pos - 1);
55         return true;
56 }
57
58
59 bool MathArray::last(int & pos) const
60 {
61         pos = bf_.size();
62         return prev(pos);
63 }
64
65
66 int MathArray::item_size(int pos) const
67 {
68         return 2 + (isInset(pos) ? sizeof(MathInset*) : 1);
69 }
70                 
71
72
73 void MathArray::substitute(MathMacro const & m)
74 {
75         MathArray tmp;
76         for (int pos = 0; pos < size(); next(pos)) {
77                 if (isInset(pos)) 
78                         GetInset(pos)->substitute(tmp, m);
79                 else 
80                         tmp.push_back(GetChar(pos), GetCode(pos));
81         }
82         swap(tmp);
83 }
84
85
86 MathArray & MathArray::operator=(MathArray const & array)
87 {
88         MathArray tmp(array);
89         swap(tmp);
90         return *this;
91 }
92
93
94 MathInset * MathArray::GetInset(int pos) const
95 {
96         if (!isInset(pos))
97                 return 0;
98         MathInset * p;
99         memcpy(&p, bf_.begin() + pos + 1, sizeof(p));
100         return p;
101 }
102
103 byte MathArray::GetChar(int pos) const
104 {
105         return pos < size() ? bf_[pos + 1] : '\0';
106 }
107
108 MathTextCodes MathArray::GetCode(int pos) const
109 {
110         return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
111 }
112
113 void MathArray::insert(int pos, MathInset * p)
114 {
115         bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
116         memcpy(&bf_[pos + 1], &p, sizeof(p));
117 }
118
119
120 void MathArray::replace(int pos, MathInset * p)
121 {
122         memcpy(&bf_[pos + 1], &p, sizeof(p));
123 }
124
125 void MathArray::insert(int pos, byte b, MathTextCodes t)
126 {
127         bf_.insert(bf_.begin() + pos, 3, t);
128         bf_[pos + 1] = b;
129 }
130
131
132 void MathArray::insert(int pos, MathArray const & array)
133 {
134 #ifdef WITH_WARNINGS
135 #warning quick and really dirty: make sure that we really own our insets
136 #endif
137         MathArray a = array;
138         bf_.insert(bf_.begin() + pos, a.bf_.begin(), a.bf_.end());
139 }
140
141
142 void MathArray::push_back(MathInset * p)
143 {       
144         insert(size(), p);
145 }
146
147 void MathArray::push_back(byte b, MathTextCodes c)
148 {
149         insert(size(), b, c);
150 }
151
152 void MathArray::push_back(MathArray const & array)
153 {
154         insert(size(), array);
155 }
156
157
158
159 void MathArray::clear()
160 {
161         bf_.clear();
162 }
163
164
165 void MathArray::swap(MathArray & array)
166 {
167         if (this != &array) 
168                 bf_.swap(array.bf_);
169 }
170
171
172 bool MathArray::empty() const
173 {
174         return bf_.empty();
175 }
176    
177
178 int MathArray::size() const
179 {
180         return bf_.size();
181 }
182
183
184 void MathArray::erase(int pos)
185 {
186         if (pos < static_cast<int>(bf_.size()))
187                 erase(pos, pos + item_size(pos));
188 }
189
190
191 void MathArray::erase(int pos1, int pos2)
192 {
193         bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
194 }
195
196
197 bool MathArray::isInset(int pos) const
198 {
199         if (pos >= size())
200                 return false;
201         return MathIsInset(bf_[pos]);
202 }
203
204
205 MathInset * MathArray::back_inset() const
206 {
207         if (!empty()) {
208                 int pos = size();
209                 prev(pos);
210                 if (isInset(pos))
211                         return GetInset(pos);
212         }
213         return 0;
214 }
215
216
217 MathScriptInset * MathArray::prevScriptInset(int pos) const
218 {
219         if (!pos)
220                 return 0;
221         prev(pos);
222
223         MathInset * inset = GetInset(pos);
224         if (inset && inset->isScriptInset()) 
225                 return static_cast<MathScriptInset *>(inset);
226
227         return 0;
228 }
229
230 MathScriptInset * MathArray::nextScriptInset(int pos) const
231 {
232         MathInset * inset = GetInset(pos);
233         if (inset && inset->isScriptInset()) 
234                 return static_cast<MathScriptInset *>(inset);
235
236         return 0;
237 }
238
239
240 void MathArray::dump2(ostream & os) const
241 {
242         for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
243                 os << int(*it) << ' ';
244         os << endl;
245 }
246
247
248
249 void MathArray::dump(ostream & os) const
250 {
251         for (int pos = 0; pos < size(); next(pos)) {
252                 if (isInset(pos)) 
253                         os << "<inset: " << GetInset(pos) << ">";
254                 else 
255                         os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
256         }
257 }
258
259
260 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
261 {
262         ar.dump2(os);
263         return os;
264 }
265
266
267 void MathArray::Write(ostream & os, bool fragile) const
268 {
269         if (empty())
270                 return;
271
272         int brace = 0;
273         
274         for (int pos = 0; pos < size(); next(pos)) {
275                 if (isInset(pos)) {
276
277                         GetInset(pos)->Write(os, fragile);
278
279                 } else {
280
281                         MathTextCodes fcode = GetCode(pos);
282                         byte c = GetChar(pos);
283
284                         if (MathIsSymbol(fcode)) {
285                                 latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
286
287                                 if (l == 0) {
288                                         l = lm_get_key_by_id(c, LM_TK_BIGSYM);
289                                 }
290
291                                 if (l) {
292                                         os << '\\' << l->name << ' ';
293                                 } else {
294                                         lyxerr << "Could not find the LaTeX name for  " << c << " and fcode " << fcode << "!" << std::endl;
295                                 }
296                         } else {
297                                 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM) 
298                                         os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
299
300                                 // Is there a standard logical XOR?
301                                 if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
302                                                 (fcode == LM_TC_SPECIAL))
303                                         os << '\\';
304                                 else {
305                                         if (c == '{')
306                                                 ++brace;
307                                         if (c == '}')
308                                                 --brace;
309                                 }
310                                 if (c == '}' && fcode == LM_TC_TEX && brace < 0) 
311                                         lyxerr <<"Math warning: Unexpected closing brace.\n";
312                                 else           
313                                         os << c;
314                         }
315
316                         if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
317                                 os << '}';
318                         
319                 }
320         }
321
322         if (brace > 0)
323                 os << string(brace, '}');
324 }
325
326
327 void MathArray::WriteNormal(ostream & os) const
328 {
329         if (empty()) {
330                 os << "[par] ";
331                 return;
332         }
333
334         Write(os, true);
335 }
336