Style fixes to StyleBuffer only

This commit is contained in:
John Scipione
2015-02-04 15:54:44 -05:00
parent 89a406bc52
commit 47205988db
2 changed files with 158 additions and 123 deletions
@@ -1,10 +1,10 @@
/* /*
* Copyright 2001-2006, 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]) * Stefano Ceccherini, [email protected]
*/ */
/** Style storage used by BTextView */ /** Style storage used by BTextView */
@@ -18,8 +18,12 @@
#include <stdio.h> #include <stdio.h>
// #pragma mark - _BStyleRunDescBuffer_
_BStyleRunDescBuffer_::_BStyleRunDescBuffer_() _BStyleRunDescBuffer_::_BStyleRunDescBuffer_()
: _BTextViewSupportBuffer_<STEStyleRunDesc>(20) :
_BTextViewSupportBuffer_<STEStyleRunDesc>(20)
{ {
} }
@@ -51,14 +55,11 @@ _BStyleRunDescBuffer_::OffsetToRun(int32 offset) const
while (minIndex < maxIndex) { while (minIndex < maxIndex) {
index = (minIndex + maxIndex) >> 1; index = (minIndex + maxIndex) >> 1;
if (offset >= fBuffer[index].offset) { if (offset >= fBuffer[index].offset) {
if (index >= (fItemCount - 1)) { if (index >= fItemCount - 1
|| offset < fBuffer[index + 1].offset) {
break; break;
} else { } else
if (offset < fBuffer[index + 1].offset) minIndex = index + 1;
break;
else
minIndex = index + 1;
}
} else } else
maxIndex = index; maxIndex = index;
} }
@@ -75,18 +76,19 @@ _BStyleRunDescBuffer_::BumpOffset(int32 delta, int32 index)
} }
// #pragma mark - // #pragma mark - _BStyleRecordBuffer_
_BStyleRecordBuffer_::_BStyleRecordBuffer_() _BStyleRecordBuffer_::_BStyleRecordBuffer_()
: _BTextViewSupportBuffer_<STEStyleRecord>() :
_BTextViewSupportBuffer_<STEStyleRecord>()
{ {
} }
int32 int32
_BStyleRecordBuffer_::InsertRecord(const BFont *inFont, _BStyleRecordBuffer_::InsertRecord(const BFont* inFont,
const rgb_color *inColor) const rgb_color* inColor)
{ {
int32 index = 0; int32 index = 0;
@@ -139,8 +141,8 @@ _BStyleRecordBuffer_::RemoveRecord(int32 index)
bool bool
_BStyleRecordBuffer_::MatchRecord(const BFont *inFont, const rgb_color *inColor, _BStyleRecordBuffer_::MatchRecord(const BFont* inFont, const rgb_color* inColor,
int32 *outIndex) int32* outIndex)
{ {
for (int32 i = 0; i < fItemCount; i++) { for (int32 i = 0; i < fItemCount; i++) {
if (*inFont == fBuffer[i].style.font if (*inFont == fBuffer[i].style.font
@@ -154,38 +156,42 @@ _BStyleRecordBuffer_::MatchRecord(const BFont *inFont, const rgb_color *inColor,
} }
// #pragma mark - // #pragma mark - SetStyleFromMode
static void static void
SetStyleFromMode(uint32 mode, const BFont *fromFont, BFont *toFont, SetStyleFromMode(uint32 mode, const BFont* fromFont, BFont* toFont,
const rgb_color *fromColor, rgb_color *toColor) const rgb_color* fromColor, rgb_color* toColor)
{ {
if (fromFont != NULL && toFont != NULL) { if (fromFont != NULL && toFont != NULL) {
if (mode & B_FONT_FAMILY_AND_STYLE) if ((mode & B_FONT_FAMILY_AND_STYLE) != 0)
toFont->SetFamilyAndStyle(fromFont->FamilyAndStyle()); toFont->SetFamilyAndStyle(fromFont->FamilyAndStyle());
if (mode & B_FONT_FACE) if ((mode & B_FONT_FACE) != 0)
toFont->SetFace(fromFont->Face()); toFont->SetFace(fromFont->Face());
if (mode & B_FONT_SIZE) if ((mode & B_FONT_SIZE) != 0)
toFont->SetSize(fromFont->Size()); toFont->SetSize(fromFont->Size());
if (mode & B_FONT_SHEAR) if ((mode & B_FONT_SHEAR) != 0)
toFont->SetShear(fromFont->Shear()); toFont->SetShear(fromFont->Shear());
if (mode & B_FONT_FALSE_BOLD_WIDTH) if ((mode & B_FONT_FALSE_BOLD_WIDTH) != 0)
toFont->SetFalseBoldWidth(fromFont->FalseBoldWidth()); toFont->SetFalseBoldWidth(fromFont->FalseBoldWidth());
} }
if (fromColor != NULL && toColor != NULL if (fromColor != NULL && toColor != NULL
&& (mode == 0 || mode == B_FONT_ALL)) && (mode == 0 || mode == B_FONT_ALL)) {
*toColor = *fromColor; *toColor = *fromColor;
}
} }
BTextView::StyleBuffer::StyleBuffer(const BFont *inFont, // #pragma mark - BTextView::StyleBuffer
const rgb_color *inColor)
BTextView::StyleBuffer::StyleBuffer(const BFont* inFont,
const rgb_color* inColor)
: :
fValidNullStyle(true) fValidNullStyle(true)
{ {
@@ -222,8 +228,8 @@ BTextView::StyleBuffer::SyncNullStyle(int32 offset)
void void
BTextView::StyleBuffer::SetNullStyle(uint32 inMode, const BFont *inFont, BTextView::StyleBuffer::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) {
SetStyleFromMode(inMode, inFont, &fNullStyle.font, inColor, SetStyleFromMode(inMode, inFont, &fNullStyle.font, inColor,
@@ -240,31 +246,33 @@ BTextView::StyleBuffer::SetNullStyle(uint32 inMode, const BFont *inFont,
void void
BTextView::StyleBuffer::GetNullStyle(const BFont **font, BTextView::StyleBuffer::GetNullStyle(const BFont** font,
const rgb_color **color) const const rgb_color** color) const
{ {
if (font != NULL) if (font != NULL)
*font = &fNullStyle.font; *font = &fNullStyle.font;
if (color != NULL) if (color != NULL)
*color = &fNullStyle.color; *color = &fNullStyle.color;
} }
STEStyleRange * STEStyleRange*
BTextView::StyleBuffer::AllocateStyleRange(const int32 numStyles) const BTextView::StyleBuffer::AllocateStyleRange(const int32 numStyles) const
{ {
STEStyleRange* range = (STEStyleRange *)malloc(sizeof(int32) STEStyleRange* range = (STEStyleRange*)malloc(sizeof(int32)
+ sizeof(STEStyleRun) * numStyles); + sizeof(STEStyleRun) * numStyles);
if (range != NULL) if (range != NULL)
range->count = numStyles; range->count = numStyles;
return range; return range;
} }
void void
BTextView::StyleBuffer::SetStyleRange(int32 fromOffset, int32 toOffset, BTextView::StyleBuffer::SetStyleRange(int32 fromOffset, int32 toOffset,
int32 textLen, uint32 inMode, const BFont *inFont, int32 textLen, uint32 inMode, const BFont* inFont,
const rgb_color *inColor) const rgb_color* inColor)
{ {
if (inFont == NULL) if (inFont == NULL)
inFont = &fNullStyle.font; inFont = &fNullStyle.font;
@@ -333,29 +341,33 @@ BTextView::StyleBuffer::SetStyleRange(int32 fromOffset, int32 toOffset,
} while (offset < toOffset); } while (offset < toOffset);
if (offset == toOffset && runIndex < fStyleRunDesc.ItemCount() if (offset == toOffset && runIndex < fStyleRunDesc.ItemCount()
&& fStyleRunDesc[runIndex]->index == styleIndex) && fStyleRunDesc[runIndex]->index == styleIndex) {
RemoveStyles(runIndex); RemoveStyles(runIndex);
}
} }
void void
BTextView::StyleBuffer::GetStyle(int32 inOffset, BFont *outFont, BTextView::StyleBuffer::GetStyle(int32 inOffset, BFont* outFont,
rgb_color *outColor) const rgb_color* outColor) const
{ {
if (fStyleRunDesc.ItemCount() < 1) { if (fStyleRunDesc.ItemCount() < 1) {
if (outFont) if (outFont != NULL)
*outFont = fNullStyle.font; *outFont = fNullStyle.font;
if (outColor)
if (outColor != NULL)
*outColor = fNullStyle.color; *outColor = fNullStyle.color;
return; return;
} }
int32 runIndex = OffsetToRun(inOffset); int32 runIndex = OffsetToRun(inOffset);
int32 styleIndex = fStyleRunDesc[runIndex]->index; int32 styleIndex = fStyleRunDesc[runIndex]->index;
if (outFont) if (outFont != NULL)
*outFont = fStyleRecord[styleIndex]->style.font; *outFont = fStyleRecord[styleIndex]->style.font;
if (outColor)
if (outColor != NULL)
*outColor = fStyleRecord[styleIndex]->style.color; *outColor = fStyleRecord[styleIndex]->style.color;
} }
@@ -371,7 +383,7 @@ BTextView::StyleBuffer::GetStyleRange(int32 startOffset, int32 endOffset) const
numStyles = 1; numStyles = 1;
STEStyleRange* result = AllocateStyleRange(numStyles); STEStyleRange* result = AllocateStyleRange(numStyles);
if (!result) if (result == NULL)
return NULL; return NULL;
STEStyleRun* run = &result->runs[0]; STEStyleRun* run = &result->runs[0];
@@ -380,6 +392,7 @@ BTextView::StyleBuffer::GetStyleRange(int32 startOffset, int32 endOffset) const
run->offset -= startOffset; run->offset -= startOffset;
if (run->offset < 0) if (run->offset < 0)
run->offset = 0; run->offset = 0;
run++; run++;
} }
@@ -425,7 +438,7 @@ BTextView::StyleBuffer::RemoveStyleRange(int32 fromOffset, int32 toOffset)
void void
BTextView::StyleBuffer::RemoveStyles(int32 index, int32 count) BTextView::StyleBuffer::RemoveStyles(int32 index, int32 count)
{ {
for (int32 i = index; i < (index + count); i++) for (int32 i = index; i < index + count; i++)
fStyleRecord.RemoveRecord(fStyleRunDesc[i]->index); fStyleRecord.RemoveRecord(fStyleRunDesc[i]->index);
fStyleRunDesc.RemoveDescs(index, count); fStyleRunDesc.RemoveDescs(index, count);
@@ -434,9 +447,9 @@ BTextView::StyleBuffer::RemoveStyles(int32 index, int32 count)
int32 int32
BTextView::StyleBuffer::Iterate(int32 fromOffset, int32 length, BTextView::StyleBuffer::Iterate(int32 fromOffset, int32 length,
InlineInput *input, InlineInput* input,
const BFont **outFont, const rgb_color **outColor, const BFont** outFont, const rgb_color** outColor,
float *outAscent, float *outDescent, uint32 *) const float* outAscent, float* outDescent, uint32*) const
{ {
// TODO: Handle the InlineInput style here in some way // TODO: Handle the InlineInput style here in some way
int32 numRuns = fStyleRunDesc.ItemCount(); int32 numRuns = fStyleRunDesc.ItemCount();
@@ -449,10 +462,13 @@ BTextView::StyleBuffer::Iterate(int32 fromOffset, int32 length,
if (outFont != NULL) if (outFont != NULL)
*outFont = &fStyleRecord[run->index]->style.font; *outFont = &fStyleRecord[run->index]->style.font;
if (outColor != NULL) if (outColor != NULL)
*outColor = &fStyleRecord[run->index]->style.color; *outColor = &fStyleRecord[run->index]->style.color;
if (outAscent != NULL) if (outAscent != NULL)
*outAscent = fStyleRecord[run->index]->ascent; *outAscent = fStyleRecord[run->index]->ascent;
if (outDescent != NULL) if (outDescent != NULL)
*outDescent = fStyleRecord[run->index]->descent; *outDescent = fStyleRecord[run->index]->descent;
@@ -504,21 +520,21 @@ static void
FixupMode(const STEStyle &firstStyle, const STEStyle &otherStyle, uint32 &mode, FixupMode(const STEStyle &firstStyle, const STEStyle &otherStyle, uint32 &mode,
bool &sameColor) bool &sameColor)
{ {
if (mode & B_FONT_FAMILY_AND_STYLE) { if ((mode & B_FONT_FAMILY_AND_STYLE) != 0) {
if (firstStyle.font != otherStyle.font) if (firstStyle.font != otherStyle.font)
mode &= ~B_FONT_FAMILY_AND_STYLE; mode &= ~B_FONT_FAMILY_AND_STYLE;
} }
if (mode & B_FONT_SIZE) { if ((mode & B_FONT_SIZE) != 0) {
if (firstStyle.font.Size() != otherStyle.font.Size()) if (firstStyle.font.Size() != otherStyle.font.Size())
mode &= ~B_FONT_SIZE; mode &= ~B_FONT_SIZE;
} }
if (mode & B_FONT_SHEAR) { if ((mode & B_FONT_SHEAR) != 0) {
if (firstStyle.font.Shear() != otherStyle.font.Shear()) if (firstStyle.font.Shear() != otherStyle.font.Shear())
mode &= ~B_FONT_SHEAR; mode &= ~B_FONT_SHEAR;
} }
if (mode & B_FONT_FALSE_BOLD_WIDTH) { if ((mode & B_FONT_FALSE_BOLD_WIDTH) != 0) {
if (firstStyle.font.FalseBoldWidth() if (firstStyle.font.FalseBoldWidth()
!= otherStyle.font.FalseBoldWidth()) { != otherStyle.font.FalseBoldWidth()) {
mode &= ~B_FONT_FALSE_BOLD_WIDTH; mode &= ~B_FONT_FALSE_BOLD_WIDTH;
} }
} }
@@ -531,8 +547,8 @@ FixupMode(const STEStyle &firstStyle, const STEStyle &otherStyle, uint32 &mode,
void void
BTextView::StyleBuffer::ContinuousGetStyle(BFont *outFont, uint32 *ioMode, BTextView::StyleBuffer::ContinuousGetStyle(BFont *outFont, uint32* ioMode,
rgb_color *outColor, bool *sameColor, int32 fromOffset, rgb_color* outColor, bool* sameColor, int32 fromOffset,
int32 toOffset) const int32 toOffset) const
{ {
uint32 mode = B_FONT_ALL; uint32 mode = B_FONT_ALL;
@@ -540,12 +556,16 @@ BTextView::StyleBuffer::ContinuousGetStyle(BFont *outFont, uint32 *ioMode,
if (fStyleRunDesc.ItemCount() < 1) { if (fStyleRunDesc.ItemCount() < 1) {
if (ioMode) if (ioMode)
*ioMode = mode; *ioMode = mode;
if (outFont)
if (outFont != NULL)
*outFont = fNullStyle.font; *outFont = fNullStyle.font;
if (outColor)
if (outColor != NULL)
*outColor = fNullStyle.color; *outColor = fNullStyle.color;
if (sameColor)
if (sameColor != NULL)
*sameColor = true; *sameColor = true;
return; return;
} }
@@ -556,13 +576,16 @@ BTextView::StyleBuffer::ContinuousGetStyle(BFont *outFont, uint32 *ioMode,
int32 styleIndex = fStyleRunDesc[fromIndex]->index; int32 styleIndex = fStyleRunDesc[fromIndex]->index;
const STEStyle* style = &fStyleRecord[styleIndex]->style; const STEStyle* style = &fStyleRecord[styleIndex]->style;
if (ioMode) if (ioMode != NULL)
*ioMode = mode; *ioMode = mode;
if (outFont)
if (outFont != NULL)
*outFont = style->font; *outFont = style->font;
if (outColor)
if (outColor != NULL)
*outColor = style->color; *outColor = style->color;
if (sameColor)
if (sameColor != NULL)
*sameColor = true; *sameColor = true;
} else { } else {
bool oneColor = true; bool oneColor = true;
@@ -575,13 +598,16 @@ BTextView::StyleBuffer::ContinuousGetStyle(BFont *outFont, uint32 *ioMode,
oneColor); oneColor);
} }
if (ioMode) if (ioMode != NULL)
*ioMode = mode; *ioMode = mode;
if (outFont)
if (outFont != NULL)
*outFont = theStyle.font; *outFont = theStyle.font;
if (outColor)
if (outColor != NULL)
*outColor = theStyle.color; *outColor = theStyle.color;
if (sameColor)
if (sameColor != NULL)
*sameColor = oneColor; *sameColor = oneColor;
} }
} }
@@ -1,10 +1,10 @@
/* /*
* Copyright 2001-2006, 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]) * Stefano Ceccherini, [email protected]
*/ */
@@ -50,16 +50,17 @@ struct STEStyleRunDesc {
// _BStyleRunDescBuffer_ class ------------------------------------------------- // _BStyleRunDescBuffer_ class -------------------------------------------------
class _BStyleRunDescBuffer_ : public _BTextViewSupportBuffer_<STEStyleRunDesc> { class _BStyleRunDescBuffer_ : public _BTextViewSupportBuffer_<STEStyleRunDesc> {
public: public:
_BStyleRunDescBuffer_(); _BStyleRunDescBuffer_();
void InsertDesc(STEStyleRunDesc* inDesc, int32 index); void InsertDesc(STEStyleRunDesc* inDesc,
void RemoveDescs(int32 index, int32 count = 1); int32 index);
void RemoveDescs(int32 index, int32 count = 1);
int32 OffsetToRun(int32 offset) const; int32 OffsetToRun(int32 offset) const;
void BumpOffset(int32 delta, int32 index); void BumpOffset(int32 delta, int32 index);
STEStyleRunDesc* operator[](int32 index) const; STEStyleRunDesc* operator[](int32 index) const;
}; };
@@ -72,17 +73,19 @@ _BStyleRunDescBuffer_::operator[](int32 index) const
// _BStyleRecordBuffer_ class -------------------------------------------------- // _BStyleRecordBuffer_ class --------------------------------------------------
class _BStyleRecordBuffer_ : public _BTextViewSupportBuffer_<STEStyleRecord> { class _BStyleRecordBuffer_ : public _BTextViewSupportBuffer_<STEStyleRecord> {
public: public:
_BStyleRecordBuffer_(); _BStyleRecordBuffer_();
int32 InsertRecord(const BFont *inFont, const rgb_color *inColor); int32 InsertRecord(const BFont* inFont,
void CommitRecord(int32 index); const rgb_color* inColor);
void RemoveRecord(int32 index); void CommitRecord(int32 index);
void RemoveRecord(int32 index);
bool MatchRecord(const BFont *inFont, const rgb_color *inColor, bool MatchRecord(const BFont* inFont,
int32 *outIndex); const rgb_color* inColor,
int32* outIndex);
STEStyleRecord* operator[](int32 index) const; STEStyleRecord* operator[](int32 index) const;
}; };
@@ -95,55 +98,61 @@ _BStyleRecordBuffer_::operator[](int32 index) const
// StyleBuffer class -------------------------------------------------------- // StyleBuffer class --------------------------------------------------------
class BTextView::StyleBuffer { class BTextView::StyleBuffer {
public: public:
StyleBuffer(const BFont *inFont, StyleBuffer(const BFont* inFont,
const rgb_color *inColor); const rgb_color* inColor);
void InvalidateNullStyle(); void InvalidateNullStyle();
bool IsValidNullStyle() const; bool IsValidNullStyle() const;
void SyncNullStyle(int32 offset); void SyncNullStyle(int32 offset);
void SetNullStyle(uint32 inMode, const BFont *inFont, void SetNullStyle(uint32 inMode,
const rgb_color *inColor, int32 offset = 0); const BFont* inFont,
void GetNullStyle(const BFont **font, const rgb_color* inColor,
const rgb_color **color) const; int32 offset = 0);
void GetNullStyle(const BFont** font,
const rgb_color** color) const;
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*,
bool *, int32, int32) const; rgb_color*, bool*, int32, int32) const;
STEStyleRange* AllocateStyleRange(const int32 numStyles) const; STEStyleRange* AllocateStyleRange(
void SetStyleRange(int32 fromOffset, int32 toOffset, const int32 numStyles) const;
int32 textLen, uint32 inMode, void SetStyleRange(int32 fromOffset,
const BFont *inFont, const rgb_color *inColor); int32 toOffset, int32 textLen,
STEStyleRange* GetStyleRange(int32 startOffset, uint32 inMode, const BFont* inFont,
int32 endOffset) const; const rgb_color* inColor);
STEStyleRange* GetStyleRange(int32 startOffset,
int32 endOffset) const;
void RemoveStyleRange(int32 fromOffset, int32 toOffset); void RemoveStyleRange(int32 fromOffset,
void RemoveStyles(int32 index, int32 count = 1); int32 toOffset);
void RemoveStyles(int32 index, int32 count = 1);
int32 Iterate(int32 fromOffset, int32 length, int32 Iterate(int32 fromOffset, int32 length,
InlineInput* input, InlineInput* input,
const BFont **outFont = NULL, const BFont** outFont = NULL,
const rgb_color **outColor = NULL, const rgb_color** outColor = NULL,
float *outAscent = NULL, float* outAscent = NULL,
float *outDescen = NULL, uint32 * = NULL) const; float* outDescen = NULL,
uint32* = NULL) const;
int32 OffsetToRun(int32 offset) const; int32 OffsetToRun(int32 offset) const;
void BumpOffset(int32 delta, int32 index); void BumpOffset(int32 delta, int32 index);
STEStyleRun operator[](int32 index) const; STEStyleRun operator[](int32 index) const;
int32 NumRuns() const; int32 NumRuns() const;
const _BStyleRunDescBuffer_& RunBuffer() const; const _BStyleRunDescBuffer_& RunBuffer() const;
const _BStyleRecordBuffer_& RecordBuffer() const; const _BStyleRecordBuffer_& RecordBuffer() const;
private: private:
_BStyleRunDescBuffer_ fStyleRunDesc; _BStyleRunDescBuffer_ fStyleRunDesc;
_BStyleRecordBuffer_ fStyleRecord; _BStyleRecordBuffer_ fStyleRecord;
bool fValidNullStyle; bool fValidNullStyle;
STEStyle fNullStyle; STEStyle fNullStyle;
}; };