3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "math_data.h"
14 #include "math_cursor.h"
15 #include "math_fontinset.h"
16 #include "math_scriptinset.h"
17 #include "math_mathmlstream.h"
18 #include "math_support.h"
19 #include "math_replace.h"
23 #include "frontends/Painter.h"
25 #include <boost/assert.hpp>
30 using std::ostringstream;
33 MathArray::MathArray()
34 : xo_(0), yo_(0), clean_(false), drawn_(false)
38 MathArray::MathArray(const_iterator from, const_iterator to)
39 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
43 void MathArray::substitute(MathMacro const & m)
45 for (iterator it = begin(); it != end(); ++it)
46 it->nucleus()->substitute(m);
50 MathAtom & MathArray::operator[](pos_type pos)
52 BOOST_ASSERT(pos < size());
53 return base_type::operator[](pos);
57 MathAtom const & MathArray::operator[](pos_type pos) const
59 BOOST_ASSERT(pos < size());
60 return base_type::operator[](pos);
64 void MathArray::insert(size_type pos, MathAtom const & t)
66 base_type::insert(begin() + pos, t);
70 void MathArray::insert(size_type pos, MathArray const & ar)
72 BOOST_ASSERT(pos <= size());
73 base_type::insert(begin() + pos, ar.begin(), ar.end());
77 void MathArray::append(MathArray const & ar)
83 void MathArray::erase(size_type pos)
90 void MathArray::erase(iterator pos1, iterator pos2)
92 base_type::erase(pos1, pos2);
96 void MathArray::erase(iterator pos)
98 base_type::erase(pos);
102 void MathArray::erase(size_type pos1, size_type pos2)
104 base_type::erase(begin() + pos1, begin() + pos2);
108 void MathArray::dump2() const
110 NormalStream ns(lyxerr);
111 for (const_iterator it = begin(); it != end(); ++it)
116 void MathArray::dump() const
118 NormalStream ns(lyxerr);
119 for (const_iterator it = begin(); it != end(); ++it)
120 ns << '<' << *it << '>';
124 void MathArray::validate(LaTeXFeatures & features) const
126 for (const_iterator it = begin(); it != end(); ++it)
127 (*it)->validate(features);
131 bool MathArray::match(MathArray const & ar) const
133 return size() == ar.size() && matchpart(ar, 0);
137 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
139 if (size() < ar.size() + pos)
141 const_iterator it = begin() + pos;
142 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
143 if (asString(*it) != asString(*jt))
149 void MathArray::replace(ReplaceData & rep)
151 for (size_type i = 0; i < size(); ++i) {
152 if (find1(rep.from, i)) {
154 lyxerr << "match found!" << endl;
155 erase(i, i + rep.from.size());
161 #warning temporarily disabled
162 // for (const_iterator it = begin(); it != end(); ++it)
163 // it->nucleus()->replace(rep);
168 bool MathArray::find1(MathArray const & ar, size_type pos) const
170 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
171 for (size_type i = 0, n = ar.size(); i < n; ++i)
172 if (asString(operator[](pos + i)) != asString(ar[i]))
178 MathArray::size_type MathArray::find(MathArray const & ar) const
180 for (int i = 0, last = size() - ar.size(); i < last; ++i)
187 MathArray::size_type MathArray::find_last(MathArray const & ar) const
189 for (int i = size() - ar.size(); i >= 0; --i)
196 bool MathArray::contains(MathArray const & ar) const
198 if (find(ar) != size())
200 for (const_iterator it = begin(); it != end(); ++it)
201 if ((*it)->contains(ar))
207 void MathArray::touch() const
214 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
221 void MathArray::metrics(MetricsInfo & mi) const
228 mathed_char_dim(mi.base.font, 'I', dim_);
233 for (const_iterator it = begin(), et = end(); it != et; ++it) {
234 (*it)->metrics(mi, d);
242 void MathArray::draw(PainterInfo & pi, int x, int y) const
244 //if (drawn_ && x == xo_ && y == yo_)
246 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
252 if (y + descent() <= 0) // don't draw above the workarea
254 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
256 if (x + width() <= 0) // don't draw left of workarea
258 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
262 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
266 for (const_iterator it = begin(), et = end(); it != et; ++it) {
267 pi.width = it->width_;
268 (*it)->draw(pi, x, y);
274 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
280 for (const_iterator it = begin(); it != end(); ++it) {
281 (*it)->metricsT(mi, d);
287 void MathArray::drawT(TextPainter & pain, int x, int y) const
289 //if (drawn_ && x == xo_ && y == yo_)
291 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
296 for (const_iterator it = begin(), et = end(); it != et; ++it) {
297 (*it)->drawT(pain, x, y);
303 int MathArray::pos2x(size_type pos) const
305 return pos2x(pos, 0);
309 int MathArray::pos2x(size_type pos, int glue) const
312 size_type target = min(pos, size());
313 for (size_type i = 0; i < target; ++i) {
314 const_iterator it = begin() + i;
315 if ((*it)->getChar() == ' ')
323 MathArray::size_type MathArray::x2pos(int targetx) const
325 return x2pos(targetx, 0);
329 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
331 const_iterator it = begin();
334 for (; currx < targetx && it < end(); ++it) {
336 if ((*it)->getChar() == ' ')
340 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
346 int MathArray::dist(int x, int y) const
353 else if (x > xo_ + width())
354 xx = x - xo_ - width();
356 if (y < yo_ - ascent())
357 yy = yo_ - ascent() - y;
358 else if (y > yo_ + descent())
359 yy = y - yo_ - descent();
365 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
370 y2 = yo_ + descent();
374 void MathArray::center(int & x, int & y) const
376 x = xo_ + width() / 2;
377 y = yo_ + (descent() - ascent()) / 2;
381 void MathArray::towards(int & x, int & y) const
388 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
390 x = cx + int(r * (x - cx));
391 y = cy + int(r * (y - cy));
395 void MathArray::setXY(int x, int y) const
402 void MathArray::notifyCursorLeaves()
407 // remove base-only "scripts"
408 for (pos_type i = 0; i + 1 < size(); ++i) {
409 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
410 if (p && p->cell(0).empty() && p->cell(1).empty()) {
411 MathArray ar = p->nuc();
414 mathcursor->adjust(i, ar.size() - 1);
418 // glue adjacent font insets of the same kind
419 for (pos_type i = 0; i + 1 < size(); ++i) {
420 MathFontInset * p = operator[](i).nucleus()->asFontInset();
421 MathFontInset const * q = operator[](i + 1)->asFontInset();
422 if (p && q && p->name() == q->name()) {
423 p->cell(0).append(q->cell(0));
425 mathcursor->adjust(i, -1);