HaikuDepot: Paragraph holds TextSpan objects

This commit is contained in:
Stephan Aßmus
2013-08-18 17:53:01 +02:00
parent f1071521b3
commit d4957a50ae
2 changed files with 36 additions and 3 deletions
+26 -3
View File
@@ -15,14 +15,16 @@ Paragraph::Paragraph()
Paragraph::Paragraph(const ParagraphStyle& style) Paragraph::Paragraph(const ParagraphStyle& style)
: :
fStyle(style) fStyle(style),
fTextSpans()
{ {
} }
Paragraph::Paragraph(const Paragraph& other) Paragraph::Paragraph(const Paragraph& other)
: :
fStyle(other.fStyle) fStyle(other.fStyle),
fTextSpans(other.fTextSpans)
{ {
} }
@@ -31,6 +33,7 @@ Paragraph&
Paragraph::operator=(const Paragraph& other) Paragraph::operator=(const Paragraph& other)
{ {
fStyle = other.fStyle; fStyle = other.fStyle;
fTextSpans = other.fTextSpans;
return *this; return *this;
} }
@@ -39,7 +42,11 @@ Paragraph::operator=(const Paragraph& other)
bool bool
Paragraph::operator==(const Paragraph& other) const 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; 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);
}
+10
View File
@@ -7,6 +7,10 @@
#include "List.h" #include "List.h"
#include "ParagraphStyle.h" #include "ParagraphStyle.h"
#include "TextSpan.h"
typedef List<TextSpan, false> TextSpanList;
class Paragraph { class Paragraph {
@@ -23,8 +27,14 @@ public:
inline const ParagraphStyle& Style() const inline const ParagraphStyle& Style() const
{ return fStyle; } { return fStyle; }
inline const TextSpanList& TextSpans() const
{ return fTextSpans; }
bool Append(const TextSpan& span);
private: private:
ParagraphStyle fStyle; ParagraphStyle fStyle;
TextSpanList fTextSpans;
}; };