From aeebc6565c77609d0c636e5102fc2fabef9af44a Mon Sep 17 00:00:00 2001 From: Abdelrazak Younes Date: Mon, 29 Oct 2007 12:21:58 +0000 Subject: [PATCH] * FontList: - clear(): new - set(): simplify a bit. * Paragraph(): optimize appendString() and resetFonts(). git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21259 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/FontList.cpp | 37 +++++++++++++++++++++++-------------- src/FontList.h | 2 ++ src/Paragraph.cpp | 7 +++++-- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/FontList.cpp b/src/FontList.cpp index 09edbefb94..bd71090fda 100644 --- a/src/FontList.cpp +++ b/src/FontList.cpp @@ -139,22 +139,36 @@ void FontList::set(pos_type pos, Font const & font) iterator beg = list_.begin(); iterator it = beg; iterator endit = list_.end(); + bool found = false; for (; it != endit; ++it) { - if (it->pos() >= pos) + if (it->pos() >= pos) { + found = true; break; + } } - size_t const i = distance(beg, it); - bool notfound = (it == endit); - - if (!notfound && list_[i].font() == font) + if (found && it->font() == font) return; - bool begin = pos == 0 || notfound || - (i > 0 && list_[i - 1].pos() == pos - 1); + size_t const i = distance(beg, it); + // Is position pos is a beginning of a font block? - bool end = !notfound && list_[i].pos() == pos; + bool begin = pos == 0 || !found + || (i > 0 && list_[i - 1].pos() == pos - 1); + // Is position pos is the end of a font block? - if (begin && end) { // A single char block + bool end = found && list_[i].pos() == pos; + + if (!begin && !end) { + // The general case: The block is splitted into 3 blocks + list_.insert(list_.begin() + i, + FontTable(pos - 1, list_[i].font())); + list_.insert(list_.begin() + i + 1, + FontTable(pos, font)); + return; + } + + if (begin && end) { + // A single char block if (i + 1 < list_.size() && list_[i + 1].font() == font) { // Merge the singleton block with the next block @@ -179,11 +193,6 @@ void FontList::set(pos_type pos, Font const & font) list_[i + 1].font() == font)) list_.insert(list_.begin() + i + 1, FontTable(pos, font)); - } else { // The general case. The block is splitted into 3 blocks - list_.insert(list_.begin() + i, - FontTable(pos - 1, list_[i].font())); - list_.insert(list_.begin() + i + 1, - FontTable(pos, font)); } } diff --git a/src/FontList.h b/src/FontList.h index 59650b85cf..d6733f9de5 100644 --- a/src/FontList.h +++ b/src/FontList.h @@ -84,6 +84,8 @@ public: /// bool empty() const { return list_.empty(); } /// + void clear() { return list_.clear(); } + /// void erase(pos_type pos); /// iterator fontIterator(pos_type pos); diff --git a/src/Paragraph.cpp b/src/Paragraph.cpp index 6684a172ee..9f0e2fcd16 100644 --- a/src/Paragraph.cpp +++ b/src/Paragraph.cpp @@ -1230,7 +1230,8 @@ void Paragraph::appendString(docstring const & s, Font const & font, // track change d->changes_.insert(change, i); } - d->fontlist_.setRange(oldsize, newsize, font); + d->fontlist_.set(oldsize, font); + d->fontlist_.set(newsize - 1, font); } @@ -1276,7 +1277,9 @@ bool Paragraph::insetAllowed(InsetCode code) void Paragraph::resetFonts(Font const & font) { - d->fontlist_.setRange(0, d->text_.size(), font); + d->fontlist_.clear(); + d->fontlist_.set(0, font); + d->fontlist_.set(d->text_.size() - 1, font); } // Gets uninstantiated font setting at position.