* 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

View File

@ -1,5 +1,5 @@
/*
* Copyright 2001-2006, Haiku.
* Copyright 2001-2008, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
@ -64,7 +64,7 @@ FontFamily::FontFamily(const char *name, uint16 id)
/*!
\brief Destructor
Deletes all attached styles. Note that a FontFamily must only be deleted
by the font manager.
*/
@ -102,7 +102,7 @@ FontFamily::AddStyle(FontStyle *style)
if (!style)
return false;
// Don't add if it already is in the family.
// Don't add if it already is in the family.
int32 count = fStyles.CountItems();
for (int32 i = 0; i < count; i++) {
FontStyle *item = fStyles.ItemAt(i);
@ -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
\param style Name of the style being checked
@ -165,11 +182,11 @@ FontFamily::CountStyles() const
bool
FontFamily::HasStyle(const char *styleName) const
{
return GetStyle(styleName) != NULL;
return _FindStyle(styleName) != NULL;
}
/*!
/*!
\brief Returns the name of a style in the family
\param index list index of the style to be found
\return name of the style or NULL if the index is not valid
@ -185,20 +202,40 @@ FontFamily::StyleAt(int32 index) const
\brief Get the FontStyle object for the name given
\param style Name of the style to be obtained
\return The FontStyle object or NULL if none was found.
The object returned belongs to the family and must not be deleted.
*/
FontStyle*
FontFamily::GetStyle(const char *styleName) const
FontFamily::GetStyle(const char *name) const
{
int32 count = fStyles.CountItems();
if (!styleName || count < 1)
if (name == NULL || !name[0])
return NULL;
for (int32 i = 0; i < count; i++) {
FontStyle *style = fStyles.ItemAt(i);
if (!strcmp(style->Name(), styleName))
return style;
FontStyle* style = _FindStyle(name);
if (style != NULL)
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;
@ -228,7 +265,7 @@ FontFamily::GetStyleMatchingFace(uint16 face) const
int32 count = fStyles.CountItems();
for (int32 i = 0; i < count; i++) {
FontStyle* style = fStyles.ItemAt(i);
if (style->Face() == face)
return style;
}
@ -253,6 +290,6 @@ FontFamily::Flags()
fFlags |= B_HAS_TUNED_FONT;
}
}
return fFlags;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2001-2006, Haiku.
* Copyright 2001-2008, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
@ -19,38 +19,40 @@
/*!
\class FontFamily FontFamily.h
\brief Class representing a collection of similar styles
FontFamily objects bring together many styles of the same face, such as
Arial Roman, Arial Italic, Arial Bold, etc.
*/
class FontFamily {
public:
FontFamily(const char* name, uint16 id);
virtual ~FontFamily();
public:
FontFamily(const char* name, uint16 id);
virtual ~FontFamily();
const char* Name() const;
const char* Name() const;
bool AddStyle(FontStyle* style);
bool RemoveStyle(FontStyle* style);
bool AddStyle(FontStyle* style);
bool RemoveStyle(FontStyle* style);
FontStyle* GetStyle(const char* style) const;
FontStyle* GetStyleMatchingFace(uint16 face) const;
FontStyle* GetStyleByID(uint16 face) const;
FontStyle* GetStyle(const char* style) const;
FontStyle* GetStyleMatchingFace(uint16 face) const;
FontStyle* GetStyleByID(uint16 face) const;
uint16 ID() const
{ return fID; }
uint32 Flags();
uint16 ID() const
{ return fID; }
uint32 Flags();
bool HasStyle(const char* style) const;
int32 CountStyles() const;
FontStyle* StyleAt(int32 index) const;
bool HasStyle(const char* style) const;
int32 CountStyles() const;
FontStyle* StyleAt(int32 index) const;
private:
BString fName;
BObjectList<FontStyle> fStyles;
uint16 fID;
uint16 fNextID;
uint32 fFlags;
private:
FontStyle* _FindStyle(const char* name) const;
BString fName;
BObjectList<FontStyle> fStyles;
uint16 fID;
uint16 fNextID;
uint32 fFlags;
};
#endif // FONT_FAMILY_H_

View File

@ -317,11 +317,11 @@ FontManager::_LoadRecentFontMappings()
// default known mappings
// TODO: load them for real, and use these as a fallback
_AddDefaultMapping("Bitstream Vera Sans", "Roman",
_AddDefaultMapping("Bitstream Vera Sans", "Roman",
"/boot/beos/etc/fonts/ttfonts/Vera.ttf");
_AddDefaultMapping("Bitstream Vera Sans", "Bold",
_AddDefaultMapping("Bitstream Vera Sans", "Bold",
"/boot/beos/etc/fonts/ttfonts/VeraBd.ttf");
_AddDefaultMapping("Bitstream Vera Sans Mono", "Roman",
_AddDefaultMapping("Bitstream Vera Sans Mono", "Roman",
"/boot/beos/etc/fonts/ttfonts/VeraMono.ttf");
return false;
@ -572,7 +572,7 @@ FontManager::_FindDirectory(node_ref& nodeRef)
for (int32 i = fDirectories.CountItems(); i-- > 0;) {
font_directory* directory = fDirectories.ItemAt(i);
if (directory->directory == nodeRef)
if (directory->directory == nodeRef)
return directory;
}
@ -768,7 +768,7 @@ FontManager::_ScanFontDirectory(font_directory& fontDirectory)
/*!
\brief Finds and returns the first valid charmap in a font
\param face Font handle obtained from FT_Load_Face()
\return An FT_CharMap or NULL if unsuccessful
*/
@ -943,8 +943,8 @@ FontManager::GetStyleByIndex(const char* familyName, int32 index)
\return The FontStyle having those attributes or NULL if not available
*/
FontStyle*
FontManager::GetStyle(const char* familyName, const char* styleName, uint16 familyID,
uint16 styleID, uint16 face)
FontManager::GetStyle(const char* familyName, const char* styleName,
uint16 familyID, uint16 styleID, uint16 face)
{
FontFamily* family;