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 "frontends/Painter.h"
16 #include "textpainter.h"
24 MathArray::MathArray()
25 : xo_(0), yo_(0), clean_(false), drawn_(false)
29 MathArray::MathArray(const_iterator from, const_iterator to)
30 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
34 void MathArray::substitute(MathMacro const & m)
36 for (iterator it = begin(); it != end(); ++it)
37 it->nucleus()->substitute(m);
41 MathAtom & MathArray::operator[](size_type pos)
43 lyx::Assert(pos < size());
44 return base_type::operator[](pos);
48 MathAtom const & MathArray::operator[](size_type pos) const
50 lyx::Assert(pos < size());
51 return base_type::operator[](pos);
55 void MathArray::insert(size_type pos, MathAtom const & t)
57 base_type::insert(begin() + pos, t);
61 void MathArray::insert(size_type pos, MathArray const & ar)
63 lyx::Assert(pos <= size());
64 base_type::insert(begin() + pos, ar.begin(), ar.end());
68 void MathArray::append(MathArray const & ar)
74 void MathArray::erase(size_type pos)
81 void MathArray::erase(iterator pos1, iterator pos2)
83 base_type::erase(pos1, pos2);
87 void MathArray::erase(iterator pos)
89 base_type::erase(pos);
93 void MathArray::erase(size_type pos1, size_type pos2)
95 base_type::erase(begin() + pos1, begin() + pos2);
99 void MathArray::dump2() const
101 NormalStream ns(lyxerr);
102 for (const_iterator it = begin(); it != end(); ++it)
107 void MathArray::dump() const
109 NormalStream ns(lyxerr);
110 for (const_iterator it = begin(); it != end(); ++it)
111 ns << '<' << *it << '>';
115 void MathArray::validate(LaTeXFeatures & features) const
117 for (const_iterator it = begin(); it != end(); ++it)
118 (*it)->validate(features);
122 bool MathArray::match(MathArray const & ar) const
124 return size() == ar.size() && matchpart(ar, 0);
128 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
130 if (size() < ar.size() + pos)
132 const_iterator it = begin() + pos;
133 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
134 if (!(*jt)->match(*it))
140 void MathArray::replace(ReplaceData & rep)
142 for (size_type i = 0; i < size(); ++i) {
143 if (find1(rep.from, i)) {
145 lyxerr << "match found!\n";
146 erase(i, i + rep.from.size());
152 #warning temporarily disabled
153 // for (const_iterator it = begin(); it != end(); ++it)
154 // it->nucleus()->replace(rep);
159 bool MathArray::find1(MathArray const & ar, size_type pos) const
161 //lyxerr << "finding '" << ar << "' in '" << *this << "'\n";
162 for (size_type i = 0, n = ar.size(); i < n; ++i)
163 if (!operator[](pos + i)->match(ar[i]))
169 MathArray::size_type MathArray::find(MathArray const & ar) const
171 for (int i = 0, last = size() - ar.size(); i < last; ++i)
178 MathArray::size_type MathArray::find_last(MathArray const & ar) const
180 for (int i = size() - ar.size(); i >= 0; --i)
187 bool MathArray::contains(MathArray const & ar) const
189 if (find(ar) != size())
191 for (const_iterator it = begin(); it != end(); ++it)
192 if ((*it)->contains(ar))
198 void MathArray::touch() const
205 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
212 void MathArray::metrics(MetricsInfo & mi) const
219 mathed_char_dim(mi.base.font, 'I', dim_);
223 for (const_iterator it = begin(), et = end(); it != et; ++it) {
224 Dimension d = (*it)->metrics(mi);
232 void MathArray::draw(PainterInfo & pi, int x, int y) const
234 //if (drawn_ && x == xo_ && y == yo_)
236 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
242 if (y + descent() <= 0) // don't draw above the workarea
244 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
246 if (x + width() <= 0) // don't draw left of workarea
248 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
252 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
256 for (const_iterator it = begin(), et = end(); it != et; ++it) {
257 pi.width = it->width_;
258 (*it)->draw(pi, x, y);
264 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
270 for (const_iterator it = begin(); it != end(); ++it) {
271 (*it)->metricsT(mi, d);
277 void MathArray::drawT(TextPainter & pain, int x, int y) const
279 //if (drawn_ && x == xo_ && y == yo_)
281 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
286 for (const_iterator it = begin(), et = end(); it != et; ++it) {
287 (*it)->drawT(pain, x, y);
293 int MathArray::pos2x(size_type pos) const
295 return pos2x(pos, 0);
299 int MathArray::pos2x(size_type pos, int glue) const
302 size_type target = min(pos, size());
303 for (size_type i = 0; i < target; ++i) {
304 const_iterator it = begin() + i;
305 if ((*it)->getChar() == ' ')
313 MathArray::size_type MathArray::x2pos(int targetx) const
315 return x2pos(targetx, 0);
319 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
321 const_iterator it = begin();
324 for (; currx < targetx && it < end(); ++it) {
326 if ((*it)->getChar() == ' ')
330 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
336 int MathArray::dist(int x, int y) const
343 else if (x > xo_ + width())
344 xx = x - xo_ - width();
346 if (y < yo_ - ascent())
347 yy = yo_ - ascent() - y;
348 else if (y > yo_ + descent())
349 yy = y - yo_ - descent();
355 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
360 y2 = yo_ + descent();
364 void MathArray::center(int & x, int & y) const
366 x = xo_ + width() / 2;
367 y = yo_ + (descent() - ascent()) / 2;
371 void MathArray::towards(int & x, int & y) const
378 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
380 x = cx + int(r * (x - cx));
381 y = cy + int(r * (y - cy));
385 void MathArray::setXY(int x, int y) const
392 void MathArray::notifyCursorLeaves()
396 // remove base-only "scripts"
397 for (pos_type i = 0; i + 1 < size(); ++i) {
398 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
399 if (p && p->cell(0).empty() && p->cell(1).empty()) {
400 MathArray ar = p->nuc();
403 mathcursor->adjust(i, ar.size() - 1);
407 // glue adjacent font insets of the same kind
408 for (pos_type i = 0; i + 1 < size(); ++i) {
409 MathFontInset * p = operator[](i).nucleus()->asFontInset();
410 MathFontInset const * q = operator[](i + 1)->asFontInset();
411 if (p && q && p->name() == q->name()) {
412 p->cell(0).append(q->cell(0));
414 mathcursor->adjust(i, -1);