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"
13 #include "support/LAssert.h"
14 #include "metricsinfo.h"
15 #include "math_data.h"
16 #include "frontends/Painter.h"
17 #include "textpainter.h"
25 MathArray::MathArray()
26 : xo_(0), yo_(0), clean_(false), drawn_(false)
30 MathArray::MathArray(const_iterator from, const_iterator to)
31 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
35 void MathArray::substitute(MathMacro const & m)
37 for (iterator it = begin(); it != end(); ++it)
38 it->nucleus()->substitute(m);
42 MathAtom & MathArray::operator[](size_type pos)
44 lyx::Assert(pos < size());
45 return base_type::operator[](pos);
49 MathAtom const & MathArray::operator[](size_type pos) const
51 lyx::Assert(pos < size());
52 return base_type::operator[](pos);
56 void MathArray::insert(size_type pos, MathAtom const & t)
58 base_type::insert(begin() + pos, t);
62 void MathArray::insert(size_type pos, MathArray const & ar)
64 lyx::Assert(pos <= size());
65 base_type::insert(begin() + pos, ar.begin(), ar.end());
69 void MathArray::append(MathArray const & ar)
75 void MathArray::erase(size_type pos)
82 void MathArray::erase(iterator pos1, iterator pos2)
84 base_type::erase(pos1, pos2);
88 void MathArray::erase(iterator pos)
90 base_type::erase(pos);
94 void MathArray::erase(size_type pos1, size_type pos2)
96 base_type::erase(begin() + pos1, begin() + pos2);
100 void MathArray::dump2() const
102 NormalStream ns(lyxerr);
103 for (const_iterator it = begin(); it != end(); ++it)
108 void MathArray::dump() const
110 NormalStream ns(lyxerr);
111 for (const_iterator it = begin(); it != end(); ++it)
112 ns << '<' << *it << '>';
116 void MathArray::validate(LaTeXFeatures & features) const
118 for (const_iterator it = begin(); it != end(); ++it)
119 (*it)->validate(features);
123 bool MathArray::match(MathArray const & ar) const
125 return size() == ar.size() && matchpart(ar, 0);
129 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
131 if (size() < ar.size() + pos)
133 const_iterator it = begin() + pos;
134 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
135 if (!(*jt)->match(*it))
141 void MathArray::replace(ReplaceData & rep)
143 for (size_type i = 0; i < size(); ++i) {
144 if (find1(rep.from, i)) {
146 lyxerr << "match found!\n";
147 erase(i, i + rep.from.size());
153 #warning temporarily disabled
154 // for (const_iterator it = begin(); it != end(); ++it)
155 // it->nucleus()->replace(rep);
160 bool MathArray::find1(MathArray const & ar, size_type pos) const
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]))
170 MathArray::size_type MathArray::find(MathArray const & ar) const
172 for (int i = 0, last = size() - ar.size(); i < last; ++i)
179 MathArray::size_type MathArray::find_last(MathArray const & ar) const
181 for (int i = size() - ar.size(); i >= 0; --i)
188 bool MathArray::contains(MathArray const & ar) const
190 if (find(ar) != size())
192 for (const_iterator it = begin(); it != end(); ++it)
193 if ((*it)->contains(ar))
199 void MathArray::touch() const
206 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
213 void MathArray::metrics(MetricsInfo & mi) const
220 mathed_char_dim(mi.base.font, 'I', dim_);
225 for (const_iterator it = begin(), et = end(); it != et; ++it) {
226 (*it)->metrics(mi, d);
234 void MathArray::draw(PainterInfo & pi, int x, int y) const
236 //if (drawn_ && x == xo_ && y == yo_)
238 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
244 if (y + descent() <= 0) // don't draw above the workarea
246 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
248 if (x + width() <= 0) // don't draw left of workarea
250 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
254 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
258 for (const_iterator it = begin(), et = end(); it != et; ++it) {
259 pi.width = it->width_;
260 (*it)->draw(pi, x, y);
266 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
272 for (const_iterator it = begin(); it != end(); ++it) {
273 (*it)->metricsT(mi, d);
279 void MathArray::drawT(TextPainter & pain, int x, int y) const
281 //if (drawn_ && x == xo_ && y == yo_)
283 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
288 for (const_iterator it = begin(), et = end(); it != et; ++it) {
289 (*it)->drawT(pain, x, y);
295 int MathArray::pos2x(size_type pos) const
297 return pos2x(pos, 0);
301 int MathArray::pos2x(size_type pos, int glue) const
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() == ' ')
315 MathArray::size_type MathArray::x2pos(int targetx) const
317 return x2pos(targetx, 0);
321 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
323 const_iterator it = begin();
326 for (; currx < targetx && it < end(); ++it) {
328 if ((*it)->getChar() == ' ')
332 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
338 int MathArray::dist(int x, int y) const
345 else if (x > xo_ + width())
346 xx = x - xo_ - width();
348 if (y < yo_ - ascent())
349 yy = yo_ - ascent() - y;
350 else if (y > yo_ + descent())
351 yy = y - yo_ - descent();
357 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
362 y2 = yo_ + descent();
366 void MathArray::center(int & x, int & y) const
368 x = xo_ + width() / 2;
369 y = yo_ + (descent() - ascent()) / 2;
373 void MathArray::towards(int & x, int & y) const
380 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
382 x = cx + int(r * (x - cx));
383 y = cy + int(r * (y - cy));
387 void MathArray::setXY(int x, int y) const
394 void MathArray::notifyCursorLeaves()
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();
405 mathcursor->adjust(i, ar.size() - 1);
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));
416 mathcursor->adjust(i, -1);