From adcb834590a663352ed8caaca7fa082e221b78cf Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Mon, 30 Nov 2009 11:25:13 +0000 Subject: [PATCH] * The WidthBuffer lock was unnecessarily static. Made it an object member. * Automatic whitespace cleanup. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34367 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/interface/WidthBuffer.h | 43 +++++++------- .../textview_support/WidthBuffer.cpp | 56 +++++++++---------- 2 files changed, 52 insertions(+), 47 deletions(-) diff --git a/headers/private/interface/WidthBuffer.h b/headers/private/interface/WidthBuffer.h index 1baf9fd2b9..048686b954 100644 --- a/headers/private/interface/WidthBuffer.h +++ b/headers/private/interface/WidthBuffer.h @@ -1,22 +1,22 @@ /* * Copyright (c) 2003-2004 OpenBeOS - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in + * + * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. * * File: WidthBuffer.cpp @@ -28,6 +28,8 @@ #ifndef __WIDTHBUFFER_H #define __WIDTHBUFFER_H + +#include #include #include "TextViewSupportBuffer.h" @@ -39,7 +41,7 @@ class BFont; // TODO: enable this as soon as we are sure opentracker works // with our libraries, since using a BFont here (as Dano does) is much better, // as fonts can be classified also by spacing mode and other attributes. -#define USE_DANO_WIDTHBUFFER 0 +#define USE_DANO_WIDTHBUFFER 0 namespace BPrivate { @@ -54,7 +56,7 @@ struct _width_table_ { #endif int32 hashCount; // number of hashed items int32 tableCount; // size of table - void *widths; // width table + void *widths; // width table }; class WidthBuffer : public _BTextViewSupportBuffer_<_width_table_> { @@ -70,12 +72,15 @@ public: private: 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); - + static uint32 Hash(uint32); + +private: + BLocker fLock; }; extern WidthBuffer* gWidthBuffer; diff --git a/src/kits/interface/textview_support/WidthBuffer.cpp b/src/kits/interface/textview_support/WidthBuffer.cpp index 0b73dee3f6..6710f25b96 100644 --- a/src/kits/interface/textview_support/WidthBuffer.cpp +++ b/src/kits/interface/textview_support/WidthBuffer.cpp @@ -27,7 +27,6 @@ namespace BPrivate { const static uint32 kTableCount = 128; const static uint32 kInvalidCode = 0xFFFFFFFF; -static BLocker sWidthLocker = BLocker("width buffer lock"); WidthBuffer* gWidthBuffer = NULL; // initialized in InterfaceDefs.cpp @@ -56,7 +55,7 @@ CharToCode(const char *text, const int32 charLen) int32 shiftVal = 24; for (int32 c = 0; c < charLen; c++) { value |= (text[c] << shiftVal); - shiftVal -= 8; + shiftVal -= 8; } return value; } @@ -66,7 +65,8 @@ CharToCode(const char *text, const int32 charLen) */ WidthBuffer::WidthBuffer() : - _BTextViewSupportBuffer_<_width_table_>(1, 0) + _BTextViewSupportBuffer_<_width_table_>(1, 0), + fLock("width buffer") { } @@ -94,13 +94,13 @@ WidthBuffer::StringWidth(const char *inText, int32 fromOffset, if (inText == NULL || length == 0) return 0; - BAutolock _(sWidthLocker); + BAutolock _(fLock); int32 index = 0; if (!FindTable(inStyle, &index)) index = InsertTable(inStyle); - char *text = NULL; + char *text = NULL; int32 numChars = 0; int32 textLen = 0; @@ -111,12 +111,12 @@ WidthBuffer::StringWidth(const char *inText, int32 fromOffset, sourceText < inText + length; sourceText += charLen) { charLen = UTF8NextCharLen(sourceText); - + // End of string, bail out if (charLen <= 0) break; - // Some magic, to uniquely identify this charachter + // Some magic, to uniquely identify this charachter const uint32 value = CharToCode(sourceText, charLen); float escapement; @@ -132,13 +132,13 @@ WidthBuffer::StringWidth(const char *inText, int32 fromOffset, 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. // Get their width via HashEscapements() - stringWidth += HashEscapements(text, numChars, textLen, index, inStyle); + stringWidth += HashEscapements(text, numChars, textLen, index, inStyle); free(text); } @@ -180,21 +180,21 @@ WidthBuffer::FindTable(const BFont *inStyle, int32 *outIndex) int32 tableIndex = -1; for (int32 i = 0; i < fItemCount; i++) { - + #if USE_DANO_WIDTHBUFFER if (*inStyle == fBuffer[i].font) #else - if (fontSize == fBuffer[i].fontSize + if (fontSize == fBuffer[i].fontSize && fontCode == fBuffer[i].fontCode) #endif { tableIndex = i; break; } - } + } if (outIndex != NULL) *outIndex = tableIndex; - + return tableIndex != -1; } @@ -207,7 +207,7 @@ int32 WidthBuffer::InsertTable(const BFont *font) { _width_table_ table; - + #if USE_DANO_WIDTHBUFFER table.font = *font; #else @@ -218,10 +218,10 @@ WidthBuffer::InsertTable(const BFont *font) table.hashCount = 0; table.tableCount = kTableCount; table.widths = new hashed_escapement[kTableCount]; - + uint32 position = fItemCount; InsertItemsAt(1, position, &table); - + return position; } @@ -234,18 +234,18 @@ WidthBuffer::InsertTable(const BFont *font) \return \c true if the function could find the escapement for the given charachter, \c false if not. */ -bool +bool 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(table.widths); uint32 hashed = Hash(value) & (table.tableCount - 1); uint32 found; while ((found = widths[hashed].code) != kInvalidCode) { if (found == value) - break; - + break; + if (++hashed >= (uint32)table.tableCount) hashed = 0; } @@ -264,8 +264,8 @@ uint32 WidthBuffer::Hash(uint32 val) { uint32 shifted = val >> 24; - uint32 result = (val >> 15) + (shifted * 3); - + uint32 result = (val >> 15) + (shifted * 3); + result ^= (val >> 6) - (shifted * 22); result ^= (val << 3); @@ -291,11 +291,11 @@ WidthBuffer::HashEscapements(const char *inText, int32 numChars, int32 textLen, ASSERT(inText != NULL); ASSERT(numChars > 0); ASSERT(textLen > 0); - + float *escapements = new float[numChars]; inStyle->GetEscapements(inText, numChars, escapements); - _width_table_ &table = fBuffer[tableIndex]; + _width_table_ &table = fBuffer[tableIndex]; hashed_escapement *widths = static_cast(table.widths); int32 charCount = 0; @@ -308,7 +308,7 @@ WidthBuffer::HashEscapements(const char *inText, int32 numChars, int32 textLen, break; const uint32 value = CharToCode(text, charLen); - + uint32 hashed = Hash(value) & (table.tableCount - 1); uint32 found; while ((found = widths[hashed].code) != kInvalidCode) { @@ -329,10 +329,10 @@ WidthBuffer::HashEscapements(const char *inText, int32 numChars, int32 textLen, // the total size. if (table.tableCount * 2 / 3 <= table.hashCount) { const int32 newSize = table.tableCount * 2; - + // Create and initialize a new hash table 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++) { if (widths[oldPos].code != kInvalidCode) { @@ -344,7 +344,7 @@ WidthBuffer::HashEscapements(const char *inText, int32 numChars, int32 textLen, newWidths[newPos] = widths[oldPos]; } } - + // Delete the old table, and put the new pointer into the _width_table_ delete[] widths; table.tableCount = newSize;