3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "math_cursor.h"
14 #include "math_fontinset.h"
15 #include "math_scriptinset.h"
16 #include "math_mathmlstream.h"
17 #include "math_support.h"
18 #include "math_replace.h"
20 #include "support/LAssert.h"
21 #include "frontends/Painter.h"
23 using namespace lyx::support;
31 MathArray::MathArray()
32 : xo_(0), yo_(0), clean_(false), drawn_(false)
36 MathArray::MathArray(const_iterator from, const_iterator to)
37 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
41 void MathArray::substitute(MathMacro const & m)
43 for (iterator it = begin(); it != end(); ++it)
44 it->nucleus()->substitute(m);
48 MathAtom & MathArray::operator[](pos_type pos)
51 return base_type::operator[](pos);
55 MathAtom const & MathArray::operator[](pos_type pos) const
58 return base_type::operator[](pos);
62 void MathArray::insert(size_type pos, MathAtom const & t)
64 base_type::insert(begin() + pos, t);
68 void MathArray::insert(size_type pos, MathArray const & ar)
70 Assert(pos <= size());
71 base_type::insert(begin() + pos, ar.begin(), ar.end());
75 void MathArray::append(MathArray const & ar)
81 void MathArray::erase(size_type pos)
88 void MathArray::erase(iterator pos1, iterator pos2)
90 base_type::erase(pos1, pos2);
94 void MathArray::erase(iterator pos)
96 base_type::erase(pos);
100 void MathArray::erase(size_type pos1, size_type pos2)
102 base_type::erase(begin() + pos1, begin() + pos2);
106 void MathArray::dump2() const
108 NormalStream ns(lyxerr);
109 for (const_iterator it = begin(); it != end(); ++it)
114 void MathArray::dump() const
116 NormalStream ns(lyxerr);
117 for (const_iterator it = begin(); it != end(); ++it)
118 ns << '<' << *it << '>';
122 void MathArray::validate(LaTeXFeatures & features) const
124 for (const_iterator it = begin(); it != end(); ++it)
125 (*it)->validate(features);
129 bool MathArray::match(MathArray const & ar) const
131 return size() == ar.size() && matchpart(ar, 0);
135 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
137 if (size() < ar.size() + pos)
139 const_iterator it = begin() + pos;
140 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
141 if (!(*jt)->match(*it))
147 void MathArray::replace(ReplaceData & rep)
149 for (size_type i = 0; i < size(); ++i) {
150 if (find1(rep.from, i)) {
152 lyxerr << "match found!" << endl;
153 erase(i, i + rep.from.size());
159 #warning temporarily disabled
160 // for (const_iterator it = begin(); it != end(); ++it)
161 // it->nucleus()->replace(rep);
166 bool MathArray::find1(MathArray const & ar, size_type pos) const
168 //lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
169 for (size_type i = 0, n = ar.size(); i < n; ++i)
170 if (!operator[](pos + i)->match(ar[i]))
176 MathArray::size_type MathArray::find(MathArray const & ar) const
178 for (int i = 0, last = size() - ar.size(); i < last; ++i)
185 MathArray::size_type MathArray::find_last(MathArray const & ar) const
187 for (int i = size() - ar.size(); i >= 0; --i)
194 bool MathArray::contains(MathArray const & ar) const
196 if (find(ar) != size())
198 for (const_iterator it = begin(); it != end(); ++it)
199 if ((*it)->contains(ar))
205 void MathArray::touch() const
212 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
219 void MathArray::metrics(MetricsInfo & mi) const
226 mathed_char_dim(mi.base.font, 'I', dim_);
231 for (const_iterator it = begin(), et = end(); it != et; ++it) {
232 (*it)->metrics(mi, d);
240 void MathArray::draw(PainterInfo & pi, int x, int y) const
242 //if (drawn_ && x == xo_ && y == yo_)
244 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
250 if (y + descent() <= 0) // don't draw above the workarea
252 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
254 if (x + width() <= 0) // don't draw left of workarea
256 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
260 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
264 for (const_iterator it = begin(), et = end(); it != et; ++it) {
265 pi.width = it->width_;
266 (*it)->draw(pi, x, y);
272 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
278 for (const_iterator it = begin(); it != end(); ++it) {
279 (*it)->metricsT(mi, d);
285 void MathArray::drawT(TextPainter & pain, int x, int y) const
287 //if (drawn_ && x == xo_ && y == yo_)
289 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
294 for (const_iterator it = begin(), et = end(); it != et; ++it) {
295 (*it)->drawT(pain, x, y);
301 int MathArray::pos2x(size_type pos) const
303 return pos2x(pos, 0);
307 int MathArray::pos2x(size_type pos, int glue) const
310 size_type target = min(pos, size());
311 for (size_type i = 0; i < target; ++i) {
312 const_iterator it = begin() + i;
313 if ((*it)->getChar() == ' ')
321 MathArray::size_type MathArray::x2pos(int targetx) const
323 return x2pos(targetx, 0);
327 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
329 const_iterator it = begin();
332 for (; currx < targetx && it < end(); ++it) {
334 if ((*it)->getChar() == ' ')
338 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
344 int MathArray::dist(int x, int y) const
351 else if (x > xo_ + width())
352 xx = x - xo_ - width();
354 if (y < yo_ - ascent())
355 yy = yo_ - ascent() - y;
356 else if (y > yo_ + descent())
357 yy = y - yo_ - descent();
363 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
368 y2 = yo_ + descent();
372 void MathArray::center(int & x, int & y) const
374 x = xo_ + width() / 2;
375 y = yo_ + (descent() - ascent()) / 2;
379 void MathArray::towards(int & x, int & y) const
386 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
388 x = cx + int(r * (x - cx));
389 y = cy + int(r * (y - cy));
393 void MathArray::setXY(int x, int y) const
400 void MathArray::notifyCursorLeaves()
404 // remove base-only "scripts"
405 for (pos_type i = 0; i + 1 < size(); ++i) {
406 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
407 if (p && p->cell(0).empty() && p->cell(1).empty()) {
408 MathArray ar = p->nuc();
411 mathcursor->adjust(i, ar.size() - 1);
415 // glue adjacent font insets of the same kind
416 for (pos_type i = 0; i + 1 < size(); ++i) {
417 MathFontInset * p = operator[](i).nucleus()->asFontInset();
418 MathFontInset const * q = operator[](i + 1)->asFontInset();
419 if (p && q && p->name() == q->name()) {
420 p->cell(0).append(q->cell(0));
422 mathcursor->adjust(i, -1);