HaikuDepot: Paragraph holds TextSpan objects

This commit is contained in:
Stephan Aßmus 2013-08-18 16:52:19 +02:00
parent f1071521b3
commit d4957a50ae
2 changed files with 36 additions and 3 deletions

View File

@ -15,14 +15,16 @@ Paragraph::Paragraph()
Paragraph::Paragraph(const ParagraphStyle& style)
:
fStyle(style)
fStyle(style),
fTextSpans()
{
}
Paragraph::Paragraph(const Paragraph& other)
:
fStyle(other.fStyle)
fStyle(other.fStyle),
fTextSpans(other.fTextSpans)
{
}
@ -31,6 +33,7 @@ Paragraph&
Paragraph::operator=(const Paragraph& other)
{
fStyle = other.fStyle;
fTextSpans = other.fTextSpans;
return *this;
}
@ -39,7 +42,11 @@ Paragraph::operator=(const Paragraph& other)
bool
Paragraph::operator==(const Paragraph& other) const
{
return fStyle == other.fStyle;
if (this == &other)
return true;
return fStyle == other.fStyle
&& fTextSpans == other.fTextSpans;
}
@ -56,3 +63,19 @@ Paragraph::SetStyle(const ParagraphStyle& style)
fStyle = style;
}
bool
Paragraph::Append(const TextSpan& span)
{
// Try to merge with last span if the TextStyles are equal
if (fTextSpans.CountItems() > 0) {
const TextSpan& lastSpan = fTextSpans.LastItem();
if (lastSpan.Style() == span.Style()) {
BString text(lastSpan.Text());
text.Append(span.Text());
fTextSpans.Remove();
return fTextSpans.Add(TextSpan(text, span.Style()));
}
}
return fTextSpans.Add(span);
}

View File

@ -7,6 +7,10 @@
#include "List.h"
#include "ParagraphStyle.h"
#include "TextSpan.h"
typedef List<TextSpan, false> TextSpanList;
class Paragraph {
@ -23,8 +27,14 @@ public:
inline const ParagraphStyle& Style() const
{ return fStyle; }
inline const TextSpanList& TextSpans() const
{ return fTextSpans; }
bool Append(const TextSpan& span);
private:
ParagraphStyle fStyle;
TextSpanList fTextSpans;
};