r20558 by craig -
scribus-commit
scribus-commit at lists.scribus.net
Mon Nov 16 22:58:58 UTC 2015
Author: craig
Date: Mon Nov 16 22:58:58 2015
New Revision: 20558
URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=20558
Log:
#12490: Fix word and sentence detection
Modified:
trunk/Scribus/scribus/text/storytext.cpp
trunk/Scribus/scribus/text/storytext.h
Modified: trunk/Scribus/scribus/text/storytext.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=20558&path=/trunk/Scribus/scribus/text/storytext.cpp
==============================================================================
--- trunk/Scribus/scribus/text/storytext.cpp (original)
+++ trunk/Scribus/scribus/text/storytext.cpp Mon Nov 16 22:58:58 2015
@@ -774,9 +774,8 @@
QString StoryText::sentence(int pos, int &posn)
{
int sentencePos=qMax(0, prevSentence(pos));
- sentencePos=qMax(sentencePos, nextWord(sentencePos));
posn=sentencePos;
- int nextSentencePos=qMin(length(), nextSentence(pos+1));
+ int nextSentencePos=qMin(length(), endOfSentence(pos));
return text(sentencePos, nextSentencePos-sentencePos);
}
@@ -1376,8 +1375,8 @@
// positioning. all positioning methods return char positions
// FIXME: make that methods use correct semantic boundaries
-static QString wordBoundaries(" .,:;\"'!?\n\t");
-static QString sentenceBoundaries(".:!?\n\t");
+static QString wordBoundaries(" .,:;\"'!?\r\n\t");
+static QString sentenceBoundaries(".!?\r\n\t");
int StoryText::nextChar(int pos)
{
@@ -1412,14 +1411,32 @@
int StoryText::nextWord(int pos)
{
int len = length();
- if (text(pos).isLetter())
- pos = qMin(len, pos+1);
+ //move to text
+ pos = qMin(len, pos+1);
+ //while not at the end, and while we don't find a word boundary, move to the next character
+ while (pos < len && wordBoundaries.indexOf(text(pos)) < 0)
+ ++pos;
+ //while not at the end, and while we find a word boundary, move to the next character
+ while (pos < len && wordBoundaries.indexOf(text(pos)) >= 0)
+ ++pos;
+ //if we didn't get to the end, return current position, otherwise return the end.
+ if (pos < len)
+ return pos;
else
- pos = qMin(len, pos);
-
- // while (pos < len && wordBoundaries.indexOf(text(pos)) < 0)
- // ++pos;
-
+ return len;
+}
+
+int StoryText::prevWord(int pos)
+{
+ pos = qMax(0, pos-1);
+ while (pos > 0 && wordBoundaries.indexOf(text(pos)) < 0)
+ --pos;
+ return wordBoundaries.indexOf(text(pos)) < 0 ? pos + 1 : pos;
+}
+
+int StoryText::endOfWord(int pos) const
+{
+ int len = length();
while (pos < len)
{
if (text(pos).isLetter())
@@ -1427,44 +1444,39 @@
else
break;
}
- return pos < len ? pos + 1 : pos;
-}
-
-int StoryText::prevWord(int pos)
-{
- pos = qMax(0, pos-1);
- while (pos > 0 && wordBoundaries.indexOf(text(pos)) < 0)
- --pos;
- return wordBoundaries.indexOf(text(pos)) < 0 ? pos + 1 : pos;
-}
-
-int StoryText::endOfWord(int pos) const
-{
- int len = length();
- while (pos < len)
- {
- if (text(pos).isLetter())
- ++pos;
- else
- break;
- }
return pos;
}
-int StoryText::nextSentence(int pos)
+int StoryText::endOfSentence(int pos) const
{
int len = length();
pos = qMin(len, pos+1);
+ //while not on a sentence boundary, keep moving forward
while (pos < len && sentenceBoundaries.indexOf(text(pos)) < 0)
++pos;
+ //return the sentence boundary too
return pos < len ? pos + 1 : pos;
}
+
+int StoryText::nextSentence(int pos)
+{
+ int len = length();
+ pos = endOfSentence(pos);
+ //while on a sentence boundary, keep moving forward
+ while (pos < len && sentenceBoundaries.indexOf(text(pos)) >= 0)
+ ++pos;
+ return pos;
+}
+
int StoryText::prevSentence(int pos)
{
- pos = qMax(0, pos-1);
- while (pos > 0 && sentenceBoundaries.indexOf(text(pos)) < 0)
+ //we cannot go before the first position so just return it.
+ if (pos == 0)
+ return 0;
+ //while not on a sentence boundary, keep moving backward
+ while (pos > 0 && sentenceBoundaries.indexOf(text(pos-1)) < 0)
--pos;
- return sentenceBoundaries.indexOf(text(pos)) < 0 ? pos + 1 : pos;
+ return pos;
}
int StoryText::nextParagraph(int pos)
{
Modified: trunk/Scribus/scribus/text/storytext.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=20558&path=/trunk/Scribus/scribus/text/storytext.h
==============================================================================
--- trunk/Scribus/scribus/text/storytext.h (original)
+++ trunk/Scribus/scribus/text/storytext.h Mon Nov 16 22:58:58 2015
@@ -202,6 +202,7 @@
int endOfWord(int pos) const;
int nextSentence(int pos);
int prevSentence(int pos);
+ int endOfSentence(int pos) const;
int nextParagraph(int pos);
int prevParagraph(int pos);
More information about the scribus-commit
mailing list