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"
22 #include "coordcache.h"
24 #include "BufferView.h"
29 #include "frontends/Painter.h"
31 #include <boost/assert.hpp>
36 using std::ostringstream;
41 MathArray::MathArray()
45 MathArray::MathArray(const_iterator from, const_iterator to)
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
212 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
221 bool isInside(DocIterator const & it, MathArray const & ar,
222 lyx::pos_type p1, lyx::pos_type p2)
224 for (size_t i = 0; i != it.depth(); ++i) {
225 CursorSlice const & sl = it[i];
226 if (sl.inset().inMathed() && &sl.cell() == &ar)
227 return p1 <= sl.pos() && sl.pos() < p2;
236 void MathArray::metrics(MetricsInfo & mi) const
238 mathed_char_dim(mi.base.font, 'I', dim_);
245 //BufferView & bv = *mi.base.bv;
246 //Buffer const & buf = *bv.buffer();
247 for (size_t i = 0, n = size(); i != n; ++i) {
248 MathAtom const & at = operator[](i);
250 MathMacro const * mac = at->asMacro();
251 if (mac && buf.hasMacro(mac->name())) {
252 MacroData const & tmpl = buf.getMacro(mac->name());
253 int numargs = tmpl.numargs();
256 lyxerr << "metrics:found macro: " << mac->name()
257 << " numargs: " << numargs << endl;
258 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
259 MathArray args(begin() + i + 1, begin() + i + numargs + 1);
261 tmpl.expand(args, exp);
262 mac->setExpansion(exp, args);
263 mac->metricsExpanded(mi, d);
264 dim_.wid += mac->widthExpanded();
276 void MathArray::draw(PainterInfo & pi, int x, int y) const
278 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
282 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
286 // don't draw outside the workarea
287 if (y + descent() <= 0
288 || y - ascent() >= pi.pain.paperHeight()
290 || x >= pi.pain.paperWidth())
293 //BufferView & bv = *pi.base.bv;
294 for (size_t i = 0, n = size(); i != n; ++i) {
295 MathAtom const & at = operator[](i);
297 Buffer const & buf = *bv.buffer();
298 // special macro handling
299 MathMacro const * mac = at->asMacro();
300 if (mac && buf.hasMacro(mac->name())) {
301 MacroData const & tmpl = buf.getMacro(mac->name());
302 int numargs = tmpl.numargs();
305 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
306 mac->drawExpanded(pi, x, y);
307 x += mac->widthExpanded();
313 theCoords.insets().add(at.nucleus(), x, y);
314 at->drawSelection(pi, x, y);
321 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
325 for (const_iterator it = begin(); it != end(); ++it) {
326 (*it)->metricsT(mi, d);
332 void MathArray::drawT(TextPainter & pain, int x, int y) const
334 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
337 for (const_iterator it = begin(), et = end(); it != et; ++it) {
338 (*it)->drawT(pain, x, y);
339 //x += (*it)->width_;
345 int MathArray::pos2x(size_type pos) const
347 return pos2x(pos, 0);
351 int MathArray::pos2x(size_type pos, int glue) const
354 size_type target = min(pos, size());
355 for (size_type i = 0; i < target; ++i) {
356 const_iterator it = begin() + i;
357 if ((*it)->getChar() == ' ')
359 //lyxerr << "char: " << (*it)->getChar()
360 // << "width: " << (*it)->width() << std::endl;
367 MathArray::size_type MathArray::x2pos(int targetx) const
369 return x2pos(targetx, 0);
373 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
375 const_iterator it = begin();
378 // find first position after targetx
379 for (; currx < targetx && it < end(); ++it) {
381 if ((*it)->getChar() == ' ')
383 currx += (*it)->width();
387 * If we are not at the beginning of the array, go to the left
388 * of the inset if one of the following two condition holds:
389 * - the current inset is editable (so that the cursor tip is
390 * deeper than us): in this case, we want all intermediate
391 * cursor slices to be before insets;
392 * - the mouse is closer to the left side of the inset than to
394 * See bug 1918 for details.
396 if (it != begin() && currx >= targetx
397 && ((*boost::prior(it))->asNestInset()
398 || abs(lastx - targetx) < abs(currx - targetx))) {
406 int MathArray::dist(int x, int y) const
411 const int xo_ = xo();
412 const int yo_ = yo();
416 else if (x > xo_ + width())
417 xx = x - xo_ - width();
419 if (y < yo_ - ascent())
420 yy = yo_ - ascent() - y;
421 else if (y > yo_ + descent())
422 yy = y - yo_ - descent();
428 void MathArray::setXY(int x, int y) const
430 //lyxerr << "setting position cache for MathArray " << this << std::endl;
431 theCoords.arrays().add(this, x, y);
435 int MathArray::xo() const
437 return theCoords.getArrays().x(this);
441 int MathArray::yo() const
443 return theCoords.getArrays().y(this);