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