]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.C
cosmetics: rename (x)array.[Ch] into math_(x)data.[Ch]
[lyx.git] / src / mathed / math_data.C
1 #ifdef __GNUG__
2 #pragma implementation
3 #endif
4
5 #include "math_inset.h"
6 #include "math_charinset.h"
7 #include "math_scriptinset.h"
8 #include "math_stringinset.h"
9 #include "math_mathmlstream.h"
10 #include "math_support.h"
11 #include "math_data.h"
12 #include "debug.h"
13 #include "support/LAssert.h"
14
15
16 MathArray::MathArray()
17 {}
18
19
20 MathArray::MathArray(MathArray const & ar, size_type from, size_type to)
21         : bf_(ar.begin() + from, ar.begin() + to)
22 {}
23
24
25 void MathArray::substitute(MathMacro const & m)
26 {
27         for (iterator it = begin(); it != end(); ++it)
28                 it->nucleus()->substitute(m);
29 }
30
31
32 MathScriptInset const * MathArray::asScript(const_iterator it) const
33 {
34         if (it->nucleus()->asScriptInset())
35                 return 0;
36         const_iterator jt = it + 1;
37         if (jt == end())
38                 return 0;
39         if (!jt->nucleus())
40                 return 0;
41         return jt->nucleus()->asScriptInset();
42 }
43
44
45 MathAtom & MathArray::at(size_type pos)
46 {
47         lyx::Assert(pos < size());
48         return bf_[pos];
49 }
50
51
52 MathAtom const & MathArray::at(size_type pos) const
53 {
54         lyx::Assert(pos < size());
55         return bf_[pos];
56 }
57
58
59 void MathArray::insert(size_type pos, MathAtom const & t)
60 {
61         bf_.insert(begin() + pos, t);
62 }
63
64
65 void MathArray::insert(size_type pos, MathArray const & ar)
66 {
67         bf_.insert(begin() + pos, ar.begin(), ar.end());
68 }
69
70
71 void MathArray::push_back(MathAtom const & t)
72 {       
73         bf_.push_back(t);
74 }
75
76
77 void MathArray::push_back(MathArray const & ar)
78 {
79         insert(size(), ar);
80 }
81
82
83 void MathArray::clear()
84 {
85         erase();
86 }
87
88
89 void MathArray::swap(MathArray & ar)
90 {
91         if (this != &ar) 
92                 bf_.swap(ar.bf_);
93 }
94
95
96 bool MathArray::empty() const
97 {
98         return bf_.empty();
99 }
100
101
102 MathArray::size_type MathArray::size() const
103 {
104         return bf_.size();
105 }
106
107
108 void MathArray::erase()
109 {
110         erase(0, size());
111 }
112
113
114 void MathArray::erase(size_type pos)
115 {
116         if (pos < size())
117                 erase(pos, pos + 1);
118 }
119
120
121 void MathArray::erase(size_type pos1, size_type pos2)
122 {
123         bf_.erase(begin() + pos1, begin() + pos2);
124 }
125
126
127 MathAtom & MathArray::back()
128 {
129         return bf_.back();
130 }
131
132
133 void MathArray::dump2() const
134 {
135         NormalStream ns(lyxerr);
136         for (const_iterator it = begin(); it != end(); ++it)
137                 ns << it->nucleus() << ' ';
138 }
139
140
141 void MathArray::dump() const
142 {
143         NormalStream ns(lyxerr);
144         for (const_iterator it = begin(); it != end(); ++it)
145                 ns << "<" << it->nucleus() << ">";
146 }
147
148
149 // returns sequence of char with same code starting at it up to end
150 // it might be less, though...
151 string charSequence(MathArray::const_iterator it, MathArray::const_iterator end)
152 {
153         string s;
154         MathCharInset const * p = it->nucleus()->asCharInset();
155         if (!p)
156                 return s;
157
158         for (MathTextCodes c = p->code(); it != end; ++it) {
159                 if (!it->nucleus())
160                         break;
161                 p = it->nucleus()->asCharInset();
162                 if (!p || p->code() != c)
163                         break;
164                 s += p->getChar();
165         }
166         return s;
167 }
168
169
170 MathArray MathArray::glueChars() const
171 {
172         MathArray ar;
173         const_iterator it = begin();
174         while (it != end()) {
175                 if (it->nucleus() && it->nucleus()->asCharInset()) {
176                         string s = charSequence(it, end());
177                         MathTextCodes c = it->nucleus()->asCharInset()->code();
178                         ar.push_back(MathAtom(new MathStringInset(s, c)));
179                         it += s.size();
180                 } else {
181                         ar.push_back(*it);
182                         ++it;
183                 }
184         }
185         return ar;
186 }
187
188
189 bool needAsterisk(MathAtom const &, MathAtom const &)
190 {
191         return false;
192 }
193
194
195 MathArray MathArray::guessAsterisks() const
196 {
197         if (size() <= 1)
198                 return *this;
199         MathArray ar;
200         ar.push_back(*begin());
201         for (const_iterator it = begin(), jt = begin()+1 ; jt != end(); ++it, ++jt) {
202                 if (needAsterisk(*it, *jt))
203                         ar.push_back(MathAtom(new MathCharInset('*')));
204                 ar.push_back(*it);
205         }
206         ar.push_back(*end());
207         return ar;
208 }
209
210
211 void MathArray::write(MathWriteInfo & wi) const
212 {
213         MathArray ar = glueChars();
214         for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
215                 MathInset const * p = it->nucleus();
216                 if (MathScriptInset const * q = ar.asScript(it)) {
217                         q->write(p, wi);
218                         ++it;
219                 } else {
220                         p->write(wi);
221                 }
222         }
223 }
224
225
226 void MathArray::writeNormal(NormalStream & os) const
227 {
228         MathArray ar = glueChars();
229         for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
230                 MathInset const * p = it->nucleus();
231                 if (MathScriptInset const * q = ar.asScript(it)) {
232                         q->writeNormal(p, os);
233                         ++it;
234                 } else 
235                         p->writeNormal(os);
236         }
237 }
238
239
240 void MathArray::octavize(OctaveStream & os) const
241 {
242         MathArray ar = glueChars();
243         for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
244                 MathInset const * p = it->nucleus();
245                 if (MathScriptInset const * q = ar.asScript(it)) {
246                         q->octavize(p, os);
247                         ++it;
248                 } else 
249                         p->octavize(os);
250         }
251 }
252
253
254 void MathArray::maplize(MapleStream & os) const
255 {
256         MathArray ar = glueChars();
257         for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
258                 MathInset const * p = it->nucleus();
259                 if (MathScriptInset const * q = ar.asScript(it)) {
260                         q->maplize(p, os);
261                         ++it;
262                 } else 
263                         p->maplize(os);
264         }
265 }
266
267
268 void MathArray::mathmlize(MathMLStream & os) const
269 {
270         MathArray ar = glueChars();
271         if (ar.size() == 0)
272                 os << "<mrow/>";
273         else if (ar.size() == 1)
274                 os << ar.begin()->nucleus();
275         else {
276                 os << MTag("mrow");
277                 for (const_iterator it = ar.begin(); it != ar.end(); ++it) {
278                         MathInset const * p = it->nucleus();
279                         if (MathScriptInset const * q = ar.asScript(it)) {
280                                 q->mathmlize(p, os);
281                                 ++it;
282                         } else 
283                                 p->mathmlize(os);
284                 }
285                 os << ETag("mrow");
286         }
287 }
288
289
290 void MathArray::validate(LaTeXFeatures & features) const
291 {
292         for (const_iterator it = begin(); it != end(); ++it)
293                 if (it->nucleus())
294                         it->nucleus()->validate(features);
295 }
296
297
298 void MathArray::pop_back()
299 {       
300         if (!size()) {
301                 lyxerr << "pop_back from empty array!\n";
302                 return;
303         }
304         bf_.pop_back();
305 }
306
307
308 MathArray::const_iterator MathArray::begin() const
309 {
310         return bf_.begin();
311 }
312
313
314 MathArray::const_iterator MathArray::end() const
315 {
316         return bf_.end();
317 }
318
319
320 MathArray::iterator MathArray::begin()
321 {
322         return bf_.begin();
323 }
324
325
326 MathArray::iterator MathArray::end()
327 {
328         return bf_.end();
329 }
330
331
332 bool MathArray::isMatrix() const
333 {
334         return size() == 1 && begin()->nucleus() && begin()->nucleus()->isMatrix();
335 }
336
337