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)
break;
else
minIndex = index + 1; minIndex = index + 1;
}
} else } else
maxIndex = index; maxIndex = index;
} }
@@ -75,11 +76,12 @@ _BStyleRunDescBuffer_::BumpOffset(int32 delta, int32 index)
} }
// #pragma mark - // #pragma mark - _BStyleRecordBuffer_
_BStyleRecordBuffer_::_BStyleRecordBuffer_() _BStyleRecordBuffer_::_BStyleRecordBuffer_()
: _BTextViewSupportBuffer_<STEStyleRecord>() :
_BTextViewSupportBuffer_<STEStyleRecord>()
{ {
} }
@@ -154,7 +156,7 @@ _BStyleRecordBuffer_::MatchRecord(const BFont *inFont, const rgb_color *inColor,
} }
// #pragma mark - // #pragma mark - SetStyleFromMode
static void static void
@@ -162,26 +164,30 @@ 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;
} }
}
// #pragma mark - BTextView::StyleBuffer
BTextView::StyleBuffer::StyleBuffer(const BFont* inFont, BTextView::StyleBuffer::StyleBuffer(const BFont* inFont,
@@ -245,6 +251,7 @@ BTextView::StyleBuffer::GetNullStyle(const BFont **font,
{ {
if (font != NULL) if (font != NULL)
*font = &fNullStyle.font; *font = &fNullStyle.font;
if (color != NULL) if (color != NULL)
*color = &fNullStyle.color; *color = &fNullStyle.color;
} }
@@ -257,6 +264,7 @@ BTextView::StyleBuffer::AllocateStyleRange(const int32 numStyles) const
+ sizeof(STEStyleRun) * numStyles); + sizeof(STEStyleRun) * numStyles);
if (range != NULL) if (range != NULL)
range->count = numStyles; range->count = numStyles;
return range; return range;
} }
@@ -333,9 +341,10 @@ 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
@@ -343,19 +352,22 @@ 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);
@@ -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,19 +520,19 @@ 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;
@@ -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]
*/ */
@@ -53,7 +53,8 @@ class _BStyleRunDescBuffer_ : public _BTextViewSupportBuffer_<STEStyleRunDesc> {
public: public:
_BStyleRunDescBuffer_(); _BStyleRunDescBuffer_();
void InsertDesc(STEStyleRunDesc* inDesc, int32 index); void InsertDesc(STEStyleRunDesc* inDesc,
int32 index);
void RemoveDescs(int32 index, int32 count = 1); void RemoveDescs(int32 index, int32 count = 1);
int32 OffsetToRun(int32 offset) const; int32 OffsetToRun(int32 offset) const;
@@ -75,11 +76,13 @@ class _BStyleRecordBuffer_ : public _BTextViewSupportBuffer_<STEStyleRecord> {
public: public:
_BStyleRecordBuffer_(); _BStyleRecordBuffer_();
int32 InsertRecord(const BFont *inFont, const rgb_color *inColor); int32 InsertRecord(const BFont* inFont,
const rgb_color* inColor);
void CommitRecord(int32 index); void CommitRecord(int32 index);
void RemoveRecord(int32 index); void RemoveRecord(int32 index);
bool MatchRecord(const BFont *inFont, const rgb_color *inColor, bool MatchRecord(const BFont* inFont,
const rgb_color* inColor,
int32* outIndex); int32* outIndex);
STEStyleRecord* operator[](int32 index) const; STEStyleRecord* operator[](int32 index) const;
@@ -103,24 +106,29 @@ class BTextView::StyleBuffer {
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,
const rgb_color* inColor,
int32 offset = 0);
void GetNullStyle(const BFont** font, void GetNullStyle(const BFont** font,
const rgb_color** color) const; 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,
uint32 inMode, const BFont* inFont,
const rgb_color* inColor);
STEStyleRange* GetStyleRange(int32 startOffset, STEStyleRange* GetStyleRange(int32 startOffset,
int32 endOffset) const; 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);
int32 Iterate(int32 fromOffset, int32 length, int32 Iterate(int32 fromOffset, int32 length,
@@ -128,7 +136,8 @@ class BTextView::StyleBuffer {
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);