]> git.lyx.org Git - features.git/blob - src/mathed/MathData.cpp
lib/lyx2lyx/lyx_1_5.py: fix revert of listings insets
[features.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 #ifdef WITH_WARNINGS
164 #warning temporarily disabled
165         // for (const_iterator it = begin(); it != end(); ++it)
166         //      it->nucleus()->replace(rep);
167 #endif
168 }
169
170
171 bool MathData::find1(MathData const & ar, size_type pos) const
172 {
173         lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
174         for (size_type i = 0, n = ar.size(); i < n; ++i)
175                 if (asString(operator[](pos + i)) != asString(ar[i]))
176                         return false;
177         return true;
178 }
179
180
181 MathData::size_type MathData::find(MathData const & ar) const
182 {
183         for (int i = 0, last = size() - ar.size(); i < last; ++i)
184                 if (find1(ar, i))
185                         return i;
186         return size();
187 }
188
189
190 MathData::size_type MathData::find_last(MathData const & ar) const
191 {
192         for (int i = size() - ar.size(); i >= 0; --i)
193                 if (find1(ar, i))
194                         return i;
195         return size();
196 }
197
198
199 bool MathData::contains(MathData const & ar) const
200 {
201         if (find(ar) != size())
202                 return true;
203         for (const_iterator it = begin(); it != end(); ++it)
204                 if ((*it)->contains(ar))
205                         return true;
206         return false;
207 }
208
209
210 void MathData::touch() const
211 {
212 }
213
214
215 bool MathData::metrics(MetricsInfo & mi, Dimension & dim) const
216 {
217         dim = dim_;
218         metrics(mi);
219         if (dim_ == dim)
220                 return false;
221         dim = dim_;
222         return true;
223 }
224
225
226 namespace {
227
228 bool isInside(DocIterator const & it, MathData const & ar,
229         pos_type p1, pos_type p2)
230 {
231         for (size_t i = 0; i != it.depth(); ++i) {
232                 CursorSlice const & sl = it[i];
233                 if (sl.inset().inMathed() && &sl.cell() == &ar)
234                         return p1 <= sl.pos() && sl.pos() < p2;
235         }
236         return false;
237 }
238
239 }
240
241
242
243 void MathData::metrics(MetricsInfo & mi) const
244 {
245         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
246         dim_ = fm.dimension('I');
247         int xascent = fm.dimension('x').ascent();
248         if (xascent >= dim_.asc)
249                 xascent = (2 * dim_.asc) / 3;
250         minasc_ = xascent;
251         mindes_ = (3 * xascent) / 4;
252         slevel_ = (4 * xascent) / 5;
253         sshift_ = xascent / 4;
254         kerning_ = 0;
255
256         if (empty())
257                 return;
258
259         dim_.asc = 0;
260         dim_.wid = 0;
261         Dimension d;
262         //BufferView & bv  = *mi.base.bv;
263         //Buffer const & buf = *bv.buffer();
264         for (size_t i = 0, n = size(); i != n; ++i) {
265                 MathAtom const & at = operator[](i);
266 #if 0
267                 MathMacro const * mac = at->asMacro();
268                 if (mac && buf.hasMacro(mac->name())) {
269                         MacroData const & tmpl = buf.getMacro(mac->name());
270                         int numargs = tmpl.numargs();
271                         if (i + numargs > n)
272                                 numargs = n - i - 1;
273                         lyxerr << "metrics:found macro: " << mac->name()
274                                 << " numargs: " << numargs << endl;
275                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
276                                 MathData args(begin() + i + 1, begin() + i + numargs + 1);
277                                 MathData exp;
278                                 tmpl.expand(args, exp);
279                                 mac->setExpansion(exp, args);
280                                 mac->metricsExpanded(mi, d);
281                                 dim_.wid += mac->widthExpanded();
282                                 i += numargs;
283                                 continue;
284                         }
285                 }
286 #endif
287                 at->metrics(mi, d);
288                 dim_ += d;
289                 if (i == n - 1)
290                         kerning_ = at->kerning();
291         }
292 }
293
294
295 void MathData::draw(PainterInfo & pi, int x, int y) const
296 {
297         //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
298         BufferView & bv  = *pi.base.bv;
299         setXY(bv, x, y);
300
301         if (empty()) {
302                 pi.pain.rectangle(x, y - ascent(), width(), height(), Color::mathline);
303                 return;
304         }
305
306         // don't draw outside the workarea
307         if (y + descent() <= 0
308                 || y - ascent() >= bv.workHeight()
309                 || x + width() <= 0
310                 || x >= bv. workWidth())
311                 return;
312
313                 
314
315         for (size_t i = 0, n = size(); i != n; ++i) {
316                 MathAtom const & at = operator[](i);
317 #if 0
318         Buffer const & buf = bv.buffer();
319                 // special macro handling
320                 MathMacro const * mac = at->asMacro();
321                 if (mac && buf.hasMacro(mac->name())) {
322                         MacroData const & tmpl = buf.getMacro(mac->name());
323                         int numargs = tmpl.numargs();
324                         if (i + numargs > n)
325                                 numargs = n - i - 1;
326                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
327                                 mac->drawExpanded(pi, x, y);
328                                 x += mac->widthExpanded();
329                                 i += numargs;
330                                 continue;
331                         }
332                 }
333 #endif
334                 bv.coordCache().insets().add(at.nucleus(), x, y);
335                 at->drawSelection(pi, x, y);
336                 at->setRedrawBackground(false);
337                 lyxerr << "selection draw " << x << " " << y << " " << at->redrawBackground() << std::endl;
338                 at->draw(pi, x, y);
339                 //at->setRedrawBackground(true);
340                 x += at->width();
341         }
342 }
343
344
345 void MathData::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
346 {
347         dim.clear();
348         Dimension d;
349         for (const_iterator it = begin(); it != end(); ++it) {
350                 (*it)->metricsT(mi, d);
351                 dim += d;
352         }
353 }
354
355
356 void MathData::drawT(TextPainter & pain, int x, int y) const
357 {
358         //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
359
360         // FIXME: Abdel 16/10/2006
361         // This drawT() method is never used, this is dead code.
362
363         for (const_iterator it = begin(), et = end(); it != et; ++it) {
364                 (*it)->drawT(pain, x, y);
365                 //x += (*it)->width_;
366                 x += 2;
367         }
368 }
369
370
371 int MathData::pos2x(size_type pos) const
372 {
373         return pos2x(pos, 0);
374 }
375
376
377 int MathData::pos2x(size_type pos, int glue) const
378 {
379         int x = 0;
380         size_type target = min(pos, size());
381         for (size_type i = 0; i < target; ++i) {
382                 const_iterator it = begin() + i;
383                 if ((*it)->getChar() == ' ')
384                         x += glue;
385                 //lyxerr << "char: " << (*it)->getChar()
386                 //      << "width: " << (*it)->width() << std::endl;
387                 x += (*it)->width();
388         }
389         return x;
390 }
391
392
393 MathData::size_type MathData::x2pos(int targetx) const
394 {
395         return x2pos(targetx, 0);
396 }
397
398
399 MathData::size_type MathData::x2pos(int targetx, int glue) const
400 {
401         const_iterator it = begin();
402         int lastx = 0;
403         int currx = 0;
404         // find first position after targetx
405         for (; currx < targetx && it < end(); ++it) {
406                 lastx = currx;
407                 if ((*it)->getChar() == ' ')
408                         currx += glue;
409                 currx += (*it)->width();
410         }
411
412         /**
413          * If we are not at the beginning of the array, go to the left
414          * of the inset if one of the following two condition holds:
415          * - the current inset is editable (so that the cursor tip is
416          *   deeper than us): in this case, we want all intermediate
417          *   cursor slices to be before insets;
418          * - the mouse is closer to the left side of the inset than to
419          *   the right one.
420          * See bug 1918 for details.
421          **/
422         if (it != begin() && currx >= targetx
423             && ((*boost::prior(it))->asNestInset()
424                 || abs(lastx - targetx) < abs(currx - targetx))) {
425                 --it;
426         }
427
428         return it - begin();
429 }
430
431
432 int MathData::dist(BufferView const & bv, int x, int y) const
433 {
434         int xx = 0;
435         int yy = 0;
436
437         const int xo_ = xo(bv);
438         const int yo_ = yo(bv);
439
440         if (x < xo_)
441                 xx = xo_ - x;
442         else if (x > xo_ + width())
443                 xx = x - xo_ - width();
444
445         if (y < yo_ - ascent())
446                 yy = yo_ - ascent() - y;
447         else if (y > yo_ + descent())
448                 yy = y - yo_ - descent();
449
450         return xx + yy;
451 }
452
453
454 void MathData::setXY(BufferView & bv, int x, int y) const
455 {
456         //lyxerr << "setting position cache for MathData " << this << std::endl;
457         bv.coordCache().arrays().add(this, x, y);
458 }
459
460
461 int MathData::xo(BufferView const & bv) const
462 {
463         return bv.coordCache().getArrays().x(this);
464 }
465
466
467 int MathData::yo(BufferView const & bv) const
468 {
469         return bv.coordCache().getArrays().y(this);
470 }
471
472
473 std::ostream & operator<<(std::ostream & os, MathData const & ar)
474 {
475         odocstringstream oss;
476         NormalStream ns(oss);
477         ns << ar;
478         return os << to_utf8(oss.str());
479 }
480
481
482 odocstream & operator<<(odocstream & os, MathData const & ar)
483 {
484         NormalStream ns(os);
485         ns << ar;
486         return os;
487 }
488
489
490 } // namespace lyx