5 #include "math_inset.h"
6 #include "math_cursor.h"
7 #include "math_deliminset.h"
8 #include "math_fontinset.h"
9 #include "math_scriptinset.h"
10 #include "math_mathmlstream.h"
11 #include "math_support.h"
12 #include "math_replace.h"
14 #include "support/LAssert.h"
15 #include "metricsinfo.h"
16 #include "frontends/Painter.h"
17 #include "textpainter.h"
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[](size_type pos)
45 lyx::Assert(pos < size());
46 return base_type::operator[](pos);
50 MathAtom const & MathArray::operator[](size_type pos) const
52 lyx::Assert(pos < size());
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 lyx::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 Dimension const & MathArray::metrics(MetricsInfo & mi) const
214 mathed_char_dim(mi.base.font, 'I', dim_);
219 for (const_iterator it = begin(), et = end(); it != et; ++it) {
221 dim_ += (*it)->dimensions();
227 void MathArray::draw(PainterInfo & pi, int x, int y) const
229 //if (drawn_ && x == xo_ && y == yo_)
231 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
237 if (y + descent() <= 0) // don't draw above the workarea
239 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
241 if (x + width() <= 0) // don't draw left of workarea
243 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
247 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
251 for (const_iterator it = begin(), et = end(); it != et; ++it) {
252 (*it)->draw(pi, x, y);
258 Dimension const & MathArray::metricsT(TextMetricsInfo const & mi) const
263 for (const_iterator it = begin(); it != end(); ++it) {
265 dim_ += (*it)->dimensions();
271 void MathArray::drawT(TextPainter & pain, int x, int y) const
273 //if (drawn_ && x == xo_ && y == yo_)
275 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
280 for (const_iterator it = begin(), et = end(); it != et; ++it) {
281 (*it)->drawT(pain, x, y);
287 int MathArray::pos2x(size_type pos) const
289 return pos2x(pos, 0);
293 int MathArray::pos2x(size_type pos, int glue) const
296 size_type target = min(pos, size());
297 for (size_type i = 0; i < target; ++i) {
298 const_iterator it = begin() + i;
299 if ((*it)->getChar() == ' ')
307 MathArray::size_type MathArray::x2pos(int targetx) const
309 return x2pos(targetx, 0);
313 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
315 const_iterator it = begin();
318 for (; currx < targetx && it < end(); ++it) {
320 if ((*it)->getChar() == ' ')
322 currx += (*it)->width();
324 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
330 int MathArray::dist(int x, int y) const
337 else if (x > xo_ + width())
338 xx = x - xo_ - width();
340 if (y < yo_ - ascent())
341 yy = yo_ - ascent() - y;
342 else if (y > yo_ + descent())
343 yy = y - yo_ - descent();
349 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
354 y2 = yo_ + descent();
358 void MathArray::center(int & x, int & y) const
360 x = xo_ + width() / 2;
361 y = yo_ + (descent() - ascent()) / 2;
365 void MathArray::towards(int & x, int & y) const
372 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
374 x = cx + int(r * (x - cx));
375 y = cy + int(r * (y - cy));
379 void MathArray::setXY(int x, int y) const
386 void MathArray::notifyCursorLeaves()
390 // remove base-only "scripts"
391 for (pos_type i = 0; i + 1 < size(); ++i) {
392 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
393 if (p && p->cell(0).empty() && p->cell(1).empty()) {
394 MathArray ar = p->nuc();
397 mathcursor->adjust(i, ar.size() - 1);
401 // glue adjacent font insets of the same kind
402 for (pos_type i = 0; i + 1 < size(); ++i) {
403 MathFontInset * p = operator[](i).nucleus()->asFontInset();
404 MathFontInset const * q = operator[](i + 1)->asFontInset();
405 if (p && q && p->name() == q->name()) {
406 p->cell(0).append(q->cell(0));
408 mathcursor->adjust(i, -1);