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"
21 #include "frontends/Painter.h"
28 MathArray::MathArray()
29 : xo_(0), yo_(0), clean_(false), drawn_(false)
33 MathArray::MathArray(const_iterator from, const_iterator to)
34 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
38 void MathArray::substitute(MathMacro const & m)
40 for (iterator it = begin(); it != end(); ++it)
41 it->nucleus()->substitute(m);
45 MathAtom & MathArray::operator[](pos_type pos)
47 BOOST_ASSERT(pos < size());
48 return base_type::operator[](pos);
52 MathAtom const & MathArray::operator[](pos_type pos) const
54 BOOST_ASSERT(pos < size());
55 return base_type::operator[](pos);
59 void MathArray::insert(size_type pos, MathAtom const & t)
61 base_type::insert(begin() + pos, t);
65 void MathArray::insert(size_type pos, MathArray const & ar)
67 BOOST_ASSERT(pos <= size());
68 base_type::insert(begin() + pos, ar.begin(), ar.end());
72 void MathArray::append(MathArray const & ar)
78 void MathArray::erase(size_type pos)
85 void MathArray::erase(iterator pos1, iterator pos2)
87 base_type::erase(pos1, pos2);
91 void MathArray::erase(iterator pos)
93 base_type::erase(pos);
97 void MathArray::erase(size_type pos1, size_type pos2)
99 base_type::erase(begin() + pos1, begin() + pos2);
103 void MathArray::dump2() const
105 NormalStream ns(lyxerr);
106 for (const_iterator it = begin(); it != end(); ++it)
111 void MathArray::dump() const
113 NormalStream ns(lyxerr);
114 for (const_iterator it = begin(); it != end(); ++it)
115 ns << '<' << *it << '>';
119 void MathArray::validate(LaTeXFeatures & features) const
121 for (const_iterator it = begin(); it != end(); ++it)
122 (*it)->validate(features);
126 bool MathArray::match(MathArray const & ar) const
128 return size() == ar.size() && matchpart(ar, 0);
132 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
134 if (size() < ar.size() + pos)
136 const_iterator it = begin() + pos;
137 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
138 if (!(*jt)->match(*it))
144 void MathArray::replace(ReplaceData & rep)
146 for (size_type i = 0; i < size(); ++i) {
147 if (find1(rep.from, i)) {
149 lyxerr << "match found!" << endl;
150 erase(i, i + rep.from.size());
156 #warning temporarily disabled
157 // for (const_iterator it = begin(); it != end(); ++it)
158 // it->nucleus()->replace(rep);
163 bool MathArray::find1(MathArray const & ar, size_type pos) const
165 //lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
166 for (size_type i = 0, n = ar.size(); i < n; ++i)
167 if (!operator[](pos + i)->match(ar[i]))
173 MathArray::size_type MathArray::find(MathArray const & ar) const
175 for (int i = 0, last = size() - ar.size(); i < last; ++i)
182 MathArray::size_type MathArray::find_last(MathArray const & ar) const
184 for (int i = size() - ar.size(); i >= 0; --i)
191 bool MathArray::contains(MathArray const & ar) const
193 if (find(ar) != size())
195 for (const_iterator it = begin(); it != end(); ++it)
196 if ((*it)->contains(ar))
202 void MathArray::touch() const
209 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
216 void MathArray::metrics(MetricsInfo & mi) const
223 mathed_char_dim(mi.base.font, 'I', dim_);
228 for (const_iterator it = begin(), et = end(); it != et; ++it) {
229 (*it)->metrics(mi, d);
237 void MathArray::draw(PainterInfo & pi, int x, int y) const
239 //if (drawn_ && x == xo_ && y == yo_)
241 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
247 if (y + descent() <= 0) // don't draw above the workarea
249 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
251 if (x + width() <= 0) // don't draw left of workarea
253 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
257 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
261 for (const_iterator it = begin(), et = end(); it != et; ++it) {
262 pi.width = it->width_;
263 (*it)->draw(pi, x, y);
269 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
275 for (const_iterator it = begin(); it != end(); ++it) {
276 (*it)->metricsT(mi, d);
282 void MathArray::drawT(TextPainter & pain, int x, int y) const
284 //if (drawn_ && x == xo_ && y == yo_)
286 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
291 for (const_iterator it = begin(), et = end(); it != et; ++it) {
292 (*it)->drawT(pain, x, y);
298 int MathArray::pos2x(size_type pos) const
300 return pos2x(pos, 0);
304 int MathArray::pos2x(size_type pos, int glue) const
307 size_type target = min(pos, size());
308 for (size_type i = 0; i < target; ++i) {
309 const_iterator it = begin() + i;
310 if ((*it)->getChar() == ' ')
318 MathArray::size_type MathArray::x2pos(int targetx) const
320 return x2pos(targetx, 0);
324 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
326 const_iterator it = begin();
329 for (; currx < targetx && it < end(); ++it) {
331 if ((*it)->getChar() == ' ')
335 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
341 int MathArray::dist(int x, int y) const
348 else if (x > xo_ + width())
349 xx = x - xo_ - width();
351 if (y < yo_ - ascent())
352 yy = yo_ - ascent() - y;
353 else if (y > yo_ + descent())
354 yy = y - yo_ - descent();
360 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
365 y2 = yo_ + descent();
369 void MathArray::center(int & x, int & y) const
371 x = xo_ + width() / 2;
372 y = yo_ + (descent() - ascent()) / 2;
376 void MathArray::towards(int & x, int & y) const
383 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
385 x = cx + int(r * (x - cx));
386 y = cy + int(r * (y - cy));
390 void MathArray::setXY(int x, int y) const
397 void MathArray::notifyCursorLeaves()
401 // remove base-only "scripts"
402 for (pos_type i = 0; i + 1 < size(); ++i) {
403 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
404 if (p && p->cell(0).empty() && p->cell(1).empty()) {
405 MathArray ar = p->nuc();
408 mathcursor->adjust(i, ar.size() - 1);
412 // glue adjacent font insets of the same kind
413 for (pos_type i = 0; i + 1 < size(); ++i) {
414 MathFontInset * p = operator[](i).nucleus()->asFontInset();
415 MathFontInset const * q = operator[](i + 1)->asFontInset();
416 if (p && q && p->name() == q->name()) {
417 p->cell(0).append(q->cell(0));
419 mathcursor->adjust(i, -1);