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"
22 #include "frontends/Painter.h"
23 #include <boost/assert.hpp>
30 MathArray::MathArray()
31 : xo_(0), yo_(0), clean_(false), drawn_(false)
35 MathArray::MathArray(const_iterator from, const_iterator to)
36 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
40 void MathArray::substitute(MathMacro const & m)
42 for (iterator it = begin(); it != end(); ++it)
43 it->nucleus()->substitute(m);
47 MathAtom & MathArray::operator[](pos_type pos)
49 BOOST_ASSERT(pos < size());
50 return base_type::operator[](pos);
54 MathAtom const & MathArray::operator[](pos_type pos) const
56 BOOST_ASSERT(pos < size());
57 return base_type::operator[](pos);
61 void MathArray::insert(size_type pos, MathAtom const & t)
63 base_type::insert(begin() + pos, t);
67 void MathArray::insert(size_type pos, MathArray const & ar)
69 BOOST_ASSERT(pos <= size());
70 base_type::insert(begin() + pos, ar.begin(), ar.end());
74 void MathArray::append(MathArray const & ar)
80 void MathArray::erase(size_type pos)
87 void MathArray::erase(iterator pos1, iterator pos2)
89 base_type::erase(pos1, pos2);
93 void MathArray::erase(iterator pos)
95 base_type::erase(pos);
99 void MathArray::erase(size_type pos1, size_type pos2)
101 base_type::erase(begin() + pos1, begin() + pos2);
105 void MathArray::dump2() const
107 NormalStream ns(lyxerr);
108 for (const_iterator it = begin(); it != end(); ++it)
113 void MathArray::dump() const
115 NormalStream ns(lyxerr);
116 for (const_iterator it = begin(); it != end(); ++it)
117 ns << '<' << *it << '>';
121 void MathArray::validate(LaTeXFeatures & features) const
123 for (const_iterator it = begin(); it != end(); ++it)
124 (*it)->validate(features);
128 bool MathArray::match(MathArray const & ar) const
130 return size() == ar.size() && matchpart(ar, 0);
134 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
136 if (size() < ar.size() + pos)
138 const_iterator it = begin() + pos;
139 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
140 if (!(*jt)->match(*it))
146 void MathArray::replace(ReplaceData & rep)
148 for (size_type i = 0; i < size(); ++i) {
149 if (find1(rep.from, i)) {
151 lyxerr << "match found!" << endl;
152 erase(i, i + rep.from.size());
158 #warning temporarily disabled
159 // for (const_iterator it = begin(); it != end(); ++it)
160 // it->nucleus()->replace(rep);
165 bool MathArray::find1(MathArray const & ar, size_type pos) const
167 //lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
168 for (size_type i = 0, n = ar.size(); i < n; ++i)
169 if (!operator[](pos + i)->match(ar[i]))
175 MathArray::size_type MathArray::find(MathArray const & ar) const
177 for (int i = 0, last = size() - ar.size(); i < last; ++i)
184 MathArray::size_type MathArray::find_last(MathArray const & ar) const
186 for (int i = size() - ar.size(); i >= 0; --i)
193 bool MathArray::contains(MathArray const & ar) const
195 if (find(ar) != size())
197 for (const_iterator it = begin(); it != end(); ++it)
198 if ((*it)->contains(ar))
204 void MathArray::touch() const
211 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
218 void MathArray::metrics(MetricsInfo & mi) const
225 mathed_char_dim(mi.base.font, 'I', dim_);
230 for (const_iterator it = begin(), et = end(); it != et; ++it) {
231 (*it)->metrics(mi, d);
239 void MathArray::draw(PainterInfo & pi, int x, int y) const
241 //if (drawn_ && x == xo_ && y == yo_)
243 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
249 if (y + descent() <= 0) // don't draw above the workarea
251 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
253 if (x + width() <= 0) // don't draw left of workarea
255 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
259 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
263 for (const_iterator it = begin(), et = end(); it != et; ++it) {
264 pi.width = it->width_;
265 (*it)->draw(pi, x, y);
271 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
277 for (const_iterator it = begin(); it != end(); ++it) {
278 (*it)->metricsT(mi, d);
284 void MathArray::drawT(TextPainter & pain, int x, int y) const
286 //if (drawn_ && x == xo_ && y == yo_)
288 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
293 for (const_iterator it = begin(), et = end(); it != et; ++it) {
294 (*it)->drawT(pain, x, y);
300 int MathArray::pos2x(size_type pos) const
302 return pos2x(pos, 0);
306 int MathArray::pos2x(size_type pos, int glue) const
309 size_type target = min(pos, size());
310 for (size_type i = 0; i < target; ++i) {
311 const_iterator it = begin() + i;
312 if ((*it)->getChar() == ' ')
320 MathArray::size_type MathArray::x2pos(int targetx) const
322 return x2pos(targetx, 0);
326 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
328 const_iterator it = begin();
331 for (; currx < targetx && it < end(); ++it) {
333 if ((*it)->getChar() == ' ')
337 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
343 int MathArray::dist(int x, int y) const
350 else if (x > xo_ + width())
351 xx = x - xo_ - width();
353 if (y < yo_ - ascent())
354 yy = yo_ - ascent() - y;
355 else if (y > yo_ + descent())
356 yy = y - yo_ - descent();
362 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
367 y2 = yo_ + descent();
371 void MathArray::center(int & x, int & y) const
373 x = xo_ + width() / 2;
374 y = yo_ + (descent() - ascent()) / 2;
378 void MathArray::towards(int & x, int & y) const
385 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
387 x = cx + int(r * (x - cx));
388 y = cy + int(r * (y - cy));
392 void MathArray::setXY(int x, int y) const
399 void MathArray::notifyCursorLeaves()
403 // remove base-only "scripts"
404 for (pos_type i = 0; i + 1 < size(); ++i) {
405 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
406 if (p && p->cell(0).empty() && p->cell(1).empty()) {
407 MathArray ar = p->nuc();
410 mathcursor->adjust(i, ar.size() - 1);
414 // glue adjacent font insets of the same kind
415 for (pos_type i = 0; i + 1 < size(); ++i) {
416 MathFontInset * p = operator[](i).nucleus()->asFontInset();
417 MathFontInset const * q = operator[](i + 1)->asFontInset();
418 if (p && q && p->name() == q->name()) {
419 p->cell(0).append(q->cell(0));
421 mathcursor->adjust(i, -1);