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