]> git.lyx.org Git - lyx.git/blob - src/mathed/array.C
mathed95.diff
[lyx.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 std::ostream;
16 using std::endl;
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_[0] + 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 string MathArray::GetString(int & pos) const
109 {
110         string s;
111         if (isInset(pos))
112                 return s;
113
114         MathTextCodes const fcode = GetCode(pos);
115         do {
116                 s += GetChar(pos);
117                 next(pos);
118         } while (pos < size() && !isInset(pos) && GetCode(pos) == fcode);
119
120         return s;
121 }
122
123 MathTextCodes MathArray::GetCode(int pos) const
124 {
125         return pos < size() ? MathTextCodes(bf_[pos]) : LM_TC_MIN;
126 }
127
128 void MathArray::setCode(int pos, MathTextCodes t)
129 {
130         if (pos > size() || isInset(pos))
131                 return;
132         bf_[pos] = t;
133         bf_[pos + 2] = t;
134 }
135
136 void MathArray::insert(int pos, MathInset * p)
137 {
138         bf_.insert(bf_.begin() + pos, 2 + sizeof(p), LM_TC_INSET);
139         memcpy(&bf_[pos + 1], &p, sizeof(p));
140 }
141
142
143 void MathArray::replace(int pos, MathInset * p)
144 {
145         memcpy(&bf_[pos + 1], &p, sizeof(p));
146 }
147
148 void MathArray::insert(int pos, byte b, MathTextCodes t)
149 {
150         bf_.insert(bf_.begin() + pos, 3, t);
151         bf_[pos + 1] = b;
152 }
153
154
155 void MathArray::insert(int pos, MathArray const & array)
156 {
157         bf_.insert(bf_.begin() + pos, array.bf_.begin(), array.bf_.end());
158         for (int p = pos; p < pos + array.size(); next(p)) 
159                 if (isInset(p)) 
160                         replace(p, GetInset(p)->clone());
161 }
162
163
164 void MathArray::push_back(MathInset * p)
165 {       
166         insert(size(), p);
167 }
168
169 void MathArray::push_back(byte b, MathTextCodes c)
170 {
171         insert(size(), b, c);
172 }
173
174 void MathArray::push_back(MathArray const & array)
175 {
176         insert(size(), array);
177 }
178
179
180
181 void MathArray::clear()
182 {
183         bf_.clear();
184 }
185
186
187 void MathArray::swap(MathArray & array)
188 {
189         if (this != &array) 
190                 bf_.swap(array.bf_);
191 }
192
193
194 bool MathArray::empty() const
195 {
196         return bf_.empty();
197 }
198    
199
200 int MathArray::size() const
201 {
202         return bf_.size();
203 }
204
205
206 void MathArray::erase(int pos)
207 {
208         if (pos < static_cast<int>(bf_.size()))
209                 erase(pos, pos + item_size(pos));
210 }
211
212
213 void MathArray::erase(int pos1, int pos2)
214 {
215         bf_.erase(bf_.begin() + pos1, bf_.begin() + pos2);
216 }
217
218
219 bool MathArray::isInset(int pos) const
220 {
221         if (pos >= size())
222                 return false;
223         return MathIsInset(bf_[pos]);
224 }
225
226
227 MathInset * MathArray::back_inset() const
228 {
229         if (!empty()) {
230                 int pos = size();
231                 prev(pos);
232                 if (isInset(pos))
233                         return GetInset(pos);
234         }
235         return 0;
236 }
237
238
239 MathScriptInset * MathArray::prevScriptInset(int pos) const
240 {
241         if (!pos)
242                 return 0;
243         prev(pos);
244
245         MathInset * inset = GetInset(pos);
246         if (inset && inset->isScriptInset()) 
247                 return static_cast<MathScriptInset *>(inset);
248
249         return 0;
250 }
251
252 MathScriptInset * MathArray::nextScriptInset(int pos) const
253 {
254         MathInset * inset = GetInset(pos);
255         if (inset && inset->isScriptInset()) 
256                 return static_cast<MathScriptInset *>(inset);
257
258         return 0;
259 }
260
261
262 void MathArray::dump2(ostream & os) const
263 {
264         for (buffer_type::const_iterator it = bf_.begin(); it != bf_.end(); ++it)
265                 os << int(*it) << ' ';
266         os << endl;
267 }
268
269
270
271 void MathArray::dump(ostream & os) const
272 {
273         for (int pos = 0; pos < size(); next(pos)) {
274                 if (isInset(pos)) 
275                         os << "<inset: " << GetInset(pos) << ">";
276                 else 
277                         os << "<" << int(bf_[pos]) << " " << int(bf_[pos+1]) << ">";
278         }
279 }
280
281
282 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
283 {
284         ar.dump2(os);
285         return os;
286 }
287
288
289 void MathArray::Write(ostream & os, bool fragile) const
290 {
291         if (empty())
292                 return;
293
294         int brace = 0;
295         
296         for (int pos = 0; pos < size(); next(pos)) {
297                 if (isInset(pos)) {
298
299                         GetInset(pos)->Write(os, fragile);
300
301                 } else {
302
303                         MathTextCodes fcode = GetCode(pos);
304                         byte c = GetChar(pos);
305
306                         if (MathIsSymbol(fcode)) {
307                                 latexkeys const * l = lm_get_key_by_id(c, LM_TK_SYM);
308
309                                 if (l == 0) {
310                                         l = lm_get_key_by_id(c, LM_TK_BIGSYM);
311                                 }
312
313                                 if (l) {
314                                         os << '\\' << l->name << ' ';
315                                 } else {
316                                         lyxerr << "Could not find the LaTeX name for  " << c << " and fcode " << fcode << "!" << std::endl;
317                                 }
318                         } else {
319                                 if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM) 
320                                         os << '\\' << math_font_name[fcode - LM_TC_RM] << '{';
321
322                                 // Is there a standard logical XOR?
323                                 if ((fcode == LM_TC_TEX && c != '{' && c != '}') ||
324                                                 (fcode == LM_TC_SPECIAL))
325                                         os << '\\';
326                                 else {
327                                         if (c == '{')
328                                                 ++brace;
329                                         if (c == '}')
330                                                 --brace;
331                                 }
332                                 if (c == '}' && fcode == LM_TC_TEX && brace < 0) 
333                                         lyxerr <<"Math warning: Unexpected closing brace.\n";
334                                 else           
335                                         os << c;
336                         }
337
338                         if (fcode >= LM_TC_RM && fcode <= LM_TC_TEXTRM)
339                                 os << '}';
340                         
341                 }
342         }
343
344         if (brace > 0)
345                 os << string(brace, '}');
346 }
347
348
349 void MathArray::WriteNormal(ostream & os) const
350 {
351         if (empty()) {
352                 os << "[par] ";
353                 return;
354         }
355
356         Write(os, true);
357 }
358