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