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