]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.C
rename commandtags.h to lfuns.h and renumber/cleanup. Rebuild the tree !
[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 "math_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(MathMetricsInfo & 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(MathPainterInfo & 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(0, pos, 0);
290 }
291
292
293 int MathArray::pos2x(size_type pos1, size_type pos2, int glue) const
294 {
295         int x = 0;
296         size_type target = min(pos2, size());
297         for (size_type i = pos1; 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(0, targetx, 0);
310 }
311
312
313 MathArray::size_type MathArray::x2pos(size_type startpos, int targetx,
314         int glue) const
315 {
316         const_iterator it = begin() + startpos;
317         int lastx = 0;
318         int currx = 0;
319         for (; currx < targetx && it < end(); ++it) {
320                 lastx = currx;
321                 if ((*it)->getChar() == ' ')
322                         currx += glue;
323                 currx += (*it)->width();
324         }
325         if (abs(lastx - targetx) < abs(currx - targetx) && it != begin() + startpos)
326                 --it;
327         return it - begin();
328 }
329
330
331 int MathArray::dist(int x, int y) const
332 {
333         int xx = 0;
334         int yy = 0;
335
336         if (x < xo_)
337                 xx = xo_ - x;
338         else if (x > xo_ + width())
339                 xx = x - xo_ - width();
340
341         if (y < yo_ - ascent())
342                 yy = yo_ - ascent() - y;
343         else if (y > yo_ + descent())
344                 yy = y - yo_ - descent();
345
346         return xx + yy;
347 }
348
349
350 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
351 {
352         x1 = xo_;
353         x2 = xo_ + width();
354         y1 = yo_ - ascent();
355         y2 = yo_ + descent();
356 }
357
358
359 void MathArray::center(int & x, int & y) const
360 {
361         x = xo_ + width() / 2;
362         y = yo_ + (descent() - ascent()) / 2;
363 }
364
365
366 void MathArray::towards(int & x, int & y) const
367 {
368         int cx = 0;
369         int cy = 0;
370         center(cx, cy);
371
372         double r = 1.0;
373         //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
374
375         x = cx + int(r * (x - cx));
376         y = cy + int(r * (y - cy));
377 }
378
379
380 void MathArray::setXY(int x, int y) const
381 {
382         xo_ = x;
383         yo_ = y;
384 }
385
386
387 void MathArray::notifyCursorLeaves()
388 {
389         // do not recurse!
390
391         // remove base-only "scripts"
392         for (pos_type i = 0; i + 1 < size(); ++i) {
393                 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
394                 if (p && p->cell(0).empty() && p->cell(1).empty()) {
395                         MathArray ar = p->nuc();
396                         erase(i);
397                         insert(i, ar);
398                         mathcursor->adjust(i, ar.size() - 1);
399                 }
400         }
401
402         // glue adjacent font insets of the same kind
403         for (pos_type i = 0; i + 1 < size(); ++i) {
404                 MathFontInset * p = operator[](i).nucleus()->asFontInset();
405                 MathFontInset const * q = operator[](i + 1)->asFontInset();
406                 if (p && q && p->name() == q->name()) {
407                         p->cell(0).append(q->cell(0));
408                         erase(i + 1);
409                         mathcursor->adjust(i, -1);
410                 }
411         }
412
413 }