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;
26 MathArray::MathArray()
27 : xo_(0), yo_(0), clean_(false), drawn_(false)
31 MathArray::MathArray(const_iterator from, const_iterator to)
32 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
36 void MathArray::substitute(MathMacro const & m)
38 for (iterator it = begin(); it != end(); ++it)
39 it->nucleus()->substitute(m);
43 MathAtom & MathArray::operator[](pos_type pos)
46 return base_type::operator[](pos);
50 MathAtom const & MathArray::operator[](pos_type pos) const
53 return base_type::operator[](pos);
57 void MathArray::insert(size_type pos, MathAtom const & t)
59 base_type::insert(begin() + pos, t);
63 void MathArray::insert(size_type pos, MathArray const & ar)
65 Assert(pos <= size());
66 base_type::insert(begin() + pos, ar.begin(), ar.end());
70 void MathArray::append(MathArray const & ar)
76 void MathArray::erase(size_type pos)
83 void MathArray::erase(iterator pos1, iterator pos2)
85 base_type::erase(pos1, pos2);
89 void MathArray::erase(iterator pos)
91 base_type::erase(pos);
95 void MathArray::erase(size_type pos1, size_type pos2)
97 base_type::erase(begin() + pos1, begin() + pos2);
101 void MathArray::dump2() const
103 NormalStream ns(lyxerr);
104 for (const_iterator it = begin(); it != end(); ++it)
109 void MathArray::dump() const
111 NormalStream ns(lyxerr);
112 for (const_iterator it = begin(); it != end(); ++it)
113 ns << '<' << *it << '>';
117 void MathArray::validate(LaTeXFeatures & features) const
119 for (const_iterator it = begin(); it != end(); ++it)
120 (*it)->validate(features);
124 bool MathArray::match(MathArray const & ar) const
126 return size() == ar.size() && matchpart(ar, 0);
130 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
132 if (size() < ar.size() + pos)
134 const_iterator it = begin() + pos;
135 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
136 if (!(*jt)->match(*it))
142 void MathArray::replace(ReplaceData & rep)
144 for (size_type i = 0; i < size(); ++i) {
145 if (find1(rep.from, i)) {
147 lyxerr << "match found!\n";
148 erase(i, i + rep.from.size());
154 #warning temporarily disabled
155 // for (const_iterator it = begin(); it != end(); ++it)
156 // it->nucleus()->replace(rep);
161 bool MathArray::find1(MathArray const & ar, size_type pos) const
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]))
171 MathArray::size_type MathArray::find(MathArray const & ar) const
173 for (int i = 0, last = size() - ar.size(); i < last; ++i)
180 MathArray::size_type MathArray::find_last(MathArray const & ar) const
182 for (int i = size() - ar.size(); i >= 0; --i)
189 bool MathArray::contains(MathArray const & ar) const
191 if (find(ar) != size())
193 for (const_iterator it = begin(); it != end(); ++it)
194 if ((*it)->contains(ar))
200 void MathArray::touch() const
207 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
214 void MathArray::metrics(MetricsInfo & mi) const
221 mathed_char_dim(mi.base.font, 'I', dim_);
226 for (const_iterator it = begin(), et = end(); it != et; ++it) {
227 (*it)->metrics(mi, d);
235 void MathArray::draw(PainterInfo & pi, int x, int y) const
237 //if (drawn_ && x == xo_ && y == yo_)
239 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
245 if (y + descent() <= 0) // don't draw above the workarea
247 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
249 if (x + width() <= 0) // don't draw left of workarea
251 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
255 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
259 for (const_iterator it = begin(), et = end(); it != et; ++it) {
260 pi.width = it->width_;
261 (*it)->draw(pi, x, y);
267 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
273 for (const_iterator it = begin(); it != end(); ++it) {
274 (*it)->metricsT(mi, d);
280 void MathArray::drawT(TextPainter & pain, int x, int y) const
282 //if (drawn_ && x == xo_ && y == yo_)
284 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
289 for (const_iterator it = begin(), et = end(); it != et; ++it) {
290 (*it)->drawT(pain, x, y);
296 int MathArray::pos2x(size_type pos) const
298 return pos2x(pos, 0);
302 int MathArray::pos2x(size_type pos, int glue) const
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() == ' ')
316 MathArray::size_type MathArray::x2pos(int targetx) const
318 return x2pos(targetx, 0);
322 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
324 const_iterator it = begin();
327 for (; currx < targetx && it < end(); ++it) {
329 if ((*it)->getChar() == ' ')
333 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
339 int MathArray::dist(int x, int y) const
346 else if (x > xo_ + width())
347 xx = x - xo_ - width();
349 if (y < yo_ - ascent())
350 yy = yo_ - ascent() - y;
351 else if (y > yo_ + descent())
352 yy = y - yo_ - descent();
358 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
363 y2 = yo_ + descent();
367 void MathArray::center(int & x, int & y) const
369 x = xo_ + width() / 2;
370 y = yo_ + (descent() - ascent()) / 2;
374 void MathArray::towards(int & x, int & y) const
381 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
383 x = cx + int(r * (x - cx));
384 y = cy + int(r * (y - cy));
388 void MathArray::setXY(int x, int y) const
395 void MathArray::notifyCursorLeaves()
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();
406 mathcursor->adjust(i, ar.size() - 1);
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));
417 mathcursor->adjust(i, -1);