From 2ea432bab156751d40f2909f73db857942cfa143 Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Sun, 2 Sep 2007 22:28:49 +0000 Subject: [PATCH] * TextMetrics: getDisplayFont() and isRTLBoundary() takes pit instead of Paragraph. git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@20015 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/Cursor.cpp | 10 +++---- src/FontIterator.cpp | 8 +++--- src/FontIterator.h | 4 ++- src/Text.cpp | 10 +++---- src/Text2.cpp | 6 ++--- src/Text3.cpp | 3 +-- src/TextMetrics.cpp | 52 +++++++++++++++---------------------- src/TextMetrics.h | 6 ++--- src/insets/InsetTabular.cpp | 9 +++---- src/rowpainter.cpp | 12 ++++----- 10 files changed, 54 insertions(+), 66 deletions(-) diff --git a/src/Cursor.cpp b/src/Cursor.cpp index 363114ef5a..f31e52fdab 100644 --- a/src/Cursor.cpp +++ b/src/Cursor.cpp @@ -1461,8 +1461,8 @@ Font Cursor::getFont() const if (pos > 0) { TextMetrics const & tm = bv().textMetrics(&text); if (pos == sl.lastpos() - || (par.isSeparator(pos) && - !tm.isRTLBoundary(par, pos))) + || (par.isSeparator(pos) + && !tm.isRTLBoundary(sl.pit(), pos))) --pos; } @@ -1526,7 +1526,7 @@ void Cursor::setCurrentFont() // abc| def -> font of c // abc |[WERBEH], i.e. boundary==true -> font of c // abc [WERBEH]| def, font of the space - if (!tm.isRTLBoundary(par, cpos)) + if (!tm.isRTLBoundary(pit(), cpos)) --cpos; } } @@ -1534,11 +1534,11 @@ void Cursor::setCurrentFont() // get font BufferParams const & bufparams = buffer().params(); current_font = par.getFontSettings(bufparams, cpos); - real_current_font = tm.getDisplayFont(par, cpos); + real_current_font = tm.getDisplayFont(pit(), cpos); // special case for paragraph end if (pos() == lastpos() - && tm.isRTLBoundary(par, pos()) + && tm.isRTLBoundary(pit(), pos()) && !boundary()) { Language const * lang = par.getParLanguage(bufparams); current_font.setLanguage(lang); diff --git a/src/FontIterator.cpp b/src/FontIterator.cpp index 116523f430..c4f553162c 100644 --- a/src/FontIterator.cpp +++ b/src/FontIterator.cpp @@ -22,9 +22,9 @@ namespace lyx { FontIterator::FontIterator(TextMetrics const & tm, - Paragraph const & par, pos_type pos) - : tm_(tm), par_(par), pos_(pos), - font_(tm.getDisplayFont(par, pos)), + Paragraph const & par, pit_type pit, pos_type pos) + : tm_(tm), par_(par), pit_(pit), pos_(pos), + font_(tm.getDisplayFont(pit, pos)), endspan_(par.fontSpan(pos).last), bodypos_(par.beginOfBody()) {} @@ -46,7 +46,7 @@ FontIterator & FontIterator::operator++() { ++pos_; if (pos_ > endspan_ || pos_ == bodypos_) { - font_ = tm_.getDisplayFont(par_, pos_); + font_ = tm_.getDisplayFont(pit_, pos_); endspan_ = par_.fontSpan(pos_).last; } return *this; diff --git a/src/FontIterator.h b/src/FontIterator.h index b5417877df..44ecb37908 100644 --- a/src/FontIterator.h +++ b/src/FontIterator.h @@ -36,7 +36,7 @@ class FontIterator : std::iterator public: /// FontIterator(TextMetrics const & tm, - Paragraph const & par, pos_type pos); + Paragraph const & par, pit_type pit, pos_type pos); /// Font const & operator*() const; /// @@ -50,6 +50,8 @@ private: /// Paragraph const & par_; /// + pit_type pit_; + /// pos_type pos_; /// Font font_; diff --git a/src/Text.cpp b/src/Text.cpp index a5103a613b..9fde15b3a2 100644 --- a/src/Text.cpp +++ b/src/Text.cpp @@ -459,8 +459,8 @@ void Text::insertChar(Cursor & cur, char_type c) !(contains(number_seperators, c) && cur.pos() != 0 && cur.pos() != cur.lastpos() && - tm.getDisplayFont(par, cur.pos()).number() == Font::ON && - tm.getDisplayFont(par, cur.pos() - 1).number() == Font::ON) + tm.getDisplayFont(pit, cur.pos()).number() == Font::ON && + tm.getDisplayFont(pit, cur.pos() - 1).number() == Font::ON) ) number(cur); // Set current_font.number to OFF } else if (isDigit(c) && @@ -478,7 +478,7 @@ void Text::insertChar(Cursor & cur, char_type c) tm.font_); } else if (contains(number_seperators, c) && cur.pos() >= 2 - && tm.getDisplayFont(par, cur.pos() - 2).number() == Font::ON) { + && tm.getDisplayFont(pit, cur.pos() - 2).number() == Font::ON) { setCharFont(buffer, pit, cur.pos() - 1, cur.current_font, tm.font_); } @@ -511,7 +511,7 @@ void Text::insertChar(Cursor & cur, char_type c) if ((cur.pos() >= 2) && (par.isLineSeparator(cur.pos() - 1))) { // get font in front and behind the space in question. But do NOT // use getFont(cur.pos()) because the character c is not inserted yet - Font const & pre_space_font = tm.getDisplayFont(par, cur.pos() - 2); + Font const & pre_space_font = tm.getDisplayFont(cur.pit(), cur.pos() - 2); Font const & post_space_font = cur.real_current_font; bool pre_space_rtl = pre_space_font.isVisibleRightToLeft(); bool post_space_rtl = post_space_font.isVisibleRightToLeft(); @@ -524,7 +524,7 @@ void Text::insertChar(Cursor & cur, char_type c) (pre_space_rtl == par.isRightToLeftPar(buffer.params())) ? pre_space_font.language() : post_space_font.language(); - Font space_font = tm.getDisplayFont(par, cur.pos() - 1); + Font space_font = tm.getDisplayFont(cur.pit(), cur.pos() - 1); space_font.setLanguage(lang); par.setFont(cur.pos() - 1, space_font); } diff --git a/src/Text2.cpp b/src/Text2.cpp index 4cac71b487..4c47d0811f 100644 --- a/src/Text2.cpp +++ b/src/Text2.cpp @@ -367,7 +367,7 @@ void Text::setFont(BufferView const & bv, CursorSlice const & begin, // of noFontChange in Inset.h setInsetFont(bv, pit, pos, font, toggleall); TextMetrics const & tm = bv.textMetrics(this); - Font f = tm.getDisplayFont(dit.paragraph(), pos); + Font f = tm.getDisplayFont(pit, pos); f.update(font, language, toggleall); setCharFont(buffer, pit, pos, f, tm.font_); } @@ -655,7 +655,7 @@ bool Text::cursorRight(Cursor & cur) TextMetrics const & tm = cur.bv().textMetrics(this); // if left of boundary -> just jump to right side // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi - if (cur.boundary() && !tm.isRTLBoundary(cur.paragraph(), cur.pos())) + if (cur.boundary() && !tm.isRTLBoundary(cur.pit(), cur.pos())) return setCursor(cur, cur.pit(), cur.pos(), true, false); // next position is left of boundary, @@ -684,7 +684,7 @@ bool Text::cursorRight(Cursor & cur) // in front of RTL boundary? Stay on this side of the boundary because: // ab|cDDEEFFghi -> abc|DDEEFFghi - if (tm.isRTLBoundary(cur.paragraph(), cur.pos() + 1)) + if (tm.isRTLBoundary(cur.pit(), cur.pos() + 1)) return setCursor(cur, cur.pit(), cur.pos() + 1, true, true); // move right diff --git a/src/Text3.cpp b/src/Text3.cpp index e89f81e77a..1c0ceacfbb 100644 --- a/src/Text3.cpp +++ b/src/Text3.cpp @@ -102,9 +102,8 @@ namespace { if (font.language() != ignore_language || font.number() != Font::IGNORE) { - Paragraph & par = cur.paragraph(); TextMetrics const & tm = cur.bv().textMetrics(text); - if (cur.boundary() != tm.isRTLBoundary(par, + if (cur.boundary() != tm.isRTLBoundary(cur.pit(), cur.pos(), cur.real_current_font)) text->setCursor(cur, cur.pit(), cur.pos(), false, !cur.boundary()); diff --git a/src/TextMetrics.cpp b/src/TextMetrics.cpp index 2d820c4cae..c69ab1a345 100644 --- a/src/TextMetrics.cpp +++ b/src/TextMetrics.cpp @@ -208,11 +208,12 @@ void TextMetrics::applyOuterFont(Font & font) const } -Font TextMetrics::getDisplayFont(Paragraph const & par, - pos_type const pos) const +Font TextMetrics::getDisplayFont(pit_type pit, pos_type pos) const { BOOST_ASSERT(pos >= 0); + ParagraphList const & pars = text_->paragraphs(); + Paragraph const & par = pars[pit]; LayoutPtr const & layout = par.layout(); Buffer const & buffer = bv_->buffer(); // FIXME: broken? @@ -252,18 +253,6 @@ Font TextMetrics::getDisplayFont(Paragraph const & par, if (!text_->isMainText(buffer)) applyOuterFont(font); - // Find the pit value belonging to paragraph. This will not break - // even if pars_ would not be a vector anymore. - // Performance appears acceptable. - - ParagraphList const & pars = text_->paragraphs(); - - pit_type pit = pars.size(); - for (pit_type it = 0; it < pit; ++it) - if (&pars[it] == &par) { - pit = it; - break; - } // Realize against environment font information // NOTE: the cast to pit_type should be removed when pit_type // changes to a unsigned integer. @@ -286,13 +275,11 @@ bool TextMetrics::isRTL(CursorSlice const & sl, bool boundary) const if (boundary && sl.pos() > 0) correction = -1; - Paragraph const & par = text_->getPar(sl.pit()); - return getDisplayFont(par, sl.pos() + correction).isVisibleRightToLeft(); + return getDisplayFont(sl.pit(), sl.pos() + correction).isVisibleRightToLeft(); } -bool TextMetrics::isRTLBoundary(Paragraph const & par, - pos_type pos) const +bool TextMetrics::isRTLBoundary(pit_type pit, pos_type pos) const { if (!lyxrc.rtl_support) return false; @@ -301,28 +288,31 @@ bool TextMetrics::isRTLBoundary(Paragraph const & par, if (pos == 0) return false; - bool left = getDisplayFont(par, pos - 1).isVisibleRightToLeft(); + Paragraph const & par = text_->getPar(pit); + + bool left = getDisplayFont(pit, pos - 1).isVisibleRightToLeft(); bool right; if (pos == par.size()) right = par.isRightToLeftPar(bv_->buffer().params()); else - right = getDisplayFont(par, pos).isVisibleRightToLeft(); + right = getDisplayFont(pit, pos).isVisibleRightToLeft(); return left != right; } -bool TextMetrics::isRTLBoundary(Paragraph const & par, - pos_type pos, Font const & font) const +bool TextMetrics::isRTLBoundary(pit_type pit, pos_type pos, + Font const & font) const { if (!lyxrc.rtl_support) return false; + Paragraph const & par = text_->getPar(pit); bool left = font.isVisibleRightToLeft(); bool right; if (pos == par.size()) right = par.isRightToLeftPar(bv_->buffer().params()); else - right = getDisplayFont(par, pos).isVisibleRightToLeft(); + right = getDisplayFont(pit, pos).isVisibleRightToLeft(); return left != right; } @@ -372,7 +362,7 @@ bool TextMetrics::redoParagraph(pit_type const pit) int const w = max_width_ - leftMargin(max_width_, pit, ii->pos) - right_margin; Font const & font = ii->inset->noFontChange() ? - bufferfont : getDisplayFont(par, ii->pos); + bufferfont : getDisplayFont(pit, ii->pos); MetricsInfo mi(bv_, font, w); changed |= ii->inset->metrics(mi, dim); changed |= (old_dim != dim); @@ -660,7 +650,7 @@ pit_type TextMetrics::rowBreakPoint(int width, pit_type const pit, // pixel width since last breakpoint int chunkwidth = 0; - FontIterator fi = FontIterator(*this, par, pos); + FontIterator fi = FontIterator(*this, par, pit, pos); pos_type point = end; pos_type i = pos; for ( ; i < end; ++i, ++fi) { @@ -751,7 +741,7 @@ int TextMetrics::rowWidth(int right_margin, pit_type const pit, pos_type i = first; if (i < end) { - FontIterator fi = FontIterator(*this, par, i); + FontIterator fi = FontIterator(*this, par, pit, i); for ( ; i < end; ++i, ++fi) { if (body_pos > 0 && i == body_pos) { FontMetrics const & fm = theFontMetrics( @@ -797,7 +787,7 @@ boost::tuple TextMetrics::rowHeight(pit_type const pit, pos_type const // start with so we don't have to do the assignment below too // often. Buffer const & buffer = bv_->buffer(); - Font font = getDisplayFont(par, first); + Font font = getDisplayFont(pit, first); Font::FONT_SIZE const tmpsize = font.size(); font = text_->getLayoutFont(buffer, pit); Font::FONT_SIZE const size = font.size(); @@ -1062,7 +1052,7 @@ pos_type TextMetrics::getColumnNearX(pit_type const pit, bool const rtl = (bidi.level(c) % 2 == 1); if (left_side == rtl) { ++c; - boundary = isRTLBoundary(par, c); + boundary = isRTLBoundary(pit, c); } } @@ -1448,7 +1438,7 @@ int TextMetrics::cursorX(CursorSlice const & sl, // Use font span to speed things up, see above if (pos < font_span.first || pos > font_span.last) { font_span = par.fontSpan(pos); - font = getDisplayFont(par, pos); + font = getDisplayFont(pit, pos); } x += pm.singleWidth(pos, font); @@ -1779,7 +1769,7 @@ int TextMetrics::singleWidth(pit_type pit, pos_type pos) const { ParagraphMetrics const & pm = par_metrics_[pit]; - return pm.singleWidth(pos, getDisplayFont(text_->getPar(pit), pos)); + return pm.singleWidth(pos, getDisplayFont(pit, pos)); } @@ -2014,7 +2004,7 @@ void TextMetrics::drawRowSelection(PainterInfo & pi, int x, Row const & row, // but for RTL boundaries don't, because: abc|DDEEFFghi -> abcDDEEF|Fghi if (cur.boundary()) { cur.boundary(false); - } else if (isRTLBoundary(cur.paragraph(), cur.pos() + 1)) { + } else if (isRTLBoundary(cur.pit(), cur.pos() + 1)) { // in front of RTL boundary -> Stay on this side of the boundary because: // ab|cDDEEFFghi -> abc|DDEEFFghi ++cur.pos(); diff --git a/src/TextMetrics.h b/src/TextMetrics.h index 556ca64b02..d850074310 100644 --- a/src/TextMetrics.h +++ b/src/TextMetrics.h @@ -54,7 +54,7 @@ public: /// The difference is that this one is used for displaying, and thus we /// are allowed to make cosmetic improvements. For instance make footnotes /// smaller. (Asger) - Font getDisplayFont(Paragraph const & par, + Font getDisplayFont(pit_type pit, pos_type pos) const; /// There are currently two font mechanisms in LyX: @@ -69,10 +69,10 @@ public: /// is this position in the paragraph right-to-left? bool isRTL(CursorSlice const & sl, bool boundary) const; /// is between pos-1 and pos an RTL<->LTR boundary? - bool isRTLBoundary(Paragraph const & par, + bool isRTLBoundary(pit_type pit, pos_type pos) const; /// would be a RTL<->LTR boundary between pos and the given font? - bool isRTLBoundary(Paragraph const & par, + bool isRTLBoundary(pit_type pit, pos_type pos, Font const & font) const; diff --git a/src/insets/InsetTabular.cpp b/src/insets/InsetTabular.cpp index 815d52826d..b7f1b49955 100644 --- a/src/insets/InsetTabular.cpp +++ b/src/insets/InsetTabular.cpp @@ -4746,9 +4746,8 @@ bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf, // we can only set this if we are not too far right if (cols < columns) { shared_ptr inset = loctab->getCellInset(cell); - Paragraph & par = inset->text_.getPar(0); Font const font = bv.textMetrics(&inset->text_). - getDisplayFont(par, 0); + getDisplayFont(0, 0); inset->setText(buf.substr(op, p - op), font, buffer.params().trackChanges); ++cols; @@ -4759,9 +4758,8 @@ bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf, // we can only set this if we are not too far right if (cols < columns) { shared_ptr inset = tabular.getCellInset(cell); - Paragraph & par = inset->text_.getPar(0); Font const font = bv.textMetrics(&inset->text_). - getDisplayFont(par, 0); + getDisplayFont(0, 0); inset->setText(buf.substr(op, p - op), font, buffer.params().trackChanges); } @@ -4777,8 +4775,7 @@ bool InsetTabular::insertPlaintextString(BufferView & bv, docstring const & buf, // check for the last cell if there is no trailing '\n' if (cell < cells && op < len) { shared_ptr inset = loctab->getCellInset(cell); - Paragraph & par = inset->text_.getPar(0); - Font const font = bv.textMetrics(&inset->text_).getDisplayFont(par, 0); + Font const font = bv.textMetrics(&inset->text_).getDisplayFont(0, 0); inset->setText(buf.substr(op, len - op), font, buffer.params().trackChanges); } diff --git a/src/rowpainter.cpp b/src/rowpainter.cpp index 1c47e6981c..342026c71d 100644 --- a/src/rowpainter.cpp +++ b/src/rowpainter.cpp @@ -123,7 +123,7 @@ void RowPainter::paintHfill(pos_type const pos, pos_type const body_pos) void RowPainter::paintInset(Inset const * inset, pos_type const pos) { - Font font = text_metrics_.getDisplayFont(par_, pos); + Font font = text_metrics_.getDisplayFont(pit_, pos); BOOST_ASSERT(inset); // FIXME: We should always use font, see documentation of @@ -194,7 +194,7 @@ void RowPainter::paintHebrewComposeChar(pos_type & vpos, Font const & font) if (!Encodings::isComposeChar_hebrew(c)) { if (isPrintableNonspace(c)) { int const width2 = pm_.singleWidth(i, - text_metrics_.getDisplayFont(par_, i)); + text_metrics_.getDisplayFont(pit_, i)); dx = (c == 0x05e8 || // resh c == 0x05d3) // dalet ? width2 - width @@ -228,7 +228,7 @@ void RowPainter::paintArabicComposeChar(pos_type & vpos, Font const & font) if (!Encodings::isComposeChar_arabic(c)) { if (isPrintableNonspace(c)) { int const width2 = pm_.singleWidth(i, - text_metrics_.getDisplayFont(par_, i)); + text_metrics_.getDisplayFont(pit_, i)); dx = (width2 - width) / 2; } break; @@ -348,7 +348,7 @@ void RowPainter::paintForeignMark(double orig_x, Font const & font, int desc) void RowPainter::paintFromPos(pos_type & vpos) { pos_type const pos = bidi_.vis2log(vpos); - Font orig_font = text_metrics_.getDisplayFont(par_, pos); + Font orig_font = text_metrics_.getDisplayFont(pit_, pos); double const orig_x = x_; // usual characters, no insets @@ -727,7 +727,7 @@ void RowPainter::paintText() // Use font span to speed things up, see above if (vpos < font_span.first || vpos > font_span.last) { font_span = par_.fontSpan(vpos); - font = text_metrics_.getDisplayFont(par_, vpos); + font = text_metrics_.getDisplayFont(pit_, vpos); } const int width_pos = pm_.singleWidth(pos, font); @@ -772,7 +772,7 @@ void RowPainter::paintText() ++vpos; } else if (par_.isSeparator(pos)) { - Font orig_font = text_metrics_.getDisplayFont(par_, pos); + Font orig_font = text_metrics_.getDisplayFont(pit_, pos); double const orig_x = x_; x_ += width_pos; if (pos >= body_pos) -- 2.39.5