Significant cleanup in api, and more importantly in sementics, of SkFontHost

- FindTypeface is now CreateTypeface, and what it returns is always considered
    a new instance, even if it is from a cache (in which case it will have been
    ref()'d. The caller must always balance its ownership by calling unref() on
    the result when they are done.
- CloseStream is gone, since the caller can/must call stream->unref() when they
    are done using it.
- ResolveTypeface is now ValidFontID, and just returns a bool.



git-svn-id: http://skia.googlecode.com/svn/trunk@109 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@android.com 2009-03-04 17:37:51 +00:00
parent 069b827f8e
commit b1d9d2ef28
10 changed files with 160 additions and 155 deletions

View File

@ -26,51 +26,58 @@ class SkWStream;
/** \class SkFontHost
This class is ported to each environment. It is responsible for bridging the gap
between SkTypeface and the resulting platform-specific instance of SkScalerContext.
This class is ported to each environment. It is responsible for bridging
the gap between the (sort of) abstract class SkTypeface and the
platform-specific implementation that provides access to font files.
One basic task is for each create (subclass of) SkTypeface, the FontHost is
resonsible for assigning a uniqueID. The ID should be unique for the
underlying font file/data, not unique per typeface instance. Thus it is
possible/common to request a typeface for the same font more than once
(e.g. asking for the same font by name several times). The FontHost may
return seperate typeface instances in that case, or it may choose to use a
cache and return the same instance (but calling typeface->ref(), since the
caller is always responsible for calling unref() on each instance that is
returned). Either way, the fontID for those instance(s) will be the same.
In addition, the fontID should never be set to 0. That value is used as a
sentinel to indicate no-font-id.
The major aspects are:
1) Given either a name/style, return a subclass of SkTypeface that
references the closest matching font available on the host system.
2) Given the data for a font (either in a stream or a file name), return
a typeface that allows access to that data.
3) Each typeface instance carries a 32bit ID for its corresponding font.
SkFontHost turns that ID into a stream to access the font's data.
4) Given a font ID, return a subclass of SkScalerContext, which connects a
font scaler (e.g. freetype or other) to the font's data.
5) Utilites to manage the font cache (budgeting) and gamma correction
*/
class SkFontHost {
public:
/** Return the closest matching typeface given either an existing family
/** Return a new, closest matching typeface given either an existing family
(specified by a typeface in that family) or by a familyName, and a
requested style.
1) If familyFace is null, use famillyName.
2) If famillyName is null, use familyFace.
3) If both are null, return the default font that best matches style
NOTE: this does not return a new typeface, nor does it affect the
owner count of an existing one, so the caller is free to ignore the
return result, or just compare it against null.
*/
static SkTypeface* FindTypeface(const SkTypeface* familyFace,
const char famillyName[],
SkTypeface::Style style);
static SkTypeface* CreateTypeface(const SkTypeface* familyFace,
const char famillyName[],
SkTypeface::Style style);
/** Return the typeface associated with the uniqueID, or null if that ID
does not match any faces.
NOTE: this does not return a new typeface, nor does it affect the
owner count of an existing one, so the caller is free to ignore the
return result, or just compare it against null.
*/
static SkTypeface* ResolveTypeface(uint32_t uniqueID);
/** Return a new stream to read the font data, or null if the uniqueID does
not match an existing typeface. The caller must call CloseStream() when
it is finished reading the stream.
*/
static SkStream* OpenStream(uint32_t uniqueID);
/** Call this when finished reading from the stream returned by OpenStream.
The caller should NOT try to delete the stream.
/** Return a new typeface given the data buffer. If the data does not
represent a valid font, returns null.
If a typeface instance is returned, the caller is responsible for
calling unref() on the typeface when they are finished with it.
The returned typeface may or may not have called ref() on the stream
parameter. If the typeface has not called ref(), then it may have made
a copy of the releveant data. In either case, the caller is still
responsible for its refcnt ownership of the stream.
*/
static void CloseStream(uint32_t uniqueID, SkStream*);
/** Return a new typeface given the data buffer (owned by the caller).
If the data does not represent a valid font, return null. The caller is
responsible for unref-ing the returned typeface (if it is not null).
*/
static SkTypeface* CreateTypeface(SkStream*);
static SkTypeface* CreateTypefaceFromStream(SkStream*);
/** Return a new typeface from the specified file path. If the file does not
represent a valid font, this returns null. If a typeface is returned,
@ -78,6 +85,20 @@ public:
*/
static SkTypeface* CreateTypefaceFromFile(const char path[]);
///////////////////////////////////////////////////////////////////////////
/** Returns true if the specified unique ID matches an existing font.
Returning false is similar to calling OpenStream with an invalid ID,
which will return NULL in that case.
*/
static bool ValidFontID(uint32_t uniqueID);
/** Return a new stream to read the font data, or null if the uniqueID does
not match an existing typeface. .The caller must call stream->unref()
when it is finished reading the data.
*/
static SkStream* OpenStream(uint32_t uniqueID);
///////////////////////////////////////////////////////////////////////////
/** Write a unique identifier to the stream, so that the same typeface can
@ -85,12 +106,8 @@ public:
*/
static void Serialize(const SkTypeface*, SkWStream*);
/** Given a stream created by Serialize(), return the corresponding typeface
or null if no match is found.
NOTE: this does not return a new typeface, nor does it affect the
owner count of an existing one, so the caller is free to ignore the
return result, or just compare it against null.
/** Given a stream created by Serialize(), return a new typeface (like
CreateTypeface) or return NULL if no match is found.
*/
static SkTypeface* Deserialize(SkStream*);
@ -100,11 +117,14 @@ public:
*/
static SkScalerContext* CreateScalerContext(const SkDescriptor* desc);
/** Return a scalercontext using the "fallback" font. If there is no designated
fallback, return null.
/** Return a scalercontext using the "fallback" font. If there is no
designated fallback, return null.
*/
static SkScalerContext* CreateFallbackScalerContext(const SkScalerContext::Rec&);
static SkScalerContext* CreateFallbackScalerContext(
const SkScalerContext::Rec&);
///////////////////////////////////////////////////////////////////////////
/** Return the number of bytes (approx) that should be purged from the font
cache. The input parameter is the cache's estimate of how much as been
allocated by the cache so far.

View File

@ -62,7 +62,8 @@ public:
uint32_t uniqueID() const { return fUniqueID; }
/** Return the uniqueID for the specified typeface. If the face is null,
resolve it to the default font and return its uniqueID.
resolve it to the default font and return its uniqueID. Will never
return 0.
*/
static uint32_t UniqueID(const SkTypeface* face);

View File

@ -1,40 +1,42 @@
#include "SkTypeface.h"
#include "SkFontHost.h"
static const SkTypeface* resolve_null_typeface(const SkTypeface* face) {
if (NULL == face) {
face = SkFontHost::FindTypeface(NULL, NULL, SkTypeface::kNormal);
SkASSERT(face);
}
return face;
}
uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
return resolve_null_typeface(face)->uniqueID();
if (face) {
return face->uniqueID();
}
// We cache the default fontID, assuming it will not change during a boot
// The initial value of 0 is fine, since a typeface's uniqueID should not
// be zero.
static uint32_t gDefaultFontID;
if (0 == gDefaultFontID) {
SkTypeface* defaultFace = SkFontHost::CreateTypeface(NULL, NULL,
SkTypeface::kNormal);
SkASSERT(defaultFace);
gDefaultFontID = defaultFace->uniqueID();
defaultFace->unref();
}
return gDefaultFontID;
}
bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
return resolve_null_typeface(facea)->uniqueID() ==
resolve_null_typeface(faceb)->uniqueID();
return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
}
///////////////////////////////////////////////////////////////////////////////
SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) {
SkTypeface* face = SkFontHost::FindTypeface(NULL, name, style);
face->ref();
return face;
return SkFontHost::CreateTypeface(NULL, name, style);
}
SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) {
family = resolve_null_typeface(family);
SkTypeface* face = SkFontHost::FindTypeface(family, NULL, s);
face->ref();
return face;
return SkFontHost::CreateTypeface(family, NULL, s);
}
SkTypeface* SkTypeface::CreateFromStream(SkStream* stream) {
return SkFontHost::CreateTypeface(stream);
return SkFontHost::CreateTypefaceFromStream(stream);
}
SkTypeface* SkTypeface::CreateFromFile(const char path[]) {

View File

@ -239,9 +239,9 @@ static uint32_t ptr2uint32(const void* p)
return (uint32_t)((char*)p - (char*)0);
}
SkTypeface* SkFontHost::FindTypeface(const SkTypeface* familyFace,
const char familyName[],
SkTypeface::Style style)
SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
const char familyName[],
SkTypeface::Style style)
{
const FontFamilyRec* family;
@ -268,7 +268,7 @@ SkTypeface* SkFontHost::FindTypeface(const SkTypeface* familyFace,
return SkNEW_ARGS(FontFaceRec_Typeface, (face));
}
SkTypeface* SkFontHost::CreateTypeface(SkStream* stream) {
SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
sk_throw(); // not implemented
return NULL;
}
@ -278,10 +278,8 @@ SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
return NULL;
}
SkTypeface* SkFontHost::ResolveTypeface(uint32_t fontID) {
// TODO: this should just return a bool if fontID is valid
// since we don't keep a global-list, this will leak at the moment
return new FontFaceRec_Typeface(*get_default_face());
bool SkFontHost::ValidFontID(uint32_t fontID) {
return get_id(*get_default_face()) == fontID;
}
SkStream* SkFontHost::OpenStream(uint32_t fontID) {
@ -289,10 +287,6 @@ SkStream* SkFontHost::OpenStream(uint32_t fontID) {
return NULL;
}
void SkFontHost::CloseStream(uint32_t fontID, SkStream* stream) {
// not implemented
}
void SkFontHost::Serialize(const SkTypeface* tface, SkWStream* stream) {
const FontFaceRec* face = &((const FontFaceRec_Typeface*)tface)->fFace;
stream->write(face, sizeof(face));

View File

@ -102,9 +102,10 @@ struct SkFaceRec {
uint32_t fRefCnt;
uint32_t fFontID;
// assumes ownership of the stream, will call unref() when its done
SkFaceRec(SkStream* strm, uint32_t fontID);
~SkFaceRec() {
SkFontHost::CloseStream(fFontID, fSkStream);
fSkStream->unref();
}
};
@ -387,7 +388,7 @@ FT_Error SkScalerContext_FreeType::setupSize() {
killing all of the contexts when we know that a given fontID is going
away...
*/
if (SkFontHost::ResolveTypeface(fRec.fFontID) == NULL) {
if (!SkFontHost::ValidFontID(fRec.fFontID)) {
return (FT_Error)-1;
}

View File

@ -127,7 +127,10 @@ static FamilyRec* find_family(const SkTypeface* member)
return NULL;
}
static SkTypeface* resolve_uniqueID(uint32_t uniqueID)
/* Returns the matching typeface, or NULL. If a typeface is found, its refcnt
is not modified.
*/
static SkTypeface* find_from_uniqueID(uint32_t uniqueID)
{
FamilyRec* curr = gFamilyHead;
while (curr != NULL) {
@ -275,7 +278,6 @@ public:
bool isSysFont() const { return fIsSysFont; }
virtual SkStream* openStream() = 0;
virtual void closeStream(SkStream*) = 0;
virtual const char* getUniqueString() const = 0;
private:
@ -292,16 +294,21 @@ public:
SkStream* stream)
: INHERITED(style, sysFont, familyMember)
{
SkASSERT(stream);
stream->ref();
fStream = stream;
}
virtual ~StreamTypeface()
{
SkDELETE(fStream);
virtual ~StreamTypeface() {
fStream->unref();
}
// overrides
virtual SkStream* openStream() { return fStream; }
virtual void closeStream(SkStream*) {}
virtual SkStream* openStream() {
// we just ref our existing stream, since the caller will call unref()
// when they are through
fStream->ref();
return fStream;
}
virtual const char* getUniqueString() const { return NULL; }
private:
@ -342,10 +349,6 @@ public:
}
return stream;
}
virtual void closeStream(SkStream* stream)
{
SkDELETE(stream);
}
virtual const char* getUniqueString() const {
const char* str = strrchr(fPath.c_str(), '/');
if (str) {
@ -495,7 +498,7 @@ static void load_system_fonts()
void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
const char* name = ((FamilyTypeface*)face)->getUniqueString();
stream->write8((uint8_t)face->getStyle());
stream->write8((uint8_t)face->style());
if (NULL == name || 0 == *name) {
stream->writePackedUInt(0);
@ -504,7 +507,7 @@ void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
uint32_t len = strlen(name);
stream->writePackedUInt(len);
stream->write(name, len);
// SkDebugf("--- fonthost serialize <%s> %d\n", name, face->getStyle());
// SkDebugf("--- fonthost serialize <%s> %d\n", name, face->style());
}
}
@ -525,21 +528,21 @@ SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
// backup until we hit the fNames
for (int j = i; j >= 0; --j) {
if (rec[j].fNames != NULL) {
return SkFontHost::FindTypeface(NULL, rec[j].fNames[0],
(SkTypeface::Style)style);
return SkFontHost::CreateTypeface(NULL,
rec[j].fNames[0], (SkTypeface::Style)style);
}
}
}
}
}
return SkFontHost::FindTypeface(NULL, NULL, (SkTypeface::Style)style);
return SkFontHost::CreateTypeface(NULL, NULL, (SkTypeface::Style)style);
}
///////////////////////////////////////////////////////////////////////////////
SkTypeface* SkFontHost::FindTypeface(const SkTypeface* familyFace,
const char familyName[],
SkTypeface::Style style)
SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
const char familyName[],
SkTypeface::Style style)
{
load_system_fonts();
@ -564,34 +567,25 @@ SkTypeface* SkFontHost::FindTypeface(const SkTypeface* familyFace,
return tf;
}
SkTypeface* SkFontHost::ResolveTypeface(uint32_t fontID)
bool SkFontHost::ValidFontID(uint32_t fontID)
{
SkAutoMutexAcquire ac(gFamilyMutex);
return resolve_uniqueID(fontID);
return find_from_uniqueID(fontID) != NULL;
}
SkStream* SkFontHost::OpenStream(uint32_t fontID)
{
FamilyTypeface* tf = (FamilyTypeface*)SkFontHost::ResolveTypeface(fontID);
FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID);
SkStream* stream = tf ? tf->openStream() : NULL;
if (NULL == stream || stream->getLength() == 0) {
delete stream;
if (stream && stream->getLength() == 0) {
stream->unref();
stream = NULL;
}
return stream;
}
void SkFontHost::CloseStream(uint32_t fontID, SkStream* stream)
{
FamilyTypeface* tf = (FamilyTypeface*)SkFontHost::ResolveTypeface(fontID);
if (NULL != tf) {
tf->closeStream(stream);
}
}
SkScalerContext* SkFontHost::CreateFallbackScalerContext(
const SkScalerContext::Rec& rec)
{
@ -612,10 +606,9 @@ SkScalerContext* SkFontHost::CreateFallbackScalerContext(
///////////////////////////////////////////////////////////////////////////////
SkTypeface* SkFontHost::CreateTypeface(SkStream* stream)
SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream)
{
if (NULL == stream || stream->getLength() <= 0) {
SkDELETE(stream);
return NULL;
}
@ -627,7 +620,11 @@ SkTypeface* SkFontHost::CreateTypeface(SkStream* stream)
SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[])
{
return SkFontHost::CreateTypeface(SkNEW_ARGS(SkMMAPStream, (path)));
SkStream* stream = SkNEW_ARGS(SkMMAPStream, (path));
SkTypeface* face = SkFontHost::CreateTypefaceFromStream(stream);
// since we created the stream, we let go of our ref() here
stream->unref();
return face;
}
///////////////////////////////////////////////////////////////////////////////

View File

@ -125,18 +125,18 @@ static FamilyRec* find_family(const SkTypeface* member) {
return NULL;
}
static SkTypeface* resolve_uniqueID(uint32_t uniqueID) {
static bool valid_uniqueID(uint32_t uniqueID) {
FamilyRec* curr = gFamilyHead;
while (curr != NULL) {
for (int i = 0; i < 4; i++) {
SkTypeface* face = curr->fFaces[i];
if (face != NULL && face->uniqueID() == uniqueID) {
return face;
return true;
}
}
curr = curr->fNext;
}
return NULL;
return false;
}
/* Remove reference to this face from its family. If the resulting family
@ -476,14 +476,14 @@ SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
// backup until we hit the fNames
for (int j = i; j >= 0; --j) {
if (rec[j].fNames != NULL) {
return SkFontHost::FindTypeface(NULL, rec[j].fNames[0],
(SkTypeface::Style)style);
return SkFontHost::CreateTypeface(NULL, rec[j].fNames[0],
(SkTypeface::Style)style);
}
}
}
}
}
return SkFontHost::FindTypeface(NULL, NULL, (SkTypeface::Style)style);
return SkFontHost::CreateTypeface(NULL, NULL, (SkTypeface::Style)style);
#endif
sk_throw();
return NULL;
@ -491,9 +491,9 @@ SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
///////////////////////////////////////////////////////////////////////////////
SkTypeface* SkFontHost::FindTypeface(const SkTypeface* familyFace,
const char familyName[],
SkTypeface::Style style) {
SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
const char familyName[],
SkTypeface::Style style) {
load_system_fonts();
SkAutoMutexAcquire ac(gFamilyMutex);
@ -517,10 +517,10 @@ SkTypeface* SkFontHost::FindTypeface(const SkTypeface* familyFace,
return tf;
}
SkTypeface* SkFontHost::ResolveTypeface(uint32_t fontID) {
SkTypeface* SkFontHost::ValidFontID(uint32_t fontID) {
SkAutoMutexAcquire ac(gFamilyMutex);
return resolve_uniqueID(fontID);
return valid_uniqueID(fontID);
}
SkStream* SkFontHost::OpenStream(uint32_t fontID) {
@ -560,7 +560,7 @@ SkScalerContext* SkFontHost::CreateFallbackScalerContext(
///////////////////////////////////////////////////////////////////////////////
SkTypeface* SkFontHost::CreateTypeface(SkStream* stream) {
SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
if (NULL == stream || stream->getLength() <= 0) {
SkDELETE(stream);
return NULL;

View File

@ -484,7 +484,7 @@ SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
return NULL;
}
SkTypeface* SkFontHost::CreateTypeface(SkStream* stream) {
SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
//Should not be used on Mac, keep linker happy
SkASSERT(false);
@ -517,17 +517,8 @@ SkScalerContext* SkFontHost::CreateFallbackScalerContext(const SkScalerContext::
return SkFontHost::CreateScalerContext(desc);
}
/** Return the closest matching typeface given either an existing family
(specified by a typeface in that family) or by a familyName, and a
requested style.
1) If familyFace is null, use famillyName.
2) If famillyName is null, use familyFace.
3) If both are null, return the default font that best matches style
This MUST not return NULL.
*/
SkTypeface* SkFontHost::FindTypeface(const SkTypeface* familyFace, const char familyName[], SkTypeface::Style style) {
SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
const char familyName[], SkTypeface::Style style) {
SkAutoMutexAcquire ac(gFTMutex);

View File

@ -15,28 +15,14 @@
#include "SkFontHost.h"
SkTypeface* SkFontHost::FindTypeface(const SkTypeface* familyFace,
SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
const char famillyName[],
SkTypeface::Style style) {
SkASSERT(!"SkFontHost::FindTypeface unimplemented");
return NULL;
}
SkTypeface* SkFontHost::ResolveTypeface(uint32_t uniqueID) {
SkASSERT(!"SkFontHost::ResolveTypeface unimplemented");
return NULL;
}
SkStream* SkFontHost::OpenStream(uint32_t uniqueID) {
SkASSERT(!"SkFontHost::OpenStream unimplemented");
return NULL;
}
void SkFontHost::CloseStream(uint32_t uniqueID, SkStream*) {
SkASSERT(!"SkFontHost::CloseStream unimplemented");
}
SkTypeface* SkFontHost::CreateTypeface(SkStream*) {
SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream*) {
SkASSERT(!"SkFontHost::CreateTypeface unimplemented");
return NULL;
}
@ -48,6 +34,18 @@ SkTypeface* SkFontHost::CreateTypefaceFromFile(char const*) {
///////////////////////////////////////////////////////////////////////////////
bool SkFontHost::ValidFontID(uint32_t uniqueID) {
SkASSERT(!"SkFontHost::ResolveTypeface unimplemented");
return false;
}
SkStream* SkFontHost::OpenStream(uint32_t uniqueID) {
SkASSERT(!"SkFontHost::OpenStream unimplemented");
return NULL;
}
///////////////////////////////////////////////////////////////////////////////
void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
SkASSERT(!"SkFontHost::Serialize unimplemented");
}

View File

@ -482,7 +482,7 @@ SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
return NULL;
}
SkTypeface* SkFontHost::CreateTypeface(SkStream* stream) {
SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
//Should not be used on Windows, keep linker happy
SkASSERT(false);
@ -521,7 +521,8 @@ SkScalerContext* SkFontHost::CreateFallbackScalerContext(const SkScalerContext::
This MUST not return NULL.
*/
SkTypeface* SkFontHost::FindTypeface(const SkTypeface* familyFace, const char familyName[], SkTypeface::Style style) {
SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
const char familyName[], SkTypeface::Style style) {
SkAutoMutexAcquire ac(gFTMutex);