]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.C
Whitespace, only whitespace...
[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 #if 0
257                 MathMacro const * mac = at->asMacro();
258                 if (mac && buf.hasMacro(mac->name())) {
259                         MacroData const & tmpl = buf.getMacro(mac->name());
260                         int numargs = tmpl.numargs();
261                         if (i + numargs > n)
262                                 numargs = n - i - 1;
263                         lyxerr << "metrics:found macro: " << mac->name()
264                                 << " numargs: " << numargs << endl;
265                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
266                                 MathArray args(begin() + i + 1, begin() + i + numargs + 1);
267                                 MathArray exp;
268                                 tmpl.expand(args, exp);
269                                 mac->setExpansion(exp, args);
270                                 mac->metricsExpanded(mi, d);
271                                 dim_.wid += mac->widthExpanded();
272                                 i += numargs;
273                                 continue;
274                         }
275                 }
276 #endif
277                 at->metrics(mi, d);
278                 dim_ += d;
279         }
280 }
281
282
283 void MathArray::draw(PainterInfo & pi, int x, int y) const
284 {
285         //if (drawn_ && x == xo_ && y == yo_)
286         //      return;
287         //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
288
289         xo_    = x;
290         yo_    = y;
291         drawn_ = true;
292
293         if (empty()) {
294                 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
295                 return;
296         }
297
298         // don't draw outside the workarea
299         if (y + descent() <= 0
300                 || y - ascent() >= pi.pain.paperHeight()
301                 || x + width() <= 0
302                 || x >= pi.pain.paperWidth())
303                 return;
304
305         //BufferView & bv  = *pi.base.bv;
306         for (size_t i = 0, n = size(); i != n; ++i) {
307                 MathAtom const & at = operator[](i);
308 #if 0
309         Buffer const & buf = *bv.buffer();
310                 // special macro handling
311                 MathMacro const * mac = at->asMacro();
312                 if (mac && buf.hasMacro(mac->name())) {
313                         MacroData const & tmpl = buf.getMacro(mac->name());
314                         int numargs = tmpl.numargs();
315                         if (i + numargs > n)
316                                 numargs = n - i - 1;
317                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
318                                 mac->drawExpanded(pi, x, y);
319                                 x += mac->widthExpanded();
320                                 i += numargs;
321                                 continue;
322                         }
323                 }
324 #endif
325                 at->drawSelection(pi, x, y);
326                 at->draw(pi, x, y);
327                 x += at->width();
328         }
329 }
330
331
332 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
333 {
334         //if (clean_)
335         //      return;
336         dim.clear();
337         Dimension d;
338         for (const_iterator it = begin(); it != end(); ++it) {
339                 (*it)->metricsT(mi, d);
340                 dim += d;
341         }
342 }
343
344
345 void MathArray::drawT(TextPainter & pain, int x, int y) const
346 {
347         //if (drawn_ && x == xo_ && y == yo_)
348         //      return;
349         //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
350         xo_    = x;
351         yo_    = y;
352         drawn_ = true;
353
354         for (const_iterator it = begin(), et = end(); it != et; ++it) {
355                 (*it)->drawT(pain, x, y);
356                 //x += (*it->width_;
357                 x += 2;
358         }
359 }
360
361
362 int MathArray::pos2x(size_type pos) const
363 {
364         return pos2x(pos, 0);
365 }
366
367
368 int MathArray::pos2x(size_type pos, int glue) const
369 {
370         int x = 0;
371         size_type target = min(pos, size());
372         for (size_type i = 0; i < target; ++i) {
373                 const_iterator it = begin() + i;
374                 if ((*it)->getChar() == ' ')
375                         x += glue;
376                 //lyxerr << "char: " << (*it)->getChar()
377                 //      << "width: " << (*it)->width() << std::endl;
378                 x += (*it)->width();
379         }
380         return x;
381 }
382
383
384 MathArray::size_type MathArray::x2pos(int targetx) const
385 {
386         return x2pos(targetx, 0);
387 }
388
389
390 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
391 {
392         const_iterator it = begin();
393         int lastx = 0;
394         int currx = 0;
395         for (; currx < targetx && it < end(); ++it) {
396                 lastx = currx;
397                 if ((*it)->getChar() == ' ')
398                         currx += glue;
399                 currx += (*it)->width();
400         }
401         if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
402                 --it;
403         return it - begin();
404 }
405
406
407 int MathArray::dist(int x, int y) const
408 {
409         int xx = 0;
410         int yy = 0;
411
412         if (x < xo_)
413                 xx = xo_ - x;
414         else if (x > xo_ + width())
415                 xx = x - xo_ - width();
416
417         if (y < yo_ - ascent())
418                 yy = yo_ - ascent() - y;
419         else if (y > yo_ + descent())
420                 yy = y - yo_ - descent();
421
422         return xx + yy;
423 }
424
425
426 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
427 {
428         x1 = xo_;
429         x2 = xo_ + width();
430         y1 = yo_ - ascent();
431         y2 = yo_ + descent();
432 }
433
434
435 bool MathArray::contains(int x, int y) const
436 {
437         return xo_ <= x && x <= xo_ + width()
438                && yo_ - ascent() <= y && y <= yo_ + descent();
439 }
440
441
442 void MathArray::center(int & x, int & y) const
443 {
444         x = xo_ + width() / 2;
445         y = yo_ + (descent() - ascent()) / 2;
446 }
447
448
449 void MathArray::towards(int & x, int & y) const
450 {
451         int cx = 0;
452         int cy = 0;
453         center(cx, cy);
454
455         double r = 1.0;
456         //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
457
458         x = cx + int(r * (x - cx));
459         y = cy + int(r * (y - cy));
460 }
461
462
463 void MathArray::setXY(int x, int y) const
464 {
465         xo_ = x;
466         yo_ = y;
467 }