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