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:
@@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
#include "Paragraph.h"
|
#include "Paragraph.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
||||||
Paragraph::Paragraph()
|
Paragraph::Paragraph()
|
||||||
:
|
:
|
||||||
@@ -149,3 +151,47 @@ Paragraph::IsEmpty() const
|
|||||||
{
|
{
|
||||||
return fTextSpans.CountItems() == 0;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -37,6 +37,8 @@ public:
|
|||||||
int32 Length() const;
|
int32 Length() const;
|
||||||
bool IsEmpty() const;
|
bool IsEmpty() const;
|
||||||
|
|
||||||
|
BString GetText(int32 start, int32 length) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ParagraphStyle fStyle;
|
ParagraphStyle fStyle;
|
||||||
TextSpanList fTextSpans;
|
TextSpanList fTextSpans;
|
||||||
|
|||||||
@@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
#include "TextDocument.h"
|
#include "TextDocument.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
|
||||||
TextDocument::TextDocument()
|
TextDocument::TextDocument()
|
||||||
:
|
:
|
||||||
@@ -172,9 +174,11 @@ TextDocument::ParagraphAt(int32 textOffset, int32& paragraphOffset) const
|
|||||||
int32 count = fParagraphs.CountItems();
|
int32 count = fParagraphs.CountItems();
|
||||||
for (int32 i = 0; i < count; i++) {
|
for (int32 i = 0; i < count; i++) {
|
||||||
const Paragraph& paragraph = fParagraphs.ItemAtFast(i);
|
const Paragraph& paragraph = fParagraphs.ItemAtFast(i);
|
||||||
paragraphOffset = textLength;
|
paragraphOffset = textOffset - textLength;
|
||||||
if (textLength + paragraph.Length() > textOffset)
|
int32 paragraphLength = paragraph.Length();
|
||||||
|
if (textLength + paragraphLength > textOffset)
|
||||||
return paragraph;
|
return paragraph;
|
||||||
|
textLength += paragraphLength;
|
||||||
}
|
}
|
||||||
return fEmptyLastParagraph;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -65,6 +65,8 @@ public:
|
|||||||
// Query information
|
// Query information
|
||||||
int32 Length() const;
|
int32 Length() const;
|
||||||
|
|
||||||
|
BString GetText(int32 start, int32 length) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ParagraphList fParagraphs;
|
ParagraphList fParagraphs;
|
||||||
Paragraph fEmptyLastParagraph;
|
Paragraph fEmptyLastParagraph;
|
||||||
|
|||||||
Reference in New Issue
Block a user