First try at cleaning up StyleBuffer.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@18600 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini
2006-08-24 09:41:22 +00:00
parent fafab8272d
commit c944ee6c2d
2 changed files with 63 additions and 56 deletions
+33 -25
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2005, Haiku, Inc. All Rights Reserved. * Copyright 2001-2006, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -157,6 +157,24 @@ _BStyleRecordBuffer_::MatchRecord(const BFont *inFont,
// #pragma mark - // #pragma mark -
static void
SetStyleFromMode(uint32 mode, const BFont *fromFont, BFont *toFont,
const rgb_color *fromColor, rgb_color *toColor)
{
if (mode & B_FONT_FAMILY_AND_STYLE)
toFont->SetFamilyAndStyle(fromFont->FamilyAndStyle());
if (mode & B_FONT_SIZE)
toFont->SetSize(fromFont->Size());
if (mode & B_FONT_SHEAR)
toFont->SetShear(fromFont->Shear());
if (!mode || (mode == B_FONT_ALL))
*toColor = *fromColor;
}
_BStyleBuffer_::_BStyleBuffer_(const BFont *inFont, const rgb_color *inColor) _BStyleBuffer_::_BStyleBuffer_(const BFont *inFont, const rgb_color *inColor)
{ {
fValidNullStyle = true; fValidNullStyle = true;
@@ -197,11 +215,11 @@ _BStyleBuffer_::SetNullStyle(uint32 inMode, const BFont *inFont,
const rgb_color *inColor, int32 offset) const rgb_color *inColor, int32 offset)
{ {
if (fValidNullStyle || fStyleRunDesc.ItemCount() < 1) if (fValidNullStyle || fStyleRunDesc.ItemCount() < 1)
SetStyle(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color); SetStyleFromMode(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color);
else { else {
int32 index = OffsetToRun(offset - 1); int32 index = OffsetToRun(offset - 1);
fNullStyle = fStyleRecord[fStyleRunDesc[index]->index]->style; fNullStyle = fStyleRecord[fStyleRunDesc[index]->index]->style;
SetStyle(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color); SetStyleFromMode(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color);
} }
fValidNullStyle = true; fValidNullStyle = true;
@@ -219,6 +237,16 @@ _BStyleBuffer_::GetNullStyle(const BFont **font,
} }
STEStyleRange *
_BStyleBuffer_::AllocateStyleRange(const int32 numStyles) const
{
STEStyleRange* range = (STEStyleRange *)malloc(sizeof(int32) + sizeof(STEStyleRun) * numStyles);
if (range)
range->count = numStyles;
return range;
}
void void
_BStyleBuffer_::SetStyleRange(int32 fromOffset, int32 toOffset, _BStyleBuffer_::SetStyleRange(int32 fromOffset, int32 toOffset,
int32 textLen, uint32 inMode, const BFont *inFont, int32 textLen, uint32 inMode, const BFont *inFont,
@@ -254,7 +282,7 @@ _BStyleBuffer_::SetStyleRange(int32 fromOffset, int32 toOffset,
runEnd = fStyleRunDesc[runIndex + 1]->offset; runEnd = fStyleRunDesc[runIndex + 1]->offset;
STEStyle style = fStyleRecord[runDesc.index]->style; STEStyle style = fStyleRecord[runDesc.index]->style;
SetStyle(inMode, inFont, &style.font, inColor, &style.color); SetStyleFromMode(inMode, inFont, &style.font, inColor, &style.color);
styleIndex = fStyleRecord.InsertRecord(&style.font, &style.color); styleIndex = fStyleRecord.InsertRecord(&style.font, &style.color);
@@ -334,12 +362,10 @@ _BStyleBuffer_::GetStyleRange(int32 startOffset, int32 endOffset) const
if (numStyles < 1) if (numStyles < 1)
numStyles = 1; numStyles = 1;
STEStyleRange* result = (STEStyleRange *)malloc(sizeof(int32) STEStyleRange* result = AllocateStyleRange(numStyles);
+ sizeof(STEStyleRun) * numStyles);
if (!result) if (!result)
return NULL; return NULL;
result->count = numStyles;
STEStyleRun* run = &result->runs[0]; STEStyleRun* run = &result->runs[0];
for (int32 index = 0; index < numStyles; index++) { for (int32 index = 0; index < numStyles; index++) {
@@ -445,24 +471,6 @@ _BStyleBuffer_::BumpOffset(int32 delta, int32 index)
} }
void
_BStyleBuffer_::SetStyle(uint32 mode, const BFont *fromFont, BFont *toFont,
const rgb_color *fromColor, rgb_color *toColor)
{
if (mode & B_FONT_FAMILY_AND_STYLE)
toFont->SetFamilyAndStyle(fromFont->FamilyAndStyle());
if (mode & B_FONT_SIZE)
toFont->SetSize(fromFont->Size());
if (mode & B_FONT_SHEAR)
toFont->SetShear(fromFont->Shear());
if (!mode || (mode == B_FONT_ALL))
*toColor = *fromColor;
}
STEStyleRun STEStyleRun
_BStyleBuffer_::operator[](int32 index) const _BStyleBuffer_::operator[](int32 index) const
{ {
+30 -31
View File
@@ -1,9 +1,10 @@
/* /*
* Copyright 2001-2005, Haiku, Inc. All Rights Reserved. * Copyright 2001-2006, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Marc Flerackers ([email protected]) * Marc Flerackers ([email protected])
* Stefano Ceccherini ([email protected])
*/ */
@@ -14,36 +15,36 @@
#include "TextViewSupportBuffer.h" #include "TextViewSupportBuffer.h"
typedef struct STEStyle { struct STEStyle {
BFont font; // font BFont font; // font
rgb_color color; // pen color rgb_color color; // pen color
} STEStyle; };
typedef struct STEStyleRun { struct STEStyleRun {
long offset; // byte offset of first character of run long offset; // byte offset of first character of run
STEStyle style; // style info STEStyle style; // style info
} STEStyleRun; };
typedef struct STEStyleRange { struct STEStyleRange {
long count; // number of style runs long count; // number of style runs
STEStyleRun runs[1]; // array of count number of runs STEStyleRun runs[1]; // array of count number of runs
} STEStyleRange; };
typedef struct STEStyleRecord { struct STEStyleRecord {
long refs; // reference count for this style long refs; // reference count for this style
float ascent; // ascent for this style float ascent; // ascent for this style
float descent; // descent for this style float descent; // descent for this style
STEStyle style; // style info STEStyle style; // style info
} STEStyleRecord; };
typedef struct STEStyleRunDesc { struct STEStyleRunDesc {
long offset; // byte offset of first character of run long offset; // byte offset of first character of run
long index; // index of corresponding style record long index; // index of corresponding style record
} STEStyleRunDesc; };
// _BStyleRunDescBuffer_ class ------------------------------------------------- // _BStyleRunDescBuffer_ class -------------------------------------------------
@@ -108,14 +109,16 @@ class _BStyleBuffer_ {
void GetNullStyle(const BFont **font, void GetNullStyle(const BFont **font,
const rgb_color **color) const; const rgb_color **color) const;
void SetStyleRange(int32 fromOffset, int32 toOffset,
int32 textLen, uint32 inMode,
const BFont *inFont, const rgb_color *inColor);
void GetStyle(int32 inOffset, BFont *outFont, void GetStyle(int32 inOffset, BFont *outFont,
rgb_color *outColor) const; rgb_color *outColor) const;
void ContinuousGetStyle(BFont *, uint32 *, rgb_color *, void ContinuousGetStyle(BFont *, uint32 *, rgb_color *,
bool *, int32, int32) const; bool *, int32, int32) const;
STEStyleRange* GetStyleRange(int32 startOffset, int32 endOffset) const;
STEStyleRange* AllocateStyleRange(const int32 numStyles) const;
void SetStyleRange(int32 fromOffset, int32 toOffset,
int32 textLen, uint32 inMode,
const BFont *inFont, const rgb_color *inColor);
STEStyleRange* GetStyleRange(int32 startOffset, int32 endOffset) const;
void RemoveStyleRange(int32 fromOffset, int32 toOffset); void RemoveStyleRange(int32 fromOffset, int32 toOffset);
void RemoveStyles(int32 index, int32 count = 1); void RemoveStyles(int32 index, int32 count = 1);
@@ -129,10 +132,6 @@ class _BStyleBuffer_ {
int32 OffsetToRun(int32 offset) const; int32 OffsetToRun(int32 offset) const;
void BumpOffset(int32 delta, int32 index); void BumpOffset(int32 delta, int32 index);
void SetStyle(uint32 mode, const BFont *fromFont,
BFont *toFont, const rgb_color *fromColor,
rgb_color *toColor);
STEStyleRun operator[](int32 index) const; STEStyleRun operator[](int32 index) const;
int32 NumRuns() const; int32 NumRuns() const;