* cleanup and style fixes in WidthBuffer

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@38033 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Oliver Tappe 2010-08-11 18:03:37 +00:00
parent 834e58f9b7
commit 2feea51f57
2 changed files with 71 additions and 51 deletions

View File

@ -17,8 +17,10 @@ class BFont;
namespace BPrivate {
class TextGapBuffer;
struct _width_table_ {
BFont font; // corresponding font
int32 hashCount; // number of hashed items
@ -29,34 +31,42 @@ struct _width_table_ {
class WidthBuffer : public _BTextViewSupportBuffer_<_width_table_> {
public:
WidthBuffer();
virtual ~WidthBuffer();
WidthBuffer();
virtual ~WidthBuffer();
float StringWidth(const char* inText, int32 fromOffset, int32 length,
const BFont* inStyle);
float StringWidth(TextGapBuffer& gapBuffer, int32 fromOffset,
int32 length, const BFont* inStyle);
float StringWidth(const char* inText,
int32 fromOffset, int32 length,
const BFont* inStyle);
float StringWidth(TextGapBuffer& gapBuffer,
int32 fromOffset, int32 length,
const BFont* inStyle);
private:
bool FindTable(const BFont* font, int32* outIndex);
int32 InsertTable(const BFont* font);
bool FindTable(const BFont* font, int32* outIndex);
int32 InsertTable(const BFont* font);
bool GetEscapement(uint32 value, int32 index, float* escapement);
float HashEscapements(const char* chars, int32 numChars, int32 numBytes,
int32 tableIndex, const BFont* font);
bool GetEscapement(uint32 value, int32 index,
float* escapement);
float HashEscapements(const char* chars,
int32 numChars, int32 numBytes,
int32 tableIndex, const BFont* font);
static uint32 Hash(uint32);
static uint32 Hash(uint32);
private:
BLocker fLock;
BLocker fLock;
};
extern WidthBuffer* gWidthBuffer;
} // namespace BPrivate
using BPrivate::WidthBuffer;
#if __GNUC__ < 3
//! NetPositive binary compatibility support
@ -70,4 +80,5 @@ _BWidthBuffer_* gCompatibilityWidthBuffer;
#endif // __GNUC__ < 3
#endif // __WIDTHBUFFER_H

View File

@ -20,16 +20,20 @@
#include <stdio.h>
//! NetPositive binary compatibility support
class _BWidthBuffer_;
namespace BPrivate {
const static uint32 kTableCount = 128;
const static uint32 kInvalidCode = 0xFFFFFFFF;
WidthBuffer* gWidthBuffer = NULL;
// initialized in InterfaceDefs.cpp
struct hashed_escapement {
uint32 code;
float escapement;
@ -43,13 +47,13 @@ struct hashed_escapement {
/*! \brief Convert a UTF8 char to a code, which will be used
to uniquely identify the charachter in the hash table.
\param text A pointer to the charachter to examine.
\param charLen the length of the charachter to examine.
\return The code for the given charachter,
to uniquely identify the character in the hash table.
\param text A pointer to the character to examine.
\param charLen the length of the character to examine.
\return The code for the given character,
*/
static inline uint32
CharToCode(const char *text, const int32 charLen)
CharToCode(const char* text, const int32 charLen)
{
uint32 value = 0;
int32 shiftVal = 24;
@ -76,7 +80,7 @@ WidthBuffer::WidthBuffer()
WidthBuffer::~WidthBuffer()
{
for (int32 x = 0; x < fItemCount; x++)
delete[] (hashed_escapement *)fBuffer[x].widths;
delete[] (hashed_escapement*)fBuffer[x].widths;
}
@ -88,8 +92,8 @@ WidthBuffer::~WidthBuffer()
\return The space (in pixels) required to draw the given string.
*/
float
WidthBuffer::StringWidth(const char *inText, int32 fromOffset,
int32 length, const BFont *inStyle)
WidthBuffer::StringWidth(const char* inText, int32 fromOffset,
int32 length, const BFont* inStyle)
{
if (inText == NULL || length == 0)
return 0;
@ -100,11 +104,11 @@ WidthBuffer::StringWidth(const char *inText, int32 fromOffset,
if (!FindTable(inStyle, &index))
index = InsertTable(inStyle);
char *text = NULL;
char* text = NULL;
int32 numChars = 0;
int32 textLen = 0;
char *sourceText = (char *)inText + fromOffset;
char* sourceText = (char*)inText + fromOffset;
const float fontSize = inStyle->Size();
float stringWidth = 0;
for (int32 charLen = 0;
@ -116,46 +120,47 @@ WidthBuffer::StringWidth(const char *inText, int32 fromOffset,
if (charLen <= 0)
break;
// Some magic, to uniquely identify this charachter
// Some magic, to uniquely identify this character
const uint32 value = CharToCode(sourceText, charLen);
float escapement;
if (GetEscapement(value, index, &escapement)) {
// Well, we've got a match for this charachter
// Well, we've got a match for this character
stringWidth += escapement;
} else {
// Store this charachter into an array, which we'll
// Store this character into an array, which we'll
// pass to HashEscapements() later
int32 offset = textLen;
textLen += charLen;
numChars++;
text = (char *)realloc(text, textLen);
text = (char*)realloc(text, textLen);
for (int32 x = 0; x < charLen; x++)
text[offset + x] = sourceText[x];
}
}
if (text != NULL) {
// We've found some charachters which aren't yet in the hash table.
// We've found some characters which aren't yet in the hash table.
// Get their width via HashEscapements()
stringWidth += HashEscapements(text, numChars, textLen, index, inStyle);
free(text);
}
return stringWidth * fontSize;
return stringWidth* fontSize;
}
/*! \brief Returns how much room is required to draw a string in the font.
\param inBuffer The TextGapBuffer to be examined.
\param fromOffset The offset in the TextGapBuffer where to begin the examination.
\param fromOffset The offset in the TextGapBuffer where to begin the
examination.
\param lenght The amount of bytes to be examined.
\param inStyle The font.
\return The space (in pixels) required to draw the given string.
*/
float
WidthBuffer::StringWidth(TextGapBuffer &inBuffer, int32 fromOffset, int32 length,
const BFont *inStyle)
WidthBuffer::StringWidth(TextGapBuffer &inBuffer, int32 fromOffset,
int32 length, const BFont* inStyle)
{
const char* text = inBuffer.GetString(fromOffset, &length);
return StringWidth(text, 0, length, inStyle);
@ -170,7 +175,7 @@ WidthBuffer::StringWidth(TextGapBuffer &inBuffer, int32 fromOffset, int32 length
\c false if not.
*/
bool
WidthBuffer::FindTable(const BFont *inStyle, int32 *outIndex)
WidthBuffer::FindTable(const BFont* inStyle, int32* outIndex)
{
if (inStyle == NULL)
return false;
@ -195,7 +200,7 @@ WidthBuffer::FindTable(const BFont *inStyle, int32 *outIndex)
\return The index of the newly created table.
*/
int32
WidthBuffer::InsertTable(const BFont *font)
WidthBuffer::InsertTable(const BFont* font)
{
_width_table_ table;
@ -211,19 +216,20 @@ WidthBuffer::InsertTable(const BFont *font)
}
/*! \brief Gets the escapement for the given charachter.
\param value An integer which uniquely identifies a charachter.
/*! \brief Gets the escapement for the given character.
\param value An integer which uniquely identifies a character.
\param index The index of the table to search.
\param escapement A pointer to a float, where the function will
store the escapement.
\return \c true if the function could find the escapement
for the given charachter, \c false if not.
for the given character, \c false if not.
*/
bool
WidthBuffer::GetEscapement(uint32 value, int32 index, float *escapement)
WidthBuffer::GetEscapement(uint32 value, int32 index, float* escapement)
{
const _width_table_ &table = fBuffer[index];
const hashed_escapement *widths = static_cast<hashed_escapement *>(table.widths);
const hashed_escapement* widths
= static_cast<hashed_escapement*>(table.widths);
uint32 hashed = Hash(value) & (table.tableCount - 1);
uint32 found;
@ -261,7 +267,7 @@ WidthBuffer::Hash(uint32 val)
/*! \brief Gets the escapements for the given string, and put them into
the hash table.
\param inText The string to be examined.
\param numChars The amount of charachters contained in the string.
\param numChars The amount of characters contained in the string.
\param textLen the amount of bytes contained in the string.
\param tableIndex the index of the table where the escapements
should be put.
@ -270,22 +276,22 @@ WidthBuffer::Hash(uint32 val)
the size of the font).
*/
float
WidthBuffer::HashEscapements(const char *inText, int32 numChars, int32 textLen,
int32 tableIndex, const BFont *inStyle)
WidthBuffer::HashEscapements(const char* inText, int32 numChars, int32 textLen,
int32 tableIndex, const BFont* inStyle)
{
ASSERT(inText != NULL);
ASSERT(numChars > 0);
ASSERT(textLen > 0);
float *escapements = new float[numChars];
float* escapements = new float[numChars];
inStyle->GetEscapements(inText, numChars, escapements);
_width_table_ &table = fBuffer[tableIndex];
hashed_escapement *widths = static_cast<hashed_escapement *>(table.widths);
hashed_escapement* widths = static_cast<hashed_escapement*>(table.widths);
int32 charCount = 0;
char *text = (char *)inText;
const char *textEnd = inText + textLen;
char* text = (char*)inText;
const char* textEnd = inText + textLen;
// Insert the escapements into the hash table
do {
const int32 charLen = UTF8NextCharLen(text);
@ -316,12 +322,14 @@ WidthBuffer::HashEscapements(const char *inText, int32 numChars, int32 textLen,
const int32 newSize = table.tableCount * 2;
// Create and initialize a new hash table
hashed_escapement *newWidths = new hashed_escapement[newSize];
hashed_escapement* newWidths = new hashed_escapement[newSize];
// Rehash the values, and put them into the new table
for (uint32 oldPos = 0; oldPos < (uint32)table.tableCount; oldPos++) {
for (uint32 oldPos = 0; oldPos < (uint32)table.tableCount;
oldPos++) {
if (widths[oldPos].code != kInvalidCode) {
uint32 newPos = Hash(widths[oldPos].code) & (newSize - 1);
uint32 newPos
= Hash(widths[oldPos].code) & (newSize - 1);
while (newWidths[newPos].code != kInvalidCode) {
if (++newPos >= (uint32)newSize)
newPos = 0;
@ -330,7 +338,8 @@ WidthBuffer::HashEscapements(const char *inText, int32 numChars, int32 textLen,
}
}
// Delete the old table, and put the new pointer into the _width_table_
// Delete the old table, and put the new pointer into the
// _width_table_
delete[] widths;
table.tableCount = newSize;
table.widths = widths = newWidths;
@ -369,7 +378,7 @@ _BWidthBuffer_* gCompatibilityWidthBuffer = NULL;
extern "C"
float
StringWidth__14_BWidthBuffer_PCcllPC5BFont(_BWidthBuffer_* widthBuffer,
const char *inText, int32 fromOffset, int32 length, const BFont *inStyle)
const char* inText, int32 fromOffset, int32 length, const BFont* inStyle)
{
return BPrivate::gWidthBuffer->StringWidth(inText, fromOffset, length,
inStyle);