]> git.lyx.org Git - lyx.git/blob - src/mathed/math_data.C
tfracinset files is missing so all usage of tfrac is commented out
[lyx.git] / src / mathed / math_data.C
1 /**
2  * \file math_data.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 "math_data.h"
14 #include "math_fontinset.h"
15 #include "math_scriptinset.h"
16 #include "math_macro.h"
17 #include "math_macrotable.h"
18 #include "math_mathmlstream.h"
19 #include "math_support.h"
20 #include "math_replace.h"
21
22 #include "coordcache.h"
23 #include "LColor.h"
24 #include "BufferView.h"
25 #include "buffer.h"
26 #include "cursor.h"
27 #include "debug.h"
28
29 #include "frontends/Painter.h"
30
31 #include <boost/assert.hpp>
32
33 using std::abs;
34 using std::endl;
35 using std::min;
36 using std::ostringstream;
37 using std::string;
38 using std::vector;
39
40
41 MathArray::MathArray()
42 {}
43
44
45 MathArray::MathArray(const_iterator from, const_iterator to)
46         : base_type(from, to)
47 {}
48
49
50 MathAtom & MathArray::operator[](pos_type pos)
51 {
52         BOOST_ASSERT(pos < size());
53         return base_type::operator[](pos);
54 }
55
56
57 MathAtom const & MathArray::operator[](pos_type pos) const
58 {
59         BOOST_ASSERT(pos < size());
60         return base_type::operator[](pos);
61 }
62
63
64 void MathArray::insert(size_type pos, MathAtom const & t)
65 {
66         base_type::insert(begin() + pos, t);
67 }
68
69
70 void MathArray::insert(size_type pos, MathArray const & ar)
71 {
72         BOOST_ASSERT(pos <= size());
73         base_type::insert(begin() + pos, ar.begin(), ar.end());
74 }
75
76
77 void MathArray::append(MathArray const & ar)
78 {
79         insert(size(), ar);
80 }
81
82
83 void MathArray::erase(size_type pos)
84 {
85         if (pos < size())
86                 erase(pos, pos + 1);
87 }
88
89
90 void MathArray::erase(iterator pos1, iterator pos2)
91 {
92         base_type::erase(pos1, pos2);
93 }
94
95
96 void MathArray::erase(iterator pos)
97 {
98         base_type::erase(pos);
99 }
100
101
102 void MathArray::erase(size_type pos1, size_type pos2)
103 {
104         base_type::erase(begin() + pos1, begin() + pos2);
105 }
106
107
108 void MathArray::dump2() const
109 {
110         NormalStream ns(lyxerr);
111         for (const_iterator it = begin(); it != end(); ++it)
112                 ns << *it << ' ';
113 }
114
115
116 void MathArray::dump() const
117 {
118         NormalStream ns(lyxerr);
119         for (const_iterator it = begin(); it != end(); ++it)
120                 ns << '<' << *it << '>';
121 }
122
123
124 void MathArray::validate(LaTeXFeatures & features) const
125 {
126         for (const_iterator it = begin(); it != end(); ++it)
127                 (*it)->validate(features);
128 }
129
130
131 bool MathArray::match(MathArray const & ar) const
132 {
133         return size() == ar.size() && matchpart(ar, 0);
134 }
135
136
137 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
138 {
139         if (size() < ar.size() + pos)
140                 return false;
141         const_iterator it = begin() + pos;
142         for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
143                 if (asString(*it) != asString(*jt))
144                         return false;
145         return true;
146 }
147
148
149 void MathArray::replace(ReplaceData & rep)
150 {
151         for (size_type i = 0; i < size(); ++i) {
152                 if (find1(rep.from, i)) {
153                         // match found
154                         lyxerr << "match found!" << endl;
155                         erase(i, i + rep.from.size());
156                         insert(i, rep.to);
157                 }
158         }
159
160 #ifdef WITH_WARNINGS
161 #warning temporarily disabled
162         // for (const_iterator it = begin(); it != end(); ++it)
163         //      it->nucleus()->replace(rep);
164 #endif
165 }
166
167
168 bool MathArray::find1(MathArray const & ar, size_type pos) const
169 {
170         lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
171         for (size_type i = 0, n = ar.size(); i < n; ++i)
172                 if (asString(operator[](pos + i)) != asString(ar[i]))
173                         return false;
174         return true;
175 }
176
177
178 MathArray::size_type MathArray::find(MathArray const & ar) const
179 {
180         for (int i = 0, last = size() - ar.size(); i < last; ++i)
181                 if (find1(ar, i))
182                         return i;
183         return size();
184 }
185
186
187 MathArray::size_type MathArray::find_last(MathArray const & ar) const
188 {
189         for (int i = size() - ar.size(); i >= 0; --i)
190                 if (find1(ar, i))
191                         return i;
192         return size();
193 }
194
195
196 bool MathArray::contains(MathArray const & ar) const
197 {
198         if (find(ar) != size())
199                 return true;
200         for (const_iterator it = begin(); it != end(); ++it)
201                 if ((*it)->contains(ar))
202                         return true;
203         return false;
204 }
205
206
207 void MathArray::touch() const
208 {
209 }
210
211
212 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
213 {
214         metrics(mi);
215         dim = dim_;
216 }
217
218
219 namespace {
220
221 bool isInside(DocIterator const & it, MathArray const & ar,
222         lyx::pos_type p1, lyx::pos_type p2)
223 {
224         for (size_t i = 0; i != it.size(); ++i) {
225                 CursorSlice const & sl = it[i];
226                 if (sl.inset().inMathed() && &sl.cell() == &ar)
227                         return p1 <= sl.pos() && sl.pos() < p2;
228         }
229         return false;
230 }
231
232 }
233
234
235
236 void MathArray::metrics(MetricsInfo & mi) const
237 {
238         mathed_char_dim(mi.base.font, 'I', dim_);
239
240         if (empty())
241                 return;
242
243         dim_.wid = 0;
244         Dimension d;
245         //BufferView & bv  = *mi.base.bv;
246         //Buffer const & buf = *bv.buffer();
247         for (size_t i = 0, n = size(); i != n; ++i) {
248                 MathAtom const & at = operator[](i);
249 #if 0
250                 MathMacro const * mac = at->asMacro();
251                 if (mac && buf.hasMacro(mac->name())) {
252                         MacroData const & tmpl = buf.getMacro(mac->name());
253                         int numargs = tmpl.numargs();
254                         if (i + numargs > n)
255                                 numargs = n - i - 1;
256                         lyxerr << "metrics:found macro: " << mac->name()
257                                 << " numargs: " << numargs << endl;
258                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
259                                 MathArray args(begin() + i + 1, begin() + i + numargs + 1);
260                                 MathArray exp;
261                                 tmpl.expand(args, exp);
262                                 mac->setExpansion(exp, args);
263                                 mac->metricsExpanded(mi, d);
264                                 dim_.wid += mac->widthExpanded();
265                                 i += numargs;
266                                 continue;
267                         }
268                 }
269 #endif
270                 at->metrics(mi, d);
271                 dim_ += d;
272         }
273 }
274
275
276 void MathArray::draw(PainterInfo & pi, int x, int y) const
277 {
278         //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
279         setXY(x, y);
280
281         if (empty()) {
282                 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
283                 return;
284         }
285
286         // don't draw outside the workarea
287         if (y + descent() <= 0
288                 || y - ascent() >= pi.pain.paperHeight()
289                 || x + width() <= 0
290                 || x >= pi.pain.paperWidth())
291                 return;
292
293         //BufferView & bv  = *pi.base.bv;
294         for (size_t i = 0, n = size(); i != n; ++i) {
295                 MathAtom const & at = operator[](i);
296 #if 0
297         Buffer const & buf = *bv.buffer();
298                 // special macro handling
299                 MathMacro const * mac = at->asMacro();
300                 if (mac && buf.hasMacro(mac->name())) {
301                         MacroData const & tmpl = buf.getMacro(mac->name());
302                         int numargs = tmpl.numargs();
303                         if (i + numargs > n)
304                                 numargs = n - i - 1;
305                         if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
306                                 mac->drawExpanded(pi, x, y);
307                                 x += mac->widthExpanded();
308                                 i += numargs;
309                                 continue;
310                         }
311                 }
312 #endif
313                 at->drawSelection(pi, x, y);
314                 at->draw(pi, x, y);
315                 x += at->width();
316         }
317 }
318
319
320 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
321 {
322         dim.clear();
323         Dimension d;
324         for (const_iterator it = begin(); it != end(); ++it) {
325                 (*it)->metricsT(mi, d);
326                 dim += d;
327         }
328 }
329
330
331 void MathArray::drawT(TextPainter & pain, int x, int y) const
332 {
333         //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
334         setXY(x, y);
335
336         for (const_iterator it = begin(), et = end(); it != et; ++it) {
337                 (*it)->drawT(pain, x, y);
338                 //x += (*it)->width_;
339                 x += 2;
340         }
341 }
342
343
344 int MathArray::pos2x(size_type pos) const
345 {
346         return pos2x(pos, 0);
347 }
348
349
350 int MathArray::pos2x(size_type pos, int glue) const
351 {
352         int x = 0;
353         size_type target = min(pos, size());
354         for (size_type i = 0; i < target; ++i) {
355                 const_iterator it = begin() + i;
356                 if ((*it)->getChar() == ' ')
357                         x += glue;
358                 //lyxerr << "char: " << (*it)->getChar()
359                 //      << "width: " << (*it)->width() << std::endl;
360                 x += (*it)->width();
361         }
362         return x;
363 }
364
365
366 MathArray::size_type MathArray::x2pos(int targetx) const
367 {
368         return x2pos(targetx, 0);
369 }
370
371
372 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
373 {
374         const_iterator it = begin();
375         int lastx = 0;
376         int currx = 0;
377         for (; currx < targetx && it < end(); ++it) {
378                 lastx = currx;
379                 if ((*it)->getChar() == ' ')
380                         currx += glue;
381                 currx += (*it)->width();
382         }
383         if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
384                 --it;
385         return it - begin();
386 }
387
388
389 int MathArray::dist(int x, int y) const
390 {
391         int xx = 0;
392         int yy = 0;
393
394         const int xo_ = xo();
395         const int yo_ = yo();
396
397         if (x < xo_)
398                 xx = xo_ - x;
399         else if (x > xo_ + width())
400                 xx = x - xo_ - width();
401
402         if (y < yo_ - ascent())
403                 yy = yo_ - ascent() - y;
404         else if (y > yo_ + descent())
405                 yy = y - yo_ - descent();
406
407         return xx + yy;
408 }
409
410
411 void MathArray::setXY(int x, int y) const
412 {
413         //lyxerr << "setting position cache for MathArray " << this << std::endl;
414         theCoords.arrays_.add(this, x, y);
415 }
416
417
418 int MathArray::xo() const
419 {
420         return theCoords.arrays_.x(this);
421 }
422
423
424 int MathArray::yo() const
425 {
426         return theCoords.arrays_.y(this);
427 }
428