* FontFamily::GetStyle() now looks for alternative names when a specific

style couldn't be found, ie. when looking for "Roman", "Regular", and
  "Book" are accepted, too, and "Italic"/"Oblique" are now synonyms as 
  well.
* This prevents StyledEdit from using the "Condensed" style when it does 
  not find a certain font.
* Minor cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24443 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2008-03-18 16:52:34 +00:00
parent e9d948ce2d
commit 98fe648b6e
3 changed files with 84 additions and 45 deletions
+46 -9
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2006, Haiku. * Copyright 2001-2008, Haiku.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -157,6 +157,23 @@ FontFamily::CountStyles() const
} }
FontStyle*
FontFamily::_FindStyle(const char* name) const
{
int32 count = fStyles.CountItems();
if (!name || count < 1)
return NULL;
for (int32 i = 0; i < count; i++) {
FontStyle *style = fStyles.ItemAt(i);
if (!strcmp(style->Name(), name))
return style;
}
return NULL;
}
/*! /*!
\brief Determines whether the style belongs to the family \brief Determines whether the style belongs to the family
\param style Name of the style being checked \param style Name of the style being checked
@@ -165,7 +182,7 @@ FontFamily::CountStyles() const
bool bool
FontFamily::HasStyle(const char *styleName) const FontFamily::HasStyle(const char *styleName) const
{ {
return GetStyle(styleName) != NULL; return _FindStyle(styleName) != NULL;
} }
@@ -189,16 +206,36 @@ FontFamily::StyleAt(int32 index) const
The object returned belongs to the family and must not be deleted. The object returned belongs to the family and must not be deleted.
*/ */
FontStyle* FontStyle*
FontFamily::GetStyle(const char *styleName) const FontFamily::GetStyle(const char *name) const
{ {
int32 count = fStyles.CountItems(); if (name == NULL || !name[0])
if (!styleName || count < 1)
return NULL; return NULL;
for (int32 i = 0; i < count; i++) { FontStyle* style = _FindStyle(name);
FontStyle *style = fStyles.ItemAt(i); if (style != NULL)
if (!strcmp(style->Name(), styleName)) return style;
return style;
// try alternative names
if (!strcmp(name, "Roman") || !strcmp(name, "Regular")
|| !strcmp(name, "Book")) {
style = _FindStyle("Roman");
if (style == NULL) {
style = _FindStyle("Regular");
if (style == NULL)
style = _FindStyle("Book");
}
return style;
}
BString alternative = name;
if (alternative.FindFirst("Italic") >= 0) {
alternative.ReplaceFirst("Italic", "Oblique");
return _FindStyle(alternative.String());
}
if (alternative.FindFirst("Oblique") >= 0) {
alternative.ReplaceFirst("Oblique", "Italic");
return _FindStyle(alternative.String());
} }
return NULL; return NULL;
+24 -22
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2006, Haiku. * Copyright 2001-2008, Haiku.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -24,33 +24,35 @@
Arial Roman, Arial Italic, Arial Bold, etc. Arial Roman, Arial Italic, Arial Bold, etc.
*/ */
class FontFamily { class FontFamily {
public: public:
FontFamily(const char* name, uint16 id); FontFamily(const char* name, uint16 id);
virtual ~FontFamily(); virtual ~FontFamily();
const char* Name() const; const char* Name() const;
bool AddStyle(FontStyle* style); bool AddStyle(FontStyle* style);
bool RemoveStyle(FontStyle* style); bool RemoveStyle(FontStyle* style);
FontStyle* GetStyle(const char* style) const; FontStyle* GetStyle(const char* style) const;
FontStyle* GetStyleMatchingFace(uint16 face) const; FontStyle* GetStyleMatchingFace(uint16 face) const;
FontStyle* GetStyleByID(uint16 face) const; FontStyle* GetStyleByID(uint16 face) const;
uint16 ID() const uint16 ID() const
{ return fID; } { return fID; }
uint32 Flags(); uint32 Flags();
bool HasStyle(const char* style) const; bool HasStyle(const char* style) const;
int32 CountStyles() const; int32 CountStyles() const;
FontStyle* StyleAt(int32 index) const; FontStyle* StyleAt(int32 index) const;
private: private:
BString fName; FontStyle* _FindStyle(const char* name) const;
BObjectList<FontStyle> fStyles;
uint16 fID; BString fName;
uint16 fNextID; BObjectList<FontStyle> fStyles;
uint32 fFlags; uint16 fID;
uint16 fNextID;
uint32 fFlags;
}; };
#endif // FONT_FAMILY_H_ #endif // FONT_FAMILY_H_
+2 -2
View File
@@ -943,8 +943,8 @@ FontManager::GetStyleByIndex(const char* familyName, int32 index)
\return The FontStyle having those attributes or NULL if not available \return The FontStyle having those attributes or NULL if not available
*/ */
FontStyle* FontStyle*
FontManager::GetStyle(const char* familyName, const char* styleName, uint16 familyID, FontManager::GetStyle(const char* familyName, const char* styleName,
uint16 styleID, uint16 face) uint16 familyID, uint16 styleID, uint16 face)
{ {
FontFamily* family; FontFamily* family;