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