2003-01-01 01:19:48 +03:00
|
|
|
//------------------------------------------------------------------------------
|
2004-09-21 02:50:02 +04:00
|
|
|
// Copyright (c) 2001-2002, Haiku, Inc.
|
2003-01-01 01:19:48 +03:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// 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
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
//
|
|
|
|
// File Name: FontFamily.cpp
|
|
|
|
// Author: DarkWyrm <bpmagic@columbus.rr.com>
|
|
|
|
// Description: classes to represent font styles and families
|
|
|
|
//
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
#include "FontFamily.h"
|
|
|
|
#include "ServerFont.h"
|
|
|
|
#include FT_CACHE_H
|
|
|
|
|
2003-07-24 23:38:24 +04:00
|
|
|
FTC_Manager ftmanager;
|
2003-01-01 01:19:48 +03:00
|
|
|
|
|
|
|
/*!
|
2003-01-18 23:32:45 +03:00
|
|
|
\brief Constructor
|
2003-01-20 02:04:58 +03:00
|
|
|
\param filepath path to a font file
|
2003-01-01 01:19:48 +03:00
|
|
|
\param face FreeType handle for the font file after it is loaded - for its info only
|
|
|
|
*/
|
|
|
|
FontStyle::FontStyle(const char *filepath, FT_Face face)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
fName=face->style_name;
|
2003-01-01 01:19:48 +03:00
|
|
|
cachedface=new CachedFaceRec;
|
|
|
|
cachedface->file_path=filepath;
|
|
|
|
family=NULL;
|
|
|
|
has_bitmaps=(face->num_fixed_sizes>0)?true:false;
|
|
|
|
is_fixedwidth=(face->face_flags & FT_FACE_FLAG_FIXED_WIDTH)?true:false;
|
|
|
|
is_scalable=(face->face_flags & FT_FACE_FLAG_SCALABLE)?true:false;
|
|
|
|
has_kerning=(face->face_flags & FT_FACE_FLAG_KERNING)?true:false;
|
|
|
|
glyphcount=face->num_glyphs;
|
|
|
|
charmapcount=face->num_charmaps;
|
|
|
|
tunedcount=face->num_fixed_sizes;
|
2005-01-17 05:05:50 +03:00
|
|
|
fPath=filepath;
|
2003-01-01 01:19:48 +03:00
|
|
|
fbounds.Set(0,0,0,0);
|
2005-01-17 05:05:50 +03:00
|
|
|
fFace=TranslateStyleToFace(face->style_name);
|
|
|
|
fID=0;
|
2005-01-17 23:08:40 +03:00
|
|
|
fHeight.ascent=face->ascender;
|
|
|
|
fHeight.descent=face->descender;
|
|
|
|
fHeight.leading=face->height;
|
|
|
|
fHeight.units_per_em=face->units_per_EM;
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2003-01-18 23:32:45 +03:00
|
|
|
\brief Destructor
|
2003-01-01 01:19:48 +03:00
|
|
|
|
|
|
|
Frees all data allocated on the heap. All child ServerFonts are marked as having
|
|
|
|
a NULL style so that each ServerFont knows that it is running on borrowed time.
|
|
|
|
This is done because a FontStyle should be deleted only when it no longer has any
|
|
|
|
dependencies.
|
|
|
|
*/
|
|
|
|
FontStyle::~FontStyle(void)
|
|
|
|
{
|
|
|
|
delete cachedface;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Returns the name of the style as a string
|
|
|
|
\return The style's name
|
|
|
|
*/
|
2005-01-17 05:05:50 +03:00
|
|
|
const char *FontStyle::Name(void) const
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
return fName.String();
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
2005-01-17 23:08:40 +03:00
|
|
|
font_height FontStyle::GetHeight(const float &size)
|
|
|
|
{
|
|
|
|
font_height fh={0,0,0};
|
|
|
|
|
|
|
|
// font units are 26.6 format, so we get REALLY big numbers if
|
|
|
|
// we don't do some shifting.
|
|
|
|
fh.ascent=(fHeight.ascent * size) / fHeight.units_per_em;
|
|
|
|
|
|
|
|
// FT2's descent numbers are negative. Be's is positive
|
|
|
|
fh.descent=(fHeight.descent * size * -1) / fHeight.units_per_em;
|
|
|
|
|
|
|
|
fh.leading=(fHeight.leading * size) / fHeight.units_per_em;
|
|
|
|
return fh;
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
|
|
|
\brief Returns the path to the style's font file
|
|
|
|
\return The style's font file path
|
|
|
|
*/
|
|
|
|
const char *FontStyle::GetPath(void)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
return fPath.String();
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 FontStyle::GetFlags(void) const
|
|
|
|
{
|
|
|
|
int32 flags=0;
|
|
|
|
if(IsFixedWidth())
|
|
|
|
flags|=B_IS_FIXED;
|
|
|
|
|
|
|
|
if(TunedCount()>0)
|
|
|
|
flags|=B_HAS_TUNED_FONT;
|
|
|
|
|
|
|
|
return flags;
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Converts an ASCII character to Unicode for the style
|
2003-01-20 02:04:58 +03:00
|
|
|
\param c An ASCII character
|
2003-01-01 01:19:48 +03:00
|
|
|
\return A Unicode value for the character
|
|
|
|
*/
|
2005-01-17 05:05:50 +03:00
|
|
|
|
|
|
|
// TODO: Re-enable when I understand how the FT2 Cache system changed from
|
|
|
|
// 2.1.4 to 2.1.8
|
|
|
|
/*
|
2003-01-01 01:19:48 +03:00
|
|
|
int16 FontStyle::ConvertToUnicode(uint16 c)
|
|
|
|
{
|
|
|
|
FT_Face f;
|
2005-01-17 05:05:50 +03:00
|
|
|
if(FTC_Manager_Lookup_Face(ftmanager,cachedface,&f)!=0)
|
2003-01-01 01:19:48 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return FT_Get_Char_Index(f,c);
|
|
|
|
}
|
|
|
|
*/
|
2005-01-17 05:05:50 +03:00
|
|
|
|
|
|
|
uint16 FontStyle::TranslateStyleToFace(const char *name) const
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
if(!name)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
BString str(name);
|
|
|
|
|
|
|
|
|
2005-01-17 05:20:57 +03:00
|
|
|
if(str.IFindFirst("bold")!=B_ERROR)
|
2005-01-17 05:05:50 +03:00
|
|
|
return B_BOLD_FACE;
|
|
|
|
|
2005-01-17 05:20:57 +03:00
|
|
|
if(str.IFindFirst("italic")!=B_ERROR)
|
|
|
|
return B_ITALIC_FACE;
|
|
|
|
if(str.IFindFirst("oblique")!=B_ERROR)
|
2005-01-17 05:05:50 +03:00
|
|
|
return B_ITALIC_FACE;
|
|
|
|
|
2005-01-17 05:20:57 +03:00
|
|
|
return B_REGULAR_FACE;
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Constructor
|
2003-01-20 02:04:58 +03:00
|
|
|
\param namestr Name of the family
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2005-01-17 05:05:50 +03:00
|
|
|
FontFamily::FontFamily(const char *namestr, const uint16 &index)
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
fName=namestr;
|
|
|
|
fID=index;
|
|
|
|
|
|
|
|
// will stay uninitialized until needed
|
|
|
|
fFlags=-1;
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Destructor
|
|
|
|
|
|
|
|
Deletes all child styles. Note that a FontFamily should not be deleted unless
|
|
|
|
its styles have no dependencies or some other really good reason, such as
|
|
|
|
system shutdown.
|
|
|
|
*/
|
|
|
|
FontFamily::~FontFamily(void)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
FontStyle *style;
|
|
|
|
for(int32 i=0; i<fStyles.CountItems(); i++)
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
style=(FontStyle *)fStyles.RemoveItem(i);
|
|
|
|
delete style;
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Returns the name of the family
|
|
|
|
\return The family's name
|
|
|
|
*/
|
|
|
|
const char *FontFamily::Name(void)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
return fName.String();
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Adds the style to the family
|
2003-01-20 02:04:58 +03:00
|
|
|
\param path full path to the style's font file
|
|
|
|
\param face FreeType face handle used to obtain info about the font
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
2005-01-17 05:05:50 +03:00
|
|
|
bool FontFamily::AddStyle(FontStyle *style)
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
if(!style)
|
|
|
|
return false;
|
2003-01-01 01:19:48 +03:00
|
|
|
|
|
|
|
FontStyle *item;
|
|
|
|
|
|
|
|
// Don't add if it already is in the family.
|
2005-01-17 05:05:50 +03:00
|
|
|
int32 count=fStyles.CountItems();
|
2003-01-01 01:19:48 +03:00
|
|
|
for(int32 i=0; i<count; i++)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
item=(FontStyle *)fStyles.ItemAt(i);
|
|
|
|
if(item->fName==style->fName)
|
|
|
|
return false;
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
2005-01-17 05:05:50 +03:00
|
|
|
|
|
|
|
style->family=this;
|
|
|
|
|
|
|
|
if(fStyles.CountItems()>0)
|
|
|
|
{
|
|
|
|
item=(FontStyle *)fStyles.ItemAt(fStyles.CountItems()-1);
|
|
|
|
style->fID=item->fID+1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
style->fID=0;
|
|
|
|
|
|
|
|
fStyles.AddItem(style);
|
2003-01-01 01:19:48 +03:00
|
|
|
AddDependent();
|
2005-01-17 05:05:50 +03:00
|
|
|
|
|
|
|
// force a refresh if a request for font flags is needed
|
|
|
|
fFlags=-1;
|
|
|
|
|
|
|
|
return true;
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Removes a style from the family and deletes it
|
2003-01-20 02:04:58 +03:00
|
|
|
\param style Name of the style to be removed from the family
|
2003-01-01 01:19:48 +03:00
|
|
|
*/
|
|
|
|
void FontFamily::RemoveStyle(const char *style)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
int32 count=fStyles.CountItems();
|
2003-01-01 01:19:48 +03:00
|
|
|
if(!style || count<1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
FontStyle *fs;
|
|
|
|
for(int32 i=0; i<count; i++)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
fs=(FontStyle *)fStyles.ItemAt(i);
|
|
|
|
if(fs && fs->fName.Compare(style)==0)
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
fs=(FontStyle *)fStyles.RemoveItem(i);
|
2003-01-01 01:19:48 +03:00
|
|
|
if(fs)
|
|
|
|
{
|
|
|
|
delete fs;
|
|
|
|
RemoveDependent();
|
2005-01-17 05:05:50 +03:00
|
|
|
|
|
|
|
// force a refresh if a request for font flags is needed
|
|
|
|
fFlags=-1;
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-17 05:05:50 +03:00
|
|
|
/*!
|
|
|
|
\brief Removes a style from the family. The caller is responsible for freeing the object
|
|
|
|
\param style The style to be removed from the family
|
|
|
|
*/
|
|
|
|
void FontFamily::RemoveStyle(FontStyle *style)
|
|
|
|
{
|
|
|
|
if(fStyles.HasItem(style))
|
|
|
|
{
|
|
|
|
fStyles.RemoveItem(style);
|
|
|
|
RemoveDependent();
|
|
|
|
|
|
|
|
// force a refresh if a request for font flags is needed
|
|
|
|
fFlags=-1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
/*!
|
|
|
|
\brief Returns the number of styles in the family
|
|
|
|
\return The number of styles in the family
|
|
|
|
*/
|
|
|
|
int32 FontFamily::CountStyles(void)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
return fStyles.CountItems();
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Determines whether the style belongs to the family
|
2003-01-20 02:04:58 +03:00
|
|
|
\param style Name of the style being checked
|
2003-01-01 01:19:48 +03:00
|
|
|
\return True if it belongs, false if not
|
|
|
|
*/
|
|
|
|
bool FontFamily::HasStyle(const char *style)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
int32 count=fStyles.CountItems();
|
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
if(!style || count<1)
|
|
|
|
return false;
|
2005-01-17 05:05:50 +03:00
|
|
|
|
2003-01-01 01:19:48 +03:00
|
|
|
FontStyle *fs;
|
|
|
|
for(int32 i=0; i<count; i++)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
fs=(FontStyle *)fStyles.ItemAt(i);
|
|
|
|
if(fs && fs->fName.Compare(style)==0)
|
2003-01-01 01:19:48 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Returns the name of a style in the family
|
2003-01-20 02:04:58 +03:00
|
|
|
\param index list index of the style to be found
|
2003-01-01 01:19:48 +03:00
|
|
|
\return name of the style or NULL if the index is not valid
|
|
|
|
*/
|
2005-01-17 05:05:50 +03:00
|
|
|
FontStyle *FontFamily::GetStyle(int32 index)
|
2003-01-01 01:19:48 +03:00
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
return (FontStyle*)fStyles.ItemAt(index);
|
2003-01-01 01:19:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\brief Get the FontStyle object for the name given
|
2003-01-20 02:04:58 +03:00
|
|
|
\param style Name of the style to be obtained
|
2003-01-01 01:19:48 +03:00
|
|
|
\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 *style)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
int32 count=fStyles.CountItems();
|
2003-01-01 01:19:48 +03:00
|
|
|
if(!style || count<1)
|
|
|
|
return NULL;
|
|
|
|
FontStyle *fs;
|
|
|
|
for(int32 i=0; i<count; i++)
|
|
|
|
{
|
2005-01-17 05:05:50 +03:00
|
|
|
fs=(FontStyle *)fStyles.ItemAt(i);
|
|
|
|
if(fs && fs->fName.Compare(style)==0)
|
2003-01-01 01:19:48 +03:00
|
|
|
return fs;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-01-17 05:05:50 +03:00
|
|
|
|
|
|
|
int32 FontFamily::GetFlags(void)
|
|
|
|
{
|
|
|
|
if(fFlags==-1)
|
|
|
|
{
|
|
|
|
fFlags=0;
|
|
|
|
|
|
|
|
for(int32 i=0; i<fStyles.CountItems(); i++)
|
|
|
|
{
|
|
|
|
FontStyle *style=(FontStyle*)fStyles.ItemAt(i);
|
|
|
|
if(style)
|
|
|
|
{
|
|
|
|
if(style->IsFixedWidth())
|
|
|
|
fFlags|=B_IS_FIXED;
|
|
|
|
if(style->TunedCount()>0)
|
|
|
|
fFlags|=B_HAS_TUNED_FONT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fFlags;
|
|
|
|
|
|
|
|
}
|