3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Stefan Schimanski
9 * Full author contact details are available in file CREDITS.
16 #include "InsetMathBrace.h"
17 #include "InsetMathFont.h"
18 #include "InsetMathScript.h"
19 #include "MacroTable.h"
20 #include "InsetMathMacro.h"
21 #include "MathStream.h"
22 #include "MathSupport.h"
23 #include "MetricsInfo.h"
24 #include "ReplaceData.h"
27 #include "BufferView.h"
28 #include "CoordCache.h"
31 #include "mathed/InsetMathUnknown.h"
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/lassert.h"
40 #include "support/lyxalgo.h"
49 MathData::MathData(Buffer * buf, const_iterator from, const_iterator to)
50 : base_type(from, to), minasc_(0), mindes_(0), slevel_(0),
51 sshift_(0), kerning_(0), buffer_(buf)
55 void MathData::setBuffer(Buffer & b)
58 for (MathAtom & at : *this)
59 at.nucleus()->setBuffer(b);
63 MathAtom & MathData::operator[](pos_type pos)
65 LBUFERR(pos < size());
66 return base_type::operator[](pos);
70 MathAtom const & MathData::operator[](pos_type pos) const
72 LBUFERR(pos < size());
73 return base_type::operator[](pos);
77 void MathData::insert(size_type pos, MathAtom const & t)
79 LBUFERR(pos <= size());
80 base_type::insert(begin() + pos, t);
84 void MathData::insert(size_type pos, MathData const & ar)
86 LBUFERR(pos <= size());
87 base_type::insert(begin() + pos, ar.begin(), ar.end());
91 void MathData::append(MathData const & ar)
97 void MathData::erase(size_type pos)
104 void MathData::erase(iterator pos1, iterator pos2)
106 base_type::erase(pos1, pos2);
110 void MathData::erase(iterator pos)
112 base_type::erase(pos);
116 void MathData::erase(size_type pos1, size_type pos2)
118 base_type::erase(begin() + pos1, begin() + pos2);
122 void MathData::dump2() const
126 for (const_iterator it = begin(); it != end(); ++it)
128 lyxerr << to_utf8(os.str());
132 void MathData::dump() const
136 for (const_iterator it = begin(); it != end(); ++it)
137 ns << '<' << *it << '>';
138 lyxerr << to_utf8(os.str());
142 void MathData::validate(LaTeXFeatures & features) const
144 for (const_iterator it = begin(); it != end(); ++it)
145 (*it)->validate(features);
149 bool MathData::match(MathData const & ar) const
151 return size() == ar.size() && matchpart(ar, 0);
155 bool MathData::matchpart(MathData const & ar, pos_type pos) const
157 if (size() < ar.size() + pos)
159 const_iterator it = begin() + pos;
160 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
161 if (asString(*it) != asString(*jt))
167 void MathData::replace(ReplaceData & rep)
169 for (size_type i = 0; i < size(); ++i) {
170 if (find1(rep.from, i)) {
172 lyxerr << "match found!" << endl;
173 erase(i, i + rep.from.size());
178 // FIXME: temporarily disabled
179 // for (const_iterator it = begin(); it != end(); ++it)
180 // it->nucleus()->replace(rep);
184 bool MathData::find1(MathData const & ar, size_type pos) const
186 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
187 for (size_type i = 0, n = ar.size(); i < n; ++i)
188 if (asString(operator[](pos + i)) != asString(ar[i]))
194 MathData::size_type MathData::find(MathData const & ar) const
196 for (int i = 0, last = size() - ar.size(); i < last; ++i)
203 MathData::size_type MathData::find_last(MathData const & ar) const
205 for (int i = size() - ar.size(); i >= 0; --i)
212 bool MathData::contains(MathData const & ar) const
214 if (find(ar) != size())
216 for (const_iterator it = begin(); it != end(); ++it)
217 if ((*it)->contains(ar))
223 bool MathData::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
225 bool has_contents = false;
226 BufferView * bv = mi.base.bv;
227 MathData * ar = const_cast<MathData*>(this);
228 ar->updateMacros(&bv->cursor(), mi.macrocontext,
229 InternalUpdate, mi.base.macro_nesting);
232 // FIXME: for completion, try to insert the relevant data in the
233 // mathrow (like is done for text rows). We could add a pair of
234 // InsetMathColor inset, but these come with extra spacing of
236 DocIterator const & inlineCompletionPos = bv->inlineCompletionPos();
237 bool const has_completion = inlineCompletionPos.inMathed()
238 && &inlineCompletionPos.cell() == this;
239 size_t const compl_pos = has_completion ? inlineCompletionPos.pos() : 0;
241 for (size_t i = 0 ; i < size() ; ++i) {
242 has_contents |= (*this)[i]->addToMathRow(mrow, mi);
243 if (i + 1 == compl_pos) {
244 mrow.back().compl_text = bv->inlineCompletion();
245 mrow.back().compl_unique_to = bv->inlineCompletionUniqueChars();
255 bool isInside(DocIterator const & it, MathData const & ar,
256 pos_type p1, pos_type p2)
258 for (size_t i = 0; i != it.depth(); ++i) {
259 CursorSlice const & sl = it[i];
260 if (sl.inset().inMathed() && &sl.cell() == &ar)
261 return p1 <= sl.pos() && sl.pos() < p2;
270 void MathData::metrics(MetricsInfo & mi, Dimension & dim, bool tight) const
272 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
273 BufferView * bv = mi.base.bv;
274 int const Iascent = fm.dimension('I').ascent();
275 int xascent = fm.xHeight();
276 if (xascent >= Iascent)
277 xascent = (2 * Iascent) / 3;
279 mindes_ = (3 * xascent) / 4;
280 slevel_ = (4 * xascent) / 5;
281 sshift_ = xascent / 4;
283 MathRow mrow(mi, this);
284 mrow.metrics(mi, dim);
285 kerning_ = mrow.kerning(bv);
287 // Set a minimal ascent/descent for the cell
289 // FIXME: this is the minimal ascent seen empirically, check
290 // what the TeXbook says.
291 dim.asc = max(dim.asc, fm.xHeight());
293 dim.asc = max(dim.asc, fm.maxAscent());
294 dim.des = max(dim.des, fm.maxDescent());
297 // This is one of the the few points where the drawing font is known,
298 // so that we can set the caret vertical dimensions.
299 mrow.caret_ascent = min(dim.asc, fm.maxAscent());
300 mrow.caret_descent = min(dim.des, fm.maxDescent());
301 /// do the same for math cells linearized in the row
302 MathRow caret_row = MathRow(mrow.caret_ascent, mrow.caret_descent);
303 for (auto const & e : mrow)
304 if (e.type == MathRow::BEGIN && e.ar)
305 bv->setMathRow(e.ar, caret_row);
307 // Cache row and dimension.
308 bv->setMathRow(this, mrow);
309 bv->coordCache().arrays().add(this, dim);
313 void MathData::drawSelection(PainterInfo & pi, int const x, int const y) const
315 BufferView const * bv = pi.base.bv;
316 Cursor const & cur = bv->cursor();
317 InsetMath const * inset = cur.inset().asInsetMath();
318 if (!cur.selection() || !inset || inset->nargs() == 0)
321 CursorSlice const s1 = cur.selBegin();
322 CursorSlice const s2 = cur.selEnd();
323 MathData const & c1 = inset->cell(s1.idx());
325 if (s1.idx() == s2.idx() && &c1 == this) {
326 // selection indide cell
327 Dimension const dim = bv->coordCache().getArrays().dim(&c1);
328 int const beg = c1.pos2x(bv, s1.pos());
329 int const end = c1.pos2x(bv, s2.pos());
330 pi.pain.fillRectangle(x + beg, y - dim.ascent(),
331 end - beg, dim.height(), Color_selection);
333 for (idx_type i = 0; i < inset->nargs(); ++i) {
334 MathData const & c = inset->cell(i);
335 if (&c == this && inset->idxBetween(i, s1.idx(), s2.idx())) {
336 // The whole cell is selected
337 Dimension const dim = bv->coordCache().getArrays().dim(&c);
338 pi.pain.fillRectangle(x, y - dim.ascent(),
339 dim.width(), dim.height(),
347 void MathData::draw(PainterInfo & pi, int const x, int const y) const
349 //lyxerr << "MathData::draw: x: " << x << " y: " << y << endl;
350 setXY(*pi.base.bv, x, y);
352 drawSelection(pi, x, y);
353 MathRow const & mrow = pi.base.bv->mathRow(this);
358 void MathData::metricsT(TextMetricsInfo const & mi, Dimension & dim) const
362 for (const_iterator it = begin(); it != end(); ++it) {
363 (*it)->metricsT(mi, d);
369 void MathData::drawT(TextPainter & pain, int x, int y) const
371 //lyxerr << "x: " << x << " y: " << y << ' ' << pain.workAreaHeight() << endl;
373 // FIXME: Abdel 16/10/2006
374 // This drawT() method is never used, this is dead code.
376 for (const_iterator it = begin(), et = end(); it != et; ++it) {
377 (*it)->drawT(pain, x, y);
378 //x += (*it)->width_;
384 void MathData::updateBuffer(ParIterator const & it, UpdateType utype)
387 for (size_t i = 0, n = size(); i != n; ++i) {
388 MathAtom & at = operator[](i);
389 at.nucleus()->updateBuffer(it, utype);
394 void MathData::updateMacros(Cursor * cur, MacroContext const & mc,
395 UpdateType utype, int nesting)
397 // If we are editing a macro, we cannot update it immediately,
398 // otherwise wrong undo steps will be recorded (bug 6208).
399 InsetMath const * inmath = cur ? cur->inset().asInsetMath() : 0;
400 InsetMathMacro const * inmacro = inmath ? inmath->asMacro() : 0;
401 docstring const edited_name = inmacro ? inmacro->name() : docstring();
403 // go over the array and look for macros
404 for (size_t i = 0; i < size(); ++i) {
405 InsetMathMacro * macroInset = operator[](i).nucleus()->asMacro();
406 if (!macroInset || macroInset->macroName().empty()
407 || macroInset->macroName()[0] == '^'
408 || macroInset->macroName()[0] == '_'
409 || (macroInset->name() == edited_name
410 && macroInset->displayMode() ==
411 InsetMathMacro::DISPLAY_UNFOLDED))
415 macroInset->updateMacro(mc);
416 size_t macroNumArgs = 0;
417 size_t macroOptionals = 0;
418 MacroData const * macro = macroInset->macro();
420 macroNumArgs = macro->numargs();
421 macroOptionals = macro->optionals();
424 // store old and compute new display mode
425 InsetMathMacro::DisplayMode newDisplayMode;
426 InsetMathMacro::DisplayMode oldDisplayMode = macroInset->displayMode();
427 newDisplayMode = macroInset->computeDisplayMode();
429 // arity changed or other reason to detach?
430 if (oldDisplayMode == InsetMathMacro::DISPLAY_NORMAL
431 && (macroInset->arity() != macroNumArgs
432 || macroInset->optionals() != macroOptionals
433 || newDisplayMode == InsetMathMacro::DISPLAY_UNFOLDED))
434 detachMacroParameters(cur, i);
436 // the macro could have been copied while resizing this
437 macroInset = operator[](i).nucleus()->asMacro();
440 if (newDisplayMode != InsetMathMacro::DISPLAY_UNFOLDED
441 && oldDisplayMode == InsetMathMacro::DISPLAY_UNFOLDED) {
442 // put cursor in front of macro
444 int macroSlice = cur->find(macroInset);
445 if (macroSlice != -1)
446 cur->cutOff(macroSlice - 1);
450 // update the display mode
451 size_t appetite = macroInset->appetite();
452 macroInset->setDisplayMode(newDisplayMode);
455 if (newDisplayMode == InsetMathMacro::DISPLAY_NORMAL
456 && (macroInset->arity() != macroNumArgs
457 || macroInset->optionals() != macroOptionals)) {
458 // is it a virgin macro which was never attached to parameters?
459 bool fromInitToNormalMode
460 = (oldDisplayMode == InsetMathMacro::DISPLAY_INIT
461 || oldDisplayMode == InsetMathMacro::DISPLAY_INTERACTIVE_INIT)
462 && newDisplayMode == InsetMathMacro::DISPLAY_NORMAL;
464 // if the macro was entered interactively (i.e. not by paste or during
465 // loading), it should not be greedy, but the cursor should
466 // automatically jump into the macro when behind
467 bool interactive = (oldDisplayMode == InsetMathMacro::DISPLAY_INTERACTIVE_INIT);
470 attachMacroParameters(cur, i, macroNumArgs, macroOptionals,
471 fromInitToNormalMode, interactive, appetite);
474 cur->updateInsets(&cur->bottom().inset());
477 // Give macro the chance to adapt to new situation.
478 // The macroInset could be invalid now because it was put into a script
479 // inset and therefore "deep" copied. So get it again from the MathData.
480 InsetMath * inset = operator[](i).nucleus();
481 if (inset->asScriptInset())
482 inset = inset->asScriptInset()->nuc()[0].nucleus();
483 LASSERT(inset->asMacro(), continue);
484 inset->asMacro()->updateRepresentation(cur, mc, utype, nesting + 1);
489 void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos)
491 InsetMathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
492 // We store this now, because the inset pointer will be invalidated in the scond loop below
493 size_t const optionals = macroInset->optionals();
495 // detach all arguments
496 vector<MathData> detachedArgs;
497 if (macroPos + 1 == size())
498 // strip arguments if we are at the MathData end
499 macroInset->detachArguments(detachedArgs, true);
501 macroInset->detachArguments(detachedArgs, false);
504 int curMacroSlice = -1;
506 curMacroSlice = cur->find(macroInset);
507 idx_type curMacroIdx = -1;
508 pos_type curMacroPos = -1;
509 vector<CursorSlice> argSlices;
510 if (curMacroSlice != -1) {
511 curMacroPos = (*cur)[curMacroSlice].pos();
512 curMacroIdx = (*cur)[curMacroSlice].idx();
513 cur->cutOff(curMacroSlice, argSlices);
517 // only [] after the last non-empty argument can be dropped later
518 size_t lastNonEmptyOptional = 0;
519 for (size_t l = 0; l < detachedArgs.size() && l < optionals; ++l) {
520 if (!detachedArgs[l].empty())
521 lastNonEmptyOptional = l;
524 // optional arguments to be put back?
525 pos_type p = macroPos + 1;
527 // We do not want to use macroInset below, the insert() call in
528 // the loop will invalidate it.
530 for (; j < detachedArgs.size() && j < optionals; ++j) {
531 // another non-empty parameter follows?
532 bool canDropEmptyOptional = j >= lastNonEmptyOptional;
534 // then we can drop empty optional parameters
535 if (detachedArgs[j].empty() && canDropEmptyOptional) {
536 if (curMacroIdx == j)
537 (*cur)[curMacroSlice - 1].pos() = macroPos + 1;
541 // Otherwise we don't drop an empty optional, put it back normally
543 asArray(from_ascii("[]"), optarg);
544 MathData & arg = detachedArgs[j];
546 // look for "]", i.e. put a brace around?
547 InsetMathBrace * brace = 0;
548 for (size_t q = 0; q < arg.size(); ++q) {
549 if (arg[q]->getChar() == ']') {
551 brace = new InsetMathBrace(buffer_);
556 // put arg between []
558 brace->cell(0) = arg;
559 optarg.insert(1, MathAtom(brace));
561 optarg.insert(1, arg);
563 // insert it into the array
568 if (curMacroSlice == -1)
571 // cursor in optional argument of macro?
572 if (curMacroIdx == j) {
574 cur->append(0, curMacroPos);
575 (*cur)[curMacroSlice - 1].pos() = macroPos + 2;
577 (*cur)[curMacroSlice - 1].pos() = macroPos + 2 + curMacroPos;
578 cur->append(argSlices);
579 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
580 // cursor right of macro
581 (*cur)[curMacroSlice - 1].pos() += optarg.size();
584 // put them back into the MathData
585 for (; j < detachedArgs.size(); ++j, ++p) {
586 MathData const & arg = detachedArgs[j];
588 && !arg[0]->asScriptInset()
589 && !(arg[0]->asMacro() && arg[0]->asMacro()->arity() > 0))
592 insert(p, MathAtom(new InsetMathBrace(arg)));
595 if (curMacroSlice == -1)
598 // cursor in j-th argument of macro?
599 if (curMacroIdx == j) {
600 if (operator[](p).nucleus()->asBraceInset()) {
601 (*cur)[curMacroSlice - 1].pos() = p;
602 cur->append(0, curMacroPos);
603 cur->append(argSlices);
605 (*cur)[curMacroSlice - 1].pos() = p; // + macroPos;
606 cur->append(argSlices);
608 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
609 ++(*cur)[curMacroSlice - 1].pos();
613 cur->updateInsets(&cur->bottom().inset());
617 void MathData::attachMacroParameters(Cursor * cur,
618 const size_type macroPos, const size_type macroNumArgs,
619 const int macroOptionals, const bool fromInitToNormalMode,
620 const bool interactiveInit, const size_t appetite)
622 InsetMathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
624 // start at atom behind the macro again, maybe with some new arguments
625 // from the detach phase above, to add them back into the macro inset
626 size_t p = macroPos + 1;
627 vector<MathData> detachedArgs;
628 MathAtom scriptToPutAround;
630 // find cursor slice again of this MathData
633 thisSlice = cur->find(*this);
636 thisPos = (*cur)[thisSlice].pos();
638 // find arguments behind the macro
639 if (!interactiveInit) {
640 collectOptionalParameters(cur, macroOptionals, detachedArgs, p,
641 scriptToPutAround, macroPos, thisPos, thisSlice);
643 collectParameters(cur, macroNumArgs, detachedArgs, p,
644 scriptToPutAround, macroPos, thisPos, thisSlice, appetite);
646 // attach arguments back to macro inset
647 macroInset->attachArguments(detachedArgs, macroNumArgs, macroOptionals);
649 // found tail script? E.g. \foo{a}b^x
650 if (scriptToPutAround.nucleus()) {
651 InsetMathScript * scriptInset =
652 scriptToPutAround.nucleus()->asScriptInset();
653 // In the math parser we remove empty braces in the base
654 // of a script inset, but we have to restore them here.
655 if (scriptInset->nuc().empty()) {
657 scriptInset->nuc().push_back(
658 MathAtom(new InsetMathBrace(ar)));
660 // put macro into a script inset
661 scriptInset->nuc()[0] = operator[](macroPos);
662 operator[](macroPos) = scriptToPutAround;
664 // go into the script inset nucleus
665 if (cur && thisPos == int(macroPos))
668 // get pointer to "deep" copied macro inset
669 scriptInset = operator[](macroPos).nucleus()->asScriptInset();
670 macroInset = scriptInset->nuc()[0].nucleus()->asMacro();
673 // remove them from the MathData
674 erase(macroPos + 1, p);
676 // cursor outside this MathData?
680 // fix cursor if right of p
681 if (thisPos >= int(p))
682 (*cur)[thisSlice].pos() -= p - (macroPos + 1);
684 // was the macro inset just inserted interactively and was now folded
685 // and the cursor is just behind?
686 if ((*cur)[thisSlice].pos() == int(macroPos + 1)
688 && fromInitToNormalMode
689 && macroInset->arity() > 0
690 && thisSlice + 1 == int(cur->depth())) {
691 // then enter it if the cursor was just behind
692 (*cur)[thisSlice].pos() = macroPos;
693 cur->push_back(CursorSlice(*macroInset));
694 macroInset->idxFirst(*cur);
699 void MathData::collectOptionalParameters(Cursor * cur,
700 const size_type numOptionalParams, vector<MathData> & params,
701 size_t & pos, MathAtom & scriptToPutAround,
702 const pos_type macroPos, const int thisPos, const int thisSlice)
704 Buffer * buf = cur ? cur->buffer() : 0;
705 // insert optional arguments?
706 while (params.size() < numOptionalParams
708 && !scriptToPutAround.nucleus()) {
709 // is a [] block following which could be an optional parameter?
710 if (operator[](pos)->getChar() != '[')
713 // found possible optional argument, look for pairing "]"
715 size_t right = pos + 1;
716 for (; right < size(); ++right) {
717 MathAtom & cell = operator[](right);
719 if (cell->getChar() == '[')
721 else if (cell->getChar() == ']' && --count == 0)
725 // maybe "]" with a script around?
726 InsetMathScript * script = cell.nucleus()->asScriptInset();
729 if (script->nuc().size() != 1)
731 if (script->nuc()[0]->getChar() == ']') {
732 // script will be put around the macro later
733 scriptToPutAround = cell;
739 if (right >= size()) {
740 // no ] found, so it's not an optional argument
744 // add everything between [ and ] as optional argument
745 MathData optarg(buf, begin() + pos + 1, begin() + right);
749 if (optarg.size() == 1 && optarg[0]->asBraceInset()) {
751 params.push_back(optarg[0]->asBraceInset()->cell(0));
753 params.push_back(optarg);
755 // place cursor in optional argument of macro
756 // Note: The two expressions on the first line are equivalent
757 // (see caller), but making this explicit pleases coverity.
758 if (cur && thisSlice != -1
759 && thisPos >= int(pos) && thisPos <= int(right)) {
760 int paramPos = max(0, thisPos - int(pos) - 1);
761 vector<CursorSlice> x;
762 cur->cutOff(thisSlice, x);
763 (*cur)[thisSlice].pos() = macroPos;
765 paramPos = x[0].pos();
768 cur->append(0, paramPos);
774 // fill up empty optional parameters
775 while (params.size() < numOptionalParams)
776 params.push_back(MathData());
780 void MathData::collectParameters(Cursor * cur,
781 const size_type numParams, vector<MathData> & params,
782 size_t & pos, MathAtom & scriptToPutAround,
783 const pos_type macroPos, const int thisPos, const int thisSlice,
784 const size_t appetite)
786 size_t startSize = params.size();
788 // insert normal arguments
789 while (params.size() < numParams
790 && params.size() - startSize < appetite
792 && !scriptToPutAround.nucleus()) {
793 MathAtom & cell = operator[](pos);
796 vector<CursorSlice> argSlices;
798 // Note: The two expressions on the first line are equivalent
799 // (see caller), but making this explicit pleases coverity.
800 if (cur && thisSlice != -1
801 && thisPos == int(pos))
802 cur->cutOff(thisSlice, argSlices);
804 // which kind of parameter is it? In {}? With index x^n?
805 InsetMathBrace const * brace = cell->asBraceInset();
807 // found brace, convert into argument
808 params.push_back(brace->cell(0));
810 // cursor inside of the brace or just in front of?
811 if (thisPos == int(pos) && !argSlices.empty()) {
812 argPos = argSlices[0].pos();
813 argSlices.erase(argSlices.begin());
815 } else if (cell->asScriptInset() && params.size() + 1 == numParams) {
816 // last inset with scripts without braces
817 // -> they belong to the macro, not the argument
818 InsetMathScript * script = cell.nucleus()->asScriptInset();
819 if (script->nuc().size() == 1 && script->nuc()[0]->asBraceInset())
820 // nucleus in brace? Unpack!
821 params.push_back(script->nuc()[0]->asBraceInset()->cell(0));
823 params.push_back(script->nuc());
825 // script will be put around below
826 scriptToPutAround = cell;
828 // this should only happen after loading, so make cursor handling simple
829 if (thisPos >= int(macroPos) && thisPos <= int(macroPos + numParams)) {
835 // the simplest case: plain inset
837 array.insert(0, cell);
838 params.push_back(array);
841 // put cursor in argument again
842 // Note: The first two expressions on the first line are
843 // equivalent (see caller), but making this explicit pleases
845 if (cur && thisSlice != -1 && thisPos == int(pos)) {
846 cur->append(params.size() - 1, argPos);
847 cur->append(argSlices);
848 (*cur)[thisSlice].pos() = macroPos;
856 int MathData::pos2x(BufferView const * bv, size_type pos) const
859 size_type target = min(pos, size());
860 CoordCache::Insets const & coords = bv->coordCache().getInsets();
861 for (size_type i = 0; i < target; ++i) {
862 const_iterator it = begin() + i;
863 //lyxerr << "char: " << (*it)->getChar()
864 // << "width: " << (*it)->width() << endl;
865 x += coords.dim((*it).nucleus()).wid;
871 MathData::size_type MathData::x2pos(BufferView const * bv, int targetx) const
873 const_iterator it = begin();
876 CoordCache::Insets const & coords = bv->coordCache().getInsets();
877 // find first position after targetx
878 for (; currx < targetx && it != end(); ++it) {
880 currx += coords.dim((*it).nucleus()).wid;
884 * If we are not at the beginning of the array, go to the left
885 * of the inset if one of the following two condition holds:
886 * - the current inset is editable (so that the cursor tip is
887 * deeper than us): in this case, we want all intermediate
888 * cursor slices to be before insets;
889 * - the mouse is closer to the left side of the inset than to
891 * See bug 1918 for details.
893 if (it != begin() && currx >= targetx
894 && ((*prev(it, 1))->asNestInset()
895 || abs(lastx - targetx) < abs(currx - targetx))) {
903 int MathData::dist(BufferView const & bv, int x, int y) const
905 return bv.coordCache().getArrays().squareDistance(this, x, y);
909 void MathData::setXY(BufferView & bv, int x, int y) const
911 //lyxerr << "setting position cache for MathData " << this << endl;
912 bv.coordCache().arrays().add(this, x, y);
916 Dimension const & MathData::dimension(BufferView const & bv) const
918 return bv.coordCache().getArrays().dim(this);
922 int MathData::xm(BufferView const & bv) const
924 Geometry const & g = bv.coordCache().getArrays().geometry(this);
926 return g.pos.x_ + g.dim.wid / 2;
930 int MathData::ym(BufferView const & bv) const
932 Geometry const & g = bv.coordCache().getArrays().geometry(this);
934 return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
938 int MathData::xo(BufferView const & bv) const
940 return bv.coordCache().getArrays().x(this);
944 int MathData::yo(BufferView const & bv) const
946 return bv.coordCache().getArrays().y(this);
950 MathClass MathData::mathClass() const
952 MathClass res = MC_UNKNOWN;
953 for (MathAtom const & at : *this) {
954 MathClass mc = at->mathClass();
955 if (res == MC_UNKNOWN)
957 else if (mc != MC_UNKNOWN && res != mc)
960 return res == MC_UNKNOWN ? MC_ORD : res;
964 ostream & operator<<(ostream & os, MathData const & ar)
966 odocstringstream oss;
967 NormalStream ns(oss);
969 return os << to_utf8(oss.str());
973 odocstream & operator<<(odocstream & os, MathData const & ar)