/** \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);
-
- /** 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.
+ static SkTypeface* CreateTypeface(const SkTypeface* familyFace,
+ const char famillyName[],
+ SkTypeface::Style style);
+
+ /** 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,
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
be retrieved with Deserialize().
*/
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*);
*/
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.
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);
#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);
+uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
+ if (face) {
+ return face->uniqueID();
}
- return face;
-}
-uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
- return resolve_null_typeface(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[]) {
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;
return SkNEW_ARGS(FontFaceRec_Typeface, (face));
}
-SkTypeface* SkFontHost::CreateTypeface(SkStream* stream) {
+SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
sk_throw(); // not implemented
return NULL;
}
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) {
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));
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();
}
};
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;
}
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) {
bool isSysFont() const { return fIsSysFont; }
virtual SkStream* openStream() = 0;
- virtual void closeStream(SkStream*) = 0;
virtual const char* getUniqueString() const = 0;
private:
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:
}
return stream;
}
- virtual void closeStream(SkStream* stream)
- {
- SkDELETE(stream);
- }
virtual const char* getUniqueString() const {
const char* str = strrchr(fPath.c_str(), '/');
if (str) {
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);
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());
}
}
// 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();
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)
{
///////////////////////////////////////////////////////////////////////////////
-SkTypeface* SkFontHost::CreateTypeface(SkStream* stream)
+SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream)
{
if (NULL == stream || stream->getLength() <= 0) {
- SkDELETE(stream);
return NULL;
}
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;
}
///////////////////////////////////////////////////////////////////////////////
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
// 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;
///////////////////////////////////////////////////////////////////////////////
-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);
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) {
///////////////////////////////////////////////////////////////////////////////
-SkTypeface* SkFontHost::CreateTypeface(SkStream* stream) {
+SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
if (NULL == stream || stream->getLength() <= 0) {
SkDELETE(stream);
return NULL;
return NULL;
}
-SkTypeface* SkFontHost::CreateTypeface(SkStream* stream) {
+SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
//Should not be used on Mac, keep linker happy
SkASSERT(false);
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);
#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");
+SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream*) {
+ SkASSERT(!"SkFontHost::CreateTypeface unimplemented");
return NULL;
}
-SkStream* SkFontHost::OpenStream(uint32_t uniqueID) {
- SkASSERT(!"SkFontHost::OpenStream unimplemented");
+SkTypeface* SkFontHost::CreateTypefaceFromFile(char const*) {
+ SkASSERT(!"SkFontHost::CreateTypefaceFromFile unimplemented");
return NULL;
}
-void SkFontHost::CloseStream(uint32_t uniqueID, SkStream*) {
- SkASSERT(!"SkFontHost::CloseStream unimplemented");
-}
+///////////////////////////////////////////////////////////////////////////////
-SkTypeface* SkFontHost::CreateTypeface(SkStream*) {
- SkASSERT(!"SkFontHost::CreateTypeface unimplemented");
- return NULL;
+bool SkFontHost::ValidFontID(uint32_t uniqueID) {
+ SkASSERT(!"SkFontHost::ResolveTypeface unimplemented");
+ return false;
}
-SkTypeface* SkFontHost::CreateTypefaceFromFile(char const*) {
- SkASSERT(!"SkFontHost::CreateTypefaceFromFile unimplemented");
+SkStream* SkFontHost::OpenStream(uint32_t uniqueID) {
+ SkASSERT(!"SkFontHost::OpenStream unimplemented");
return NULL;
}
return NULL;\r
}\r
\r
-SkTypeface* SkFontHost::CreateTypeface(SkStream* stream) {\r
+SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {\r
\r
//Should not be used on Windows, keep linker happy\r
SkASSERT(false);\r
This MUST not return NULL.\r
*/\r
\r
-SkTypeface* SkFontHost::FindTypeface(const SkTypeface* familyFace, const char familyName[], SkTypeface::Style style) {\r
+SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,\r
+ const char familyName[], SkTypeface::Style style) {\r
\r
SkAutoMutexAcquire ac(gFTMutex);\r
\r