Skip to content

Commit

Permalink
HaikuDepot: Paragraph holds TextSpan objects
Browse files Browse the repository at this point in the history
  • Loading branch information
stippi committed Aug 18, 2013
1 parent f107152 commit d4957a5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/apps/haiku-depot/textview/Paragraph.cpp
Expand Up @@ -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)
{
}

Expand All @@ -31,6 +33,7 @@ Paragraph&
Paragraph::operator=(const Paragraph& other)
{
fStyle = other.fStyle;
fTextSpans = other.fTextSpans;

return *this;
}
Expand All @@ -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;
}


Expand All @@ -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);
}
10 changes: 10 additions & 0 deletions src/apps/haiku-depot/textview/Paragraph.h
Expand Up @@ -7,6 +7,10 @@

#include "List.h"
#include "ParagraphStyle.h"
#include "TextSpan.h"


typedef List<TextSpan, false> TextSpanList;


class Paragraph {
Expand All @@ -23,8 +27,14 @@ class Paragraph {
inline const ParagraphStyle& Style() const
{ return fStyle; }

inline const TextSpanList& TextSpans() const
{ return fTextSpans; }

bool Append(const TextSpan& span);

private:
ParagraphStyle fStyle;
TextSpanList fTextSpans;
};


Expand Down

0 comments on commit d4957a5

Please sign in to comment.