* 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:
@@ -17,8 +17,10 @@ class BFont;
|
|||||||
|
|
||||||
namespace BPrivate {
|
namespace BPrivate {
|
||||||
|
|
||||||
|
|
||||||
class TextGapBuffer;
|
class TextGapBuffer;
|
||||||
|
|
||||||
|
|
||||||
struct _width_table_ {
|
struct _width_table_ {
|
||||||
BFont font; // corresponding font
|
BFont font; // corresponding font
|
||||||
int32 hashCount; // number of hashed items
|
int32 hashCount; // number of hashed items
|
||||||
@@ -32,17 +34,21 @@ public:
|
|||||||
WidthBuffer();
|
WidthBuffer();
|
||||||
virtual ~WidthBuffer();
|
virtual ~WidthBuffer();
|
||||||
|
|
||||||
float StringWidth(const char* inText, int32 fromOffset, int32 length,
|
float StringWidth(const char* inText,
|
||||||
|
int32 fromOffset, int32 length,
|
||||||
|
const BFont* inStyle);
|
||||||
|
float StringWidth(TextGapBuffer& gapBuffer,
|
||||||
|
int32 fromOffset, int32 length,
|
||||||
const BFont* inStyle);
|
const BFont* inStyle);
|
||||||
float StringWidth(TextGapBuffer& gapBuffer, int32 fromOffset,
|
|
||||||
int32 length, const BFont* inStyle);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool FindTable(const BFont* font, int32* outIndex);
|
bool FindTable(const BFont* font, int32* outIndex);
|
||||||
int32 InsertTable(const BFont* font);
|
int32 InsertTable(const BFont* font);
|
||||||
|
|
||||||
bool GetEscapement(uint32 value, int32 index, float* escapement);
|
bool GetEscapement(uint32 value, int32 index,
|
||||||
float HashEscapements(const char* chars, int32 numChars, int32 numBytes,
|
float* escapement);
|
||||||
|
float HashEscapements(const char* chars,
|
||||||
|
int32 numChars, int32 numBytes,
|
||||||
int32 tableIndex, const BFont* font);
|
int32 tableIndex, const BFont* font);
|
||||||
|
|
||||||
static uint32 Hash(uint32);
|
static uint32 Hash(uint32);
|
||||||
@@ -51,12 +57,16 @@ private:
|
|||||||
BLocker fLock;
|
BLocker fLock;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
extern WidthBuffer* gWidthBuffer;
|
extern WidthBuffer* gWidthBuffer;
|
||||||
|
|
||||||
|
|
||||||
} // namespace BPrivate
|
} // namespace BPrivate
|
||||||
|
|
||||||
|
|
||||||
using BPrivate::WidthBuffer;
|
using BPrivate::WidthBuffer;
|
||||||
|
|
||||||
|
|
||||||
#if __GNUC__ < 3
|
#if __GNUC__ < 3
|
||||||
//! NetPositive binary compatibility support
|
//! NetPositive binary compatibility support
|
||||||
|
|
||||||
@@ -70,4 +80,5 @@ _BWidthBuffer_* gCompatibilityWidthBuffer;
|
|||||||
|
|
||||||
#endif // __GNUC__ < 3
|
#endif // __GNUC__ < 3
|
||||||
|
|
||||||
|
|
||||||
#endif // __WIDTHBUFFER_H
|
#endif // __WIDTHBUFFER_H
|
||||||
|
|||||||
@@ -20,16 +20,20 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
|
||||||
//! NetPositive binary compatibility support
|
//! NetPositive binary compatibility support
|
||||||
class _BWidthBuffer_;
|
class _BWidthBuffer_;
|
||||||
|
|
||||||
|
|
||||||
namespace BPrivate {
|
namespace BPrivate {
|
||||||
|
|
||||||
|
|
||||||
const static uint32 kTableCount = 128;
|
const static uint32 kTableCount = 128;
|
||||||
const static uint32 kInvalidCode = 0xFFFFFFFF;
|
const static uint32 kInvalidCode = 0xFFFFFFFF;
|
||||||
WidthBuffer* gWidthBuffer = NULL;
|
WidthBuffer* gWidthBuffer = NULL;
|
||||||
// initialized in InterfaceDefs.cpp
|
// initialized in InterfaceDefs.cpp
|
||||||
|
|
||||||
|
|
||||||
struct hashed_escapement {
|
struct hashed_escapement {
|
||||||
uint32 code;
|
uint32 code;
|
||||||
float escapement;
|
float escapement;
|
||||||
@@ -43,10 +47,10 @@ struct hashed_escapement {
|
|||||||
|
|
||||||
|
|
||||||
/*! \brief Convert a UTF8 char to a code, which will be used
|
/*! \brief Convert a UTF8 char to a code, which will be used
|
||||||
to uniquely identify the charachter in the hash table.
|
to uniquely identify the character in the hash table.
|
||||||
\param text A pointer to the charachter to examine.
|
\param text A pointer to the character to examine.
|
||||||
\param charLen the length of the charachter to examine.
|
\param charLen the length of the character to examine.
|
||||||
\return The code for the given charachter,
|
\return The code for the given character,
|
||||||
*/
|
*/
|
||||||
static inline uint32
|
static inline uint32
|
||||||
CharToCode(const char* text, const int32 charLen)
|
CharToCode(const char* text, const int32 charLen)
|
||||||
@@ -116,15 +120,15 @@ WidthBuffer::StringWidth(const char *inText, int32 fromOffset,
|
|||||||
if (charLen <= 0)
|
if (charLen <= 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
// Some magic, to uniquely identify this charachter
|
// Some magic, to uniquely identify this character
|
||||||
const uint32 value = CharToCode(sourceText, charLen);
|
const uint32 value = CharToCode(sourceText, charLen);
|
||||||
|
|
||||||
float escapement;
|
float escapement;
|
||||||
if (GetEscapement(value, index, &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;
|
stringWidth += escapement;
|
||||||
} else {
|
} else {
|
||||||
// Store this charachter into an array, which we'll
|
// Store this character into an array, which we'll
|
||||||
// pass to HashEscapements() later
|
// pass to HashEscapements() later
|
||||||
int32 offset = textLen;
|
int32 offset = textLen;
|
||||||
textLen += charLen;
|
textLen += charLen;
|
||||||
@@ -136,7 +140,7 @@ WidthBuffer::StringWidth(const char *inText, int32 fromOffset,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (text != NULL) {
|
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()
|
// Get their width via HashEscapements()
|
||||||
stringWidth += HashEscapements(text, numChars, textLen, index, inStyle);
|
stringWidth += HashEscapements(text, numChars, textLen, index, inStyle);
|
||||||
free(text);
|
free(text);
|
||||||
@@ -148,14 +152,15 @@ WidthBuffer::StringWidth(const char *inText, int32 fromOffset,
|
|||||||
|
|
||||||
/*! \brief Returns how much room is required to draw a string in the font.
|
/*! \brief Returns how much room is required to draw a string in the font.
|
||||||
\param inBuffer The TextGapBuffer to be examined.
|
\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 lenght The amount of bytes to be examined.
|
||||||
\param inStyle The font.
|
\param inStyle The font.
|
||||||
\return The space (in pixels) required to draw the given string.
|
\return The space (in pixels) required to draw the given string.
|
||||||
*/
|
*/
|
||||||
float
|
float
|
||||||
WidthBuffer::StringWidth(TextGapBuffer &inBuffer, int32 fromOffset, int32 length,
|
WidthBuffer::StringWidth(TextGapBuffer &inBuffer, int32 fromOffset,
|
||||||
const BFont *inStyle)
|
int32 length, const BFont* inStyle)
|
||||||
{
|
{
|
||||||
const char* text = inBuffer.GetString(fromOffset, &length);
|
const char* text = inBuffer.GetString(fromOffset, &length);
|
||||||
return StringWidth(text, 0, length, inStyle);
|
return StringWidth(text, 0, length, inStyle);
|
||||||
@@ -211,19 +216,20 @@ WidthBuffer::InsertTable(const BFont *font)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*! \brief Gets the escapement for the given charachter.
|
/*! \brief Gets the escapement for the given character.
|
||||||
\param value An integer which uniquely identifies a charachter.
|
\param value An integer which uniquely identifies a character.
|
||||||
\param index The index of the table to search.
|
\param index The index of the table to search.
|
||||||
\param escapement A pointer to a float, where the function will
|
\param escapement A pointer to a float, where the function will
|
||||||
store the escapement.
|
store the escapement.
|
||||||
\return \c true if the function could find 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
|
bool
|
||||||
WidthBuffer::GetEscapement(uint32 value, int32 index, float* escapement)
|
WidthBuffer::GetEscapement(uint32 value, int32 index, float* escapement)
|
||||||
{
|
{
|
||||||
const _width_table_ &table = fBuffer[index];
|
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 hashed = Hash(value) & (table.tableCount - 1);
|
||||||
|
|
||||||
uint32 found;
|
uint32 found;
|
||||||
@@ -261,7 +267,7 @@ WidthBuffer::Hash(uint32 val)
|
|||||||
/*! \brief Gets the escapements for the given string, and put them into
|
/*! \brief Gets the escapements for the given string, and put them into
|
||||||
the hash table.
|
the hash table.
|
||||||
\param inText The string to be examined.
|
\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 textLen the amount of bytes contained in the string.
|
||||||
\param tableIndex the index of the table where the escapements
|
\param tableIndex the index of the table where the escapements
|
||||||
should be put.
|
should be put.
|
||||||
@@ -319,9 +325,11 @@ WidthBuffer::HashEscapements(const char *inText, int32 numChars, int32 textLen,
|
|||||||
hashed_escapement* newWidths = new hashed_escapement[newSize];
|
hashed_escapement* newWidths = new hashed_escapement[newSize];
|
||||||
|
|
||||||
// Rehash the values, and put them into the new table
|
// 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) {
|
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) {
|
while (newWidths[newPos].code != kInvalidCode) {
|
||||||
if (++newPos >= (uint32)newSize)
|
if (++newPos >= (uint32)newSize)
|
||||||
newPos = 0;
|
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;
|
delete[] widths;
|
||||||
table.tableCount = newSize;
|
table.tableCount = newSize;
|
||||||
table.widths = widths = newWidths;
|
table.widths = widths = newWidths;
|
||||||
|
|||||||
Reference in New Issue
Block a user