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"
19 using namespace lyx::support;
27 MathArray::MathArray()
28 : xo_(0), yo_(0), clean_(false), drawn_(false)
32 MathArray::MathArray(const_iterator from, const_iterator to)
33 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
37 void MathArray::substitute(MathMacro const & m)
39 for (iterator it = begin(); it != end(); ++it)
40 it->nucleus()->substitute(m);
44 MathAtom & MathArray::operator[](pos_type pos)
47 return base_type::operator[](pos);
51 MathAtom const & MathArray::operator[](pos_type pos) const
54 return base_type::operator[](pos);
58 void MathArray::insert(size_type pos, MathAtom const & t)
60 base_type::insert(begin() + pos, t);
64 void MathArray::insert(size_type pos, MathArray const & ar)
66 Assert(pos <= size());
67 base_type::insert(begin() + pos, ar.begin(), ar.end());
71 void MathArray::append(MathArray const & ar)
77 void MathArray::erase(size_type pos)
84 void MathArray::erase(iterator pos1, iterator pos2)
86 base_type::erase(pos1, pos2);
90 void MathArray::erase(iterator pos)
92 base_type::erase(pos);
96 void MathArray::erase(size_type pos1, size_type pos2)
98 base_type::erase(begin() + pos1, begin() + pos2);
102 void MathArray::dump2() const
104 NormalStream ns(lyxerr);
105 for (const_iterator it = begin(); it != end(); ++it)
110 void MathArray::dump() const
112 NormalStream ns(lyxerr);
113 for (const_iterator it = begin(); it != end(); ++it)
114 ns << '<' << *it << '>';
118 void MathArray::validate(LaTeXFeatures & features) const
120 for (const_iterator it = begin(); it != end(); ++it)
121 (*it)->validate(features);
125 bool MathArray::match(MathArray const & ar) const
127 return size() == ar.size() && matchpart(ar, 0);
131 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
133 if (size() < ar.size() + pos)
135 const_iterator it = begin() + pos;
136 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
137 if (!(*jt)->match(*it))
143 void MathArray::replace(ReplaceData & rep)
145 for (size_type i = 0; i < size(); ++i) {
146 if (find1(rep.from, i)) {
148 lyxerr << "match found!" << endl;
149 erase(i, i + rep.from.size());
155 #warning temporarily disabled
156 // for (const_iterator it = begin(); it != end(); ++it)
157 // it->nucleus()->replace(rep);
162 bool MathArray::find1(MathArray const & ar, size_type pos) const
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]))
172 MathArray::size_type MathArray::find(MathArray const & ar) const
174 for (int i = 0, last = size() - ar.size(); i < last; ++i)
181 MathArray::size_type MathArray::find_last(MathArray const & ar) const
183 for (int i = size() - ar.size(); i >= 0; --i)
190 bool MathArray::contains(MathArray const & ar) const
192 if (find(ar) != size())
194 for (const_iterator it = begin(); it != end(); ++it)
195 if ((*it)->contains(ar))
201 void MathArray::touch() const
208 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
215 void MathArray::metrics(MetricsInfo & mi) const
222 mathed_char_dim(mi.base.font, 'I', dim_);
227 for (const_iterator it = begin(), et = end(); it != et; ++it) {
228 (*it)->metrics(mi, d);
236 void MathArray::draw(PainterInfo & pi, int x, int y) const
238 //if (drawn_ && x == xo_ && y == yo_)
240 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
246 if (y + descent() <= 0) // don't draw above the workarea
248 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
250 if (x + width() <= 0) // don't draw left of workarea
252 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
256 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
260 for (const_iterator it = begin(), et = end(); it != et; ++it) {
261 pi.width = it->width_;
262 (*it)->draw(pi, x, y);
268 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
274 for (const_iterator it = begin(); it != end(); ++it) {
275 (*it)->metricsT(mi, d);
281 void MathArray::drawT(TextPainter & pain, int x, int y) const
283 //if (drawn_ && x == xo_ && y == yo_)
285 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
290 for (const_iterator it = begin(), et = end(); it != et; ++it) {
291 (*it)->drawT(pain, x, y);
297 int MathArray::pos2x(size_type pos) const
299 return pos2x(pos, 0);
303 int MathArray::pos2x(size_type pos, int glue) const
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() == ' ')
317 MathArray::size_type MathArray::x2pos(int targetx) const
319 return x2pos(targetx, 0);
323 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
325 const_iterator it = begin();
328 for (; currx < targetx && it < end(); ++it) {
330 if ((*it)->getChar() == ' ')
334 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
340 int MathArray::dist(int x, int y) const
347 else if (x > xo_ + width())
348 xx = x - xo_ - width();
350 if (y < yo_ - ascent())
351 yy = yo_ - ascent() - y;
352 else if (y > yo_ + descent())
353 yy = y - yo_ - descent();
359 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
364 y2 = yo_ + descent();
368 void MathArray::center(int & x, int & y) const
370 x = xo_ + width() / 2;
371 y = yo_ + (descent() - ascent()) / 2;
375 void MathArray::towards(int & x, int & y) const
382 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
384 x = cx + int(r * (x - cx));
385 y = cy + int(r * (y - cy));
389 void MathArray::setXY(int x, int y) const
396 void MathArray::notifyCursorLeaves()
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();
407 mathcursor->adjust(i, ar.size() - 1);
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));
418 mathcursor->adjust(i, -1);