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_fontinset.h"
15 #include "math_scriptinset.h"
16 #include "math_mathmlstream.h"
17 #include "math_support.h"
18 #include "math_replace.h"
22 #include "frontends/Painter.h"
24 #include <boost/assert.hpp>
29 using std::ostringstream;
32 MathArray::MathArray()
33 : xo_(0), yo_(0), clean_(false), drawn_(false)
37 MathArray::MathArray(const_iterator from, const_iterator to)
38 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
42 void MathArray::substitute(MathMacro const & m)
44 for (iterator it = begin(); it != end(); ++it)
45 it->nucleus()->substitute(m);
49 MathAtom & MathArray::operator[](pos_type pos)
51 BOOST_ASSERT(pos < size());
52 return base_type::operator[](pos);
56 MathAtom const & MathArray::operator[](pos_type pos) const
58 BOOST_ASSERT(pos < size());
59 return base_type::operator[](pos);
63 void MathArray::insert(size_type pos, MathAtom const & t)
65 base_type::insert(begin() + pos, t);
69 void MathArray::insert(size_type pos, MathArray const & ar)
71 BOOST_ASSERT(pos <= size());
72 base_type::insert(begin() + pos, ar.begin(), ar.end());
76 void MathArray::append(MathArray const & ar)
82 void MathArray::erase(size_type pos)
89 void MathArray::erase(iterator pos1, iterator pos2)
91 base_type::erase(pos1, pos2);
95 void MathArray::erase(iterator pos)
97 base_type::erase(pos);
101 void MathArray::erase(size_type pos1, size_type pos2)
103 base_type::erase(begin() + pos1, begin() + pos2);
107 void MathArray::dump2() const
109 NormalStream ns(lyxerr);
110 for (const_iterator it = begin(); it != end(); ++it)
115 void MathArray::dump() const
117 NormalStream ns(lyxerr);
118 for (const_iterator it = begin(); it != end(); ++it)
119 ns << '<' << *it << '>';
123 void MathArray::validate(LaTeXFeatures & features) const
125 for (const_iterator it = begin(); it != end(); ++it)
126 (*it)->validate(features);
130 bool MathArray::match(MathArray const & ar) const
132 return size() == ar.size() && matchpart(ar, 0);
136 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
138 if (size() < ar.size() + pos)
140 const_iterator it = begin() + pos;
141 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
142 if (asString(*it) != asString(*jt))
148 void MathArray::replace(ReplaceData & rep)
150 for (size_type i = 0; i < size(); ++i) {
151 if (find1(rep.from, i)) {
153 lyxerr << "match found!" << endl;
154 erase(i, i + rep.from.size());
160 #warning temporarily disabled
161 // for (const_iterator it = begin(); it != end(); ++it)
162 // it->nucleus()->replace(rep);
167 bool MathArray::find1(MathArray const & ar, size_type pos) const
169 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
170 for (size_type i = 0, n = ar.size(); i < n; ++i)
171 if (asString(operator[](pos + i)) != asString(ar[i]))
177 MathArray::size_type MathArray::find(MathArray const & ar) const
179 for (int i = 0, last = size() - ar.size(); i < last; ++i)
186 MathArray::size_type MathArray::find_last(MathArray const & ar) const
188 for (int i = size() - ar.size(); i >= 0; --i)
195 bool MathArray::contains(MathArray const & ar) const
197 if (find(ar) != size())
199 for (const_iterator it = begin(); it != end(); ++it)
200 if ((*it)->contains(ar))
206 void MathArray::touch() const
213 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
220 void MathArray::metrics(MetricsInfo & mi) const
227 mathed_char_dim(mi.base.font, 'I', dim_);
232 for (const_iterator it = begin(), et = end(); it != et; ++it) {
233 (*it)->metrics(mi, d);
235 //it->width_ = d.wid;
241 void MathArray::draw(PainterInfo & pi, int x, int y) const
243 //if (drawn_ && x == xo_ && y == yo_)
245 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
251 if (y + descent() <= 0) // don't draw above the workarea
253 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
255 if (x + width() <= 0) // don't draw left of workarea
257 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
261 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
265 for (const_iterator it = begin(), et = end(); it != et; ++it) {
266 //pi.width = it->width_;
267 (*it)->draw(pi, x, y);
273 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
279 for (const_iterator it = begin(); it != end(); ++it) {
280 (*it)->metricsT(mi, d);
286 void MathArray::drawT(TextPainter & pain, int x, int y) const
288 //if (drawn_ && x == xo_ && y == yo_)
290 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
295 for (const_iterator it = begin(), et = end(); it != et; ++it) {
296 (*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() == ' ')
317 //lyxerr << "char: " << (*it)->getChar()
318 // << "width: " << (*it)->width() << std::endl;
325 MathArray::size_type MathArray::x2pos(int targetx) const
327 return x2pos(targetx, 0);
331 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
333 const_iterator it = begin();
336 for (; currx < targetx && it < end(); ++it) {
338 if ((*it)->getChar() == ' ')
340 currx += (*it)->width();
342 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
348 int MathArray::dist(int x, int y) const
355 else if (x > xo_ + width())
356 xx = x - xo_ - width();
358 if (y < yo_ - ascent())
359 yy = yo_ - ascent() - y;
360 else if (y > yo_ + descent())
361 yy = y - yo_ - descent();
367 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
372 y2 = yo_ + descent();
376 bool MathArray::contains(int x, int y) const
378 return xo_ <= x && x <= xo_ + width()
379 && yo_ - ascent() <= y && y <= yo_ + descent();
383 void MathArray::center(int & x, int & y) const
385 x = xo_ + width() / 2;
386 y = yo_ + (descent() - ascent()) / 2;
390 void MathArray::towards(int & x, int & y) const
397 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
399 x = cx + int(r * (x - cx));
400 y = cy + int(r * (y - cy));
404 void MathArray::setXY(int x, int y) const
411 void MathArray::notifyCursorLeaves()
416 // remove base-only "scripts"
417 for (pos_type i = 0; i + 1 < size(); ++i) {
418 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
419 if (p && p->cell(0).empty() && p->cell(1).empty()) {
420 MathArray ar = p->nuc();
423 mathcursor->adjust(i, ar.size() - 1);
427 // glue adjacent font insets of the same kind
428 for (pos_type i = 0; i + 1 < size(); ++i) {
429 MathFontInset * p = operator[](i).nucleus()->asFontInset();
430 MathFontInset const * q = operator[](i + 1)->asFontInset();
431 if (p && q && p->name() == q->name()) {
432 p->cell(0).append(q->cell(0));
434 mathcursor->adjust(i, -1);