]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.C
revert Buffer LyxText->InsetText commit
[lyx.git] / src / mathed / math_data.C
1 /**
2  * \file math_data.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "math_data.h"
14 #include "math_fontinset.h"
15 #include "math_scriptinset.h"
16 #include "math_mathmlstream.h"
17 #include "math_support.h"
18 #include "math_replace.h"
19 #include "debug.h"
20 #include "LColor.h"
21
22 #include "frontends/Painter.h"
23
24 #include <boost/assert.hpp>
25
26 using std::abs;
27 using std::endl;
28 using std::min;
29 using std::ostringstream;
30
31
32 MathArray::MathArray()
33         : xo_(0), yo_(0), clean_(false), drawn_(false)
34 {}
35
36
37 MathArray::MathArray(const_iterator from, const_iterator to)
38         : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
39 {}
40
41
42 void MathArray::substitute(MathMacro const & m)
43 {
44         for (iterator it = begin(); it != end(); ++it)
45                 it->nucleus()->substitute(m);
46 }
47
48
49 MathAtom & MathArray::operator[](pos_type pos)
50 {
51         BOOST_ASSERT(pos < size());
52         return base_type::operator[](pos);
53 }
54
55
56 MathAtom const & MathArray::operator[](pos_type pos) const
57 {
58         BOOST_ASSERT(pos < size());
59         return base_type::operator[](pos);
60 }
61
62
63 void MathArray::insert(size_type pos, MathAtom const & t)
64 {
65         base_type::insert(begin() + pos, t);
66 }
67
68
69 void MathArray::insert(size_type pos, MathArray const & ar)
70 {
71         BOOST_ASSERT(pos <= size());
72         base_type::insert(begin() + pos, ar.begin(), ar.end());
73 }
74
75
76 void MathArray::append(MathArray const & ar)
77 {
78         insert(size(), ar);
79 }
80
81
82 void MathArray::erase(size_type pos)
83 {
84         if (pos < size())
85                 erase(pos, pos + 1);
86 }
87
88
89 void MathArray::erase(iterator pos1, iterator pos2)
90 {
91         base_type::erase(pos1, pos2);
92 }
93
94
95 void MathArray::erase(iterator pos)
96 {
97         base_type::erase(pos);
98 }
99
100
101 void MathArray::erase(size_type pos1, size_type pos2)
102 {
103         base_type::erase(begin() + pos1, begin() + pos2);
104 }
105
106
107 void MathArray::dump2() const
108 {
109         NormalStream ns(lyxerr);
110         for (const_iterator it = begin(); it != end(); ++it)
111                 ns << *it << ' ';
112 }
113
114
115 void MathArray::dump() const
116 {
117         NormalStream ns(lyxerr);
118         for (const_iterator it = begin(); it != end(); ++it)
119                 ns << '<' << *it << '>';
120 }
121
122
123 void MathArray::validate(LaTeXFeatures & features) const
124 {
125         for (const_iterator it = begin(); it != end(); ++it)
126                 (*it)->validate(features);
127 }
128
129
130 bool MathArray::match(MathArray const & ar) const
131 {
132         return size() == ar.size() && matchpart(ar, 0);
133 }
134
135
136 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
137 {
138         if (size() < ar.size() + pos)
139                 return false;
140         const_iterator it = begin() + pos;
141         for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
142                 if (asString(*it) != asString(*jt))
143                         return false;
144         return true;
145 }
146
147
148 void MathArray::replace(ReplaceData & rep)
149 {
150         for (size_type i = 0; i < size(); ++i) {
151                 if (find1(rep.from, i)) {
152                         // match found
153                         lyxerr << "match found!" << endl;
154                         erase(i, i + rep.from.size());
155                         insert(i, rep.to);
156                 }
157         }
158
159 #ifdef WITH_WARNINGS
160 #warning temporarily disabled
161         // for (const_iterator it = begin(); it != end(); ++it)
162         //      it->nucleus()->replace(rep);
163 #endif
164 }
165
166
167 bool MathArray::find1(MathArray const & ar, size_type pos) const
168 {
169         lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
170         for (size_type i = 0, n = ar.size(); i < n; ++i)
171                 if (asString(operator[](pos + i)) != asString(ar[i]))
172                         return false;
173         return true;
174 }
175
176
177 MathArray::size_type MathArray::find(MathArray const & ar) const
178 {
179         for (int i = 0, last = size() - ar.size(); i < last; ++i)
180                 if (find1(ar, i))
181                         return i;
182         return size();
183 }
184
185
186 MathArray::size_type MathArray::find_last(MathArray const & ar) const
187 {
188         for (int i = size() - ar.size(); i >= 0; --i)
189                 if (find1(ar, i))
190                         return i;
191         return size();
192 }
193
194
195 bool MathArray::contains(MathArray const & ar) const
196 {
197         if (find(ar) != size())
198                 return true;
199         for (const_iterator it = begin(); it != end(); ++it)
200                 if ((*it)->contains(ar))
201                         return true;
202         return false;
203 }
204
205
206 void MathArray::touch() const
207 {
208         clean_  = false;
209         drawn_  = false;
210 }
211
212
213 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
214 {
215         metrics(mi);
216         dim = dim_;
217 }
218
219
220 void MathArray::metrics(MetricsInfo & mi) const
221 {
222         //if (clean_)
223         //      return;
224         clean_  = true;
225         drawn_  = false;
226
227         mathed_char_dim(mi.base.font, 'I', dim_);
228
229         if (!empty()) {
230                 dim_.wid = 0;
231                 Dimension d;
232                 for (const_iterator it = begin(), et = end(); it != et; ++it) {
233                         (*it)->metrics(mi, d);
234                         dim_ += d;
235                         //it->width_ = d.wid;
236                 }
237         }
238 }
239
240
241 void MathArray::draw(PainterInfo & pi, int x, int y) const
242 {
243         //if (drawn_ && x == xo_ && y == yo_)
244         //      return;
245         //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
246
247         xo_    = x;
248         yo_    = y;
249         drawn_ = true;
250
251         if (y + descent() <= 0)                   // don't draw above the workarea
252                 return;
253         if (y - ascent() >= pi.pain.paperHeight())   // don't draw below the workarea
254                 return;
255         if (x + width() <= 0)                     // don't draw left of workarea
256                 return;
257         if (x >= pi.pain.paperWidth())              // don't draw right of workarea
258                 return;
259
260         if (empty()) {
261                 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
262                 return;
263         }
264
265         for (const_iterator it = begin(), et = end(); it != et; ++it) {
266                 //pi.width = it->width_;
267                 (*it)->draw(pi, x, y);
268                 x += (*it)->width();
269         }
270 }
271
272
273 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
274 {
275         //if (clean_)
276         //      return;
277         dim.clear();
278         Dimension d;
279         for (const_iterator it = begin(); it != end(); ++it) {
280                 (*it)->metricsT(mi, d);
281                 dim += d;
282         }
283 }
284
285
286 void MathArray::drawT(TextPainter & pain, int x, int y) const
287 {
288         //if (drawn_ && x == xo_ && y == yo_)
289         //      return;
290         //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
291         xo_    = x;
292         yo_    = y;
293         drawn_ = true;
294
295         for (const_iterator it = begin(), et = end(); it != et; ++it) {
296                 (*it)->drawT(pain, x, y);
297                 //x += (*it->width_;
298                 x += 2;
299         }
300 }
301
302
303 int MathArray::pos2x(size_type pos) const
304 {
305         return pos2x(pos, 0);
306 }
307
308
309 int MathArray::pos2x(size_type pos, int glue) const
310 {
311         int x = 0;
312         size_type target = min(pos, size());
313         for (size_type i = 0; i < target; ++i) {
314                 const_iterator it = begin() + i;
315                 if ((*it)->getChar() == ' ')
316                         x += glue;
317                 //lyxerr << "char: " << (*it)->getChar()
318                 //      << "width: " << (*it)->width() << std::endl;
319                 x += (*it)->width();
320         }
321         return x;
322 }
323
324
325 MathArray::size_type MathArray::x2pos(int targetx) const
326 {
327         return x2pos(targetx, 0);
328 }
329
330
331 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
332 {
333         const_iterator it = begin();
334         int lastx = 0;
335         int currx = 0;
336         for (; currx < targetx && it < end(); ++it) {
337                 lastx = currx;
338                 if ((*it)->getChar() == ' ')
339                         currx += glue;
340                 currx += (*it)->width();
341         }
342         if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
343                 --it;
344         return it - begin();
345 }
346
347
348 int MathArray::dist(int x, int y) const
349 {
350         int xx = 0;
351         int yy = 0;
352
353         if (x < xo_)
354                 xx = xo_ - x;
355         else if (x > xo_ + width())
356                 xx = x - xo_ - width();
357
358         if (y < yo_ - ascent())
359                 yy = yo_ - ascent() - y;
360         else if (y > yo_ + descent())
361                 yy = y - yo_ - descent();
362
363         return xx + yy;
364 }
365
366
367 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
368 {
369         x1 = xo_;
370         x2 = xo_ + width();
371         y1 = yo_ - ascent();
372         y2 = yo_ + descent();
373 }
374
375
376 bool MathArray::contains(int x, int y) const
377 {
378         return xo_ <= x && x <= xo_ + width()
379                && yo_ - ascent() <= y && y <= yo_ + descent();
380 }
381
382
383 void MathArray::center(int & x, int & y) const
384 {
385         x = xo_ + width() / 2;
386         y = yo_ + (descent() - ascent()) / 2;
387 }
388
389
390 void MathArray::towards(int & x, int & y) const
391 {
392         int cx = 0;
393         int cy = 0;
394         center(cx, cy);
395
396         double r = 1.0;
397         //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
398
399         x = cx + int(r * (x - cx));
400         y = cy + int(r * (y - cy));
401 }
402
403
404 void MathArray::setXY(int x, int y) const
405 {
406         xo_ = x;
407         yo_ = y;
408 }
409
410
411 void MathArray::notifyCursorLeaves()
412 {
413         // do not recurse!
414
415 /*
416         // remove base-only "scripts"
417         for (pos_type i = 0; i + 1 < size(); ++i) {
418                 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
419                 if (p && p->cell(0).empty() && p->cell(1).empty()) {
420                         MathArray ar = p->nuc();
421                         erase(i);
422                         insert(i, ar);
423                         mathcursor->adjust(i, ar.size() - 1);
424                 }
425         }
426
427         // glue adjacent font insets of the same kind
428         for (pos_type i = 0; i + 1 < size(); ++i) {
429                 MathFontInset * p = operator[](i).nucleus()->asFontInset();
430                 MathFontInset const * q = operator[](i + 1)->asFontInset();
431                 if (p && q && p->name() == q->name()) {
432                         p->cell(0).append(q->cell(0));
433                         erase(i + 1);
434                         mathcursor->adjust(i, -1);
435                 }
436         }
437 */
438 }