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_macro.h"
17 #include "math_macrotable.h"
18 #include "math_mathmlstream.h"
19 #include "math_support.h"
20 #include "math_replace.h"
23 #include "BufferView.h"
28 #include "frontends/Painter.h"
30 #include <boost/assert.hpp>
35 using std::ostringstream;
40 MathArray::MathArray()
41 : xo_(0), yo_(0), clean_(false), drawn_(false)
45 MathArray::MathArray(const_iterator from, const_iterator to)
46 : base_type(from, to), xo_(0), yo_(0), clean_(false), drawn_(false)
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
223 bool isInside(DocIterator const & it, MathArray const & ar,
224 lyx::pos_type p1, lyx::pos_type p2)
226 for (size_t i = 0; i != it.size(); ++i) {
227 CursorSlice const & sl = it[i];
228 if (sl.inset().inMathed() && &sl.cell() == &ar)
229 return p1 <= sl.pos() && sl.pos() < p2;
238 void MathArray::metrics(MetricsInfo & mi) const
245 mathed_char_dim(mi.base.font, 'I', dim_);
252 //BufferView & bv = *mi.base.bv;
253 //Buffer const & buf = *bv.buffer();
254 for (size_t i = 0, n = size(); i != n; ++i) {
255 MathAtom const & at = operator[](i);
257 MathMacro const * mac = at->asMacro();
258 if (mac && buf.hasMacro(mac->name())) {
259 MacroData const & tmpl = buf.getMacro(mac->name());
260 int numargs = tmpl.numargs();
263 lyxerr << "metrics:found macro: " << mac->name()
264 << " numargs: " << numargs << endl;
265 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
266 MathArray args(begin() + i + 1, begin() + i + numargs + 1);
268 tmpl.expand(args, exp);
269 mac->setExpansion(exp, args);
270 mac->metricsExpanded(mi, d);
271 dim_.wid += mac->widthExpanded();
283 void MathArray::draw(PainterInfo & pi, int x, int y) const
285 //if (drawn_ && x == xo_ && y == yo_)
287 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
294 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
298 // don't draw outside the workarea
299 if (y + descent() <= 0
300 || y - ascent() >= pi.pain.paperHeight()
302 || x >= pi.pain.paperWidth())
305 //BufferView & bv = *pi.base.bv;
306 for (size_t i = 0, n = size(); i != n; ++i) {
307 MathAtom const & at = operator[](i);
309 Buffer const & buf = *bv.buffer();
310 // special macro handling
311 MathMacro const * mac = at->asMacro();
312 if (mac && buf.hasMacro(mac->name())) {
313 MacroData const & tmpl = buf.getMacro(mac->name());
314 int numargs = tmpl.numargs();
317 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
318 mac->drawExpanded(pi, x, y);
319 x += mac->widthExpanded();
325 at->drawSelection(pi, x, y);
332 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
338 for (const_iterator it = begin(); it != end(); ++it) {
339 (*it)->metricsT(mi, d);
345 void MathArray::drawT(TextPainter & pain, int x, int y) const
347 //if (drawn_ && x == xo_ && y == yo_)
349 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
354 for (const_iterator it = begin(), et = end(); it != et; ++it) {
355 (*it)->drawT(pain, x, y);
362 int MathArray::pos2x(size_type pos) const
364 return pos2x(pos, 0);
368 int MathArray::pos2x(size_type pos, int glue) const
371 size_type target = min(pos, size());
372 for (size_type i = 0; i < target; ++i) {
373 const_iterator it = begin() + i;
374 if ((*it)->getChar() == ' ')
376 //lyxerr << "char: " << (*it)->getChar()
377 // << "width: " << (*it)->width() << std::endl;
384 MathArray::size_type MathArray::x2pos(int targetx) const
386 return x2pos(targetx, 0);
390 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
392 const_iterator it = begin();
395 for (; currx < targetx && it < end(); ++it) {
397 if ((*it)->getChar() == ' ')
399 currx += (*it)->width();
401 if (abs(lastx - targetx) < abs(currx - targetx) && it != begin())
407 int MathArray::dist(int x, int y) const
414 else if (x > xo_ + width())
415 xx = x - xo_ - width();
417 if (y < yo_ - ascent())
418 yy = yo_ - ascent() - y;
419 else if (y > yo_ + descent())
420 yy = y - yo_ - descent();
426 bool MathArray::contains(int x, int y) const
428 return xo_ <= x && x <= xo_ + width()
429 && yo_ - ascent() <= y && y <= yo_ + descent();
433 void MathArray::center(int & x, int & y) const
435 x = xo_ + width() / 2;
436 y = yo_ + (descent() - ascent()) / 2;
440 void MathArray::towards(int & x, int & y) const
447 //int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);
449 x = cx + int(r * (x - cx));
450 y = cy + int(r * (y - cy));
454 void MathArray::setXY(int x, int y) const