2011-07-28 14:26:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2006 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkFontHost.h"
|
2012-05-31 19:55:08 +00:00
|
|
|
#include "SkFontDescriptor.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkDescriptor.h"
|
2013-03-18 20:53:49 +00:00
|
|
|
#include "SkMMapStream.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkThread.h"
|
|
|
|
#include "SkTSearch.h"
|
|
|
|
|
|
|
|
#ifndef SK_FONT_FILE_PREFIX
|
2012-09-26 19:16:54 +00:00
|
|
|
#define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/"
|
|
|
|
#endif
|
|
|
|
#ifndef SK_FONT_FILE_DIR_SEPERATOR
|
|
|
|
#define SK_FONT_FILE_DIR_SEPERATOR "/"
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|
|
|
|
|
2012-02-15 21:03:45 +00:00
|
|
|
bool find_name_and_attributes(SkStream* stream, SkString* name,
|
|
|
|
SkTypeface::Style* style, bool* isFixedWidth);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
struct FamilyRec;
|
|
|
|
|
|
|
|
/* This guy holds a mapping of a name -> family, used for looking up fonts.
|
|
|
|
Since it is stored in a stretchy array that doesn't preserve object
|
|
|
|
semantics, we don't use constructor/destructors, but just have explicit
|
|
|
|
helpers to manage our internal bookkeeping.
|
|
|
|
*/
|
|
|
|
struct NameFamilyPair {
|
|
|
|
const char* fName; // we own this
|
|
|
|
FamilyRec* fFamily; // we don't own this, we just reference it
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
void construct(const char name[], FamilyRec* family)
|
|
|
|
{
|
|
|
|
fName = strdup(name);
|
|
|
|
fFamily = family; // we don't own this, so just record the referene
|
|
|
|
}
|
|
|
|
void destruct()
|
|
|
|
{
|
|
|
|
free((char*)fName);
|
|
|
|
// we don't own family, so just ignore our reference
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// we use atomic_inc to grow this for each typeface we create
|
|
|
|
static int32_t gUniqueFontID;
|
|
|
|
|
|
|
|
// this is the mutex that protects these globals
|
2012-01-26 21:26:40 +00:00
|
|
|
SK_DECLARE_STATIC_MUTEX(gFamilyMutex);
|
2008-12-17 15:59:43 +00:00
|
|
|
static FamilyRec* gFamilyHead;
|
|
|
|
static SkTDArray<NameFamilyPair> gNameList;
|
|
|
|
|
|
|
|
struct FamilyRec {
|
|
|
|
FamilyRec* fNext;
|
|
|
|
SkTypeface* fFaces[4];
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
FamilyRec()
|
|
|
|
{
|
|
|
|
fNext = gFamilyHead;
|
|
|
|
memset(fFaces, 0, sizeof(fFaces));
|
|
|
|
gFamilyHead = this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static SkTypeface* find_best_face(const FamilyRec* family,
|
2009-02-20 14:22:36 +00:00
|
|
|
SkTypeface::Style style) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkTypeface* const* faces = family->fFaces;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (faces[style] != NULL) { // exact match
|
|
|
|
return faces[style];
|
|
|
|
}
|
|
|
|
// look for a matching bold
|
|
|
|
style = (SkTypeface::Style)(style ^ SkTypeface::kItalic);
|
|
|
|
if (faces[style] != NULL) {
|
|
|
|
return faces[style];
|
|
|
|
}
|
|
|
|
// look for the plain
|
|
|
|
if (faces[SkTypeface::kNormal] != NULL) {
|
|
|
|
return faces[SkTypeface::kNormal];
|
|
|
|
}
|
|
|
|
// look for anything
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (faces[i] != NULL) {
|
|
|
|
return faces[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// should never get here, since the faces list should not be empty
|
2011-12-28 14:59:50 +00:00
|
|
|
SkDEBUGFAIL("faces list is empty");
|
2008-12-17 15:59:43 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-20 14:22:36 +00:00
|
|
|
static FamilyRec* find_family(const SkTypeface* member) {
|
2008-12-17 15:59:43 +00:00
|
|
|
FamilyRec* curr = gFamilyHead;
|
|
|
|
while (curr != NULL) {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (curr->fFaces[i] == member) {
|
|
|
|
return curr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curr = curr->fNext;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-28 16:12:48 +00:00
|
|
|
static SkTypeface* find_from_uniqueID(uint32_t uniqueID) {
|
2008-12-17 15:59:43 +00:00
|
|
|
FamilyRec* curr = gFamilyHead;
|
|
|
|
while (curr != NULL) {
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
SkTypeface* face = curr->fFaces[i];
|
|
|
|
if (face != NULL && face->uniqueID() == uniqueID) {
|
2009-09-28 16:12:48 +00:00
|
|
|
return face;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
curr = curr->fNext;
|
|
|
|
}
|
2011-08-26 14:40:38 +00:00
|
|
|
return NULL;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove reference to this face from its family. If the resulting family
|
|
|
|
is empty (has no faces), return that family, otherwise return NULL
|
|
|
|
*/
|
2009-02-20 14:22:36 +00:00
|
|
|
static FamilyRec* remove_from_family(const SkTypeface* face) {
|
2008-12-17 15:59:43 +00:00
|
|
|
FamilyRec* family = find_family(face);
|
|
|
|
SkASSERT(family->fFaces[face->style()] == face);
|
|
|
|
family->fFaces[face->style()] = NULL;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if (family->fFaces[i] != NULL) { // family is non-empty
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return family; // return the empty family
|
|
|
|
}
|
|
|
|
|
|
|
|
// maybe we should make FamilyRec be doubly-linked
|
2009-02-20 14:22:36 +00:00
|
|
|
static void detach_and_delete_family(FamilyRec* family) {
|
2008-12-17 15:59:43 +00:00
|
|
|
FamilyRec* curr = gFamilyHead;
|
|
|
|
FamilyRec* prev = NULL;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
while (curr != NULL) {
|
|
|
|
FamilyRec* next = curr->fNext;
|
|
|
|
if (curr == family) {
|
|
|
|
if (prev == NULL) {
|
|
|
|
gFamilyHead = next;
|
|
|
|
} else {
|
|
|
|
prev->fNext = next;
|
|
|
|
}
|
|
|
|
SkDELETE(family);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
prev = curr;
|
|
|
|
curr = next;
|
|
|
|
}
|
2011-12-28 14:59:50 +00:00
|
|
|
SkDEBUGFAIL("Yikes, couldn't find family in our list to remove/delete");
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2012-05-31 19:55:08 +00:00
|
|
|
static const char* find_family_name(const SkTypeface* familyMember) {
|
|
|
|
const FamilyRec* familyRec = find_family(familyMember);
|
|
|
|
for (int i = 0; i < gNameList.count(); i++) {
|
|
|
|
if (gNameList[i].fFamily == familyRec) {
|
|
|
|
return gNameList[i].fName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
static FamilyRec* find_familyrec(const char name[]) {
|
2012-07-30 12:59:12 +00:00
|
|
|
const NameFamilyPair* list = gNameList.begin();
|
2008-12-17 15:59:43 +00:00
|
|
|
int index = SkStrLCSearch(&list[0].fName, gNameList.count(), name,
|
|
|
|
sizeof(list[0]));
|
|
|
|
return index >= 0 ? list[index].fFamily : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkTypeface* find_typeface(const char name[], SkTypeface::Style style) {
|
|
|
|
FamilyRec* rec = find_familyrec(name);
|
|
|
|
return rec ? find_best_face(rec, style) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkTypeface* find_typeface(const SkTypeface* familyMember,
|
2009-02-20 14:22:36 +00:00
|
|
|
SkTypeface::Style style) {
|
2008-12-17 15:59:43 +00:00
|
|
|
const FamilyRec* family = find_family(familyMember);
|
|
|
|
return family ? find_best_face(family, style) : NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-20 14:22:36 +00:00
|
|
|
static void add_name(const char name[], FamilyRec* family) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkAutoAsciiToLC tolc(name);
|
|
|
|
name = tolc.lc();
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
NameFamilyPair* list = gNameList.begin();
|
|
|
|
int count = gNameList.count();
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
int index = SkStrLCSearch(&list[0].fName, count, name, sizeof(list[0]));
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (index < 0) {
|
|
|
|
list = gNameList.insert(~index);
|
|
|
|
list->construct(name, family);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-20 14:22:36 +00:00
|
|
|
static void remove_from_names(FamilyRec* emptyFamily) {
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifdef SK_DEBUG
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
SkASSERT(emptyFamily->fFaces[i] == NULL);
|
|
|
|
}
|
|
|
|
#endif
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkTDArray<NameFamilyPair>& list = gNameList;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// must go backwards when removing
|
|
|
|
for (int i = list.count() - 1; i >= 0; --i) {
|
|
|
|
NameFamilyPair* pair = &list[i];
|
|
|
|
if (pair->fFamily == emptyFamily) {
|
|
|
|
pair->destruct();
|
|
|
|
list.remove(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class FamilyTypeface : public SkTypeface {
|
|
|
|
public:
|
2011-02-23 14:49:33 +00:00
|
|
|
FamilyTypeface(Style style, bool sysFont, FamilyRec* family, bool isFixedWidth)
|
|
|
|
: SkTypeface(style, sk_atomic_inc(&gUniqueFontID) + 1, isFixedWidth) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fIsSysFont = sysFont;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkAutoMutexAcquire ac(gFamilyMutex);
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (NULL == family) {
|
|
|
|
family = SkNEW(FamilyRec);
|
|
|
|
}
|
|
|
|
family->fFaces[style] = this;
|
|
|
|
fFamilyRec = family; // just record it so we can return it if asked
|
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2009-02-20 14:22:36 +00:00
|
|
|
virtual ~FamilyTypeface() {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkAutoMutexAcquire ac(gFamilyMutex);
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// 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);
|
|
|
|
if (NULL != family) {
|
|
|
|
remove_from_names(family);
|
|
|
|
detach_and_delete_family(family);
|
|
|
|
}
|
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
bool isSysFont() const { return fIsSysFont; }
|
|
|
|
FamilyRec* getFamily() const { return fFamilyRec; }
|
2009-05-18 00:43:58 +00:00
|
|
|
// openStream returns a SkStream that has been ref-ed
|
2008-12-17 15:59:43 +00:00
|
|
|
virtual SkStream* openStream() = 0;
|
|
|
|
virtual const char* getUniqueString() const = 0;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
FamilyRec* fFamilyRec; // we don't own this, just point to it
|
|
|
|
bool fIsSysFont;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
typedef SkTypeface INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2010-04-16 12:40:08 +00:00
|
|
|
/* This subclass is just a place holder for when we have no fonts available.
|
|
|
|
It exists so that our globals (e.g. gFamilyHead) that expect *something*
|
|
|
|
will not be null.
|
|
|
|
*/
|
|
|
|
class EmptyTypeface : public FamilyTypeface {
|
|
|
|
public:
|
2011-02-23 14:49:33 +00:00
|
|
|
EmptyTypeface() : INHERITED(SkTypeface::kNormal, true, NULL, false) {}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2010-04-16 12:40:08 +00:00
|
|
|
// overrides
|
2013-03-18 20:53:49 +00:00
|
|
|
virtual SkStream* openStream() { return NULL; }
|
|
|
|
virtual const char* getUniqueString() const { return NULL; }
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2010-04-16 12:40:08 +00:00
|
|
|
private:
|
|
|
|
typedef FamilyTypeface INHERITED;
|
|
|
|
};
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
class StreamTypeface : public FamilyTypeface {
|
|
|
|
public:
|
|
|
|
StreamTypeface(Style style, bool sysFont, FamilyRec* family,
|
2011-02-23 14:49:33 +00:00
|
|
|
SkStream* stream, bool isFixedWidth)
|
|
|
|
: INHERITED(style, sysFont, family, isFixedWidth) {
|
2010-04-12 20:16:49 +00:00
|
|
|
stream->ref();
|
2008-12-17 15:59:43 +00:00
|
|
|
fStream = stream;
|
|
|
|
}
|
2009-02-20 14:22:36 +00:00
|
|
|
virtual ~StreamTypeface() {
|
2010-04-12 20:16:49 +00:00
|
|
|
fStream->unref();
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2013-03-18 20:53:49 +00:00
|
|
|
// overrides
|
|
|
|
virtual SkStream* openStream()
|
|
|
|
{
|
2009-05-18 00:43:58 +00:00
|
|
|
// openStream returns a refed stream.
|
|
|
|
fStream->ref();
|
|
|
|
return fStream;
|
|
|
|
}
|
2013-03-18 20:53:49 +00:00
|
|
|
virtual const char* getUniqueString() const { return NULL; }
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkStream* fStream;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
typedef FamilyTypeface INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
class FileTypeface : public FamilyTypeface {
|
|
|
|
public:
|
|
|
|
FileTypeface(Style style, bool sysFont, FamilyRec* family,
|
2011-02-23 14:49:33 +00:00
|
|
|
const char path[], bool isFixedWidth)
|
|
|
|
: INHERITED(style, sysFont, family, isFixedWidth) {
|
2008-12-17 15:59:43 +00:00
|
|
|
fPath.set(path);
|
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2013-03-18 20:53:49 +00:00
|
|
|
// overrides
|
|
|
|
virtual SkStream* openStream()
|
|
|
|
{
|
|
|
|
SkStream* stream = SkNEW_ARGS(SkMMAPStream, (fPath.c_str()));
|
|
|
|
|
|
|
|
// check for failure
|
|
|
|
if (stream->getLength() <= 0) {
|
|
|
|
SkDELETE(stream);
|
|
|
|
// maybe MMAP isn't supported. try FILE
|
|
|
|
stream = SkNEW_ARGS(SkFILEStream, (fPath.c_str()));
|
|
|
|
if (stream->getLength() <= 0) {
|
|
|
|
SkDELETE(stream);
|
|
|
|
stream = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return stream;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2010-04-16 12:51:29 +00:00
|
|
|
|
2013-03-18 20:53:49 +00:00
|
|
|
virtual const char* getUniqueString() const {
|
2008-12-17 15:59:43 +00:00
|
|
|
const char* str = strrchr(fPath.c_str(), '/');
|
|
|
|
if (str) {
|
|
|
|
str += 1; // skip the '/'
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkString fPath;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
typedef FamilyTypeface INHERITED;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
static bool get_name_and_style(const char path[], SkString* name,
|
2012-07-30 12:59:12 +00:00
|
|
|
SkTypeface::Style* style, bool* isFixedWidth) {
|
2013-03-18 20:53:49 +00:00
|
|
|
SkMMAPStream stream(path);
|
|
|
|
if (stream.getLength() > 0) {
|
|
|
|
return find_name_and_attributes(&stream, name, style, isFixedWidth);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SkFILEStream stream(path);
|
|
|
|
if (stream.getLength() > 0) {
|
|
|
|
return find_name_and_attributes(&stream, name, style, isFixedWidth);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2013-03-18 20:53:49 +00:00
|
|
|
|
|
|
|
SkDebugf("---- failed to open <%s> as a font\n", path);
|
|
|
|
return false;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// these globals are assigned (once) by load_system_fonts()
|
|
|
|
static SkTypeface* gFallBackTypeface;
|
|
|
|
static FamilyRec* gDefaultFamily;
|
|
|
|
static SkTypeface* gDefaultNormal;
|
|
|
|
|
2012-09-26 19:16:54 +00:00
|
|
|
static void load_directory_fonts(const SkString& directory, unsigned int* count) {
|
|
|
|
SkOSFile::Iter iter(directory.c_str(), ".ttf");
|
2010-04-16 12:40:08 +00:00
|
|
|
SkString name;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
|
|
|
while (iter.next(&name, false)) {
|
2012-09-26 19:16:54 +00:00
|
|
|
SkString filename(directory);
|
|
|
|
filename.append(name);
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2011-02-23 14:49:33 +00:00
|
|
|
bool isFixedWidth;
|
2008-12-17 15:59:43 +00:00
|
|
|
SkString realname;
|
2011-01-04 12:52:02 +00:00
|
|
|
SkTypeface::Style style = SkTypeface::kNormal; // avoid uninitialized warning
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2011-02-23 14:49:33 +00:00
|
|
|
if (!get_name_and_style(filename.c_str(), &realname, &style, &isFixedWidth)) {
|
2008-12-17 15:59:43 +00:00
|
|
|
SkDebugf("------ can't load <%s> as a font\n", filename.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-28 16:12:48 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
FamilyRec* family = find_familyrec(realname.c_str());
|
2010-04-15 14:04:52 +00:00
|
|
|
if (family && family->fFaces[style]) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// this constructor puts us into the global gFamilyHead llist
|
|
|
|
FamilyTypeface* tf = SkNEW_ARGS(FileTypeface,
|
|
|
|
(style,
|
|
|
|
true, // system-font (cannot delete)
|
|
|
|
family, // what family to join
|
2011-02-23 14:49:33 +00:00
|
|
|
filename.c_str(),
|
|
|
|
isFixedWidth) // filename
|
2008-12-17 15:59:43 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (NULL == family) {
|
|
|
|
add_name(realname.c_str(), tf->getFamily());
|
|
|
|
}
|
2012-09-26 19:16:54 +00:00
|
|
|
*count += 1;
|
2010-04-16 12:40:08 +00:00
|
|
|
}
|
|
|
|
|
2012-09-26 19:16:54 +00:00
|
|
|
SkOSFile::Iter dirIter(directory.c_str());
|
|
|
|
while (dirIter.next(&name, true)) {
|
|
|
|
if (name.startsWith(".")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
SkString dirname(directory);
|
|
|
|
dirname.append(name);
|
|
|
|
dirname.append(SK_FONT_FILE_DIR_SEPERATOR);
|
|
|
|
load_directory_fonts(dirname, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void load_system_fonts() {
|
|
|
|
// check if we've already be called
|
|
|
|
if (NULL != gDefaultNormal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkString baseDirectory(SK_FONT_FILE_PREFIX);
|
|
|
|
unsigned int count = 0;
|
|
|
|
load_directory_fonts(baseDirectory, &count);
|
|
|
|
|
2010-04-16 12:40:08 +00:00
|
|
|
if (0 == count) {
|
|
|
|
SkNEW(EmptyTypeface);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2009-09-28 16:12:48 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// do this after all fonts are loaded. This is our default font, and it
|
|
|
|
// acts as a sentinel so we only execute load_system_fonts() once
|
|
|
|
static const char* gDefaultNames[] = {
|
|
|
|
"Arial", "Verdana", "Times New Roman", NULL
|
|
|
|
};
|
|
|
|
const char** names = gDefaultNames;
|
|
|
|
while (*names) {
|
|
|
|
SkTypeface* tf = find_typeface(*names++, SkTypeface::kNormal);
|
|
|
|
if (tf) {
|
|
|
|
gDefaultNormal = tf;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// check if we found *something*
|
|
|
|
if (NULL == gDefaultNormal) {
|
|
|
|
if (NULL == gFamilyHead) {
|
|
|
|
sk_throw();
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 4; i++) {
|
|
|
|
if ((gDefaultNormal = gFamilyHead->fFaces[i]) != NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NULL == gDefaultNormal) {
|
|
|
|
sk_throw();
|
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
gFallBackTypeface = gDefaultNormal;
|
2008-12-17 15:59:43 +00:00
|
|
|
gDefaultFamily = find_family(gDefaultNormal);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void SkFontHost::Serialize(const SkTypeface* face, SkWStream* stream) {
|
2012-05-31 19:55:08 +00:00
|
|
|
|
|
|
|
SkFontDescriptor descriptor;
|
2012-06-01 15:53:06 +00:00
|
|
|
descriptor.setFamilyName(find_family_name(face));
|
|
|
|
descriptor.setStyle(face->style());
|
2012-05-31 19:55:08 +00:00
|
|
|
descriptor.setFontFileName(((FamilyTypeface*)face)->getUniqueString());
|
|
|
|
|
|
|
|
descriptor.serialize(stream);
|
|
|
|
|
|
|
|
const bool isCustomFont = !((FamilyTypeface*)face)->isSysFont();
|
|
|
|
if (isCustomFont) {
|
|
|
|
// store the entire font in the fontData
|
|
|
|
SkStream* fontStream = ((FamilyTypeface*)face)->openStream();
|
|
|
|
const uint32_t length = fontStream->getLength();
|
|
|
|
|
|
|
|
stream->writePackedUInt(length);
|
|
|
|
stream->writeStream(fontStream, length);
|
|
|
|
|
|
|
|
fontStream->unref();
|
2012-03-29 16:09:48 +00:00
|
|
|
} else {
|
2012-05-31 19:55:08 +00:00
|
|
|
stream->writePackedUInt(0);
|
2012-03-29 16:09:48 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SkTypeface* SkFontHost::Deserialize(SkStream* stream) {
|
2012-07-30 12:59:12 +00:00
|
|
|
load_system_fonts();
|
2012-05-31 19:55:08 +00:00
|
|
|
|
|
|
|
SkFontDescriptor descriptor(stream);
|
2012-06-01 15:53:06 +00:00
|
|
|
const char* familyName = descriptor.getFamilyName();
|
|
|
|
const SkTypeface::Style style = descriptor.getStyle();
|
2012-05-31 19:55:08 +00:00
|
|
|
|
|
|
|
const uint32_t customFontDataLength = stream->readPackedUInt();
|
|
|
|
if (customFontDataLength > 0) {
|
|
|
|
|
|
|
|
// generate a new stream to store the custom typeface
|
|
|
|
SkMemoryStream* fontStream = new SkMemoryStream(customFontDataLength - 1);
|
|
|
|
stream->read((void*)fontStream->getMemoryBase(), customFontDataLength - 1);
|
|
|
|
|
|
|
|
SkTypeface* face = CreateTypefaceFromStream(fontStream);
|
|
|
|
|
|
|
|
fontStream->unref();
|
|
|
|
return face;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SkFontHost::CreateTypeface(NULL, familyName, style);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-03-04 17:37:51 +00:00
|
|
|
SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace,
|
|
|
|
const char familyName[],
|
|
|
|
SkTypeface::Style style) {
|
2008-12-17 15:59:43 +00:00
|
|
|
load_system_fonts();
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkAutoMutexAcquire ac(gFamilyMutex);
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
// clip to legal style bits
|
|
|
|
style = (SkTypeface::Style)(style & SkTypeface::kBoldItalic);
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
SkTypeface* tf = NULL;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (NULL != familyFace) {
|
|
|
|
tf = find_typeface(familyFace, style);
|
|
|
|
} else if (NULL != familyName) {
|
|
|
|
// SkDebugf("======= familyName <%s>\n", familyName);
|
|
|
|
tf = find_typeface(familyName, style);
|
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
if (NULL == tf) {
|
|
|
|
tf = find_best_face(gDefaultFamily, style);
|
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
|
|
|
SkSafeRef(tf);
|
2008-12-17 15:59:43 +00:00
|
|
|
return tf;
|
|
|
|
}
|
|
|
|
|
2009-02-20 14:22:36 +00:00
|
|
|
SkStream* SkFontHost::OpenStream(uint32_t fontID) {
|
2009-09-28 16:12:48 +00:00
|
|
|
FamilyTypeface* tf = (FamilyTypeface*)find_from_uniqueID(fontID);
|
2008-12-17 15:59:43 +00:00
|
|
|
SkStream* stream = tf ? tf->openStream() : NULL;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2010-04-12 20:16:49 +00:00
|
|
|
if (stream && stream->getLength() == 0) {
|
|
|
|
stream->unref();
|
2008-12-17 15:59:43 +00:00
|
|
|
stream = NULL;
|
|
|
|
}
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2009-07-31 16:17:01 +00:00
|
|
|
size_t SkFontHost::GetFileName(SkFontID fontID, char path[], size_t length,
|
|
|
|
int32_t* index) {
|
2012-10-11 19:12:03 +00:00
|
|
|
// SkDebugf("SkFontHost::GetFileName unimplemented\n");
|
2009-07-31 16:17:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-06-13 13:01:10 +00:00
|
|
|
SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) {
|
2009-09-28 16:12:48 +00:00
|
|
|
return 0;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2009-03-04 17:37:51 +00:00
|
|
|
SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) {
|
2008-12-17 15:59:43 +00:00
|
|
|
if (NULL == stream || stream->getLength() <= 0) {
|
|
|
|
SkDELETE(stream);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-02-23 14:49:33 +00:00
|
|
|
|
|
|
|
bool isFixedWidth;
|
2012-02-15 21:03:45 +00:00
|
|
|
SkTypeface::Style style;
|
|
|
|
if (find_name_and_attributes(stream, NULL, &style, &isFixedWidth)) {
|
|
|
|
return SkNEW_ARGS(StreamTypeface, (style, false, NULL, stream, isFixedWidth));
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2009-02-20 14:22:36 +00:00
|
|
|
SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) {
|
|
|
|
SkTypeface* face = NULL;
|
|
|
|
SkFILEStream* stream = SkNEW_ARGS(SkFILEStream, (path));
|
|
|
|
|
|
|
|
if (stream->isValid()) {
|
2009-09-28 16:12:48 +00:00
|
|
|
face = CreateTypefaceFromStream(stream);
|
2009-02-20 14:22:36 +00:00
|
|
|
}
|
|
|
|
stream->unref();
|
2009-09-28 16:12:48 +00:00
|
|
|
return face;
|
2009-01-13 13:26:44 +00:00
|
|
|
}
|