]> git.lyx.org Git - lyx.git/blob - src/mathed/MathData.cpp
a7013b0529b65c0d5af10710244be4cb7574bf62
[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 "Buffer.h"
23 #include "BufferView.h"
24 #include "CoordCache.h"
25 #include "Color.h"
26 #include "Cursor.h"
27 #include "debug.h"
28
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
31
32 #include <boost/assert.hpp>
33 #include <boost/next_prior.hpp>
34
35
36 namespace lyx {
37
38 using std::abs;
39 using std::endl;
40 using std::min;
41 using std::ostringstream;
42 using std::string;
43 using std::vector;
44
45
46 MathData::MathData(const_iterator from, const_iterator to)
47         : base_type(from, to)
48 {}
49
50
51 MathAtom & MathData::operator[](pos_type pos)
52 {
53         BOOST_ASSERT(pos < size());
54         return base_type::operator[](pos);
55 }
56
57
58 MathAtom const & MathData::operator[](pos_type pos) const
59 {
60         BOOST_ASSERT(pos < size());
61         return base_type::operator[](pos);
62 }
63
64
65 void MathData::insert(size_type pos, MathAtom const & t)
66 {
67         base_type::insert(begin() + pos, t);
68 }
69
70
71 void MathData::insert(size_type pos, MathData const & ar)
72 {
73         BOOST_ASSERT(pos <= size());
74         base_type::insert(begin() + pos, ar.begin(), ar.end());
75 }
76
77
78 void MathData::append(MathData const & ar)
79 {
80         insert(size(), ar);
81 }
82
83
84 void MathData::erase(size_type pos)
85 {
86         if (pos < size())
87                 erase(pos, pos + 1);
88 }
89
90
91 void MathData::erase(iterator pos1, iterator pos2)
92 {
93         base_type::erase(pos1, pos2);
94 }
95
96
97 void MathData::erase(iterator pos)
98 {
99         base_type::erase(pos);
100 }
101
102
103 void MathData::erase(size_type pos1, size_type pos2)
104 {
105         base_type::erase(begin() + pos1, begin() + pos2);
106 }
107
108
109 void MathData::dump2() const
110 {
111         odocstringstream os;
112         NormalStream ns(os);
113         for (const_iterator it = begin(); it != end(); ++it)
114                 ns << *it << ' ';
115         lyxerr << to_utf8(os.str());
116 }
117
118
119 void MathData::dump() const
120 {
121         odocstringstream os;
122         NormalStream ns(os);
123         for (const_iterator it = begin(); it != end(); ++it)
124                 ns << '<' << *it << '>';
125         lyxerr << to_utf8(os.str());
126 }
127
128
129 void MathData::validate(LaTeXFeatures & features) const
130 {
131         for (const_iterator it = begin(); it != end(); ++it)
132                 (*it)->validate(features);
133 }
134
135
136 bool MathData::match(MathData const & ar) const
137 {
138         return size() == ar.size() && matchpart(ar, 0);
139 }
140
141
142 bool MathData::matchpart(MathData const & ar, pos_type pos) const
143 {
144         if (size() < ar.size() + pos)
145                 return false;
146         const_iterator it = begin() + pos;
147         for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
148                 if (asString(*it) != asString(*jt))
149                         return false;
150         return true;
151 }
152
153
154 void MathData::replace(ReplaceData & rep)
155 {
156         for (size_type i = 0; i < size(); ++i) {
157                 if (find1(rep.from, i)) {
158                         // match found
159                         lyxerr << "match found!" << endl;
160                         erase(i, i + rep.from.size());
161                         insert(i, rep.to);
162                 }
163         }
164
165         // FIXME: temporarily disabled
166         // for (const_iterator it = begin(); it != end(); ++it)
167         //      it->nucleus()->replace(rep);
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 namespace {
216
217 bool isInside(DocIterator const & it, MathData const & ar,
218         pos_type p1, pos_type p2)
219 {
220         for (size_t i = 0; i != it.depth(); ++i) {
221                 CursorSlice const & sl = it[i];
222                 if (sl.inset().inMathed() && &sl.cell() == &ar)
223                         return p1 <= sl.pos() && sl.pos() < p2;
224         }
225         return false;
226 }
227
228 }
229
230
231
232 void MathData::metrics(MetricsInfo & mi, Dimension & dim) const
233 {
234         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
235         dim = fm.dimension('I');
236         int xascent = fm.dimension('x').ascent();
237         if (xascent >= dim.asc)
238                 xascent = (2 * dim.asc) / 3;
239         minasc_ = xascent;
240         mindes_ = (3 * xascent) / 4;
241         slevel_ = (4 * xascent) / 5;
242         sshift_ = xascent / 4;
243         kerning_ = 0;
244
245         if (empty()) {
246                 // Cache the dimension.
247                 mi.base.bv->coordCache().arrays().add(this, dim);
248                 return;
249         }
250
251         dim.asc = 0;
252         dim.wid = 0;
253         Dimension d;
254         atom_dims_.clear();
255         //BufferView & bv  = *mi.base.bv;
256         //Buffer const & buf = bv.buffer();
257         for (size_t i = 0, n = size(); i != n; ++i) {
258                 MathAtom const & at = operator[](i);
259 #if 0
260                 MathMacro const * mac = at->asMacro();
261                 if (mac && buf.hasMacro(mac->name())) {
262                         MacroData const & tmpl = buf.getMacro(mac->name());
263                         int numargs = tmpl.numargs();
264                         if (i + numargs > n)
265                                 numargs = n - i - 1;
266                         lyxerr << "metrics:found macro: " << mac->name()
267                                 << " numargs: " << numargs << endl;
268                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
269                                 MathData args(begin() + i + 1, begin() + i + numargs + 1);
270                                 MathData exp;
271                                 tmpl.expand(args, exp);
272                                 mac->setExpansion(exp, args);
273                                 mac->metricsExpanded(mi, d);
274                                 dim.wid += mac->widthExpanded();
275                                 i += numargs;
276                                 continue;
277                         }
278                 }
279 #endif
280                 at->metrics(mi, d);
281                 atom_dims_.push_back(d);
282                 dim += d;
283                 if (i == n - 1)
284                         kerning_ = at->kerning();
285         }
286         // Cache the dimension.
287         mi.base.bv->coordCache().arrays().add(this, dim);
288 }
289
290
291 void MathData::draw(PainterInfo & pi, int x, int y) const
292 {
293         //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
294         BufferView & bv  = *pi.base.bv;
295         setXY(bv, x, y);
296
297         Dimension const & dim = bv.coordCache().getArrays().dim(this);
298
299         if (empty()) {
300                 pi.pain.rectangle(x, y - dim.ascent(), dim.width(), dim.height(), Color::mathline);
301                 return;
302         }
303
304         // don't draw outside the workarea
305         if (y + dim.descent() <= 0
306                 || y - dim.ascent() >= bv.workHeight()
307                 || x + dim.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 += atom_dims_[i].wid;
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 += atom_dims_[i].wid;
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 += atom_dims_[it - begin()].wid;
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         return bv.coordCache().getArrays().squareDistance(this, x, y);
428 }
429
430
431 void MathData::setXY(BufferView & bv, int x, int y) const
432 {
433         //lyxerr << "setting position cache for MathData " << this << std::endl;
434         bv.coordCache().arrays().add(this, x, y);
435 }
436
437
438 Dimension const & MathData::dimension(BufferView const & bv) const
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