"Fossies" - the Fresh Open Source Software Archive

Member "scribus-1.5.6.1/scribus/text/scworditerator.cpp" (14 Nov 2020, 2541 Bytes) of package /linux/misc/scribus-1.5.6.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "scworditerator.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.5.6_vs_1.5.6.1.

    1 /*
    2  For general Scribus (>=1.3.2) copyright and licensing information please refer
    3  to the COPYING file provided with the program. Following this notice may exist
    4  a copyright and/or license notice that predates the release of Scribus 1.3.2
    5  for which a new license (GPL+exception) is in place.
    6  */
    7 
    8 #include <unicode/brkiter.h>
    9 #include "scworditerator.h"
   10 
   11 using namespace icu;
   12 
   13 ScWordIterator::ScWordIterator(StoryText& story)
   14     : m_story(story)
   15 {
   16     m_wordIterator = getWordIterator();
   17 }
   18 
   19 ScWordIterator::~ScWordIterator()
   20 {
   21     if (m_wordIterator)
   22     {
   23         delete m_wordIterator;
   24         m_wordIterator = nullptr;
   25     }
   26     if (m_unicodeString)
   27     {
   28         delete m_unicodeString;
   29         m_unicodeString = nullptr;
   30     }
   31 }
   32 
   33 BreakIterator* ScWordIterator::getWordIterator()
   34 {
   35     if (m_wordIterator != nullptr)
   36         return m_wordIterator;
   37     
   38     UErrorCode status = U_ZERO_ERROR;
   39     m_wordIterator = BreakIterator::createWordInstance(Locale(), status);
   40     if (U_FAILURE(status))
   41     {
   42         delete m_wordIterator;
   43         m_wordIterator = nullptr;
   44         return  nullptr;
   45     }
   46     
   47     if (m_unicodeString)
   48     {
   49         delete m_unicodeString;
   50         m_unicodeString = nullptr;
   51     }
   52     m_unicodeString = new icu::UnicodeString((const UChar*) m_story.plainText().utf16());
   53     m_wordIterator->setText(*m_unicodeString);
   54     return m_wordIterator;
   55 }
   56 
   57 int ScWordIterator::firstWord()
   58 {
   59     int len = m_story.length();
   60     int pos = m_wordIterator->first();
   61 
   62     while (pos < len)
   63     {
   64         if (m_story.text(pos).isLetter())
   65             break;
   66         ++pos;
   67     }
   68     return pos;
   69 }
   70 
   71 int ScWordIterator::nextWord(int pos)
   72 {
   73     if (!m_wordIterator)
   74         return pos;
   75     pos = m_wordIterator->following(pos);
   76     
   77     int len = m_story.length();
   78     while ((pos < len) && (m_story.text(pos).isSpace() || m_story.text(pos).isPunct()))
   79         ++pos;
   80     return pos;
   81 }
   82 
   83 int ScWordIterator::prevWord(int pos)
   84 {
   85     if (!m_wordIterator)
   86         return pos;
   87     pos = m_wordIterator->preceding(pos);
   88     return pos;
   89 }
   90 
   91 int ScWordIterator::endOfWord(int pos)
   92 {
   93     if (!m_wordIterator)
   94         return pos;
   95     pos = m_wordIterator->following(pos);
   96     return pos;
   97 }
   98 
   99 QString ScWordIterator::word(int pos)
  100 {
  101     if (m_wordIterator)
  102     {
  103         int end = m_wordIterator->following(pos);
  104         return m_story.text(pos, end - pos);
  105     }
  106 
  107     // Fallback to old code
  108     int eoWord = pos;
  109     int length =  m_story.length();
  110     while (eoWord < length)
  111     {
  112         QChar qChar = m_story.text(eoWord);
  113         if (qChar.isLetterOrNumber() || SpecialChars::isIgnorableCodePoint(qChar.unicode()))
  114             ++eoWord;
  115         else
  116             break;
  117     }
  118     return m_story.text(pos, eoWord - pos);
  119 }