From c944ee6c2d3741737964213796fb1cdc84481045 Mon Sep 17 00:00:00 2001 From: Stefano Ceccherini Date: Thu, 24 Aug 2006 09:41:22 +0000 Subject: [PATCH] First try at cleaning up StyleBuffer. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@18600 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/interface/BTextView/StyleBuffer.cpp | 58 +++++++++++-------- src/kits/interface/BTextView/StyleBuffer.h | 61 ++++++++++---------- 2 files changed, 63 insertions(+), 56 deletions(-) diff --git a/src/kits/interface/BTextView/StyleBuffer.cpp b/src/kits/interface/BTextView/StyleBuffer.cpp index 366d86cb58..72f56ac838 100644 --- a/src/kits/interface/BTextView/StyleBuffer.cpp +++ b/src/kits/interface/BTextView/StyleBuffer.cpp @@ -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. * * Authors: @@ -157,6 +157,24 @@ _BStyleRecordBuffer_::MatchRecord(const BFont *inFont, // #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) { fValidNullStyle = true; @@ -197,11 +215,11 @@ _BStyleBuffer_::SetNullStyle(uint32 inMode, const BFont *inFont, const rgb_color *inColor, int32 offset) { if (fValidNullStyle || fStyleRunDesc.ItemCount() < 1) - SetStyle(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color); + SetStyleFromMode(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color); else { int32 index = OffsetToRun(offset - 1); fNullStyle = fStyleRecord[fStyleRunDesc[index]->index]->style; - SetStyle(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color); + SetStyleFromMode(inMode, inFont, &fNullStyle.font, inColor, &fNullStyle.color); } 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 _BStyleBuffer_::SetStyleRange(int32 fromOffset, int32 toOffset, int32 textLen, uint32 inMode, const BFont *inFont, @@ -254,7 +282,7 @@ _BStyleBuffer_::SetStyleRange(int32 fromOffset, int32 toOffset, runEnd = fStyleRunDesc[runIndex + 1]->offset; 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); @@ -334,12 +362,10 @@ _BStyleBuffer_::GetStyleRange(int32 startOffset, int32 endOffset) const if (numStyles < 1) numStyles = 1; - STEStyleRange* result = (STEStyleRange *)malloc(sizeof(int32) - + sizeof(STEStyleRun) * numStyles); + STEStyleRange* result = AllocateStyleRange(numStyles); if (!result) return NULL; - result->count = numStyles; STEStyleRun* run = &result->runs[0]; 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 _BStyleBuffer_::operator[](int32 index) const { diff --git a/src/kits/interface/BTextView/StyleBuffer.h b/src/kits/interface/BTextView/StyleBuffer.h index 7dec2f7191..c6125b588d 100644 --- a/src/kits/interface/BTextView/StyleBuffer.h +++ b/src/kits/interface/BTextView/StyleBuffer.h @@ -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. * * Authors: * Marc Flerackers (mflerackers@androme.be) + * Stefano Ceccherini (burton666@libero.it) */ @@ -14,36 +15,36 @@ #include "TextViewSupportBuffer.h" -typedef struct STEStyle { - BFont font; // font - rgb_color color; // pen color -} STEStyle; +struct STEStyle { + BFont font; // font + rgb_color color; // pen color +}; -typedef struct STEStyleRun { - long offset; // byte offset of first character of run - STEStyle style; // style info -} STEStyleRun; +struct STEStyleRun { + long offset; // byte offset of first character of run + STEStyle style; // style info +}; -typedef struct STEStyleRange { - long count; // number of style runs - STEStyleRun runs[1]; // array of count number of runs -} STEStyleRange; +struct STEStyleRange { + long count; // number of style runs + STEStyleRun runs[1]; // array of count number of runs +}; -typedef struct STEStyleRecord { - long refs; // reference count for this style - float ascent; // ascent for this style - float descent; // descent for this style - STEStyle style; // style info -} STEStyleRecord; +struct STEStyleRecord { + long refs; // reference count for this style + float ascent; // ascent for this style + float descent; // descent for this style + STEStyle style; // style info +}; -typedef struct STEStyleRunDesc { - long offset; // byte offset of first character of run - long index; // index of corresponding style record -} STEStyleRunDesc; +struct STEStyleRunDesc { + long offset; // byte offset of first character of run + long index; // index of corresponding style record +}; // _BStyleRunDescBuffer_ class ------------------------------------------------- @@ -108,14 +109,16 @@ class _BStyleBuffer_ { void GetNullStyle(const BFont **font, 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, rgb_color *outColor) const; void ContinuousGetStyle(BFont *, uint32 *, rgb_color *, 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 RemoveStyles(int32 index, int32 count = 1); @@ -129,10 +132,6 @@ class _BStyleBuffer_ { int32 OffsetToRun(int32 offset) const; 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; int32 NumRuns() const;