Paragraph+TextDocument: Added GetText() method

* GetText() extracts the text from a certain range as BString.
 * Probably fixed TextDocument::ParagraphAt(), though it is still untested.
This commit is contained in:
Stephan Aßmus 2013-12-07 22:49:21 +01:00
parent e36ad522d6
commit a9fcf3d6b5
4 changed files with 94 additions and 2 deletions

View File

@ -5,6 +5,8 @@
#include "Paragraph.h"
#include <algorithm>
Paragraph::Paragraph()
:
@ -149,3 +151,47 @@ Paragraph::IsEmpty() const
{
return fTextSpans.CountItems() == 0;
}
BString
Paragraph::GetText(int32 start, int32 length) const
{
if (start < 0)
start = 0;
BString text;
int32 count = fTextSpans.CountItems();
for (int32 i = 0; i < count; i++) {
const TextSpan& span = fTextSpans.ItemAtFast(i);
int32 spanLength = span.CharCount();
if (spanLength == 0)
continue;
if (start > spanLength) {
// Skip span if its before start
start -= spanLength;
continue;
}
// Remaining span length after start
spanLength -= start;
int32 copyLength = std::min(spanLength, length);
if (start == 0 && length == spanLength) {
text << span.Text();
} else {
BString subString;
span.Text().CopyCharsInto(subString, start, copyLength);
text << subString;
}
length -= copyLength;
if (length == 0)
break;
// Next span is copied from its beginning
start = 0;
}
return text;
}

View File

@ -37,6 +37,8 @@ public:
int32 Length() const;
bool IsEmpty() const;
BString GetText(int32 start, int32 length) const;
private:
ParagraphStyle fStyle;
TextSpanList fTextSpans;

View File

@ -5,6 +5,8 @@
#include "TextDocument.h"
#include <algorithm>
TextDocument::TextDocument()
:
@ -172,9 +174,11 @@ TextDocument::ParagraphAt(int32 textOffset, int32& paragraphOffset) const
int32 count = fParagraphs.CountItems();
for (int32 i = 0; i < count; i++) {
const Paragraph& paragraph = fParagraphs.ItemAtFast(i);
paragraphOffset = textLength;
if (textLength + paragraph.Length() > textOffset)
paragraphOffset = textOffset - textLength;
int32 paragraphLength = paragraph.Length();
if (textLength + paragraphLength > textOffset)
return paragraph;
textLength += paragraphLength;
}
return fEmptyLastParagraph;
}
@ -211,3 +215,41 @@ TextDocument::Length() const
}
BString
TextDocument::GetText(int32 start, int32 length) const
{
if (start < 0)
start = 0;
BString text;
int32 count = fParagraphs.CountItems();
for (int32 i = 0; i < count; i++) {
const Paragraph& paragraph = fParagraphs.ItemAtFast(i);
int32 paragraphLength = paragraph.Length();
if (paragraphLength == 0)
continue;
if (start > paragraphLength) {
// Skip paragraph if its before start
start -= paragraphLength;
continue;
}
// Remaining paragraph length after start
paragraphLength -= start;
int32 copyLength = std::min(paragraphLength, length);
text << paragraph.GetText(start, copyLength);
length -= copyLength;
if (length == 0)
break;
else if (i < count - 1)
text << '\n';
// Next paragraph is copied from its beginning
start = 0;
}
return text;
}

View File

@ -65,6 +65,8 @@ public:
// Query information
int32 Length() const;
BString GetText(int32 start, int32 length) const;
private:
ParagraphList fParagraphs;
Paragraph fEmptyLastParagraph;