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 "BufferView.h"
28 #include "frontends/Painter.h"
30 #include <boost/assert.hpp>
32 using lyx::odocstream;
37 using std::ostringstream;
42 MathArray::MathArray()
46 MathArray::MathArray(const_iterator from, const_iterator to)
51 MathAtom & MathArray::operator[](pos_type pos)
53 BOOST_ASSERT(pos < size());
54 return base_type::operator[](pos);
58 MathAtom const & MathArray::operator[](pos_type pos) const
60 BOOST_ASSERT(pos < size());
61 return base_type::operator[](pos);
65 void MathArray::insert(size_type pos, MathAtom const & t)
67 base_type::insert(begin() + pos, t);
71 void MathArray::insert(size_type pos, MathArray const & ar)
73 BOOST_ASSERT(pos <= size());
74 base_type::insert(begin() + pos, ar.begin(), ar.end());
78 void MathArray::append(MathArray const & ar)
84 void MathArray::erase(size_type pos)
91 void MathArray::erase(iterator pos1, iterator pos2)
93 base_type::erase(pos1, pos2);
97 void MathArray::erase(iterator pos)
99 base_type::erase(pos);
103 void MathArray::erase(size_type pos1, size_type pos2)
105 base_type::erase(begin() + pos1, begin() + pos2);
109 void MathArray::dump2() const
111 lyx::odocstringstream os;
113 for (const_iterator it = begin(); it != end(); ++it)
115 lyxerr << lyx::to_utf8(os.str());
119 void MathArray::dump() const
121 lyx::odocstringstream os;
123 for (const_iterator it = begin(); it != end(); ++it)
124 ns << '<' << *it << '>';
125 lyxerr << lyx::to_utf8(os.str());
129 void MathArray::validate(LaTeXFeatures & features) const
131 for (const_iterator it = begin(); it != end(); ++it)
132 (*it)->validate(features);
136 bool MathArray::match(MathArray const & ar) const
138 return size() == ar.size() && matchpart(ar, 0);
142 bool MathArray::matchpart(MathArray const & ar, pos_type pos) const
144 if (size() < ar.size() + pos)
146 const_iterator it = begin() + pos;
147 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
148 if (asString(*it) != asString(*jt))
154 void MathArray::replace(ReplaceData & rep)
156 for (size_type i = 0; i < size(); ++i) {
157 if (find1(rep.from, i)) {
159 lyxerr << "match found!" << endl;
160 erase(i, i + rep.from.size());
166 #warning temporarily disabled
167 // for (const_iterator it = begin(); it != end(); ++it)
168 // it->nucleus()->replace(rep);
173 bool MathArray::find1(MathArray const & ar, size_type pos) const
175 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
176 for (size_type i = 0, n = ar.size(); i < n; ++i)
177 if (asString(operator[](pos + i)) != asString(ar[i]))
183 MathArray::size_type MathArray::find(MathArray const & ar) const
185 for (int i = 0, last = size() - ar.size(); i < last; ++i)
192 MathArray::size_type MathArray::find_last(MathArray const & ar) const
194 for (int i = size() - ar.size(); i >= 0; --i)
201 bool MathArray::contains(MathArray const & ar) const
203 if (find(ar) != size())
205 for (const_iterator it = begin(); it != end(); ++it)
206 if ((*it)->contains(ar))
212 void MathArray::touch() const
217 void MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
226 bool isInside(DocIterator const & it, MathArray const & ar,
227 lyx::pos_type p1, lyx::pos_type p2)
229 for (size_t i = 0; i != it.depth(); ++i) {
230 CursorSlice const & sl = it[i];
231 if (sl.inset().inMathed() && &sl.cell() == &ar)
232 return p1 <= sl.pos() && sl.pos() < p2;
241 void MathArray::metrics(MetricsInfo & mi) const
243 mathed_char_dim(mi.base.font, 'I', dim_);
250 //BufferView & bv = *mi.base.bv;
251 //Buffer const & buf = *bv.buffer();
252 for (size_t i = 0, n = size(); i != n; ++i) {
253 MathAtom const & at = operator[](i);
255 MathMacro const * mac = at->asMacro();
256 if (mac && buf.hasMacro(mac->name())) {
257 MacroData const & tmpl = buf.getMacro(mac->name());
258 int numargs = tmpl.numargs();
261 lyxerr << "metrics:found macro: " << mac->name()
262 << " numargs: " << numargs << endl;
263 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
264 MathArray args(begin() + i + 1, begin() + i + numargs + 1);
266 tmpl.expand(args, exp);
267 mac->setExpansion(exp, args);
268 mac->metricsExpanded(mi, d);
269 dim_.wid += mac->widthExpanded();
281 void MathArray::draw(PainterInfo & pi, int x, int y) const
283 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
284 setXY(*pi.base.bv, x, y);
287 pi.pain.rectangle(x, y - ascent(), width(), height(), LColor::mathline);
291 // don't draw outside the workarea
292 if (y + descent() <= 0
293 || y - ascent() >= pi.pain.paperHeight()
295 || x >= pi.pain.paperWidth())
298 //BufferView & bv = *pi.base.bv;
299 for (size_t i = 0, n = size(); i != n; ++i) {
300 MathAtom const & at = operator[](i);
302 Buffer const & buf = *bv.buffer();
303 // special macro handling
304 MathMacro const * mac = at->asMacro();
305 if (mac && buf.hasMacro(mac->name())) {
306 MacroData const & tmpl = buf.getMacro(mac->name());
307 int numargs = tmpl.numargs();
310 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
311 mac->drawExpanded(pi, x, y);
312 x += mac->widthExpanded();
318 //BufferView & bv = *pi.base.bv;
319 pi.base.bv->coordCache().insets().add(at.nucleus(), x, y);
320 at->drawSelection(pi, x, y);
327 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
331 for (const_iterator it = begin(); it != end(); ++it) {
332 (*it)->metricsT(mi, d);
338 void MathArray::drawT(TextPainter & pain, int x, int y) const
340 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
342 // FIXME: Abdel 16/10/2006
343 // This drawT() method is never used, this is dead code.
345 for (const_iterator it = begin(), et = end(); it != et; ++it) {
346 (*it)->drawT(pain, x, y);
347 //x += (*it)->width_;
353 int MathArray::pos2x(size_type pos) const
355 return pos2x(pos, 0);
359 int MathArray::pos2x(size_type pos, int glue) const
362 size_type target = min(pos, size());
363 for (size_type i = 0; i < target; ++i) {
364 const_iterator it = begin() + i;
365 if ((*it)->getChar() == ' ')
367 //lyxerr << "char: " << (*it)->getChar()
368 // << "width: " << (*it)->width() << std::endl;
375 MathArray::size_type MathArray::x2pos(int targetx) const
377 return x2pos(targetx, 0);
381 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
383 const_iterator it = begin();
386 // find first position after targetx
387 for (; currx < targetx && it < end(); ++it) {
389 if ((*it)->getChar() == ' ')
391 currx += (*it)->width();
395 * If we are not at the beginning of the array, go to the left
396 * of the inset if one of the following two condition holds:
397 * - the current inset is editable (so that the cursor tip is
398 * deeper than us): in this case, we want all intermediate
399 * cursor slices to be before insets;
400 * - the mouse is closer to the left side of the inset than to
402 * See bug 1918 for details.
404 if (it != begin() && currx >= targetx
405 && ((*boost::prior(it))->asNestInset()
406 || abs(lastx - targetx) < abs(currx - targetx))) {
414 int MathArray::dist(BufferView & bv, int x, int y) const
419 const int xo_ = xo(bv);
420 const int yo_ = yo(bv);
424 else if (x > xo_ + width())
425 xx = x - xo_ - width();
427 if (y < yo_ - ascent())
428 yy = yo_ - ascent() - y;
429 else if (y > yo_ + descent())
430 yy = y - yo_ - descent();
436 void MathArray::setXY(BufferView & bv, int x, int y) const
438 //lyxerr << "setting position cache for MathArray " << this << std::endl;
439 bv.coordCache().arrays().add(this, x, y);
443 int MathArray::xo(BufferView & bv) const
445 return bv.coordCache().getArrays().x(this);
449 int MathArray::yo(BufferView & bv) const
451 return bv.coordCache().getArrays().y(this);
455 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
457 lyx::odocstringstream oss;
458 NormalStream ns(oss);
460 return os << lyx::to_utf8(oss.str());
464 odocstream & operator<<(odocstream & os, MathArray const & ar)