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