Font.cpp: Style cleanup

* Unify asterisk style
 * switch indentation style of FontList declaration
 * Order FontList methods according to declaration
 * Use named constants in default BFont constructor for spacing and encoding.
This commit is contained in:
Stephan Aßmus
2014-02-26 11:51:36 +01:00
parent 6d3363214f
commit 569eed2db5
+154 -146
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2008, Haiku, Inc. * Copyright 2001-2014, Haiku, Inc.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -41,9 +41,9 @@ static BFont sPlainFont;
static BFont sBoldFont; static BFont sBoldFont;
static BFont sFixedFont; static BFont sFixedFont;
const BFont *be_plain_font = &sPlainFont; const BFont* be_plain_font = &sPlainFont;
const BFont *be_bold_font = &sBoldFont; const BFont* be_bold_font = &sBoldFont;
const BFont *be_fixed_font = &sFixedFont; const BFont* be_fixed_font = &sFixedFont;
struct style { struct style {
@@ -61,35 +61,38 @@ struct family {
namespace { namespace {
class FontList : public BLocker { class FontList : public BLocker {
public: public:
FontList(); FontList();
~FontList(); virtual ~FontList();
static FontList* Default(); static FontList* Default();
bool UpdatedOnServer(); bool UpdatedOnServer();
status_t FamilyAt(int32 index, font_family *_family, uint32 *_flags); status_t FamilyAt(int32 index, font_family* _family,
status_t StyleAt(font_family family, int32 index, font_style *_style, uint32* _flags);
uint16 *_face, uint32 *_flags); status_t StyleAt(font_family family, int32 index,
font_style* _style, uint16* _face,
uint32* _flags);
int32 CountFamilies(); int32 CountFamilies();
int32 CountStyles(font_family family); int32 CountStyles(font_family family);
private: private:
status_t _UpdateIfNecessary(); status_t _UpdateIfNecessary();
status_t _Update(); status_t _Update();
int32 _RevisionOnServer(); int32 _RevisionOnServer();
family* _FindFamily(font_family name); family* _FindFamily(font_family name);
static void _InitSingleton(); static void _InitSingleton();
BObjectList<family> fFamilies; private:
family* fLastFamily; BObjectList<family> fFamilies;
bigtime_t fLastUpdate; family* fLastFamily;
int32 fRevision; bigtime_t fLastUpdate;
int32 fRevision;
static pthread_once_t sDefaultInitOnce; static pthread_once_t sDefaultInitOnce;
static FontList* sDefaultInstance; static FontList* sDefaultInstance;
}; };
pthread_once_t FontList::sDefaultInitOnce = PTHREAD_ONCE_INIT; pthread_once_t FontList::sDefaultInitOnce = PTHREAD_ONCE_INIT;
@@ -142,20 +145,75 @@ FontList::UpdatedOnServer()
} }
int32 status_t
FontList::_RevisionOnServer() FontList::FamilyAt(int32 index, font_family* _family, uint32* _flags)
{ {
BPrivate::AppServerLink link; BAutolock locker(this);
link.StartMessage(AS_GET_FONT_LIST_REVISION);
int32 code; status_t status = _UpdateIfNecessary();
if (link.FlushWithReply(code) != B_OK || code != B_OK) if (status < B_OK)
return B_ERROR; return status;
int32 revision; ::family* family = fFamilies.ItemAt(index);
link.Read<int32>(&revision); if (family == NULL)
return B_BAD_VALUE;
return revision; memcpy(*_family, family->name.String(), family->name.Length() + 1);
if (_flags)
*_flags = family->flags;
return B_OK;
}
status_t
FontList::StyleAt(font_family familyName, int32 index, font_style* _style,
uint16* _face, uint32* _flags)
{
BAutolock locker(this);
status_t status = _UpdateIfNecessary();
if (status < B_OK)
return status;
::family* family = _FindFamily(familyName);
if (family == NULL)
return B_BAD_VALUE;
::style* style = family->styles.ItemAt(index);
if (style == NULL)
return B_BAD_VALUE;
memcpy(*_style, style->name.String(), style->name.Length() + 1);
if (_face)
*_face = style->face;
if (_flags)
*_flags = style->flags;
return B_OK;
}
int32
FontList::CountFamilies()
{
BAutolock locker(this);
_UpdateIfNecessary();
return fFamilies.CountItems();
}
int32
FontList::CountStyles(font_family familyName)
{
BAutolock locker(this);
_UpdateIfNecessary();
::family* family = _FindFamily(familyName);
if (family == NULL)
return 0;
return family->styles.CountItems();
} }
@@ -233,6 +291,23 @@ FontList::_UpdateIfNecessary()
} }
int32
FontList::_RevisionOnServer()
{
BPrivate::AppServerLink link;
link.StartMessage(AS_GET_FONT_LIST_REVISION);
int32 code;
if (link.FlushWithReply(code) != B_OK || code != B_OK)
return B_ERROR;
int32 revision;
link.Read<int32>(&revision);
return revision;
}
family* family*
FontList::_FindFamily(font_family name) FontList::_FindFamily(font_family name)
{ {
@@ -247,78 +322,6 @@ FontList::_FindFamily(font_family name)
} }
status_t
FontList::FamilyAt(int32 index, font_family *_family, uint32 *_flags)
{
BAutolock locker(this);
status_t status = _UpdateIfNecessary();
if (status < B_OK)
return status;
::family* family = fFamilies.ItemAt(index);
if (family == NULL)
return B_BAD_VALUE;
memcpy(*_family, family->name.String(), family->name.Length() + 1);
if (_flags)
*_flags = family->flags;
return B_OK;
}
status_t
FontList::StyleAt(font_family familyName, int32 index, font_style *_style,
uint16 *_face, uint32 *_flags)
{
BAutolock locker(this);
status_t status = _UpdateIfNecessary();
if (status < B_OK)
return status;
::family* family = _FindFamily(familyName);
if (family == NULL)
return B_BAD_VALUE;
::style* style = family->styles.ItemAt(index);
if (style == NULL)
return B_BAD_VALUE;
memcpy(*_style, style->name.String(), style->name.Length() + 1);
if (_face)
*_face = style->face;
if (_flags)
*_flags = style->flags;
return B_OK;
}
int32
FontList::CountFamilies()
{
BAutolock locker(this);
_UpdateIfNecessary();
return fFamilies.CountItems();
}
int32
FontList::CountStyles(font_family familyName)
{
BAutolock locker(this);
_UpdateIfNecessary();
::family* family = _FindFamily(familyName);
if (family == NULL)
return 0;
return family->styles.CountItems();
}
/*static*/ void /*static*/ void
FontList::_InitSingleton() FontList::_InitSingleton()
{ {
@@ -348,7 +351,7 @@ _init_global_fonts_()
while (link.ReadString(type, sizeof(type)) >= B_OK && type[0]) { while (link.ReadString(type, sizeof(type)) >= B_OK && type[0]) {
BFont dummy; BFont dummy;
BFont *font = &dummy; BFont* font = &dummy;
if (!strcmp(type, "plain")) if (!strcmp(type, "plain"))
font = &sPlainFont; font = &sPlainFont;
@@ -369,16 +372,21 @@ _init_global_fonts_()
} }
void _font_control_(BFont *font, int32 cmd, void *data) void
_font_control_(BFont* font, int32 cmd, void* data)
{ {
} }
status_t get_font_cache_info(uint32 id, void *set)
status_t
get_font_cache_info(uint32 id, void* set)
{ {
return B_ERROR; return B_ERROR;
} }
status_t set_font_cache_info(uint32 id, void *set)
status_t
set_font_cache_info(uint32 id, void* set)
{ {
return B_ERROR; return B_ERROR;
} }
@@ -386,7 +394,7 @@ status_t set_font_cache_info(uint32 id, void *set)
// Private function used to replace the R5 hack which sets a system font // Private function used to replace the R5 hack which sets a system font
void void
_set_system_font_(const char *which, font_family family, font_style style, _set_system_font_(const char* which, font_family family, font_style style,
float size) float size)
{ {
// R5 used a global area offset table to set the system fonts in the Font // R5 used a global area offset table to set the system fonts in the Font
@@ -441,7 +449,7 @@ count_font_styles(font_family family)
// Retrieves the family name at the specified index // Retrieves the family name at the specified index
status_t status_t
get_font_family(int32 index, font_family *_name, uint32 *_flags) get_font_family(int32 index, font_family* _name, uint32* _flags)
{ {
if (_name == NULL) if (_name == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
@@ -452,8 +460,8 @@ get_font_family(int32 index, font_family *_name, uint32 *_flags)
// Retrieves the family name at the specified index // Retrieves the family name at the specified index
status_t status_t
get_font_style(font_family family, int32 index, font_style *_name, get_font_style(font_family family, int32 index, font_style* _name,
uint32 *_flags) uint32* _flags)
{ {
return get_font_style(family, index, _name, NULL, _flags); return get_font_style(family, index, _name, NULL, _flags);
} }
@@ -461,8 +469,8 @@ get_font_style(font_family family, int32 index, font_style *_name,
// Retrieves the family name at the specified index // Retrieves the family name at the specified index
status_t status_t
get_font_style(font_family family, int32 index, font_style *_name, get_font_style(font_family family, int32 index, font_style* _name,
uint16 *_face, uint32 *_flags) uint16* _face, uint32* _flags)
{ {
// The face value returned by this function is not very reliable. At the // The face value returned by this function is not very reliable. At the
// same time, the value returned should be fairly reliable, returning the // same time, the value returned should be fairly reliable, returning the
@@ -495,8 +503,8 @@ BFont::BFont()
fShear(90.0), fShear(90.0),
fRotation(0.0), fRotation(0.0),
fFalseBoldWidth(0.0), fFalseBoldWidth(0.0),
fSpacing(0), fSpacing(B_CHAR_SPACING),
fEncoding(0), fEncoding(B_UNICODE_UTF8),
fFace(0), fFace(0),
fFlags(0), fFlags(0),
fExtraFlags(kUninitializedExtraFlags) fExtraFlags(kUninitializedExtraFlags)
@@ -511,13 +519,13 @@ BFont::BFont()
} }
BFont::BFont(const BFont &font) BFont::BFont(const BFont& font)
{ {
*this = font; *this = font;
} }
BFont::BFont(const BFont *font) BFont::BFont(const BFont* font)
{ {
if (font != NULL) if (font != NULL)
*this = *font; *this = *font;
@@ -684,7 +692,7 @@ BFont::SetFlags(uint32 flags)
void void
BFont::GetFamilyAndStyle(font_family *family, font_style *style) const BFont::GetFamilyAndStyle(font_family* family, font_style* style) const
{ {
if (family == NULL && style == NULL) if (family == NULL && style == NULL)
return; return;
@@ -878,7 +886,7 @@ BFont::CountTuned() const
void void
BFont::GetTunedInfo(int32 index, tuned_font_info *info) const BFont::GetTunedInfo(int32 index, tuned_font_info* info) const
{ {
if (info == NULL) if (info == NULL)
return; return;
@@ -898,18 +906,18 @@ BFont::GetTunedInfo(int32 index, tuned_font_info *info) const
void void
BFont::TruncateString(BString *inOut, uint32 mode, float width) const BFont::TruncateString(BString* inOut, uint32 mode, float width) const
{ {
// NOTE: Careful, we cannot directly use "inOut->String()" as result // NOTE: Careful, we cannot directly use "inOut->String()" as result
// array, because the string length increases by 3 bytes in the worst // array, because the string length increases by 3 bytes in the worst
// case scenario. // case scenario.
const char *string = inOut->String(); const char* string = inOut->String();
GetTruncatedStrings(&string, 1, mode, width, inOut); GetTruncatedStrings(&string, 1, mode, width, inOut);
} }
void void
BFont::GetTruncatedStrings(const char *stringArray[], int32 numStrings, BFont::GetTruncatedStrings(const char* stringArray[], int32 numStrings,
uint32 mode, float width, BString resultArray[]) const uint32 mode, float width, BString resultArray[]) const
{ {
if (stringArray != NULL && numStrings > 0) { if (stringArray != NULL && numStrings > 0) {
@@ -921,7 +929,7 @@ BFont::GetTruncatedStrings(const char *stringArray[], int32 numStrings,
int32 numChars = resultArray[i].CountChars(); int32 numChars = resultArray[i].CountChars();
// get the escapement of each glyph in font units // get the escapement of each glyph in font units
float *escapementArray = new float[numChars]; float* escapementArray = new float[numChars];
GetEscapements(stringArray[i], numChars, NULL, escapementArray); GetEscapements(stringArray[i], numChars, NULL, escapementArray);
truncate_string(resultArray[i], mode, width, escapementArray, truncate_string(resultArray[i], mode, width, escapementArray,
@@ -934,12 +942,12 @@ BFont::GetTruncatedStrings(const char *stringArray[], int32 numStrings,
void void
BFont::GetTruncatedStrings(const char *stringArray[], int32 numStrings, BFont::GetTruncatedStrings(const char* stringArray[], int32 numStrings,
uint32 mode, float width, char *resultArray[]) const uint32 mode, float width, char* resultArray[]) const
{ {
if (stringArray != NULL && numStrings > 0) { if (stringArray != NULL && numStrings > 0) {
for (int32 i = 0; i < numStrings; i++) { for (int32 i = 0; i < numStrings; i++) {
BString *strings = new BString[numStrings]; BString* strings = new BString[numStrings];
GetTruncatedStrings(stringArray, numStrings, mode, width, strings); GetTruncatedStrings(stringArray, numStrings, mode, width, strings);
for (int32 i = 0; i < numStrings; i++) for (int32 i = 0; i < numStrings; i++)
@@ -952,7 +960,7 @@ BFont::GetTruncatedStrings(const char *stringArray[], int32 numStrings,
float float
BFont::StringWidth(const char *string) const BFont::StringWidth(const char* string) const
{ {
if (string == NULL) if (string == NULL)
return 0.0; return 0.0;
@@ -966,7 +974,7 @@ BFont::StringWidth(const char *string) const
float float
BFont::StringWidth(const char *string, int32 length) const BFont::StringWidth(const char* string, int32 length) const
{ {
if (!string || length < 1) if (!string || length < 1)
return 0.0f; return 0.0f;
@@ -979,7 +987,7 @@ BFont::StringWidth(const char *string, int32 length) const
void void
BFont::GetStringWidths(const char *stringArray[], const int32 lengthArray[], BFont::GetStringWidths(const char* stringArray[], const int32 lengthArray[],
int32 numStrings, float widthArray[]) const int32 numStrings, float widthArray[]) const
{ {
if (stringArray == NULL || lengthArray == NULL || numStrings < 1 if (stringArray == NULL || lengthArray == NULL || numStrings < 1
@@ -1019,7 +1027,7 @@ BFont::GetEscapements(const char charArray[], int32 numChars,
void void
BFont::GetEscapements(const char charArray[], int32 numChars, BFont::GetEscapements(const char charArray[], int32 numChars,
escapement_delta *delta, float escapementArray[]) const escapement_delta* delta, float escapementArray[]) const
{ {
if (charArray == NULL || numChars < 1 || escapementArray == NULL) if (charArray == NULL || numChars < 1 || escapementArray == NULL)
return; return;
@@ -1052,7 +1060,7 @@ BFont::GetEscapements(const char charArray[], int32 numChars,
void void
BFont::GetEscapements(const char charArray[], int32 numChars, BFont::GetEscapements(const char charArray[], int32 numChars,
escapement_delta *delta, BPoint escapementArray[]) const escapement_delta* delta, BPoint escapementArray[]) const
{ {
GetEscapements(charArray, numChars, delta, escapementArray, NULL); GetEscapements(charArray, numChars, delta, escapementArray, NULL);
} }
@@ -1060,7 +1068,7 @@ BFont::GetEscapements(const char charArray[], int32 numChars,
void void
BFont::GetEscapements(const char charArray[], int32 numChars, BFont::GetEscapements(const char charArray[], int32 numChars,
escapement_delta *delta, BPoint escapementArray[], escapement_delta* delta, BPoint escapementArray[],
BPoint offsetArray[]) const BPoint offsetArray[]) const
{ {
if (charArray == NULL || numChars < 1 || escapementArray == NULL) if (charArray == NULL || numChars < 1 || escapementArray == NULL)
@@ -1122,7 +1130,7 @@ BFont::GetEdges(const char charArray[], int32 numChars,
void void
BFont::GetHeight(font_height *_height) const BFont::GetHeight(font_height* _height) const
{ {
if (_height == NULL) if (_height == NULL)
return; return;
@@ -1161,7 +1169,7 @@ BFont::GetBoundingBoxesAsGlyphs(const char charArray[], int32 numChars,
void void
BFont::GetBoundingBoxesAsString(const char charArray[], int32 numChars, BFont::GetBoundingBoxesAsString(const char charArray[], int32 numChars,
font_metric_mode mode, escapement_delta *delta, font_metric_mode mode, escapement_delta* delta,
BRect boundingBoxArray[]) const BRect boundingBoxArray[]) const
{ {
_GetBoundingBoxes(charArray, numChars, mode, true, delta, _GetBoundingBoxes(charArray, numChars, mode, true, delta,
@@ -1171,7 +1179,7 @@ BFont::GetBoundingBoxesAsString(const char charArray[], int32 numChars,
void void
BFont::_GetBoundingBoxes(const char charArray[], int32 numChars, BFont::_GetBoundingBoxes(const char charArray[], int32 numChars,
font_metric_mode mode, bool string_escapement, escapement_delta *delta, font_metric_mode mode, bool string_escapement, escapement_delta* delta,
BRect boundingBoxArray[], bool asString) const BRect boundingBoxArray[], bool asString) const
{ {
if (charArray == NULL || numChars < 1 || boundingBoxArray == NULL) if (charArray == NULL || numChars < 1 || boundingBoxArray == NULL)
@@ -1214,7 +1222,7 @@ BFont::_GetBoundingBoxes(const char charArray[], int32 numChars,
void void
BFont::GetBoundingBoxesForStrings(const char *stringArray[], int32 numStrings, BFont::GetBoundingBoxesForStrings(const char* stringArray[], int32 numStrings,
font_metric_mode mode, escapement_delta deltas[], font_metric_mode mode, escapement_delta deltas[],
BRect boundingBoxArray[]) const BRect boundingBoxArray[]) const
{ {
@@ -1259,7 +1267,7 @@ BFont::GetBoundingBoxesForStrings(const char *stringArray[], int32 numStrings,
void void
BFont::GetGlyphShapes(const char charArray[], int32 numChars, BFont::GetGlyphShapes(const char charArray[], int32 numChars,
BShape *glyphShapeArray[]) const BShape* glyphShapeArray[]) const
{ {
// TODO: implement code specifically for passing BShapes to and // TODO: implement code specifically for passing BShapes to and
// from the server // from the server
@@ -1317,8 +1325,8 @@ BFont::GetHasGlyphs(const char charArray[], int32 numChars,
} }
BFont & BFont&
BFont::operator=(const BFont &font) BFont::operator=(const BFont& font)
{ {
fFamilyID = font.fFamilyID; fFamilyID = font.fFamilyID;
fStyleID = font.fStyleID; fStyleID = font.fStyleID;
@@ -1338,7 +1346,7 @@ BFont::operator=(const BFont &font)
bool bool
BFont::operator==(const BFont &font) const BFont::operator==(const BFont& font) const
{ {
return fFamilyID == font.fFamilyID return fFamilyID == font.fFamilyID
&& fStyleID == font.fStyleID && fStyleID == font.fStyleID
@@ -1353,7 +1361,7 @@ BFont::operator==(const BFont &font) const
bool bool
BFont::operator!=(const BFont &font) const BFont::operator!=(const BFont& font) const
{ {
return fFamilyID != font.fFamilyID return fFamilyID != font.fFamilyID
|| fStyleID != font.fStyleID || fStyleID != font.fStyleID