HaikuDepot: Redesign TextStyle as CharacterStyle ...

... following the example set by ParagraphStyle and
 ParagraphStyleData.
This commit is contained in:
Stephan Aßmus 2013-09-02 12:04:33 +02:00
parent 621cb2bf13
commit 947ba26bd0
5 changed files with 582 additions and 0 deletions

View File

@ -27,9 +27,12 @@ Application HaikuDepot :
support.cpp
# textview stuff
CharacterStyle.cpp
CharacterStyleData.cpp
Paragraph.cpp
ParagraphStyle.cpp
ParagraphStyleData.cpp
TextDocument.cpp
TextLayout.cpp
TextSpan.cpp
TextStyle.cpp

View File

@ -0,0 +1,176 @@
/*
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "CharacterStyle.h"
CharacterStyle::CharacterStyle()
:
fStyleData(new CharacterStyleData(), true)
{
}
CharacterStyle::CharacterStyle(const CharacterStyle& other)
:
fStyleData(other.fStyleData)
{
}
CharacterStyle&
CharacterStyle::operator=(const CharacterStyle& other)
{
if (this == &other)
return *this;
fStyleData = other.fStyleData;
return *this;
}
bool
CharacterStyle::operator==(const CharacterStyle& other) const
{
if (this == &other)
return true;
if (fStyleData == other.fStyleData)
return true;
if (fStyleData.Get() != NULL && other.fStyleData.Get() != NULL)
return *fStyleData.Get() == *other.fStyleData.Get();
return false;
}
bool
CharacterStyle::operator!=(const CharacterStyle& other) const
{
return !(*this == other);
}
bool
CharacterStyle::SetFont(const BFont& font)
{
CharacterStyleDataRef data = fStyleData->SetFont(font);
if (data == fStyleData)
return data->Font() == font;
fStyleData = data;
return true;
}
bool
CharacterStyle::SetAscent(float ascent)
{
CharacterStyleDataRef data = fStyleData->SetAscent(ascent);
if (data == fStyleData)
return data->Ascent() == ascent;
fStyleData = data;
return true;
}
bool
CharacterStyle::SetDescent(float descent)
{
CharacterStyleDataRef data = fStyleData->SetDescent(descent);
if (data == fStyleData)
return data->Descent() == descent;
fStyleData = data;
return true;
}
bool
CharacterStyle::SetWidth(float width)
{
CharacterStyleDataRef data = fStyleData->SetWidth(width);
if (data == fStyleData)
return data->Width() == width;
fStyleData = data;
return true;
}
bool
CharacterStyle::SetForegroundColor(rgb_color color)
{
CharacterStyleDataRef data = fStyleData->SetForegroundColor(color);
if (data == fStyleData)
return data->ForegroundColor() == color;
fStyleData = data;
return true;
}
bool
CharacterStyle::SetBackgroundColor(rgb_color color)
{
CharacterStyleDataRef data = fStyleData->SetBackgroundColor(color);
if (data == fStyleData)
return data->BackgroundColor() == color;
fStyleData = data;
return true;
}
bool
CharacterStyle::SetStrikeOutColor(rgb_color color)
{
CharacterStyleDataRef data = fStyleData->SetStrikeOutColor(color);
if (data == fStyleData)
return data->StrikeOutColor() == color;
fStyleData = data;
return true;
}
bool
CharacterStyle::SetUnderlineColor(rgb_color color)
{
CharacterStyleDataRef data = fStyleData->SetUnderlineColor(color);
if (data == fStyleData)
return data->UnderlineColor() == color;
fStyleData = data;
return true;
}
bool
CharacterStyle::SetStrikeOut(uint8 strikeOut)
{
CharacterStyleDataRef data = fStyleData->SetStrikeOut(strikeOut);
if (data == fStyleData)
return data->StrikeOut() == strikeOut;
fStyleData = data;
return true;
}
bool
CharacterStyle::SetUnderline(uint8 underline)
{
CharacterStyleDataRef data = fStyleData->SetUnderline(underline);
if (data == fStyleData)
return data->Underline() == underline;
fStyleData = data;
return true;
}

View File

@ -0,0 +1,55 @@
/*
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#ifndef CHARACTER_STYLE_H
#define CHARACTER_STYLE_H
#include "CharacterStyleData.h"
class CharacterStyle {
public:
CharacterStyle();
CharacterStyle(const CharacterStyle& other);
CharacterStyle& operator=(const CharacterStyle& other);
bool operator==(const CharacterStyle& other) const;
bool operator!=(const CharacterStyle& other) const;
bool SetFont(const BFont& font);
const BFont& Font() const;
bool SetAscent(float ascent);
float Ascent() const;
bool SetDescent(float descent);
float Descent() const;
bool SetWidth(float width);
float Width() const;
bool SetForegroundColor(rgb_color color);
rgb_color ForegroundColor() const;
bool SetBackgroundColor(rgb_color color);
rgb_color BackgroundColor() const;
bool SetStrikeOutColor(rgb_color color);
rgb_color StrikeOutColor() const;
bool SetUnderlineColor(rgb_color color);
rgb_color UnderlineColor() const;
bool SetStrikeOut(uint8 strikeOut);
uint8 StrikeOut() const;
bool SetUnderline(uint8 underline);
uint8 Underline() const;
private:
CharacterStyleDataRef fStyleData;
};
#endif // CHARACTER_STYLE_H

View File

@ -0,0 +1,240 @@
/*
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "CharacterStyleData.h"
#include <new>
CharacterStyleData::CharacterStyleData()
:
fFont(),
fAscent(-1.0f),
fDescent(-1.0f),
fWidth(-1.0f),
fGlyphSpacing(0.0f),
fFgColor((rgb_color){ 0, 0, 0, 255 }),
fBgColor((rgb_color){ 255, 255, 255, 255 }),
fStrikeOutColor((rgb_color){ 0, 0, 0, 255 }),
fUnderlineColor((rgb_color){ 0, 0, 0, 255 }),
fStrikeOutStyle(STRIKE_OUT_NONE),
fUnderlineStyle(UNDERLINE_NONE)
{
}
CharacterStyleData::CharacterStyleData(const CharacterStyleData& other)
:
fFont(other.fFont),
fAscent(other.fAscent),
fDescent(other.fDescent),
fWidth(other.fWidth),
fGlyphSpacing(other.fGlyphSpacing),
fFgColor(other.fFgColor),
fBgColor(other.fBgColor),
fStrikeOutColor(other.fStrikeOutColor),
fUnderlineColor(other.fUnderlineColor),
fStrikeOutStyle(other.fStrikeOutStyle),
fUnderlineStyle(other.fUnderlineStyle)
{
}
bool
CharacterStyleData::operator==(const CharacterStyleData& other) const
{
if (this == &other)
return true;
return fFont == other.fFont
&& fAscent == other.fAscent
&& fDescent == other.fDescent
&& fWidth == other.fWidth
&& fGlyphSpacing == other.fGlyphSpacing
&& fFgColor == other.fFgColor
&& fBgColor == other.fBgColor
&& fStrikeOutColor == other.fStrikeOutColor
&& fUnderlineColor == other.fUnderlineColor
&& fStrikeOutStyle == other.fStrikeOutStyle
&& fUnderlineStyle == other.fUnderlineStyle;
}
bool
CharacterStyleData::operator!=(const CharacterStyleData& other) const
{
return !(*this == other);
}
CharacterStyleDataRef
CharacterStyleData::SetFont(const BFont& font)
{
if (fFont == font)
return CharacterStyleDataRef(this);
CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this);
if (ret == NULL)
return CharacterStyleDataRef(this);
ret->fFont = font;
return CharacterStyleDataRef(ret, true);
}
CharacterStyleDataRef
CharacterStyleData::SetAscent(float ascent)
{
if (fAscent == ascent)
return CharacterStyleDataRef(this);
CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this);
if (ret == NULL)
return CharacterStyleDataRef(this);
ret->fAscent = ascent;
return CharacterStyleDataRef(ret, true);
}
CharacterStyleDataRef
CharacterStyleData::SetDescent(float descent)
{
if (fDescent == descent)
return CharacterStyleDataRef(this);
CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this);
if (ret == NULL)
return CharacterStyleDataRef(this);
ret->fDescent = descent;
return CharacterStyleDataRef(ret, true);
}
CharacterStyleDataRef
CharacterStyleData::SetWidth(float width)
{
if (fWidth == width)
return CharacterStyleDataRef(this);
CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this);
if (ret == NULL)
return CharacterStyleDataRef(this);
ret->fWidth = width;
return CharacterStyleDataRef(ret, true);
}
CharacterStyleDataRef
CharacterStyleData::SetForegroundColor(rgb_color color)
{
if (fFgColor == color)
return CharacterStyleDataRef(this);
CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this);
if (ret == NULL)
return CharacterStyleDataRef(this);
ret->fFgColor = color;
return CharacterStyleDataRef(ret, true);
}
CharacterStyleDataRef
CharacterStyleData::SetBackgroundColor(rgb_color color)
{
if (fBgColor == color)
return CharacterStyleDataRef(this);
CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this);
if (ret == NULL)
return CharacterStyleDataRef(this);
ret->fBgColor = color;
return CharacterStyleDataRef(ret, true);
}
CharacterStyleDataRef
CharacterStyleData::SetStrikeOutColor(rgb_color color)
{
if (fStrikeOutColor == color)
return CharacterStyleDataRef(this);
CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this);
if (ret == NULL)
return CharacterStyleDataRef(this);
ret->fStrikeOutColor = color;
return CharacterStyleDataRef(ret, true);
}
CharacterStyleDataRef
CharacterStyleData::SetUnderlineColor(rgb_color color)
{
if (fUnderlineColor == color)
return CharacterStyleDataRef(this);
CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this);
if (ret == NULL)
return CharacterStyleDataRef(this);
ret->fUnderlineColor = color;
return CharacterStyleDataRef(ret, true);
}
CharacterStyleDataRef
CharacterStyleData::SetStrikeOut(uint8 strikeOut)
{
if (fStrikeOutStyle == strikeOut)
return CharacterStyleDataRef(this);
CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this);
if (ret == NULL)
return CharacterStyleDataRef(this);
ret->fStrikeOutStyle = strikeOut;
return CharacterStyleDataRef(ret, true);
}
CharacterStyleDataRef
CharacterStyleData::SetUnderline(uint8 underline)
{
if (fUnderlineStyle == underline)
return CharacterStyleDataRef(this);
CharacterStyleData* ret = new(std::nothrow) CharacterStyleData(*this);
if (ret == NULL)
return CharacterStyleDataRef(this);
ret->fUnderlineStyle = underline;
return CharacterStyleDataRef(ret, true);
}
// #pragma mark - private
CharacterStyleData&
CharacterStyleData::operator=(const CharacterStyleData& other)
{
return *this;
}

View File

@ -0,0 +1,108 @@
/*
* Copyright 2013, Stephan Aßmus <superstippi@gmx.de>.
* All rights reserved. Distributed under the terms of the MIT License.
*/
#ifndef CHARACTER_STYLE_DATA_H
#define CHARACTER_STYLE_DATA_H
#include <Font.h>
#include <Referenceable.h>
enum {
STRIKE_OUT_NONE = 0,
STRIKE_OUT_SINGLE = 1,
};
enum {
UNDERLINE_NONE = 0,
UNDERLINE_SINGLE = 1,
UNDERLINE_DOUBLE = 2,
UNDERLINE_WIGGLE = 3,
UNDERLINE_DOTTED = 4,
};
class CharacterStyleData;
typedef BReference<CharacterStyleData> CharacterStyleDataRef;
// You cannot modify a CharacterStyleData object once it has been
// created.
class CharacterStyleData : public BReferenceable {
public:
CharacterStyleData();
CharacterStyleData(
const CharacterStyleData& other);
bool operator==(
const CharacterStyleData& other) const;
bool operator!=(
const CharacterStyleData& other) const;
CharacterStyleDataRef SetFont(const BFont& font);
inline const BFont& Font() const
{ return fFont; }
CharacterStyleDataRef SetAscent(float ascent);
inline float Ascent() const
{ return fAscent; }
CharacterStyleDataRef SetDescent(float descent);
inline float Descent() const
{ return fDescent; }
CharacterStyleDataRef SetWidth(float width);
inline float Width() const
{ return fWidth; }
CharacterStyleDataRef SetForegroundColor(rgb_color color);
inline rgb_color ForegroundColor() const
{ return fFgColor; }
CharacterStyleDataRef SetBackgroundColor(rgb_color color);
inline rgb_color BackgroundColor() const
{ return fBgColor; }
CharacterStyleDataRef SetStrikeOutColor(rgb_color color);
inline rgb_color StrikeOutColor() const
{ return fStrikeOutColor; }
CharacterStyleDataRef SetUnderlineColor(rgb_color color);
inline rgb_color UnderlineColor() const
{ return fUnderlineColor; }
CharacterStyleDataRef SetStrikeOut(uint8 strikeOut);
inline uint8 StrikeOut() const
{ return fStrikeOutStyle; }
CharacterStyleDataRef SetUnderline(uint8 underline);
inline uint8 Underline() const
{ return fUnderlineStyle; }
private:
CharacterStyleData& operator=(const CharacterStyleData& other);
private:
BFont fFont;
// The following three values override glyph metrics unless -1
float fAscent;
float fDescent;
float fWidth;
float fGlyphSpacing;
rgb_color fFgColor;
rgb_color fBgColor;
rgb_color fStrikeOutColor;
rgb_color fUnderlineColor;
uint8 fStrikeOutStyle;
uint8 fUnderlineStyle;
};
#endif // CHARACTER_STYLE_DATA_H