]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.C
macro rework
[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_macro.h"
17 #include "math_macrotable.h"
18 #include "math_mathmlstream.h"
19 #include "math_support.h"
20 #include "math_replace.h"
21
22 #include "LColor.h"
23 #include "BufferView.h"
24 #include "buffer.h"
25 #include "cursor.h"
26 #include "debug.h"
27
28 #include "frontends/Painter.h"
29
30 #include <boost/assert.hpp>
31
32 using std::abs;
33 using std::endl;
34 using std::min;
35 using std::ostringstream;
36 using std::string;
37 using std::vector;
38
39
40 MathArray::MathArray()
41         : xo_(0), yo_(0), clean_(false), drawn_(false)
42 {}
43
44
45 MathArray::MathArray(const_iterator from, const_iterator to)
46         : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
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 namespace {
222
223 bool isInside(DocIterator const & it, MathArray const & ar, 
224         lyx::pos_type p1, lyx::pos_type p2)
225 {
226         for (size_t i = 0; i != it.size(); ++i) {
227                 CursorSlice const & sl = it[i];
228                 if (sl.inset().inMathed() && &sl.cell() == &ar)
229                         return p1 <= sl.pos() && sl.pos() < p2;
230         }
231         return false;
232 }
233
234 }
235
236
237
238 void MathArray::metrics(MetricsInfo & mi) const
239 {
240         //if (clean_)
241         //      return;
242         clean_  = true;
243         drawn_  = false;
244
245         mathed_char_dim(mi.base.font, 'I', dim_);
246
247         if (empty())
248                 return;
249
250         dim_.wid = 0;
251         Dimension d;
252         BufferView & bv  = *mi.base.bv;
253         Buffer const & buf = *bv.buffer();
254         for (size_t i = 0, n = size(); i != n; ++i) {
255                 MathAtom const & at = operator[](i);
256                 MathMacro const * mac = at->asMacro();
257                 if (mac && buf.hasMacro(mac->name())) {
258                         MacroData const & tmpl = buf.getMacro(mac->name());
259                         int numargs = tmpl.numargs();
260                         if (i + numargs > n)
261                                 numargs = n - i - 1;
262                         lyxerr << "metrics:found macro: " << mac->name()
263                                 << " numargs: " << numargs << endl;
264                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
265                                 MathArray args(begin() + i + 1, begin() + i + numargs + 1);
266                                 MathArray exp;
267                                 tmpl.expand(args, exp);
268                                 mac->setExpansion(exp, args);
269                                 mac->metricsExpanded(mi, d);
270                                 dim_.wid += mac->widthExpanded();
271                                 i += numargs;
272                                 continue;
273                         }
274                 }
275                 at->metrics(mi, d);
276                 dim_ += d;
277         }
278 }
279
280
281 void MathArray::draw(PainterInfo & pi, int x, int y) const
282 {
283         //if (drawn_ && x == xo_ && y == yo_)
284         //      return;
285         //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
286
287         xo_    = x;
288         yo_    = y;
289         drawn_ = true;
290
291         if (empty()) {
292                 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
293                 return;
294         }
295
296         // don't draw outside the workarea
297         if (y + descent() <= 0
298                 || y - ascent() >= pi.pain.paperHeight()
299                 || x + width() <= 0
300                 || x >= pi.pain.paperWidth())
301                 return;
302
303         BufferView & bv  = *pi.base.bv;
304         Buffer const & buf = *bv.buffer();
305         for (size_t i = 0, n = size(); i != n; ++i) {
306                 MathAtom const & at = operator[](i);
307                 // special macro handling
308                 MathMacro const * mac = at->asMacro();
309                 if (mac && buf.hasMacro(mac->name())) {
310                         MacroData const & tmpl = buf.getMacro(mac->name());
311                         int numargs = tmpl.numargs();
312                         if (i + numargs > n)
313                                 numargs = n - i - 1;
314                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
315                                 mac->drawExpanded(pi, x, y);
316                                 x += mac->widthExpanded();
317                                 i += numargs;
318                                 continue;
319                         }
320                 }
321                 at->drawSelection(pi, x, y);
322                 at->draw(pi, x, y);
323                 x += at->width();
324         }
325 }
326
327
328 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
329 {
330         //if (clean_)
331         //      return;
332         dim.clear();
333         Dimension d;
334         for (const_iterator it = begin(); it != end(); ++it) {
335                 (*it)->metricsT(mi, d);
336                 dim += d;
337         }
338 }
339
340
341 void MathArray::drawT(TextPainter & pain, int x, int y) const
342 {
343         //if (drawn_ && x == xo_ && y == yo_)
344         //      return;
345         //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
346         xo_    = x;
347         yo_    = y;
348         drawn_ = true;
349
350         for (const_iterator it = begin(), et = end(); it != et; ++it) {
351                 (*it)->drawT(pain, x, y);
352                 //x += (*it->width_;
353                 x += 2;
354         }
355 }
356
357
358 int MathArray::pos2x(size_type pos) const
359 {
360         return pos2x(pos, 0);
361 }
362
363
364 int MathArray::pos2x(size_type pos, int glue) const
365 {
366         int x = 0;
367         size_type target = min(pos, size());
368         for (size_type i = 0; i < target; ++i) {
369                 const_iterator it = begin() + i;
370                 if ((*it)->getChar() == ' ')
371                         x += glue;
372                 //lyxerr << "char: " << (*it)->getChar()
373                 //      << "width: " << (*it)->width() << std::endl;
374                 x += (*it)->width();
375         }
376         return x;
377 }
378
379
380 MathArray::size_type MathArray::x2pos(int targetx) const
381 {
382         return x2pos(targetx, 0);
383 }
384
385
386 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
387 {
388         const_iterator it = begin();
389         int lastx = 0;
390         int currx = 0;
391         for (; currx < targetx && it < end(); ++it) {
392                 lastx = currx;
393                 if ((*it)->getChar() == ' ')
394                         currx += glue;
395                 currx += (*it)->width();
396         }
397         if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
398                 --it;
399         return it - begin();
400 }
401
402
403 int MathArray::dist(int x, int y) const
404 {
405         int xx = 0;
406         int yy = 0;
407
408         if (x < xo_)
409                 xx = xo_ - x;
410         else if (x > xo_ + width())
411                 xx = x - xo_ - width();
412
413         if (y < yo_ - ascent())
414                 yy = yo_ - ascent() - y;
415         else if (y > yo_ + descent())
416                 yy = y - yo_ - descent();
417
418         return xx + yy;
419 }
420
421
422 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
423 {
424         x1 = xo_;
425         x2 = xo_ + width();
426         y1 = yo_ - ascent();
427         y2 = yo_ + descent();
428 }
429
430
431 bool MathArray::contains(int x, int y) const
432 {
433         return xo_ <= x && x <= xo_ + width()
434                && yo_ - ascent() <= y && y <= yo_ + descent();
435 }
436
437
438 void MathArray::center(int & x, int & y) const
439 {
440         x = xo_ + width() / 2;
441         y = yo_ + (descent() - ascent()) / 2;
442 }
443
444
445 void MathArray::towards(int & x, int & y) const
446 {
447         int cx = 0;
448         int cy = 0;
449         center(cx, cy);
450
451         double r = 1.0;
452         //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
453
454         x = cx + int(r * (x - cx));
455         y = cy + int(r * (y - cy));
456 }
457
458
459 void MathArray::setXY(int x, int y) const
460 {
461         xo_ = x;
462         yo_ = y;
463 }