]> git.lyx.org Git - lyx.git/blobdiff - src/FontList.cpp
Workaround for #6865: smarter FontList::setMisspelled implementation
[lyx.git] / src / FontList.cpp
index f6b456d9db495d4383cede00d8e38a71dbd77151..d4cb66e46ea1be2ff82d8289e90533ef84ce821a 100644 (file)
@@ -4,13 +4,13 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Asger Alstrup
- * \author Lars Gullik Bjønnes
+ * \author Lars Gullik Bjønnes
  * \author Jean-Marc Lasgouttes
  * \author Angus Leeming
  * \author John Levon
- * \author André Pönitz
+ * \author André Pönitz
  * \author Dekel Tsur
- * \author Jürgen Vigna
+ * \author Jürgen Vigna
  * \author Abdelrazak Younes
  *
  * Full author contact details are available in file CREDITS.
@@ -28,34 +28,28 @@ using namespace std;
 
 namespace lyx {
 
-namespace {
-
-class matchFT
-{
-public:
-       /// used by lower_bound and upper_bound
-       int operator()(FontTable const & a, FontTable const & b) const {
-               return a.pos() < b.pos();
-       }
-};
-
-} // anon namespace
 
 FontList::iterator FontList::fontIterator(pos_type pos)
 {
-       static Font dummy;
-       FontTable search_elem(pos, dummy);
-       return lower_bound(list_.begin(), list_.end(), search_elem,
-               matchFT());
+       FontList::iterator it = list_.begin();
+       FontList::iterator end = list_.end();
+       for (; it != end; ++it) {
+               if (it->pos() >= pos)
+                       break;
+       }
+       return it;
 }
 
 
 FontList::const_iterator FontList::fontIterator(pos_type pos) const
 {
-       static Font dummy;
-       FontTable search_elem(pos, dummy);
-       return lower_bound(list_.begin(), list_.end(), search_elem,
-               matchFT());
+       FontList::const_iterator it = list_.begin();
+       FontList::const_iterator end = list_.end();
+       for (; it != end; ++it) {
+               if (it->pos() >= pos)
+                       break;
+       }
+       return it;
 }
 
 
@@ -133,27 +127,20 @@ void FontList::set(pos_type pos, Font const & font)
        // in a new kernel. (Asger)
        // Next search font table
 
-       iterator beg = list_.begin();
-       iterator it = beg;
-       iterator endit = list_.end();
-       bool found = false;
-       for (; it != endit; ++it) {
-               if (it->pos() >= pos) {
-                       found = true;
-                       break;
-               }
-       }
+       List::iterator it = fontIterator(pos);
+       bool const found = it != list_.end();
        if (found && it->font() == font)
+               // Font is already set.
                return;
 
-       size_t const i = distance(beg, it);
+       size_t const i = distance(list_.begin(), it);
 
-       // Is position pos is a beginning of a font block?
-       bool begin = pos == 0 || !found 
+       // Is position pos a beginning of a font block?
+       bool const begin = pos == 0 || !found 
                || (i > 0 && list_[i - 1].pos() == pos - 1);
 
-       // Is position pos is the end of a font block?
-       bool end = found && list_[i].pos() == pos;
+       // Is position pos at the end of a font block?
+       bool const end = found && list_[i].pos() == pos;
 
        if (!begin && !end) {
                // The general case: The block is splitted into 3 blocks
@@ -194,27 +181,32 @@ void FontList::set(pos_type pos, Font const & font)
 }
 
 
-FontSize FontList::highestInRange
-       (pos_type startpos, pos_type endpos, FontSize def_size) const
+void FontList::setMisspelled(pos_type startpos, pos_type endpos,
+       bool misspelled)
+{
+       // FIXME: move misspelled state out of font!?
+       for (pos_type p = startpos; p <= endpos; ++p) {
+               Font f = fontIterator(p)->font();
+               if (f.isMisspelled() != misspelled) {
+                       f.setMisspelled(misspelled);
+                       set(p, f);
+               }
+       }
+}
+
+
+FontSize FontList::highestInRange(pos_type startpos, pos_type endpos,
+       FontSize def_size) const
 {
        if (list_.empty())
                return def_size;
 
-       const_iterator end_it = list_.begin();
+       List::const_iterator end_it = fontIterator(endpos);
        const_iterator const end = list_.end();
-       for (; end_it != end; ++end_it) {
-               if (end_it->pos() >= endpos)
-                       break;
-       }
-
        if (end_it != end)
                ++end_it;
 
-       FontList::const_iterator cit = list_.begin();
-       for (; cit != end; ++cit) {
-               if (cit->pos() >= startpos)
-                       break;
-       }
+       List::const_iterator cit = fontIterator(startpos);
 
        FontSize maxsize = FONT_SIZE_TINY;
        for (; cit != end_it; ++cit) {
@@ -230,17 +222,8 @@ FontSize FontList::highestInRange
 
 bool FontList::hasChangeInRange(pos_type pos, int len) const
 {
-       // FIXME: can't we use fontIterator(pos) instead?
-       const_iterator cit = list_.begin();
-       const_iterator end = list_.end();
-       for (; cit != end; ++cit) {
-               if (cit->pos() >= pos)
-                       break;
-       }
-       if (cit != end && pos + len - 1 > cit->pos())
-               return false;
-
-       return true;
+       List::const_iterator cit = fontIterator(pos);
+       return cit == list_.end() || pos + len - 1 <= cit->pos();
 }