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