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 "InsetMathBrace.h"
18 #include "MathMacroTable.h"
19 #include "MathStream.h"
20 #include "MathSupport.h"
21 #include "MathReplace.h"
23 #include "BufferView.h"
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
32 #include <boost/assert.hpp>
40 using std::ostringstream;
45 MathArray::MathArray(const_iterator from, const_iterator to)
50 MathAtom & MathArray::operator[](pos_type pos)
52 BOOST_ASSERT(pos < size());
53 return base_type::operator[](pos);
57 MathAtom const & MathArray::operator[](pos_type pos) const
59 BOOST_ASSERT(pos < size());
60 return base_type::operator[](pos);
64 void MathArray::insert(size_type pos, MathAtom const & t)
66 base_type::insert(begin() + pos, t);
70 void MathArray::insert(size_type pos, MathArray const & ar)
72 BOOST_ASSERT(pos <= size());
73 base_type::insert(begin() + pos, ar.begin(), ar.end());
77 void MathArray::append(MathArray const & ar)
83 void MathArray::erase(size_type pos)
90 void MathArray::erase(iterator pos1, iterator pos2)
92 base_type::erase(pos1, pos2);
96 void MathArray::erase(iterator pos)
98 base_type::erase(pos);
102 void MathArray::erase(size_type pos1, size_type pos2)
104 base_type::erase(begin() + pos1, begin() + pos2);
108 void MathArray::dump2() const
112 for (const_iterator it = begin(); it != end(); ++it)
114 lyxerr << to_utf8(os.str());
118 void MathArray::dump() const
122 for (const_iterator it = begin(); it != end(); ++it)
123 ns << '<' << *it << '>';
124 lyxerr << to_utf8(os.str());
128 void MathArray::validate(LaTeXFeatures & features) const
130 for (const_iterator it = begin(); it != end(); ++it)
131 (*it)->validate(features);
135 bool MathArray::match(MathArray const & ar) const
137 return size() == ar.size() && matchpart(ar, 0);
141 bool MathArray::matchpart(MathArray 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 MathArray::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());
165 #warning temporarily disabled
166 // for (const_iterator it = begin(); it != end(); ++it)
167 // it->nucleus()->replace(rep);
172 bool MathArray::find1(MathArray const & ar, size_type pos) const
174 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
175 for (size_type i = 0, n = ar.size(); i < n; ++i)
176 if (asString(operator[](pos + i)) != asString(ar[i]))
182 MathArray::size_type MathArray::find(MathArray const & ar) const
184 for (int i = 0, last = size() - ar.size(); i < last; ++i)
191 MathArray::size_type MathArray::find_last(MathArray const & ar) const
193 for (int i = size() - ar.size(); i >= 0; --i)
200 bool MathArray::contains(MathArray const & ar) const
202 if (find(ar) != size())
204 for (const_iterator it = begin(); it != end(); ++it)
205 if ((*it)->contains(ar))
211 void MathArray::touch() const
216 bool MathArray::metrics(MetricsInfo & mi, Dimension & dim) const
229 bool isInside(DocIterator const & it, MathArray const & ar,
230 pos_type p1, pos_type p2)
232 for (size_t i = 0; i != it.depth(); ++i) {
233 CursorSlice const & sl = it[i];
234 if (sl.inset().inMathed() && &sl.cell() == &ar)
235 return p1 <= sl.pos() && sl.pos() < p2;
243 void MathArray::metrics(MetricsInfo & mi) const
245 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
246 dim_ = fm.dimension('I');
247 int xascent = fm.dimension('x').ascent();
248 if (xascent >= dim_.asc)
249 xascent = (2 * dim_.asc) / 3;
251 mindes_ = (3 * xascent) / 4;
252 slevel_ = (4 * xascent) / 5;
253 sshift_ = xascent / 4;
259 const_cast<MathArray*>(this)->updateMacros( mi );
264 for (size_t i = 0; i != size(); ++i) {
265 MathAtom const & at = operator[](i);
269 kerning_ = at->kerning();
274 void MathArray::draw(PainterInfo & pi, int x, int y) const
276 //lyxerr << "MathArray::draw: x: " << x << " y: " << y << endl;
277 BufferView & bv = *pi.base.bv;
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() >= bv.workHeight()
289 || x >= bv. workWidth())
292 for (size_t i = 0, n = size(); i != n; ++i) {
293 MathAtom const & at = operator[](i);
294 bv.coordCache().insets().add(at.nucleus(), x, y);
295 at->drawSelection(pi, x, y);
302 void MathArray::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
306 for (const_iterator it = begin(); it != end(); ++it) {
307 (*it)->metricsT(mi, d);
313 void MathArray::drawT(TextPainter & pain, int x, int y) const
315 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
317 // FIXME: Abdel 16/10/2006
318 // This drawT() method is never used, this is dead code.
320 for (const_iterator it = begin(), et = end(); it != et; ++it) {
321 (*it)->drawT(pain, x, y);
322 //x += (*it)->width_;
328 void MathArray::updateMacros(MetricsInfo & mi) {
329 Buffer *buf = mi.base.bv->buffer();
331 // go over the array and look for macros
332 for (size_t i = 0; i != size(); ++i) {
333 InsetMath * at = operator[](i).nucleus();
334 MathMacro * macroInset = at->asMacro();
336 // get arity of macro or 0 if unknown
338 if (buf->hasMacro(macroInset->name())) {
339 MacroData const & macro = buf->getMacro(macroInset->name());
340 numargs = macro.numargs();
343 // arity of macro changed?
344 if (macroInset->nargs() != numargs) {
345 // detach all arguments
346 std::vector<MathArray> detachedArgs;
347 macroInset->detachArguments( detachedArgs );
349 // too many arguments in the macro inset?
350 if (detachedArgs.size() > numargs) {
351 // insert overlap back as braces
352 std::vector<MathArray> overlap(detachedArgs.begin()+numargs, detachedArgs.end());
353 detachedArgs.erase(detachedArgs.begin()+numargs, detachedArgs.end());
354 for (size_t j = 0; j < overlap.size(); ++j) {
355 MathArray const & arg = overlap[j];
357 insert(i+j+1, MathAtom(new InsetMathBrace(arg)));
359 insert(i+j+1, arg[0]);
363 // insert some cells from the array into the macro inset
364 size_t missingArgs = numargs-detachedArgs.size();
366 for (j = 0; j < missingArgs && i+1+j < size(); ++j) {
367 MathAtom & cell = operator[](i+1+j);
368 InsetMathBrace const * brace = cell->asBraceInset();
370 // found brace, convert into argument
371 detachedArgs.push_back(brace->cell(0));
374 array.insert(0, cell);
375 detachedArgs.push_back(array);
379 // remove them from the array
380 erase(begin()+i+1, begin()+i+1+j);
382 // enough for the macro inset now?
383 // Add some empty ones of necessary
384 for (; j < missingArgs; ++j)
385 detachedArgs.insert(detachedArgs.end(), MathArray());
388 // attach arguments back to macro inset
389 macroInset->attachArguments(detachedArgs);
396 int MathArray::pos2x(size_type pos) const
398 return pos2x(pos, 0);
402 int MathArray::pos2x(size_type pos, int glue) const
405 size_type target = min(pos, size());
406 for (size_type i = 0; i < target; ++i) {
407 const_iterator it = begin() + i;
408 if ((*it)->getChar() == ' ')
410 //lyxerr << "char: " << (*it)->getChar()
411 // << "width: " << (*it)->width() << std::endl;
418 MathArray::size_type MathArray::x2pos(int targetx) const
420 return x2pos(targetx, 0);
424 MathArray::size_type MathArray::x2pos(int targetx, int glue) const
426 const_iterator it = begin();
429 // find first position after targetx
430 for (; currx < targetx && it < end(); ++it) {
432 if ((*it)->getChar() == ' ')
434 currx += (*it)->width();
438 * If we are not at the beginning of the array, go to the left
439 * of the inset if one of the following two condition holds:
440 * - the current inset is editable (so that the cursor tip is
441 * deeper than us): in this case, we want all intermediate
442 * cursor slices to be before insets;
443 * - the mouse is closer to the left side of the inset than to
445 * See bug 1918 for details.
447 if (it != begin() && currx >= targetx
448 && ((*boost::prior(it))->asNestInset()
449 || abs(lastx - targetx) < abs(currx - targetx))) {
457 int MathArray::dist(BufferView const & bv, int x, int y) const
462 const int xo_ = xo(bv);
463 const int yo_ = yo(bv);
467 else if (x > xo_ + width())
468 xx = x - xo_ - width();
470 if (y < yo_ - ascent())
471 yy = yo_ - ascent() - y;
472 else if (y > yo_ + descent())
473 yy = y - yo_ - descent();
479 void MathArray::setXY(BufferView & bv, int x, int y) const
481 //lyxerr << "setting position cache for MathArray " << this << std::endl;
482 bv.coordCache().arrays().add(this, x, y);
486 int MathArray::xo(BufferView const & bv) const
488 return bv.coordCache().getArrays().x(this);
492 int MathArray::yo(BufferView const & bv) const
494 return bv.coordCache().getArrays().y(this);
498 std::ostream & operator<<(std::ostream & os, MathArray const & ar)
500 odocstringstream oss;
501 NormalStream ns(oss);
503 return os << to_utf8(oss.str());
507 odocstream & operator<<(odocstream & os, MathArray const & ar)