struct NameFamilyPair {
const char* fName; // we own this
FamilyRec* fFamily; // we don't own this, we just reference it
-
+
void construct(const char name[], FamilyRec* family)
{
fName = strdup(name);
struct FamilyRec {
FamilyRec* fNext;
SkTypeface* fFaces[4];
-
+
FamilyRec()
{
fNext = gFamilyHead;
static SkTypeface* find_best_face(const FamilyRec* family,
SkTypeface::Style style) {
SkTypeface* const* faces = family->fFaces;
-
+
if (faces[style] != NULL) { // exact match
return faces[style];
}
FamilyRec* family = find_family(face);
SkASSERT(family->fFaces[face->style()] == face);
family->fFaces[face->style()] = NULL;
-
+
for (int i = 0; i < 4; i++) {
if (family->fFaces[i] != NULL) { // family is non-empty
return NULL;
static void detach_and_delete_family(FamilyRec* family) {
FamilyRec* curr = gFamilyHead;
FamilyRec* prev = NULL;
-
+
while (curr != NULL) {
FamilyRec* next = curr->fNext;
if (curr == family) {
}
static FamilyRec* find_familyrec(const char name[]) {
- const NameFamilyPair* list = gNameList.begin();
+ const NameFamilyPair* list = gNameList.begin();
int index = SkStrLCSearch(&list[0].fName, gNameList.count(), name,
sizeof(list[0]));
return index >= 0 ? list[index].fFamily : NULL;
static void add_name(const char name[], FamilyRec* family) {
SkAutoAsciiToLC tolc(name);
name = tolc.lc();
-
+
NameFamilyPair* list = gNameList.begin();
int count = gNameList.count();
-
+
int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0]));
-
+
if (index < 0) {
list = gNameList.insert(~index);
list->construct(name, family);
SkASSERT(emptyFamily->fFaces[i] == NULL);
}
#endif
-
+
SkTDArray<NameFamilyPair>& list = gNameList;
-
+
// must go backwards when removing
for (int i = list.count() - 1; i >= 0; --i) {
NameFamilyPair* pair = &list[i];
FamilyTypeface(Style style, bool sysFont, FamilyRec* family, bool isFixedWidth)
: SkTypeface(style, sk_atomic_inc(&gUniqueFontID) + 1, isFixedWidth) {
fIsSysFont = sysFont;
-
+
SkAutoMutexAcquire ac(gFamilyMutex);
-
+
if (NULL == family) {
family = SkNEW(FamilyRec);
}
family->fFaces[style] = this;
fFamilyRec = family; // just record it so we can return it if asked
}
-
+
virtual ~FamilyTypeface() {
SkAutoMutexAcquire ac(gFamilyMutex);
-
+
// remove us from our family. If the family is now empty, we return
// that and then remove that family from the name list
FamilyRec* family = remove_from_family(this);
detach_and_delete_family(family);
}
}
-
+
bool isSysFont() const { return fIsSysFont; }
FamilyRec* getFamily() const { return fFamilyRec; }
// openStream returns a SkStream that has been ref-ed
virtual SkStream* openStream() = 0;
virtual const char* getUniqueString() const = 0;
-
+
private:
FamilyRec* fFamilyRec; // we don't own this, just point to it
bool fIsSysFont;
-
+
typedef SkTypeface INHERITED;
};
class EmptyTypeface : public FamilyTypeface {
public:
EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {}
-
+
// overrides
virtual SkStream* openStream() { return NULL; }
virtual const char* getUniqueString() const { return NULL; }
-
+
private:
typedef FamilyTypeface INHERITED;
};
virtual ~StreamTypeface() {
fStream->unref();
}
-
+
// overrides
- virtual SkStream* openStream()
- {
+ virtual SkStream* openStream()
+ {
// openStream returns a refed stream.
fStream->ref();
return fStream;
}
virtual const char* getUniqueString() const { return NULL; }
-
+
private:
SkStream* fStream;
-
+
typedef FamilyTypeface INHERITED;
};
: INHERITED(style, sysFont, family, isFixedWidth) {
fPath.set(path);
}
-
+
// overrides
virtual SkStream* openStream()
{
SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str()));
-
+
// check for failure
if (stream->getLength() <= 0) {
SkDELETE(stream);
}
return str;
}
-
+
private:
SkString fPath;
-
+
typedef FamilyTypeface INHERITED;
};
///////////////////////////////////////////////////////////////////////////////
static bool get_name_and_style(const char path[], SkString* name,
- SkTypeface::Style* style, bool* isFixedWidth) {
+ SkTypeface::Style* style, bool* isFixedWidth) {
SkMMAPStream stream(path);
if (stream.getLength() > 0) {
return find_name_and_attributes(&stream, name, style, isFixedWidth);
return find_name_and_attributes(&stream, name, style, isFixedWidth);
}
}
-
+
SkDebugf("---- failed to open <%s> as a font\n", path);
return false;
}
bool isFixedWidth;
SkString realname;
SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
-
+
if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedWidth)) {
SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
continue;
}
// SkDebugf("font: <%s> %d <%s>\n", realname.c_str(), style, filename.c_str());
-
+
FamilyRec* family = find_familyrec(realname.c_str());
if (family && family->fFaces[style]) {
// SkDebugf("---- skipping duplicate typeface %s style %d\n",
if (NULL == gDefaultNormal) {
sk_throw();
}
- gFallBackTypeface = gDefaultNormal;
+ gFallBackTypeface = gDefaultNormal;
gDefaultFamily = find_family(gDefaultNormal);
// SkDebugf("---- default %p head %p family %p\n", gDefaultNormal, gFamilyHead, gDefaultFamily);
}
SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
- {
- SkAutoMutexAcquire ac(gFamilyMutex);
- load_system_fonts();
- }
+ load_system_fonts();
SkFontDescriptor descriptor(stream);
const char* familyName = descriptor.getFamilyName();
const char familyName[],
SkTypeface::Style style) {
load_system_fonts();
-
+
SkAutoMutexAcquire ac(gFamilyMutex);
-
+
// clip to legal style bits
style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic);
-
+
SkTypeface* tf = NULL;
-
+
if (NULL != familyFace) {
tf = find_typeface(familyFace, style);
} else if (NULL != familyName) {
// SkDebugf("======= familyName <%s>\n", familyName);
tf = find_typeface(familyName, style);
}
-
+
if (NULL == tf) {
tf = find_best_face(gDefaultFamily, style);
}
-
- SkSafeRef(tf);
+
+ SkSafeRef(tf);
return tf;
}
SkStream* SkFontHost::OpenStream(uint32_t fontID) {
FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID);
SkStream* stream = tf ? tf->openStream() : NULL;
-
+
if (stream && stream->getLength() == 0) {
stream->unref();
stream = NULL;