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"
30 #include "Dimension.h"
32 #include "mathed/InsetMathUnknown.h"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/gettext.h"
40 #include "support/lassert.h"
49 MathData::MathData(Buffer * buf, const_iterator from, const_iterator to)
50 : base_type(from, to), buffer_(buf)
54 void MathData::setBuffer(Buffer & b)
57 for (MathAtom & at : *this)
58 at.nucleus()->setBuffer(b);
62 MathAtom & MathData::operator[](pos_type pos)
64 LBUFERR(pos < size());
65 return base_type::operator[](pos);
69 MathAtom const & MathData::operator[](pos_type pos) const
71 LBUFERR(pos < size());
72 return base_type::operator[](pos);
76 void MathData::insert(size_type pos, MathAtom const & t)
78 LBUFERR(pos <= size());
79 base_type::insert(begin() + pos, t);
83 void MathData::insert(size_type pos, MathData const & ar)
85 LBUFERR(pos <= size());
86 base_type::insert(begin() + pos, ar.begin(), ar.end());
90 void MathData::append(MathData const & ar)
96 void MathData::erase(size_type pos)
103 void MathData::erase(iterator pos1, iterator pos2)
105 base_type::erase(pos1, pos2);
109 void MathData::erase(iterator pos)
111 base_type::erase(pos);
115 void MathData::erase(size_type pos1, size_type pos2)
117 base_type::erase(begin() + pos1, begin() + pos2);
121 void MathData::dump2() const
125 for (const_iterator it = begin(); it != end(); ++it)
127 lyxerr << to_utf8(os.str());
131 void MathData::dump() const
135 for (const_iterator it = begin(); it != end(); ++it)
136 ns << '<' << *it << '>';
137 lyxerr << to_utf8(os.str());
141 void MathData::validate(LaTeXFeatures & features) const
143 for (const_iterator it = begin(); it != end(); ++it)
144 (*it)->validate(features);
148 bool MathData::match(MathData const & ar) const
150 return size() == ar.size() && matchpart(ar, 0);
154 bool MathData::matchpart(MathData const & ar, pos_type pos) const
156 if (size() < ar.size() + pos)
158 const_iterator it = begin() + pos;
159 for (const_iterator jt = ar.begin(); jt != ar.end(); ++jt, ++it)
160 if (asString(*it) != asString(*jt))
166 void MathData::replace(ReplaceData & rep)
168 for (size_type i = 0; i < size(); ++i) {
169 if (find1(rep.from, i)) {
171 lyxerr << "match found!" << endl;
172 erase(i, i + rep.from.size());
177 // FIXME: temporarily disabled
178 // for (const_iterator it = begin(); it != end(); ++it)
179 // it->nucleus()->replace(rep);
183 bool MathData::find1(MathData const & ar, size_type pos) const
185 lyxerr << "finding '" << ar << "' in '" << *this << "'" << endl;
186 for (size_type i = 0, n = ar.size(); i < n; ++i)
187 if (asString(operator[](pos + i)) != asString(ar[i]))
193 MathData::size_type MathData::find(MathData const & ar) const
195 for (int i = 0, last = size() - ar.size(); i < last; ++i)
202 MathData::size_type MathData::find_last(MathData const & ar) const
204 for (int i = size() - ar.size(); i >= 0; --i)
211 bool MathData::contains(MathData const & ar) const
213 if (find(ar) != size())
215 for (const_iterator it = begin(); it != end(); ++it)
216 if ((*it)->contains(ar))
222 bool MathData::addToMathRow(MathRow & mrow, MetricsInfo & mi) const
224 bool has_contents = false;
225 BufferView * bv = mi.base.bv;
226 MathData * ar = const_cast<MathData*>(this);
227 ar->updateMacros(&bv->cursor(), mi.macrocontext,
228 InternalUpdate, mi.base.macro_nesting);
231 // FIXME: for completion, try to insert the relevant data in the
232 // mathrow (like is done for text rows). We could add a pair of
233 // InsetMathColor inset, but these come with extra spacing of
235 DocIterator const & inlineCompletionPos = bv->inlineCompletionPos();
236 bool const has_completion = inlineCompletionPos.inMathed()
237 && &inlineCompletionPos.cell() == this;
238 size_t const compl_pos = has_completion ? inlineCompletionPos.pos() : 0;
240 for (size_t i = 0 ; i < size() ; ++i) {
241 has_contents |= (*this)[i]->addToMathRow(mrow, mi);
242 if (i + 1 == compl_pos) {
243 mrow.back().compl_text = bv->inlineCompletion();
244 mrow.back().compl_unique_to = bv->inlineCompletionUniqueChars();
254 bool isInside(DocIterator const & it, MathData const & ar,
255 pos_type p1, pos_type p2)
257 for (size_t i = 0; i != it.depth(); ++i) {
258 CursorSlice const & sl = it[i];
259 if (sl.inset().inMathed() && &sl.cell() == &ar)
260 return p1 <= sl.pos() && sl.pos() < p2;
269 void MathData::metrics(MetricsInfo & mi, Dimension & dim, bool tight) const
271 frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
272 BufferView * bv = mi.base.bv;
273 int const Iascent = fm.dimension('I').ascent();
274 int xascent = fm.xHeight();
275 if (xascent >= Iascent)
276 xascent = (2 * Iascent) / 3;
278 mindes_ = (3 * xascent) / 4;
279 slevel_ = (4 * xascent) / 5;
280 sshift_ = xascent / 4;
282 MathRow mrow(mi, this);
283 mrow.metrics(mi, dim);
285 // Set a minimal ascent/descent for the cell
287 // FIXME: this is the minimal ascent seen empirically, check
288 // what the TeXbook says.
289 dim.asc = max(dim.asc, fm.xHeight());
291 dim.asc = max(dim.asc, fm.maxAscent());
292 dim.des = max(dim.des, fm.maxDescent());
295 // This is one of the the few points where the drawing font is known,
296 // so that we can set the caret vertical dimensions.
297 mrow.caret_dim.asc = min(dim.asc, fm.maxAscent());
298 mrow.caret_dim.des = min(dim.des, fm.maxDescent());
299 mrow.caret_dim.wid = fm.lineWidth();
301 /// do the same for math cells linearized in the row
302 MathRow caret_row = MathRow(mrow.caret_dim);
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 inside 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 (auto const & it : *this) {
377 it->drawT(pain, x, y);
384 int MathData::kerning(BufferView const * bv) const
386 return bv->mathRow(this).kerning(bv);
390 void MathData::updateBuffer(ParIterator const & it, UpdateType utype, bool const deleted)
393 for (size_t i = 0, n = size(); i != n; ++i) {
394 MathAtom & at = operator[](i);
395 at.nucleus()->updateBuffer(it, utype, deleted);
400 void MathData::updateMacros(Cursor * cur, MacroContext const & mc,
401 UpdateType utype, int nesting)
403 // If we are editing a macro, we cannot update it immediately,
404 // otherwise wrong undo steps will be recorded (bug 6208).
405 InsetMath const * inmath = cur ? cur->inset().asInsetMath() : 0;
406 InsetMathMacro const * inmacro = inmath ? inmath->asMacro() : 0;
407 docstring const edited_name = inmacro ? inmacro->name() : docstring();
409 // go over the array and look for macros
410 for (size_t i = 0; i < size(); ++i) {
411 InsetMathMacro * macroInset = operator[](i).nucleus()->asMacro();
412 if (!macroInset || macroInset->macroName().empty()
413 || macroInset->macroName()[0] == '^'
414 || macroInset->macroName()[0] == '_'
415 || (macroInset->name() == edited_name
416 && macroInset->displayMode() ==
417 InsetMathMacro::DISPLAY_UNFOLDED))
421 macroInset->updateMacro(mc);
422 size_t macroNumArgs = 0;
423 size_t macroOptionals = 0;
424 MacroData const * macro = macroInset->macro();
426 macroNumArgs = macro->numargs();
427 macroOptionals = macro->optionals();
430 // store old and compute new display mode
431 InsetMathMacro::DisplayMode newDisplayMode;
432 InsetMathMacro::DisplayMode oldDisplayMode = macroInset->displayMode();
433 newDisplayMode = macroInset->computeDisplayMode();
435 // arity changed or other reason to detach?
436 if (oldDisplayMode == InsetMathMacro::DISPLAY_NORMAL
437 && (macroInset->arity() != macroNumArgs
438 || macroInset->optionals() != macroOptionals
439 || newDisplayMode == InsetMathMacro::DISPLAY_UNFOLDED))
440 detachMacroParameters(cur, i);
442 // the macro could have been copied while resizing this
443 macroInset = operator[](i).nucleus()->asMacro();
446 if (newDisplayMode != InsetMathMacro::DISPLAY_UNFOLDED
447 && oldDisplayMode == InsetMathMacro::DISPLAY_UNFOLDED) {
448 // put cursor in front of macro
450 int macroSlice = cur->find(macroInset);
451 if (macroSlice != -1)
452 cur->cutOff(macroSlice - 1);
456 // update the display mode
457 size_t appetite = macroInset->appetite();
458 macroInset->setDisplayMode(newDisplayMode);
461 if (newDisplayMode == InsetMathMacro::DISPLAY_NORMAL
462 && (macroInset->arity() != macroNumArgs
463 || macroInset->optionals() != macroOptionals)) {
464 // is it a virgin macro which was never attached to parameters?
465 bool fromInitToNormalMode
466 = (oldDisplayMode == InsetMathMacro::DISPLAY_INIT
467 || oldDisplayMode == InsetMathMacro::DISPLAY_INTERACTIVE_INIT)
468 && newDisplayMode == InsetMathMacro::DISPLAY_NORMAL;
470 // if the macro was entered interactively (i.e. not by paste or during
471 // loading), it should not be greedy, but the cursor should
472 // automatically jump into the macro when behind
473 bool interactive = (oldDisplayMode == InsetMathMacro::DISPLAY_INTERACTIVE_INIT);
476 attachMacroParameters(cur, i, macroNumArgs, macroOptionals,
477 fromInitToNormalMode, interactive, appetite);
480 cur->updateInsets(&cur->bottom().inset());
483 // Give macro the chance to adapt to new situation.
484 // The macroInset could be invalid now because it was put into a script
485 // inset and therefore "deep" copied. So get it again from the MathData.
486 InsetMath * inset = operator[](i).nucleus();
487 if (inset->asScriptInset())
488 inset = inset->asScriptInset()->nuc()[0].nucleus();
489 LASSERT(inset->asMacro(), continue);
490 inset->asMacro()->updateRepresentation(cur, mc, utype, nesting + 1);
495 void MathData::detachMacroParameters(DocIterator * cur, const size_type macroPos)
497 InsetMathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
498 // We store this now, because the inset pointer will be invalidated in the scond loop below
499 size_t const optionals = macroInset->optionals();
501 // detach all arguments
502 vector<MathData> detachedArgs;
503 if (macroPos + 1 == size())
504 // strip arguments if we are at the MathData end
505 macroInset->detachArguments(detachedArgs, true);
507 macroInset->detachArguments(detachedArgs, false);
510 int curMacroSlice = -1;
512 curMacroSlice = cur->find(macroInset);
513 idx_type curMacroIdx = -1;
514 pos_type curMacroPos = -1;
515 vector<CursorSlice> argSlices;
516 if (curMacroSlice != -1) {
517 curMacroPos = (*cur)[curMacroSlice].pos();
518 curMacroIdx = (*cur)[curMacroSlice].idx();
519 cur->cutOff(curMacroSlice, argSlices);
523 // only [] after the last non-empty argument can be dropped later
524 size_t lastNonEmptyOptional = 0;
525 for (size_t l = 0; l < detachedArgs.size() && l < optionals; ++l) {
526 if (!detachedArgs[l].empty())
527 lastNonEmptyOptional = l;
530 // optional arguments to be put back?
531 pos_type p = macroPos + 1;
533 // We do not want to use macroInset below, the insert() call in
534 // the loop will invalidate it.
536 for (; j < detachedArgs.size() && j < optionals; ++j) {
537 // another non-empty parameter follows?
538 bool canDropEmptyOptional = j >= lastNonEmptyOptional;
540 // then we can drop empty optional parameters
541 if (detachedArgs[j].empty() && canDropEmptyOptional) {
542 if (curMacroIdx == j)
543 (*cur)[curMacroSlice - 1].pos() = macroPos + 1;
547 // Otherwise we don't drop an empty optional, put it back normally
549 asArray(from_ascii("[]"), optarg);
550 MathData & arg = detachedArgs[j];
552 // look for "]", i.e. put a brace around?
553 InsetMathBrace * brace = 0;
554 for (size_t q = 0; q < arg.size(); ++q) {
555 if (arg[q]->getChar() == ']') {
557 brace = new InsetMathBrace(buffer_);
562 // put arg between []
564 brace->cell(0) = arg;
565 optarg.insert(1, MathAtom(brace));
567 optarg.insert(1, arg);
569 // insert it into the array
574 if (curMacroSlice == -1)
577 // cursor in optional argument of macro?
578 if (curMacroIdx == j) {
580 cur->append(0, curMacroPos);
581 (*cur)[curMacroSlice - 1].pos() = macroPos + 2;
583 (*cur)[curMacroSlice - 1].pos() = macroPos + 2 + curMacroPos;
584 cur->append(argSlices);
585 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
586 // cursor right of macro
587 (*cur)[curMacroSlice - 1].pos() += optarg.size();
590 // put them back into the MathData
591 for (; j < detachedArgs.size(); ++j, ++p) {
592 MathData const & arg = detachedArgs[j];
594 && !arg[0]->asScriptInset()
595 && !(arg[0]->asMacro() && arg[0]->asMacro()->arity() > 0))
598 insert(p, MathAtom(new InsetMathBrace(arg)));
601 if (curMacroSlice == -1)
604 // cursor in j-th argument of macro?
605 if (curMacroIdx == j) {
606 if (operator[](p).nucleus()->asBraceInset()) {
607 (*cur)[curMacroSlice - 1].pos() = p;
608 cur->append(0, curMacroPos);
609 cur->append(argSlices);
611 (*cur)[curMacroSlice - 1].pos() = p; // + macroPos;
612 cur->append(argSlices);
614 } else if ((*cur)[curMacroSlice - 1].pos() >= int(p))
615 ++(*cur)[curMacroSlice - 1].pos();
619 cur->updateInsets(&cur->bottom().inset());
623 void MathData::attachMacroParameters(Cursor * cur,
624 const size_type macroPos, const size_type macroNumArgs,
625 const int macroOptionals, const bool fromInitToNormalMode,
626 const bool interactiveInit, const size_t appetite)
628 InsetMathMacro * macroInset = operator[](macroPos).nucleus()->asMacro();
630 // start at atom behind the macro again, maybe with some new arguments
631 // from the detach phase above, to add them back into the macro inset
632 size_t p = macroPos + 1;
633 vector<MathData> detachedArgs;
634 MathAtom scriptToPutAround;
636 // find cursor slice again of this MathData
639 thisSlice = cur->find(*this);
642 thisPos = (*cur)[thisSlice].pos();
644 // find arguments behind the macro
645 if (!interactiveInit) {
646 collectOptionalParameters(cur, macroOptionals, detachedArgs, p,
647 scriptToPutAround, macroPos, thisPos, thisSlice);
649 collectParameters(cur, macroNumArgs, detachedArgs, p,
650 scriptToPutAround, macroPos, thisPos, thisSlice, appetite);
652 // attach arguments back to macro inset
653 macroInset->attachArguments(detachedArgs, macroNumArgs, macroOptionals);
655 // found tail script? E.g. \foo{a}b^x
656 if (scriptToPutAround.nucleus()) {
657 InsetMathScript * scriptInset =
658 scriptToPutAround.nucleus()->asScriptInset();
659 // In the math parser we remove empty braces in the base
660 // of a script inset, but we have to restore them here.
661 if (scriptInset->nuc().empty()) {
663 scriptInset->nuc().push_back(
664 MathAtom(new InsetMathBrace(ar)));
666 // put macro into a script inset
667 scriptInset->nuc()[0] = operator[](macroPos);
668 operator[](macroPos) = scriptToPutAround;
670 // go into the script inset nucleus
671 if (cur && thisPos == int(macroPos))
674 // get pointer to "deep" copied macro inset
675 scriptInset = operator[](macroPos).nucleus()->asScriptInset();
676 macroInset = scriptInset->nuc()[0].nucleus()->asMacro();
679 // remove them from the MathData
680 erase(macroPos + 1, p);
682 // cursor outside this MathData?
686 // fix cursor if right of p
687 if (thisPos >= int(p))
688 (*cur)[thisSlice].pos() -= p - (macroPos + 1);
690 // was the macro inset just inserted interactively and was now folded
691 // and the cursor is just behind?
692 if ((*cur)[thisSlice].pos() == int(macroPos + 1)
694 && fromInitToNormalMode
695 && macroInset->arity() > 0
696 && thisSlice + 1 == int(cur->depth())) {
697 // then enter it if the cursor was just behind
698 (*cur)[thisSlice].pos() = macroPos;
699 cur->push_back(CursorSlice(*macroInset));
700 macroInset->idxFirst(*cur);
705 void MathData::collectOptionalParameters(Cursor * cur,
706 const size_type numOptionalParams, vector<MathData> & params,
707 size_t & pos, MathAtom & scriptToPutAround,
708 const pos_type macroPos, const int thisPos, const int thisSlice)
710 Buffer * buf = cur ? cur->buffer() : 0;
711 // insert optional arguments?
712 while (params.size() < numOptionalParams
714 && !scriptToPutAround.nucleus()) {
715 // is a [] block following which could be an optional parameter?
716 if (operator[](pos)->getChar() != '[')
719 // found possible optional argument, look for pairing "]"
721 size_t right = pos + 1;
722 for (; right < size(); ++right) {
723 MathAtom & cell = operator[](right);
725 if (cell->getChar() == '[')
727 else if (cell->getChar() == ']' && --count == 0)
731 // maybe "]" with a script around?
732 InsetMathScript * script = cell.nucleus()->asScriptInset();
735 if (script->nuc().size() != 1)
737 if (script->nuc()[0]->getChar() == ']') {
738 // script will be put around the macro later
739 scriptToPutAround = cell;
745 if (right >= size()) {
746 // no ] found, so it's not an optional argument
750 // add everything between [ and ] as optional argument
751 MathData optarg(buf, begin() + pos + 1, begin() + right);
755 if (optarg.size() == 1 && optarg[0]->asBraceInset()) {
757 params.push_back(optarg[0]->asBraceInset()->cell(0));
759 params.push_back(optarg);
761 // place cursor in optional argument of macro
762 // Note: The two expressions on the first line are equivalent
763 // (see caller), but making this explicit pleases coverity.
764 if (cur && thisSlice != -1
765 && thisPos >= int(pos) && thisPos <= int(right)) {
766 int paramPos = max(0, thisPos - int(pos) - 1);
767 vector<CursorSlice> x;
768 cur->cutOff(thisSlice, x);
769 (*cur)[thisSlice].pos() = macroPos;
771 paramPos = x[0].pos();
774 cur->append(0, paramPos);
780 // fill up empty optional parameters
781 while (params.size() < numOptionalParams)
782 params.push_back(MathData());
786 void MathData::collectParameters(Cursor * cur,
787 const size_type numParams, vector<MathData> & params,
788 size_t & pos, MathAtom & scriptToPutAround,
789 const pos_type macroPos, const int thisPos, const int thisSlice,
790 const size_t appetite)
792 size_t startSize = params.size();
794 // insert normal arguments
795 while (params.size() < numParams
796 && params.size() - startSize < appetite
798 && !scriptToPutAround.nucleus()) {
799 MathAtom & cell = operator[](pos);
802 vector<CursorSlice> argSlices;
804 // Note: The two expressions on the first line are equivalent
805 // (see caller), but making this explicit pleases coverity.
806 if (cur && thisSlice != -1
807 && thisPos == int(pos))
808 cur->cutOff(thisSlice, argSlices);
810 // which kind of parameter is it? In {}? With index x^n?
811 InsetMathBrace const * brace = cell->asBraceInset();
813 // found brace, convert into argument
814 params.push_back(brace->cell(0));
816 // cursor inside of the brace or just in front of?
817 if (thisPos == int(pos) && !argSlices.empty()) {
818 argPos = argSlices[0].pos();
819 argSlices.erase(argSlices.begin());
821 } else if (cell->asScriptInset() && params.size() + 1 == numParams) {
822 // last inset with scripts without braces
823 // -> they belong to the macro, not the argument
824 InsetMathScript * script = cell.nucleus()->asScriptInset();
825 if (script->nuc().size() == 1 && script->nuc()[0]->asBraceInset())
826 // nucleus in brace? Unpack!
827 params.push_back(script->nuc()[0]->asBraceInset()->cell(0));
829 params.push_back(script->nuc());
831 // script will be put around below
832 scriptToPutAround = cell;
834 // this should only happen after loading, so make cursor handling simple
835 if (thisPos >= int(macroPos) && thisPos <= int(macroPos + numParams)) {
841 // the simplest case: plain inset
843 array.insert(0, cell);
844 params.push_back(array);
847 // put cursor in argument again
848 // Note: The first two expressions on the first line are
849 // equivalent (see caller), but making this explicit pleases
851 if (cur && thisSlice != -1 && thisPos == int(pos)) {
852 cur->append(params.size() - 1, argPos);
853 cur->append(argSlices);
854 (*cur)[thisSlice].pos() = macroPos;
862 int MathData::pos2x(BufferView const * bv, size_type pos) const
865 size_type target = min(pos, size());
866 CoordCache::Insets const & coords = bv->coordCache().getInsets();
867 for (size_type i = 0; i < target; ++i) {
868 const_iterator it = begin() + i;
869 //lyxerr << "char: " << (*it)->getChar()
870 // << "width: " << (*it)->width() << endl;
871 x += coords.dim((*it).nucleus()).wid;
877 MathData::size_type MathData::x2pos(BufferView const * bv, int targetx) const
879 const_iterator it = begin();
882 CoordCache::Insets const & coords = bv->coordCache().getInsets();
883 // find first position after targetx
884 for (; currx < targetx && it != end(); ++it) {
886 currx += coords.dim((*it).nucleus()).wid;
890 * If we are not at the beginning of the array, go to the left
891 * of the inset if one of the following two condition holds:
892 * - the current inset is editable (so that the cursor tip is
893 * deeper than us): in this case, we want all intermediate
894 * cursor slices to be before insets;
895 * - the mouse is closer to the left side of the inset than to
897 * See bug 1918 for details.
899 if (it != begin() && currx >= targetx
900 && ((*prev(it, 1))->asNestInset()
901 || abs(lastx - targetx) < abs(currx - targetx))) {
909 int MathData::dist(BufferView const & bv, int x, int y) const
911 return bv.coordCache().getArrays().squareDistance(this, x, y);
915 void MathData::setXY(BufferView & bv, int x, int y) const
917 //lyxerr << "setting position cache for MathData " << this << endl;
918 bv.coordCache().arrays().add(this, x, y);
922 Dimension const & MathData::dimension(BufferView const & bv) const
924 return bv.coordCache().getArrays().dim(this);
928 int MathData::xm(BufferView const & bv) const
930 Geometry const & g = bv.coordCache().getArrays().geometry(this);
932 return g.pos.x_ + g.dim.wid / 2;
936 int MathData::ym(BufferView const & bv) const
938 Geometry const & g = bv.coordCache().getArrays().geometry(this);
940 return g.pos.y_ + (g.dim.des - g.dim.asc) / 2;
944 int MathData::xo(BufferView const & bv) const
946 return bv.coordCache().getArrays().x(this);
950 int MathData::yo(BufferView const & bv) const
952 return bv.coordCache().getArrays().y(this);
956 MathClass MathData::mathClass() const
958 MathClass res = MC_UNKNOWN;
959 for (MathAtom const & at : *this) {
960 MathClass mc = at->mathClass();
961 if (res == MC_UNKNOWN)
963 else if (mc != MC_UNKNOWN && res != mc)
966 return res == MC_UNKNOWN ? MC_ORD : res;
970 MathClass MathData::lastMathClass() const
972 MathClass res = MC_ORD;
973 for (MathAtom const & at : *this) {
974 MathClass mc = at->mathClass();
975 if (mc != MC_UNKNOWN)
982 ostream & operator<<(ostream & os, MathData const & ar)
984 odocstringstream oss;
985 NormalStream ns(oss);
987 return os << to_utf8(oss.str());
991 odocstream & operator<<(odocstream & os, MathData const & ar)