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 "MathMacro.h"
17 #include "MacroTable.h"
18 #include "MathStream.h"
19 #include "MathSupport.h"
20 #include "ReplaceData.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 MathData::MathData(const_iterator from, const_iterator to)
49 MathAtom & MathData::operator[](pos_type pos)
51 BOOST_ASSERT(pos < size());
52 return base_type::operator[](pos);
56 MathAtom const & MathData::operator[](pos_type pos) const
58 BOOST_ASSERT(pos < size());
59 return base_type::operator[](pos);
63 void MathData::insert(size_type pos, MathAtom const & t)
65 base_type::insert(begin() + pos, t);
69 void MathData::insert(size_type pos, MathData const & ar)
71 BOOST_ASSERT(pos <= size());
72 base_type::insert(begin() + pos, ar.begin(), ar.end());
76 void MathData::append(MathData const & ar)
82 void MathData::erase(size_type pos)
89 void MathData::erase(iterator pos1, iterator pos2)
91 base_type::erase(pos1, pos2);
95 void MathData::erase(iterator pos)
97 base_type::erase(pos);
101 void MathData::erase(size_type pos1, size_type pos2)
103 base_type::erase(begin() + pos1, begin() + pos2);
107 void MathData::dump2() const
111 for (const_iterator it = begin(); it != end(); ++it)
113 lyxerr << to_utf8(os.str());
117 void MathData::dump() const
121 for (const_iterator it = begin(); it != end(); ++it)
122 ns << '<' << *it << '>';
123 lyxerr << to_utf8(os.str());
127 void MathData::validate(LaTeXFeatures & features) const
129 for (const_iterator it = begin(); it != end(); ++it)
130 (*it)->validate(features);
134 bool MathData::match(MathData const & ar) const
136 return size() == ar.size() && matchpart(ar, 0);
140 bool MathData::matchpart(MathData 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 MathData::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());
163 // FIXME: temporarily disabled
164 // for (const_iterator it = begin(); it != end(); ++it)
165 // it->nucleus()->replace(rep);
169 bool MathData::find1(MathData const & ar, size_type pos) const
171 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
172 for (size_type i = 0, n = ar.size(); i < n; ++i)
173 if (asString(operator[](pos + i)) != asString(ar[i]))
179 MathData::size_type MathData::find(MathData const & ar) const
181 for (int i = 0, last = size() - ar.size(); i < last; ++i)
188 MathData::size_type MathData::find_last(MathData const & ar) const
190 for (int i = size() - ar.size(); i >= 0; --i)
197 bool MathData::contains(MathData const & ar) const
199 if (find(ar) != size())
201 for (const_iterator it = begin(); it != end(); ++it)
202 if ((*it)->contains(ar))
208 void MathData::touch() const
215 bool isInside(DocIterator const & it, MathData const & ar,
216 pos_type p1, pos_type p2)
218 for (size_t i = 0; i != it.depth(); ++i) {
219 CursorSlice const & sl = it[i];
220 if (sl.inset().inMathed() && &sl.cell() == &ar)
221 return p1 <= sl.pos() && sl.pos() < p2;
230 void MathData::metrics(MetricsInfo & mi, Dimension & dim) const
232 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
233 dim = fm.dimension('I');
234 int xascent = fm.dimension('x').ascent();
235 if (xascent >= dim.asc)
236 xascent = (2 * dim.asc) / 3;
238 mindes_ = (3 * xascent) / 4;
239 slevel_ = (4 * xascent) / 5;
240 sshift_ = xascent / 4;
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 MathData 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();
276 atom_dims_.push_back(d);
279 kerning_ = at->kerning();
281 // Cache the dimension.
282 mi.base.bv->coordCache().arrays().add(this, dim);
286 void MathData::draw(PainterInfo & pi, int x, int y) const
288 //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
289 BufferView & bv = *pi.base.bv;
292 Dimension const & dim = bv.coordCache().getArrays().dim(this);
295 pi.pain.rectangle(x, y - dim.ascent(), dim.width(), dim.height(), Color::mathline);
299 // don't draw outside the workarea
300 if (y + dim.descent() <= 0
301 || y - dim.ascent() >= bv.workHeight()
302 || x + dim.width() <= 0
303 || x >= bv. workWidth())
306 for (size_t i = 0, n = size(); i != n; ++i) {
307 MathAtom const & at = operator[](i);
309 Buffer const & buf = bv.buffer();
310 // special macro handling
311 MathMacro const * mac = at->asMacro();
312 if (mac && buf.hasMacro(mac->name())) {
313 MacroData const & tmpl = buf.getMacro(mac->name());
314 int numargs = tmpl.numargs();
317 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
318 mac->drawExpanded(pi, x, y);
319 x += mac->widthExpanded();
325 bv.coordCache().insets().add(at.nucleus(), x, y);
326 at->drawSelection(pi, x, y);
328 x += atom_dims_[i].wid;
333 void MathData::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
337 for (const_iterator it = begin(); it != end(); ++it) {
338 (*it)->metricsT(mi, d);
344 void MathData::drawT(TextPainter & pain, int x, int y) const
346 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
348 // FIXME: Abdel 16/10/2006
349 // This drawT() method is never used, this is dead code.
351 for (const_iterator it = begin(), et = end(); it != et; ++it) {
352 (*it)->drawT(pain, x, y);
353 //x += (*it)->width_;
359 int MathData::pos2x(size_type pos) const
361 return pos2x(pos, 0);
365 int MathData::pos2x(size_type pos, int glue) const
368 size_type target = min(pos, size());
369 for (size_type i = 0; i < target; ++i) {
370 const_iterator it = begin() + i;
371 if ((*it)->getChar() == ' ')
373 //lyxerr << "char: " << (*it)->getChar()
374 // << "width: " << (*it)->width() << std::endl;
375 x += atom_dims_[i].wid;
381 MathData::size_type MathData::x2pos(int targetx) const
383 return x2pos(targetx, 0);
387 MathData::size_type MathData::x2pos(int targetx, int glue) const
389 const_iterator it = begin();
392 // find first position after targetx
393 for (; currx < targetx && it < end(); ++it) {
395 if ((*it)->getChar() == ' ')
397 currx += atom_dims_[it - begin()].wid;
401 * If we are not at the beginning of the array, go to the left
402 * of the inset if one of the following two condition holds:
403 * - the current inset is editable (so that the cursor tip is
404 * deeper than us): in this case, we want all intermediate
405 * cursor slices to be before insets;
406 * - the mouse is closer to the left side of the inset than to
408 * See bug 1918 for details.
410 if (it != begin() && currx >= targetx
411 && ((*boost::prior(it))->asNestInset()
412 || abs(lastx - targetx) < abs(currx - targetx))) {
420 int MathData::dist(BufferView const & bv, int x, int y) const
422 return bv.coordCache().getArrays().squareDistance(this, x, y);
426 void MathData::setXY(BufferView & bv, int x, int y) const
428 //lyxerr << "setting position cache for MathData " << this << std::endl;
429 bv.coordCache().arrays().add(this, x, y);
433 Dimension const & MathData::dimension(BufferView const & bv) const
436 static Dimension dummy;
440 return bv.coordCache().getArrays().dim(this);
444 int MathData::xm(BufferView const & bv) const
446 Geometry const & g = bv.coordCache().getArrays().geometry(this);
448 return g.pos.x_ + g.dim.wid / 2;
452 int MathData::ym(BufferView const & bv) const
454 Geometry const & g = bv.coordCache().getArrays().geometry(this);
456 return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
460 int MathData::xo(BufferView const & bv) const
462 return bv.coordCache().getArrays().x(this);
466 int MathData::yo(BufferView const & bv) const
468 return bv.coordCache().getArrays().y(this);
472 std::ostream & operator<<(std::ostream & os, MathData const & ar)
474 odocstringstream oss;
475 NormalStream ns(oss);
477 return os << to_utf8(oss.str());
481 odocstream & operator<<(odocstream & os, MathData const & ar)