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