minor cleanup, changed some variables into constants
git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@15112 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -33,14 +33,16 @@
|
|||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
const uint32 kTableCount = 128;
|
const static uint32 kTableCount = 128;
|
||||||
|
const static uint32 kInvalidCode = 0xFFFFFFFF;
|
||||||
|
|
||||||
|
|
||||||
struct hashed_escapement
|
struct hashed_escapement
|
||||||
{
|
{
|
||||||
uint32 code;
|
uint32 code;
|
||||||
float escapement;
|
float escapement;
|
||||||
hashed_escapement() {
|
hashed_escapement() {
|
||||||
code = (uint32)-1;
|
code = kInvalidCode;
|
||||||
escapement = 0;
|
escapement = 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -58,8 +60,7 @@ CharToCode(const char *text, const int32 charLen)
|
|||||||
uint32 value = 0;
|
uint32 value = 0;
|
||||||
int32 shiftVal = 24;
|
int32 shiftVal = 24;
|
||||||
for (int32 c = 0; c < charLen; c++) {
|
for (int32 c = 0; c < charLen; c++) {
|
||||||
uchar ch = text[c];
|
value |= (text[c] << shiftVal);
|
||||||
value |= (ch << shiftVal);
|
|
||||||
shiftVal -= 8;
|
shiftVal -= 8;
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
@@ -106,7 +107,7 @@ _BWidthBuffer_::StringWidth(const char *inText, int32 fromOffset, int32 length,
|
|||||||
int32 numChars = 0;
|
int32 numChars = 0;
|
||||||
int32 textLen = 0;
|
int32 textLen = 0;
|
||||||
|
|
||||||
float fontSize = inStyle->Size();
|
const float fontSize = inStyle->Size();
|
||||||
float stringWidth = 0;
|
float stringWidth = 0;
|
||||||
if (length > 0) {
|
if (length > 0) {
|
||||||
for (int32 charLen = 0, currentOffset = fromOffset;
|
for (int32 charLen = 0, currentOffset = fromOffset;
|
||||||
@@ -119,7 +120,7 @@ _BWidthBuffer_::StringWidth(const char *inText, int32 fromOffset, int32 length,
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
// Some magic, to uniquely identify this charachter
|
// Some magic, to uniquely identify this charachter
|
||||||
uint32 value = CharToCode(inText + currentOffset, charLen);
|
const uint32 value = CharToCode(inText + currentOffset, charLen);
|
||||||
|
|
||||||
float escapement;
|
float escapement;
|
||||||
if (GetEscapement(value, index, &escapement)) {
|
if (GetEscapement(value, index, &escapement)) {
|
||||||
@@ -221,7 +222,7 @@ _BWidthBuffer_::InsertTable(const BFont *font)
|
|||||||
table.tableCount = kTableCount;
|
table.tableCount = kTableCount;
|
||||||
table.widths = deltas;
|
table.widths = deltas;
|
||||||
|
|
||||||
int32 position = fItemCount;
|
uint32 position = fItemCount;
|
||||||
InsertItemsAt(1, position, &table);
|
InsertItemsAt(1, position, &table);
|
||||||
|
|
||||||
return position;
|
return position;
|
||||||
@@ -238,22 +239,22 @@ _BWidthBuffer_::InsertTable(const BFont *font)
|
|||||||
bool
|
bool
|
||||||
_BWidthBuffer_::GetEscapement(uint32 value, int32 index, float *escapement)
|
_BWidthBuffer_::GetEscapement(uint32 value, int32 index, float *escapement)
|
||||||
{
|
{
|
||||||
_width_table_ *table = &fBuffer[index];
|
const _width_table_ &table = fBuffer[index];
|
||||||
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);
|
||||||
|
|
||||||
DEBUG_ONLY(uint32 iterations = 1;)
|
DEBUG_ONLY(uint32 iterations = 1;)
|
||||||
uint32 found;
|
uint32 found;
|
||||||
while ((found = widths[hashed].code) != (uint32)-1) {
|
while ((found = widths[hashed].code) != kInvalidCode) {
|
||||||
if (found == value)
|
if (found == value)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (++hashed >= (uint32)table->tableCount)
|
if (++hashed >= (uint32)table.tableCount)
|
||||||
hashed = 0;
|
hashed = 0;
|
||||||
DEBUG_ONLY(iterations++;)
|
DEBUG_ONLY(iterations++;)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found == (uint32)-1)
|
if (found == kInvalidCode)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
PRINT(("Value found with %d iterations\n", iterations));
|
PRINT(("Value found with %d iterations\n", iterations));
|
||||||
@@ -296,63 +297,63 @@ _BWidthBuffer_::HashEscapements(const char *inText, int32 numChars, int32 textLe
|
|||||||
float *escapements = new float[numChars];
|
float *escapements = new float[numChars];
|
||||||
inStyle->GetEscapements(inText, numChars, escapements);
|
inStyle->GetEscapements(inText, numChars, escapements);
|
||||||
|
|
||||||
_width_table_ *table = &fBuffer[tableIndex];
|
_width_table_ &table = fBuffer[tableIndex];
|
||||||
hashed_escapement *widths = static_cast<hashed_escapement *>(table->widths);
|
hashed_escapement *widths = static_cast<hashed_escapement *>(table.widths);
|
||||||
|
|
||||||
int32 offset = 0;
|
int32 offset = 0;
|
||||||
int32 charCount = 0;
|
int32 charCount = 0;
|
||||||
|
|
||||||
// Insert the escapements into the hash table
|
// Insert the escapements into the hash table
|
||||||
do {
|
do {
|
||||||
int32 charLen = UTF8NextCharLen(inText + offset);
|
const int32 charLen = UTF8NextCharLen(inText + offset);
|
||||||
if (charLen == 0)
|
if (charLen == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
uint32 value = CharToCode(inText + offset, charLen);
|
const uint32 value = CharToCode(inText + offset, charLen);
|
||||||
|
|
||||||
uint32 hashed = Hash(value) & (table->tableCount - 1);
|
uint32 hashed = Hash(value) & (table.tableCount - 1);
|
||||||
uint32 found = widths[hashed].code;
|
uint32 found = widths[hashed].code;
|
||||||
|
|
||||||
// Check if the value is already in the table
|
// Check if the value is already in the table
|
||||||
if (found != value) {
|
if (found != value) {
|
||||||
while ((found = widths[hashed].code) != (uint32)-1) {
|
while ((found = widths[hashed].code) != kInvalidCode) {
|
||||||
if (found == value)
|
if (found == value)
|
||||||
break;
|
break;
|
||||||
if (++hashed >= (uint32)table->tableCount)
|
if (++hashed >= (uint32)table.tableCount)
|
||||||
hashed = 0;
|
hashed = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found == (uint32)-1) {
|
if (found == kInvalidCode) {
|
||||||
// The value is not in the table. Add it.
|
// The value is not in the table. Add it.
|
||||||
widths[hashed].code = value;
|
widths[hashed].code = value;
|
||||||
widths[hashed].escapement = escapements[charCount];
|
widths[hashed].escapement = escapements[charCount];
|
||||||
table->hashCount++;
|
table.hashCount++;
|
||||||
|
|
||||||
// We always keep some free space in the hash table
|
// We always keep some free space in the hash table
|
||||||
// TODO: Not sure how much space, currently we double
|
// TODO: Not sure how much space, currently we double
|
||||||
// the current size when hashCount is at least 2/3 of
|
// the current size when hashCount is at least 2/3 of
|
||||||
// the total size.
|
// the total size.
|
||||||
if (table->tableCount * 2 / 3 <= table->hashCount) {
|
if (table.tableCount * 2 / 3 <= table.hashCount) {
|
||||||
table->hashCount = 0;
|
table.hashCount = 0;
|
||||||
int32 newSize = table->tableCount * 2;
|
const int32 newSize = table.tableCount * 2;
|
||||||
|
|
||||||
// Create and initialize a new hash table
|
// 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
|
// Rehash the values, and put them into the new table
|
||||||
for (int32 oldPos = 0; oldPos < table->tableCount; oldPos++) {
|
for (int32 oldPos = 0; oldPos < table.tableCount; oldPos++) {
|
||||||
if (widths[oldPos].code != (uint32) -1) {
|
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 != (uint32)-1) {
|
while (newWidths[newPos].code != kInvalidCode) {
|
||||||
if (++newPos >= (uint32)newSize)
|
if (++newPos >= (uint32)newSize)
|
||||||
newPos = 0;
|
newPos = 0;
|
||||||
}
|
}
|
||||||
newWidths[newPos].code = widths[oldPos].code;
|
newWidths[newPos].code = widths[oldPos].code;
|
||||||
newWidths[newPos].escapement = widths[oldPos].escapement;
|
newWidths[newPos].escapement = widths[oldPos].escapement;
|
||||||
table->hashCount++;
|
table.hashCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
table->tableCount = newSize;
|
table.tableCount = newSize;
|
||||||
|
|
||||||
// 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;
|
||||||
|
|||||||
Reference in New Issue
Block a user