HaikuDepot: Added Insert(TextSpan) to Paragraph
This commit is contained in:
parent
be42d7a9a3
commit
03891f74db
@ -79,3 +79,47 @@ Paragraph::Append(const TextSpan& span)
|
||||
}
|
||||
return fTextSpans.Add(span);
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
Paragraph::Insert(int32 offset, const TextSpan& newSpan)
|
||||
{
|
||||
int32 index = 0;
|
||||
while (index < fTextSpans.CountItems()) {
|
||||
const TextSpan& span = fTextSpans.ItemAtFast(index);
|
||||
if (offset - span.CharCount() < 0)
|
||||
break;
|
||||
offset -= span.CharCount();
|
||||
}
|
||||
|
||||
if (fTextSpans.CountItems() == index)
|
||||
return Append(newSpan);
|
||||
|
||||
// Try to merge with span at index if the TextStyles are equal
|
||||
TextSpan span = fTextSpans.ItemAtFast(index);
|
||||
if (span.Style() == newSpan.Style()) {
|
||||
span.Insert(offset, newSpan.Text());
|
||||
return fTextSpans.Replace(span, index);
|
||||
}
|
||||
|
||||
if (offset == 0) {
|
||||
if (index > 0) {
|
||||
// Try to merge with TextSpan before if offset == 0 && index > 0
|
||||
TextSpan span = fTextSpans.ItemAtFast(index - 1);
|
||||
if (span.Style() == newSpan.Style()) {
|
||||
span.Insert(span.CharCount(), newSpan.Text());
|
||||
return fTextSpans.Replace(span, index - 1);
|
||||
}
|
||||
}
|
||||
// Just insert the new span before the one at index
|
||||
return fTextSpans.Add(newSpan, index);
|
||||
}
|
||||
|
||||
// Split the span,
|
||||
TextSpan spanBefore = span.SubSpan(0, offset);
|
||||
TextSpan spanAfter = span.SubSpan(offset, span.CharCount() - offset);
|
||||
|
||||
return fTextSpans.Replace(spanBefore, index)
|
||||
&& fTextSpans.Add(newSpan, index + 1)
|
||||
&& fTextSpans.Add(spanAfter, index + 2);
|
||||
}
|
||||
|
@ -31,6 +31,7 @@ public:
|
||||
{ return fTextSpans; }
|
||||
|
||||
bool Append(const TextSpan& span);
|
||||
bool Insert(int32 offset, const TextSpan& span);
|
||||
|
||||
private:
|
||||
ParagraphStyle fStyle;
|
||||
|
Loading…
Reference in New Issue
Block a user