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_inset.h"
15 #include "math_cursor.h"
16 #include "math_deliminset.h"
17 #include "math_fontinset.h"
18 #include "math_scriptinset.h"
19 #include "math_mathmlstream.h"
20 #include "math_support.h"
21 #include "math_replace.h"
23 #include "support/LAssert.h"
24 #include "metricsinfo.h"
25 #include "math_data.h"
26 #include "frontends/Painter.h"
27 #include "textpainter.h"
29 using namespace lyx::support;
37 MathArray::MathArray()
38 : xo_(0), yo_(0), clean_(false), drawn_(false)
42 MathArray::MathArray(const_iterator from, const_iterator to)
43 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
47 void MathArray::substitute(MathMacro const & m)
49 for (iterator it = begin(); it != end(); ++it)
50 it->nucleus()->substitute(m);
54 MathAtom & MathArray::operator[](pos_type pos)
57 return base_type::operator[](pos);
61 MathAtom const & MathArray::operator[](pos_type pos) const
64 return base_type::operator[](pos);
68 void MathArray::insert(size_type pos, MathAtom const & t)
70 base_type::insert(begin() + pos, t);
74 void MathArray::insert(size_type pos, MathArray const & ar)
76 Assert(pos <= size());
77 base_type::insert(begin() + pos, ar.begin(), ar.end());
81 void MathArray::append(MathArray const & ar)
87 void MathArray::erase(size_type pos)
94 void MathArray::erase(iterator pos1, iterator pos2)
96 base_type::erase(pos1, pos2);
100 void MathArray::erase(iterator pos)
102 base_type::erase(pos);
106 void MathArray::erase(size_type pos1, size_type pos2)
108 base_type::erase(begin() + pos1, begin() + pos2);
112 void MathArray::dump2() const
114 NormalStream ns(lyxerr);
115 for (const_iterator it = begin(); it != end(); ++it)
120 void MathArray::dump() const
122 NormalStream ns(lyxerr);
123 for (const_iterator it = begin(); it != end(); ++it)
124 ns << '<' << *it << '>';
128 void MathArray::validate(LaTeXFeatures & features) const
130 for (const_iterator it = begin(); it != end(); ++it)
131 (*it)->validate(features);
135 bool MathArray::match(MathArray const & ar) const
137 return size() == ar.size() && matchpart(ar, 0);
141 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
143 if (size() < ar.size() + pos)
145 const_iterator it = begin() + pos;
146 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
147 if (!(*jt)->match(*it))
153 void MathArray::replace(ReplaceData & rep)
155 for (size_type i = 0; i < size(); ++i) {
156 if (find1(rep.from, i)) {
158 lyxerr << "match found!" << endl;
159 erase(i, i + rep.from.size());
165 #warning temporarily disabled
166 // for (const_iterator it = begin(); it != end(); ++it)
167 // it->nucleus()->replace(rep);
172 bool MathArray::find1(MathArray const & ar, size_type pos) const
174 //lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
175 for (size_type i = 0, n = ar.size(); i < n; ++i)
176 if (!operator[](pos + i)->match(ar[i]))
182 MathArray::size_type MathArray::find(MathArray const & ar) const
184 for (int i = 0, last = size() - ar.size(); i < last; ++i)
191 MathArray::size_type MathArray::find_last(MathArray const & ar) const
193 for (int i = size() - ar.size(); i >= 0; --i)
200 bool MathArray::contains(MathArray const & ar) const
202 if (find(ar) != size())
204 for (const_iterator it = begin(); it != end(); ++it)
205 if ((*it)->contains(ar))
211 void MathArray::touch() const
218 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
225 void MathArray::metrics(MetricsInfo & mi) const
232 mathed_char_dim(mi.base.font, 'I', dim_);
237 for (const_iterator it = begin(), et = end(); it != et; ++it) {
238 (*it)->metrics(mi, d);
246 void MathArray::draw(PainterInfo & pi, int x, int y) const
248 //if (drawn_ && x == xo_ && y == yo_)
250 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
256 if (y + descent() <= 0) // don't draw above the workarea
258 if (y - ascent() >= pi.pain.paperHeight()) // don't draw below the workarea
260 if (x + width() <= 0) // don't draw left of workarea
262 if (x >= pi.pain.paperWidth()) // don't draw right of workarea
266 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
270 for (const_iterator it = begin(), et = end(); it != et; ++it) {
271 pi.width = it->width_;
272 (*it)->draw(pi, x, y);
278 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
284 for (const_iterator it = begin(); it != end(); ++it) {
285 (*it)->metricsT(mi, d);
291 void MathArray::drawT(TextPainter & pain, int x, int y) const
293 //if (drawn_ && x == xo_ && y == yo_)
295 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
300 for (const_iterator it = begin(), et = end(); it != et; ++it) {
301 (*it)->drawT(pain, x, y);
307 int MathArray::pos2x(size_type pos) const
309 return pos2x(pos, 0);
313 int MathArray::pos2x(size_type pos, int glue) const
316 size_type target = min(pos, size());
317 for (size_type i = 0; i < target; ++i) {
318 const_iterator it = begin() + i;
319 if ((*it)->getChar() == ' ')
327 MathArray::size_type MathArray::x2pos(int targetx) const
329 return x2pos(targetx, 0);
333 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
335 const_iterator it = begin();
338 for (; currx < targetx && it < end(); ++it) {
340 if ((*it)->getChar() == ' ')
344 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
350 int MathArray::dist(int x, int y) const
357 else if (x > xo_ + width())
358 xx = x - xo_ - width();
360 if (y < yo_ - ascent())
361 yy = yo_ - ascent() - y;
362 else if (y > yo_ + descent())
363 yy = y - yo_ - descent();
369 void MathArray::boundingBox(int & x1, int & x2, int & y1, int & y2)
374 y2 = yo_ + descent();
378 void MathArray::center(int & x, int & y) const
380 x = xo_ + width() / 2;
381 y = yo_ + (descent() - ascent()) / 2;
385 void MathArray::towards(int & x, int & y) const
392 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
394 x = cx + int(r * (x - cx));
395 y = cy + int(r * (y - cy));
399 void MathArray::setXY(int x, int y) const
406 void MathArray::notifyCursorLeaves()
410 // remove base-only "scripts"
411 for (pos_type i = 0; i + 1 < size(); ++i) {
412 MathScriptInset * p = operator[](i).nucleus()->asScriptInset();
413 if (p && p->cell(0).empty() && p->cell(1).empty()) {
414 MathArray ar = p->nuc();
417 mathcursor->adjust(i, ar.size() - 1);
421 // glue adjacent font insets of the same kind
422 for (pos_type i = 0; i + 1 < size(); ++i) {
423 MathFontInset * p = operator[](i).nucleus()->asFontInset();
424 MathFontInset const * q = operator[](i + 1)->asFontInset();
425 if (p && q && p->name() == q->name()) {
426 p->cell(0).append(q->cell(0));
428 mathcursor->adjust(i, -1);