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>
32 #include <boost/next_prior.hpp>
40 using std::ostringstream;
45 MathData::MathData(const_iterator from, const_iterator to)
50 MathAtom & MathData::operator[](pos_type pos)
52 BOOST_ASSERT(pos < size());
53 return base_type::operator[](pos);
57 MathAtom const & MathData::operator[](pos_type pos) const
59 BOOST_ASSERT(pos < size());
60 return base_type::operator[](pos);
64 void MathData::insert(size_type pos, MathAtom const & t)
66 base_type::insert(begin() + pos, t);
70 void MathData::insert(size_type pos, MathData const & ar)
72 BOOST_ASSERT(pos <= size());
73 base_type::insert(begin() + pos, ar.begin(), ar.end());
77 void MathData::append(MathData const & ar)
83 void MathData::erase(size_type pos)
90 void MathData::erase(iterator pos1, iterator pos2)
92 base_type::erase(pos1, pos2);
96 void MathData::erase(iterator pos)
98 base_type::erase(pos);
102 void MathData::erase(size_type pos1, size_type pos2)
104 base_type::erase(begin() + pos1, begin() + pos2);
108 void MathData::dump2() const
112 for (const_iterator it = begin(); it != end(); ++it)
114 lyxerr << to_utf8(os.str());
118 void MathData::dump() const
122 for (const_iterator it = begin(); it != end(); ++it)
123 ns << '<' << *it << '>';
124 lyxerr << to_utf8(os.str());
128 void MathData::validate(LaTeXFeatures & features) const
130 for (const_iterator it = begin(); it != end(); ++it)
131 (*it)->validate(features);
135 bool MathData::match(MathData const & ar) const
137 return size() == ar.size() && matchpart(ar, 0);
141 bool MathData::matchpart(MathData const & ar, pos_type pos) const
143 if (size() < ar.size() + pos)
145 const_iterator it = begin() + pos;
146 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
147 if (asString(*it) != asString(*jt))
153 void MathData::replace(ReplaceData & rep)
155 for (size_type i = 0; i < size(); ++i) {
156 if (find1(rep.from, i)) {
158 lyxerr << "match found!" << endl;
159 erase(i, i + rep.from.size());
164 // FIXME: temporarily disabled
165 // for (const_iterator it = begin(); it != end(); ++it)
166 // it->nucleus()->replace(rep);
170 bool MathData::find1(MathData 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 MathData::size_type MathData::find(MathData const & ar) const
182 for (int i = 0, last = size() - ar.size(); i < last; ++i)
189 MathData::size_type MathData::find_last(MathData const & ar) const
191 for (int i = size() - ar.size(); i >= 0; --i)
198 bool MathData::contains(MathData const & ar) const
200 if (find(ar) != size())
202 for (const_iterator it = begin(); it != end(); ++it)
203 if ((*it)->contains(ar))
209 void MathData::touch() const
216 bool isInside(DocIterator const & it, MathData const & ar,
217 pos_type p1, pos_type p2)
219 for (size_t i = 0; i != it.depth(); ++i) {
220 CursorSlice const & sl = it[i];
221 if (sl.inset().inMathed() && &sl.cell() == &ar)
222 return p1 <= sl.pos() && sl.pos() < p2;
231 void MathData::metrics(MetricsInfo & mi, Dimension & dim) const
233 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
234 dim = fm.dimension('I');
235 int xascent = fm.dimension('x').ascent();
236 if (xascent >= dim.asc)
237 xascent = (2 * dim.asc) / 3;
239 mindes_ = (3 * xascent) / 4;
240 slevel_ = (4 * xascent) / 5;
241 sshift_ = xascent / 4;
251 //BufferView & bv = *mi.base.bv;
252 //Buffer const & buf = bv.buffer();
253 for (size_t i = 0, n = size(); i != n; ++i) {
254 MathAtom const & at = operator[](i);
256 MathMacro const * mac = at->asMacro();
257 if (mac && buf.hasMacro(mac->name())) {
258 MacroData const & tmpl = buf.getMacro(mac->name());
259 int numargs = tmpl.numargs();
262 lyxerr << "metrics:found macro: " << mac->name()
263 << " numargs: " << numargs << endl;
264 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
265 MathData args(begin() + i + 1, begin() + i + numargs + 1);
267 tmpl.expand(args, exp);
268 mac->setExpansion(exp, args);
269 mac->metricsExpanded(mi, d);
270 dim.wid += mac->widthExpanded();
277 atom_dims_.push_back(d);
280 kerning_ = at->kerning();
282 // Cache the dimension.
283 mi.base.bv->coordCache().arrays().add(this, dim);
287 void MathData::draw(PainterInfo & pi, int x, int y) const
289 //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
290 BufferView & bv = *pi.base.bv;
293 Dimension const & dim = bv.coordCache().getArrays().dim(this);
296 pi.pain.rectangle(x, y - dim.ascent(), dim.width(), dim.height(), Color::mathline);
300 // don't draw outside the workarea
301 if (y + dim.descent() <= 0
302 || y - dim.ascent() >= bv.workHeight()
303 || x + dim.width() <= 0
304 || x >= bv. workWidth())
307 for (size_t i = 0, n = size(); i != n; ++i) {
308 MathAtom const & at = operator[](i);
310 Buffer const & buf = bv.buffer();
311 // special macro handling
312 MathMacro const * mac = at->asMacro();
313 if (mac && buf.hasMacro(mac->name())) {
314 MacroData const & tmpl = buf.getMacro(mac->name());
315 int numargs = tmpl.numargs();
318 if (!isInside(bv.cursor(), *this, i + 1, i + numargs + 1)) {
319 mac->drawExpanded(pi, x, y);
320 x += mac->widthExpanded();
326 bv.coordCache().insets().add(at.nucleus(), x, y);
327 at->drawSelection(pi, x, y);
329 x += atom_dims_[i].wid;
334 void MathData::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
338 for (const_iterator it = begin(); it != end(); ++it) {
339 (*it)->metricsT(mi, d);
345 void MathData::drawT(TextPainter & pain, int x, int y) const
347 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
349 // FIXME: Abdel 16/10/2006
350 // This drawT() method is never used, this is dead code.
352 for (const_iterator it = begin(), et = end(); it != et; ++it) {
353 (*it)->drawT(pain, x, y);
354 //x += (*it)->width_;
360 int MathData::pos2x(size_type pos) const
362 return pos2x(pos, 0);
366 int MathData::pos2x(size_type pos, int glue) const
369 size_type target = min(pos, size());
370 for (size_type i = 0; i < target; ++i) {
371 const_iterator it = begin() + i;
372 if ((*it)->getChar() == ' ')
374 //lyxerr << "char: " << (*it)->getChar()
375 // << "width: " << (*it)->width() << std::endl;
376 x += atom_dims_[i].wid;
382 MathData::size_type MathData::x2pos(int targetx) const
384 return x2pos(targetx, 0);
388 MathData::size_type MathData::x2pos(int targetx, int glue) const
390 const_iterator it = begin();
393 // find first position after targetx
394 for (; currx < targetx && it < end(); ++it) {
396 if ((*it)->getChar() == ' ')
398 currx += atom_dims_[it - begin()].wid;
402 * If we are not at the beginning of the array, go to the left
403 * of the inset if one of the following two condition holds:
404 * - the current inset is editable (so that the cursor tip is
405 * deeper than us): in this case, we want all intermediate
406 * cursor slices to be before insets;
407 * - the mouse is closer to the left side of the inset than to
409 * See bug 1918 for details.
411 if (it != begin() && currx >= targetx
412 && ((*boost::prior(it))->asNestInset()
413 || abs(lastx - targetx) < abs(currx - targetx))) {
421 int MathData::dist(BufferView const & bv, int x, int y) const
423 return bv.coordCache().getArrays().squareDistance(this, x, y);
427 void MathData::setXY(BufferView & bv, int x, int y) const
429 //lyxerr << "setting position cache for MathData " << this << std::endl;
430 bv.coordCache().arrays().add(this, x, y);
434 Dimension const & MathData::dimension(BufferView const & bv) const
437 static Dimension dummy;
441 return bv.coordCache().getArrays().dim(this);
445 int MathData::xm(BufferView const & bv) const
447 Geometry const & g = bv.coordCache().getArrays().geometry(this);
449 return g.pos.x_ + g.dim.wid / 2;
453 int MathData::ym(BufferView const & bv) const
455 Geometry const & g = bv.coordCache().getArrays().geometry(this);
457 return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
461 int MathData::xo(BufferView const & bv) const
463 return bv.coordCache().getArrays().x(this);
467 int MathData::yo(BufferView const & bv) const
469 return bv.coordCache().getArrays().y(this);
473 std::ostream & operator<<(std::ostream & os, MathData const & ar)
475 odocstringstream oss;
476 NormalStream ns(oss);
478 return os << to_utf8(oss.str());
482 odocstream & operator<<(odocstream & os, MathData const & ar)