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.
14 #include "InsetMathFont.h"
15 #include "InsetMathScript.h"
16 #include "InsetMathMacro.h"
17 #include "MathMacroTable.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "MathReplace.h"
22 #include "BufferView.h"
28 #include "frontends/Painter.h"
30 #include <boost/assert.hpp>
38 using std::ostringstream;
43 MathArray::MathArray(const_iterator from, const_iterator to)
48 MathAtom & MathArray::operator[](pos_type pos)
50 BOOST_ASSERT(pos < size());
51 return base_type::operator[](pos);
55 MathAtom const & MathArray::operator[](pos_type pos) const
57 BOOST_ASSERT(pos < size());
58 return base_type::operator[](pos);
62 void MathArray::insert(size_type pos, MathAtom const & t)
64 base_type::insert(begin() + pos, t);
68 void MathArray::insert(size_type pos, MathArray const & ar)
70 BOOST_ASSERT(pos <= size());
71 base_type::insert(begin() + pos, ar.begin(), ar.end());
75 void MathArray::append(MathArray const & ar)
81 void MathArray::erase(size_type pos)
88 void MathArray::erase(iterator pos1, iterator pos2)
90 base_type::erase(pos1, pos2);
94 void MathArray::erase(iterator pos)
96 base_type::erase(pos);
100 void MathArray::erase(size_type pos1, size_type pos2)
102 base_type::erase(begin() + pos1, begin() + pos2);
106 void MathArray::dump2() const
110 for (const_iterator it = begin(); it != end(); ++it)
112 lyxerr << to_utf8(os.str());
116 void MathArray::dump() const
120 for (const_iterator it = begin(); it != end(); ++it)
121 ns << '<' << *it << '>';
122 lyxerr << to_utf8(os.str());
126 void MathArray::validate(LaTeXFeatures & features) const
128 for (const_iterator it = begin(); it != end(); ++it)
129 (*it)->validate(features);
133 bool MathArray::match(MathArray const & ar) const
135 return size() == ar.size() && matchpart(ar, 0);
139 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
141 if (size() < ar.size() + pos)
143 const_iterator it = begin() + pos;
144 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
145 if (asString(*it) != asString(*jt))
151 void MathArray::replace(ReplaceData & rep)
153 for (size_type i = 0; i < size(); ++i) {
154 if (find1(rep.from, i)) {
156 lyxerr << "match found!" << endl;
157 erase(i, i + rep.from.size());
163 #warning temporarily disabled
164 // for (const_iterator it = begin(); it != end(); ++it)
165 // it->nucleus()->replace(rep);
170 bool MathArray::find1(MathArray const & ar, size_type pos) const
172 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
173 for (size_type i = 0, n = ar.size(); i < n; ++i)
174 if (asString(operator[](pos + i)) != asString(ar[i]))
180 MathArray::size_type MathArray::find(MathArray const & ar) const
182 for (int i = 0, last = size() - ar.size(); i < last; ++i)
189 MathArray::size_type MathArray::find_last(MathArray const & ar) const
191 for (int i = size() - ar.size(); i >= 0; --i)
198 bool MathArray::contains(MathArray const & ar) const
200 if (find(ar) != size())
202 for (const_iterator it = begin(); it != end(); ++it)
203 if ((*it)->contains(ar))
209 void MathArray::touch() const
214 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
223 bool isInside(DocIterator const & it, MathArray const & ar,
224 pos_type p1, pos_type p2)
226 for (size_t i = 0; i != it.depth(); ++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
240 mathed_char_dim(mi.base.font, 'I', dim_);
247 //BufferView & bv = *mi.base.bv;
248 //Buffer const & buf = *bv.buffer();
249 for (size_t i = 0, n = size(); i != n; ++i) {
250 MathAtom const & at = operator[](i);
252 MathMacro const * mac = at->asMacro();
253 if (mac && buf.hasMacro(mac->name())) {
254 MacroData const & tmpl = buf.getMacro(mac->name());
255 int numargs = tmpl.numargs();
258 lyxerr << "metrics:found macro: " << mac->name()
259 << " numargs: " << numargs << endl;
260 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
261 MathArray args(begin() + i + 1, begin() + i + numargs + 1);
263 tmpl.expand(args, exp);
264 mac->setExpansion(exp, args);
265 mac->metricsExpanded(mi, d);
266 dim_.wid += mac->widthExpanded();
278 void MathArray::draw(PainterInfo & pi, int x, int y) const
280 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
281 BufferView & bv = *pi.base.bv;
285 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
289 // don't draw outside the workarea
290 if (y + descent() <= 0
291 || y - ascent() >= bv.workHeight()
293 || x >= bv. workWidth())
296 for (size_t i = 0, n = size(); i != n; ++i) {
297 MathAtom const & at = operator[](i);
299 Buffer const & buf = bv.buffer();
300 // special macro handling
301 MathMacro const * mac = at->asMacro();
302 if (mac && buf.hasMacro(mac->name())) {
303 MacroData const & tmpl = buf.getMacro(mac->name());
304 int numargs = tmpl.numargs();
307 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
308 mac->drawExpanded(pi, x, y);
309 x += mac->widthExpanded();
315 bv.coordCache().insets().add(at.nucleus(), x, y);
316 at->drawSelection(pi, x, y);
323 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
327 for (const_iterator it = begin(); it != end(); ++it) {
328 (*it)->metricsT(mi, d);
334 void MathArray::drawT(TextPainter & pain, int x, int y) const
336 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
338 // FIXME: Abdel 16/10/2006
339 // This drawT() method is never used, this is dead code.
341 for (const_iterator it = begin(), et = end(); it != et; ++it) {
342 (*it)->drawT(pain, x, y);
343 //x += (*it)->width_;
349 int MathArray::pos2x(size_type pos) const
351 return pos2x(pos, 0);
355 int MathArray::pos2x(size_type pos, int glue) const
358 size_type target = min(pos, size());
359 for (size_type i = 0; i < target; ++i) {
360 const_iterator it = begin() + i;
361 if ((*it)->getChar() == ' ')
363 //lyxerr << "char: " << (*it)->getChar()
364 // << "width: " << (*it)->width() << std::endl;
371 MathArray::size_type MathArray::x2pos(int targetx) const
373 return x2pos(targetx, 0);
377 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
379 const_iterator it = begin();
382 // find first position after targetx
383 for (; currx < targetx && it < end(); ++it) {
385 if ((*it)->getChar() == ' ')
387 currx += (*it)->width();
391 * If we are not at the beginning of the array, go to the left
392 * of the inset if one of the following two condition holds:
393 * - the current inset is editable (so that the cursor tip is
394 * deeper than us): in this case, we want all intermediate
395 * cursor slices to be before insets;
396 * - the mouse is closer to the left side of the inset than to
398 * See bug 1918 for details.
400 if (it != begin() && currx >= targetx
401 && ((*boost::prior(it))->asNestInset()
402 || abs(lastx - targetx) < abs(currx - targetx))) {
410 int MathArray::dist(BufferView const & bv, int x, int y) const
415 const int xo_ = xo(bv);
416 const int yo_ = yo(bv);
420 else if (x > xo_ + width())
421 xx = x - xo_ - width();
423 if (y < yo_ - ascent())
424 yy = yo_ - ascent() - y;
425 else if (y > yo_ + descent())
426 yy = y - yo_ - descent();
432 void MathArray::setXY(BufferView & bv, int x, int y) const
434 //lyxerr << "setting position cache for MathArray " << this << std::endl;
435 bv.coordCache().arrays().add(this, x, y);
439 int MathArray::xo(BufferView const & bv) const
441 return bv.coordCache().getArrays().x(this);
445 int MathArray::yo(BufferView const & bv) const
447 return bv.coordCache().getArrays().y(this);
451 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
453 odocstringstream oss;
454 NormalStream ns(oss);
456 return os << to_utf8(oss.str());
460 odocstream & operator<<(odocstream & os, MathArray const & ar)