From d4957a50aef6609213f5f637a2c9034b7c15157f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sun, 18 Aug 2013 16:52:19 +0200 Subject: [PATCH] HaikuDepot: Paragraph holds TextSpan objects --- src/apps/haiku-depot/textview/Paragraph.cpp | 29 ++++++++++++++++++--- src/apps/haiku-depot/textview/Paragraph.h | 10 +++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/src/apps/haiku-depot/textview/Paragraph.cpp b/src/apps/haiku-depot/textview/Paragraph.cpp index b92bcf3c0c..ce2c6cce7f 100644 --- a/src/apps/haiku-depot/textview/Paragraph.cpp +++ b/src/apps/haiku-depot/textview/Paragraph.cpp @@ -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); +} diff --git a/src/apps/haiku-depot/textview/Paragraph.h b/src/apps/haiku-depot/textview/Paragraph.h index 64476a0faf..56b439a105 100644 --- a/src/apps/haiku-depot/textview/Paragraph.h +++ b/src/apps/haiku-depot/textview/Paragraph.h @@ -7,6 +7,10 @@ #include "List.h" #include "ParagraphStyle.h" +#include "TextSpan.h" + + +typedef List 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; };