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 "MathMLStream.h"
19 #include "MathSupport.h"
20 #include "MathReplace.h"
22 #include "coordcache.h"
28 #include "frontends/Painter.h"
30 #include <boost/assert.hpp>
35 using std::ostringstream;
40 MathArray::MathArray()
44 MathArray::MathArray(const_iterator from, const_iterator to)
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
211 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
220 bool isInside(DocIterator const & it, MathArray const & ar,
221 lyx::pos_type p1, lyx::pos_type p2)
223 for (size_t i = 0; i != it.depth(); ++i) {
224 CursorSlice const & sl = it[i];
225 if (sl.inset().inMathed() && &sl.cell() == &ar)
226 return p1 <= sl.pos() && sl.pos() < p2;
235 void MathArray::metrics(MetricsInfo & mi) const
237 mathed_char_dim(mi.base.font, 'I', dim_);
244 //BufferView & bv = *mi.base.bv;
245 //Buffer const & buf = *bv.buffer();
246 for (size_t i = 0, n = size(); i != n; ++i) {
247 MathAtom const & at = operator[](i);
249 MathMacro const * mac = at->asMacro();
250 if (mac && buf.hasMacro(mac->name())) {
251 MacroData const & tmpl = buf.getMacro(mac->name());
252 int numargs = tmpl.numargs();
255 lyxerr << "metrics:found macro: " << mac->name()
256 << " numargs: " << numargs << endl;
257 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
258 MathArray args(begin() + i + 1, begin() + i + numargs + 1);
260 tmpl.expand(args, exp);
261 mac->setExpansion(exp, args);
262 mac->metricsExpanded(mi, d);
263 dim_.wid += mac->widthExpanded();
275 void MathArray::draw(PainterInfo & pi, int x, int y) const
277 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
281 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
285 // don't draw outside the workarea
286 if (y + descent() <= 0
287 || y - ascent() >= pi.pain.paperHeight()
289 || x >= pi.pain.paperWidth())
292 //BufferView & bv = *pi.base.bv;
293 for (size_t i = 0, n = size(); i != n; ++i) {
294 MathAtom const & at = operator[](i);
296 Buffer const & buf = *bv.buffer();
297 // special macro handling
298 MathMacro const * mac = at->asMacro();
299 if (mac && buf.hasMacro(mac->name())) {
300 MacroData const & tmpl = buf.getMacro(mac->name());
301 int numargs = tmpl.numargs();
304 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
305 mac->drawExpanded(pi, x, y);
306 x += mac->widthExpanded();
312 theCoords.insets().add(at.nucleus(), x, y);
313 at->drawSelection(pi, x, y);
320 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
324 for (const_iterator it = begin(); it != end(); ++it) {
325 (*it)->metricsT(mi, d);
331 void MathArray::drawT(TextPainter & pain, int x, int y) const
333 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
336 for (const_iterator it = begin(), et = end(); it != et; ++it) {
337 (*it)->drawT(pain, x, y);
338 //x += (*it)->width_;
344 int MathArray::pos2x(size_type pos) const
346 return pos2x(pos, 0);
350 int MathArray::pos2x(size_type pos, int glue) const
353 size_type target = min(pos, size());
354 for (size_type i = 0; i < target; ++i) {
355 const_iterator it = begin() + i;
356 if ((*it)->getChar() == ' ')
358 //lyxerr << "char: " << (*it)->getChar()
359 // << "width: " << (*it)->width() << std::endl;
366 MathArray::size_type MathArray::x2pos(int targetx) const
368 return x2pos(targetx, 0);
372 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
374 const_iterator it = begin();
377 // find first position after targetx
378 for (; currx < targetx && it < end(); ++it) {
380 if ((*it)->getChar() == ' ')
382 currx += (*it)->width();
386 * If we are not at the beginning of the array, go to the left
387 * of the inset if one of the following two condition holds:
388 * - the current inset is editable (so that the cursor tip is
389 * deeper than us): in this case, we want all intermediate
390 * cursor slices to be before insets;
391 * - the mouse is closer to the left side of the inset than to
393 * See bug 1918 for details.
395 if (it != begin() && currx >= targetx
396 && ((*boost::prior(it))->asNestInset()
397 || abs(lastx - targetx) < abs(currx - targetx))) {
405 int MathArray::dist(int x, int y) const
410 const int xo_ = xo();
411 const int yo_ = yo();
415 else if (x > xo_ + width())
416 xx = x - xo_ - width();
418 if (y < yo_ - ascent())
419 yy = yo_ - ascent() - y;
420 else if (y > yo_ + descent())
421 yy = y - yo_ - descent();
427 void MathArray::setXY(int x, int y) const
429 //lyxerr << "setting position cache for MathArray " << this << std::endl;
430 theCoords.arrays().add(this, x, y);
434 int MathArray::xo() const
436 return theCoords.getArrays().x(this);
440 int MathArray::yo() const
442 return theCoords.getArrays().y(this);