]> git.lyx.org Git - lyx.git/blob - src/mathed/MathData.cpp
* BufferView::buffer() returns a reference instead of a pointer.
[lyx.git] / src / mathed / MathData.cpp
1 /**
2  * \file MathData.cpp
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 "MathData.h"
14 #include "InsetMathFont.h"
15 #include "InsetMathScript.h"
16 #include "MathMacro.h"
17 #include "MacroTable.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "ReplaceData.h"
21
22 #include "BufferView.h"
23 #include "Buffer.h"
24 #include "Cursor.h"
25 #include "debug.h"
26 #include "Color.h"
27
28 #include "frontends/FontMetrics.h"
29 #include "frontends/Painter.h"
30
31 #include <boost/assert.hpp>
32
33
34 namespace lyx {
35
36 using std::abs;
37 using std::endl;
38 using std::min;
39 using std::ostringstream;
40 using std::string;
41 using std::vector;
42
43
44 MathData::MathData(const_iterator from, const_iterator to)
45         : base_type(from, to)
46 {}
47
48
49 MathAtom & MathData::operator[](pos_type pos)
50 {
51         BOOST_ASSERT(pos < size());
52         return base_type::operator[](pos);
53 }
54
55
56 MathAtom const & MathData::operator[](pos_type pos) const
57 {
58         BOOST_ASSERT(pos < size());
59         return base_type::operator[](pos);
60 }
61
62
63 void MathData::insert(size_type pos, MathAtom const & t)
64 {
65         base_type::insert(begin() + pos, t);
66 }
67
68
69 void MathData::insert(size_type pos, MathData const & ar)
70 {
71         BOOST_ASSERT(pos <= size());
72         base_type::insert(begin() + pos, ar.begin(), ar.end());
73 }
74
75
76 void MathData::append(MathData const & ar)
77 {
78         insert(size(), ar);
79 }
80
81
82 void MathData::erase(size_type pos)
83 {
84         if (pos < size())
85                 erase(pos, pos + 1);
86 }
87
88
89 void MathData::erase(iterator pos1, iterator pos2)
90 {
91         base_type::erase(pos1, pos2);
92 }
93
94
95 void MathData::erase(iterator pos)
96 {
97         base_type::erase(pos);
98 }
99
100
101 void MathData::erase(size_type pos1, size_type pos2)
102 {
103         base_type::erase(begin() + pos1, begin() + pos2);
104 }
105
106
107 void MathData::dump2() const
108 {
109         odocstringstream os;
110         NormalStream ns(os);
111         for (const_iterator it = begin(); it != end(); ++it)
112                 ns << *it << ' ';
113         lyxerr << to_utf8(os.str());
114 }
115
116
117 void MathData::dump() const
118 {
119         odocstringstream os;
120         NormalStream ns(os);
121         for (const_iterator it = begin(); it != end(); ++it)
122                 ns << '<' << *it << '>';
123         lyxerr << to_utf8(os.str());
124 }
125
126
127 void MathData::validate(LaTeXFeatures & features) const
128 {
129         for (const_iterator it = begin(); it != end(); ++it)
130                 (*it)->validate(features);
131 }
132
133
134 bool MathData::match(MathData const & ar) const
135 {
136         return size() == ar.size() && matchpart(ar, 0);
137 }
138
139
140 bool MathData::matchpart(MathData const & ar, pos_type pos) const
141 {
142         if (size() < ar.size() + pos)
143                 return false;
144         const_iterator it = begin() + pos;
145         for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
146                 if (asString(*it) != asString(*jt))
147                         return false;
148         return true;
149 }
150
151
152 void MathData::replace(ReplaceData & rep)
153 {
154         for (size_type i = 0; i < size(); ++i) {
155                 if (find1(rep.from, i)) {
156                         // match found
157                         lyxerr << "match found!" << endl;
158                         erase(i, i + rep.from.size());
159                         insert(i, rep.to);
160                 }
161         }
162
163         // FIXME: temporarily disabled
164         // for (const_iterator it = begin(); it != end(); ++it)
165         //      it->nucleus()->replace(rep);
166 }
167
168
169 bool MathData::find1(MathData const & ar, size_type pos) const
170 {
171         lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
172         for (size_type i = 0, n = ar.size(); i < n; ++i)
173                 if (asString(operator[](pos + i)) != asString(ar[i]))
174                         return false;
175         return true;
176 }
177
178
179 MathData::size_type MathData::find(MathData const & ar) const
180 {
181         for (int i = 0, last = size() - ar.size(); i < last; ++i)
182                 if (find1(ar, i))
183                         return i;
184         return size();
185 }
186
187
188 MathData::size_type MathData::find_last(MathData const & ar) const
189 {
190         for (int i = size() - ar.size(); i >= 0; --i)
191                 if (find1(ar, i))
192                         return i;
193         return size();
194 }
195
196
197 bool MathData::contains(MathData const & ar) const
198 {
199         if (find(ar) != size())
200                 return true;
201         for (const_iterator it = begin(); it != end(); ++it)
202                 if ((*it)->contains(ar))
203                         return true;
204         return false;
205 }
206
207
208 void MathData::touch() const
209 {
210 }
211
212
213 bool MathData::metrics(MetricsInfo & mi, Dimension & dim) const
214 {
215         dim = dim_;
216         metrics(mi);
217         if (dim_ == dim)
218                 return false;
219         dim = dim_;
220         return true;
221 }
222
223
224 namespace {
225
226 bool isInside(DocIterator const & it, MathData const & ar,
227         pos_type p1, pos_type p2)
228 {
229         for (size_t i = 0; i != it.depth(); ++i) {
230                 CursorSlice const & sl = it[i];
231                 if (sl.inset().inMathed() && &sl.cell() == &ar)
232                         return p1 <= sl.pos() && sl.pos() < p2;
233         }
234         return false;
235 }
236
237 }
238
239
240
241 void MathData::metrics(MetricsInfo & mi) const
242 {
243         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
244         dim_ = fm.dimension('I');
245         int xascent = fm.dimension('x').ascent();
246         if (xascent >= dim_.asc)
247                 xascent = (2 * dim_.asc) / 3;
248         minasc_ = xascent;
249         mindes_ = (3 * xascent) / 4;
250         slevel_ = (4 * xascent) / 5;
251         sshift_ = xascent / 4;
252         kerning_ = 0;
253
254         if (empty())
255                 return;
256
257         dim_.asc = 0;
258         dim_.wid = 0;
259         Dimension d;
260         //BufferView & bv  = *mi.base.bv;
261         //Buffer const & buf = bv.buffer();
262         for (size_t i = 0, n = size(); i != n; ++i) {
263                 MathAtom const & at = operator[](i);
264 #if 0
265                 MathMacro const * mac = at->asMacro();
266                 if (mac && buf.hasMacro(mac->name())) {
267                         MacroData const & tmpl = buf.getMacro(mac->name());
268                         int numargs = tmpl.numargs();
269                         if (i + numargs > n)
270                                 numargs = n - i - 1;
271                         lyxerr << "metrics:found macro: " << mac->name()
272                                 << " numargs: " << numargs << endl;
273                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
274                                 MathData args(begin() + i + 1, begin() + i + numargs + 1);
275                                 MathData exp;
276                                 tmpl.expand(args, exp);
277                                 mac->setExpansion(exp, args);
278                                 mac->metricsExpanded(mi, d);
279                                 dim_.wid += mac->widthExpanded();
280                                 i += numargs;
281                                 continue;
282                         }
283                 }
284 #endif
285                 at->metrics(mi, d);
286                 dim_ += d;
287                 if (i == n - 1)
288                         kerning_ = at->kerning();
289         }
290 }
291
292
293 void MathData::draw(PainterInfo & pi, int x, int y) const
294 {
295         //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
296         BufferView & bv  = *pi.base.bv;
297         setXY(bv, x, y);
298
299         if (empty()) {
300                 pi.pain.rectangle(x, y - ascent(), width(), height(), Color::mathline);
301                 return;
302         }
303
304         // don't draw outside the workarea
305         if (y + descent() <= 0
306                 || y - ascent() >= bv.workHeight()
307                 || x + width() <= 0
308                 || x >= bv. workWidth())
309                 return;
310
311         for (size_t i = 0, n = size(); i != n; ++i) {
312                 MathAtom const & at = operator[](i);
313 #if 0
314         Buffer const & buf = bv.buffer();
315                 // special macro handling
316                 MathMacro const * mac = at->asMacro();
317                 if (mac && buf.hasMacro(mac->name())) {
318                         MacroData const & tmpl = buf.getMacro(mac->name());
319                         int numargs = tmpl.numargs();
320                         if (i + numargs > n)
321                                 numargs = n - i - 1;
322                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
323                                 mac->drawExpanded(pi, x, y);
324                                 x += mac->widthExpanded();
325                                 i += numargs;
326                                 continue;
327                         }
328                 }
329 #endif
330                 bv.coordCache().insets().add(at.nucleus(), x, y);
331                 at->drawSelection(pi, x, y);
332                 at->draw(pi, x, y);
333                 x += at->width();
334         }
335 }
336
337
338 void MathData::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
339 {
340         dim.clear();
341         Dimension d;
342         for (const_iterator it = begin(); it != end(); ++it) {
343                 (*it)->metricsT(mi, d);
344                 dim += d;
345         }
346 }
347
348
349 void MathData::drawT(TextPainter & pain, int x, int y) const
350 {
351         //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
352
353         // FIXME: Abdel 16/10/2006
354         // This drawT() method is never used, this is dead code.
355
356         for (const_iterator it = begin(), et = end(); it != et; ++it) {
357                 (*it)->drawT(pain, x, y);
358                 //x += (*it)->width_;
359                 x += 2;
360         }
361 }
362
363
364 int MathData::pos2x(size_type pos) const
365 {
366         return pos2x(pos, 0);
367 }
368
369
370 int MathData::pos2x(size_type pos, int glue) const
371 {
372         int x = 0;
373         size_type target = min(pos, size());
374         for (size_type i = 0; i < target; ++i) {
375                 const_iterator it = begin() + i;
376                 if ((*it)->getChar() == ' ')
377                         x += glue;
378                 //lyxerr << "char: " << (*it)->getChar()
379                 //      << "width: " << (*it)->width() << std::endl;
380                 x += (*it)->width();
381         }
382         return x;
383 }
384
385
386 MathData::size_type MathData::x2pos(int targetx) const
387 {
388         return x2pos(targetx, 0);
389 }
390
391
392 MathData::size_type MathData::x2pos(int targetx, int glue) const
393 {
394         const_iterator it = begin();
395         int lastx = 0;
396         int currx = 0;
397         // find first position after targetx
398         for (; currx < targetx && it < end(); ++it) {
399                 lastx = currx;
400                 if ((*it)->getChar() == ' ')
401                         currx += glue;
402                 currx += (*it)->width();
403         }
404
405         /**
406          * If we are not at the beginning of the array, go to the left
407          * of the inset if one of the following two condition holds:
408          * - the current inset is editable (so that the cursor tip is
409          *   deeper than us): in this case, we want all intermediate
410          *   cursor slices to be before insets;
411          * - the mouse is closer to the left side of the inset than to
412          *   the right one.
413          * See bug 1918 for details.
414          **/
415         if (it != begin() && currx >= targetx
416             && ((*boost::prior(it))->asNestInset()
417                 || abs(lastx - targetx) < abs(currx - targetx))) {
418                 --it;
419         }
420
421         return it - begin();
422 }
423
424
425 int MathData::dist(BufferView const & bv, int x, int y) const
426 {
427         int xx = 0;
428         int yy = 0;
429
430         const int xo_ = xo(bv);
431         const int yo_ = yo(bv);
432
433         if (x < xo_)
434                 xx = xo_ - x;
435         else if (x > xo_ + width())
436                 xx = x - xo_ - width();
437
438         if (y < yo_ - ascent())
439                 yy = yo_ - ascent() - y;
440         else if (y > yo_ + descent())
441                 yy = y - yo_ - descent();
442
443         return xx + yy;
444 }
445
446
447 void MathData::setXY(BufferView & bv, int x, int y) const
448 {
449         //lyxerr << "setting position cache for MathData " << this << std::endl;
450         bv.coordCache().arrays().add(this, x, y);
451 }
452
453
454 int MathData::xo(BufferView const & bv) const
455 {
456         return bv.coordCache().getArrays().x(this);
457 }
458
459
460 int MathData::yo(BufferView const & bv) const
461 {
462         return bv.coordCache().getArrays().y(this);
463 }
464
465
466 std::ostream & operator<<(std::ostream & os, MathData const & ar)
467 {
468         odocstringstream oss;
469         NormalStream ns(oss);
470         ns << ar;
471         return os << to_utf8(oss.str());
472 }
473
474
475 odocstream & operator<<(odocstream & os, MathData const & ar)
476 {
477         NormalStream ns(os);
478         ns << ar;
479         return os;
480 }
481
482
483 } // namespace lyx