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/FontMetrics.h"
29 #include "frontends/Painter.h"
31 #include <boost/assert.hpp>
39 using std::ostringstream;
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
111 for (const_iterator it = begin(); it != end(); ++it)
113 lyxerr << to_utf8(os.str());
117 void MathArray::dump() const
121 for (const_iterator it = begin(); it != end(); ++it)
122 ns << '<' << *it << '>';
123 lyxerr << to_utf8(os.str());
127 void MathArray::validate(LaTeXFeatures & features) const
129 for (const_iterator it = begin(); it != end(); ++it)
130 (*it)->validate(features);
134 bool MathArray::match(MathArray const & ar) const
136 return size() == ar.size() && matchpart(ar, 0);
140 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
142 if (size() < ar.size() + pos)
144 const_iterator it = begin() + pos;
145 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
146 if (asString(*it) != asString(*jt))
152 void MathArray::replace(ReplaceData & rep)
154 for (size_type i = 0; i < size(); ++i) {
155 if (find1(rep.from, i)) {
157 lyxerr << "match found!" << endl;
158 erase(i, i + rep.from.size());
164 #warning temporarily disabled
165 // for (const_iterator it = begin(); it != end(); ++it)
166 // it->nucleus()->replace(rep);
171 bool MathArray::find1(MathArray const & ar, size_type pos) const
173 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
174 for (size_type i = 0, n = ar.size(); i < n; ++i)
175 if (asString(operator[](pos + i)) != asString(ar[i]))
181 MathArray::size_type MathArray::find(MathArray const & ar) const
183 for (int i = 0, last = size() - ar.size(); i < last; ++i)
190 MathArray::size_type MathArray::find_last(MathArray const & ar) const
192 for (int i = size() - ar.size(); i >= 0; --i)
199 bool MathArray::contains(MathArray const & ar) const
201 if (find(ar) != size())
203 for (const_iterator it = begin(); it != end(); ++it)
204 if ((*it)->contains(ar))
210 void MathArray::touch() const
215 bool MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
228 bool isInside(DocIterator const & it, MathArray const & ar,
229 pos_type p1, pos_type p2)
231 for (size_t i = 0; i != it.depth(); ++i) {
232 CursorSlice const & sl = it[i];
233 if (sl.inset().inMathed() && &sl.cell() == &ar)
234 return p1 <= sl.pos() && sl.pos() < p2;
243 void MathArray::metrics(MetricsInfo & mi) const
245 dim_ = theFontMetrics(mi.base.font).dimension('I');
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 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
286 BufferView & bv = *pi.base.bv;
290 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
294 // don't draw outside the workarea
295 if (y + descent() <= 0
296 || y - ascent() >= bv.workHeight()
298 || x >= bv. workWidth())
301 for (size_t i = 0, n = size(); i != n; ++i) {
302 MathAtom const & at = operator[](i);
304 Buffer const & buf = bv.buffer();
305 // special macro handling
306 MathMacro const * mac = at->asMacro();
307 if (mac && buf.hasMacro(mac->name())) {
308 MacroData const & tmpl = buf.getMacro(mac->name());
309 int numargs = tmpl.numargs();
312 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
313 mac->drawExpanded(pi, x, y);
314 x += mac->widthExpanded();
320 bv.coordCache().insets().add(at.nucleus(), x, y);
321 at->drawSelection(pi, x, y);
328 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
332 for (const_iterator it = begin(); it != end(); ++it) {
333 (*it)->metricsT(mi, d);
339 void MathArray::drawT(TextPainter & pain, int x, int y) const
341 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
343 // FIXME: Abdel 16/10/2006
344 // This drawT() method is never used, this is dead code.
346 for (const_iterator it = begin(), et = end(); it != et; ++it) {
347 (*it)->drawT(pain, x, y);
348 //x += (*it)->width_;
354 int MathArray::pos2x(size_type pos) const
356 return pos2x(pos, 0);
360 int MathArray::pos2x(size_type pos, int glue) const
363 size_type target = min(pos, size());
364 for (size_type i = 0; i < target; ++i) {
365 const_iterator it = begin() + i;
366 if ((*it)->getChar() == ' ')
368 //lyxerr << "char: " << (*it)->getChar()
369 // << "width: " << (*it)->width() << std::endl;
376 MathArray::size_type MathArray::x2pos(int targetx) const
378 return x2pos(targetx, 0);
382 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
384 const_iterator it = begin();
387 // find first position after targetx
388 for (; currx < targetx && it < end(); ++it) {
390 if ((*it)->getChar() == ' ')
392 currx += (*it)->width();
396 * If we are not at the beginning of the array, go to the left
397 * of the inset if one of the following two condition holds:
398 * - the current inset is editable (so that the cursor tip is
399 * deeper than us): in this case, we want all intermediate
400 * cursor slices to be before insets;
401 * - the mouse is closer to the left side of the inset than to
403 * See bug 1918 for details.
405 if (it != begin() && currx >= targetx
406 && ((*boost::prior(it))->asNestInset()
407 || abs(lastx - targetx) < abs(currx - targetx))) {
415 int MathArray::dist(BufferView const & bv, int x, int y) const
420 const int xo_ = xo(bv);
421 const int yo_ = yo(bv);
425 else if (x > xo_ + width())
426 xx = x - xo_ - width();
428 if (y < yo_ - ascent())
429 yy = yo_ - ascent() - y;
430 else if (y > yo_ + descent())
431 yy = y - yo_ - descent();
437 void MathArray::setXY(BufferView & bv, int x, int y) const
439 //lyxerr << "setting position cache for MathArray " << this << std::endl;
440 bv.coordCache().arrays().add(this, x, y);
444 int MathArray::xo(BufferView const & bv) const
446 return bv.coordCache().getArrays().x(this);
450 int MathArray::yo(BufferView const & bv) const
452 return bv.coordCache().getArrays().y(this);
456 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
458 odocstringstream oss;
459 NormalStream ns(oss);
461 return os << to_utf8(oss.str());
465 odocstream & operator<<(odocstream & os, MathArray const & ar)