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
|
|
|
*/
|
|
|
|
|
2013-03-21 13:33:49 +00:00
|
|
|
#include "SkFontHost_FreeType_common.h"
|
2012-05-31 19:55:08 +00:00
|
|
|
#include "SkFontDescriptor.h"
|
2013-11-11 15:53:29 +00:00
|
|
|
#include "SkFontMgr.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkDescriptor.h"
|
|
|
|
#include "SkOSFile.h"
|
|
|
|
#include "SkPaint.h"
|
2014-11-19 16:32:19 +00:00
|
|
|
#include "SkRTConf.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkString.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkThread.h"
|
|
|
|
#include "SkTSearch.h"
|
2013-11-11 15:53:29 +00:00
|
|
|
#include "SkTypefaceCache.h"
|
|
|
|
#include "SkTArray.h"
|
|
|
|
|
|
|
|
#include <limits>
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
/** The base SkTypeface implementation for the custom font manager. */
|
|
|
|
class SkTypeface_Custom : public SkTypeface_FreeType {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2014-10-20 20:33:19 +00:00
|
|
|
SkTypeface_Custom(const SkFontStyle& style, bool isFixedPitch,
|
2014-09-18 17:55:32 +00:00
|
|
|
bool sysFont, const SkString familyName, int index)
|
2013-11-11 15:53:29 +00:00
|
|
|
: INHERITED(style, SkTypefaceCache::NewFontID(), isFixedPitch)
|
2014-09-18 17:55:32 +00:00
|
|
|
, fIsSysFont(sysFont), fFamilyName(familyName), fIndex(index)
|
2013-11-11 15:53:29 +00:00
|
|
|
{ }
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
bool isSysFont() const { return fIsSysFont; }
|
2013-03-22 17:21:59 +00:00
|
|
|
|
2013-03-25 13:03:37 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
void onGetFamilyName(SkString* familyName) const override {
|
2014-09-17 14:48:59 +00:00
|
|
|
*familyName = fFamilyName;
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocal) const override {
|
2013-11-11 15:53:29 +00:00
|
|
|
desc->setFamilyName(fFamilyName.c_str());
|
2015-05-15 18:30:41 +00:00
|
|
|
desc->setFontIndex(fIndex);
|
2013-11-11 15:53:29 +00:00
|
|
|
*isLocal = !this->isSysFont();
|
|
|
|
}
|
2013-03-25 13:03:37 +00:00
|
|
|
|
2014-09-18 17:55:32 +00:00
|
|
|
int getIndex() const { return fIndex; }
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2014-09-18 17:55:32 +00:00
|
|
|
const bool fIsSysFont;
|
|
|
|
const SkString fFamilyName;
|
|
|
|
const int fIndex;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2013-03-21 13:38:18 +00:00
|
|
|
typedef SkTypeface_FreeType INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
/** The empty SkTypeface implementation for the custom font manager.
|
|
|
|
* Used as the last resort fallback typeface.
|
2010-04-16 12:40:08 +00:00
|
|
|
*/
|
2013-11-11 15:53:29 +00:00
|
|
|
class SkTypeface_Empty : public SkTypeface_Custom {
|
2010-04-16 12:40:08 +00:00
|
|
|
public:
|
2014-10-20 20:33:19 +00:00
|
|
|
SkTypeface_Empty() : INHERITED(SkFontStyle(), false, true, SkString(), 0) {}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2013-03-22 17:21:59 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkStreamAsset* onOpenStream(int*) const override { return NULL; }
|
2013-03-22 17:21:59 +00:00
|
|
|
|
2010-04-16 12:40:08 +00:00
|
|
|
private:
|
2013-11-11 15:53:29 +00:00
|
|
|
typedef SkTypeface_Custom INHERITED;
|
2010-04-16 12:40:08 +00:00
|
|
|
};
|
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
/** The stream SkTypeface implementation for the custom font manager. */
|
|
|
|
class SkTypeface_Stream : public SkTypeface_Custom {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2014-10-20 20:33:19 +00:00
|
|
|
SkTypeface_Stream(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
|
2015-01-27 13:39:10 +00:00
|
|
|
const SkString familyName, SkStreamAsset* stream, int index)
|
2014-10-20 20:33:19 +00:00
|
|
|
: INHERITED(style, isFixedPitch, sysFont, familyName, index)
|
2015-01-21 20:23:20 +00:00
|
|
|
, fStream(stream)
|
2013-11-11 15:53:29 +00:00
|
|
|
{ }
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2013-03-22 17:21:59 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkStreamAsset* onOpenStream(int* ttcIndex) const override {
|
2014-09-18 17:55:32 +00:00
|
|
|
*ttcIndex = this->getIndex();
|
2014-03-24 21:42:01 +00:00
|
|
|
return fStream->duplicate();
|
2013-03-22 17:21:59 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2015-01-27 13:39:10 +00:00
|
|
|
const SkAutoTDelete<const SkStreamAsset> fStream;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
typedef SkTypeface_Custom INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
2014-11-19 16:32:19 +00:00
|
|
|
// This configuration option is useful if we need to open and hold handles to
|
|
|
|
// all found system font data (e.g., for skfiddle, where the application can't
|
|
|
|
// access the filesystem to read fonts on demand)
|
|
|
|
|
|
|
|
SK_CONF_DECLARE(bool, c_CustomTypefaceRetain, "fonts.customFont.retainAllData", false,
|
|
|
|
"Retain the open stream for each found font on the system.");
|
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
/** The file SkTypeface implementation for the custom font manager. */
|
|
|
|
class SkTypeface_File : public SkTypeface_Custom {
|
2008-12-17 15:59:43 +00:00
|
|
|
public:
|
2014-10-20 20:33:19 +00:00
|
|
|
SkTypeface_File(const SkFontStyle& style, bool isFixedPitch, bool sysFont,
|
|
|
|
const SkString familyName, const char path[], int index)
|
2014-09-18 17:55:32 +00:00
|
|
|
: INHERITED(style, isFixedPitch, sysFont, familyName, index)
|
2013-11-11 15:53:29 +00:00
|
|
|
, fPath(path)
|
2014-11-19 16:32:19 +00:00
|
|
|
, fStream(c_CustomTypefaceRetain ? SkStream::NewFromFile(fPath.c_str()) : NULL)
|
2013-11-11 15:53:29 +00:00
|
|
|
{ }
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2013-03-22 17:21:59 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkStreamAsset* onOpenStream(int* ttcIndex) const override {
|
2014-09-18 17:55:32 +00:00
|
|
|
*ttcIndex = this->getIndex();
|
2014-11-19 16:32:19 +00:00
|
|
|
if (fStream.get()) {
|
|
|
|
return fStream->duplicate();
|
|
|
|
} else {
|
|
|
|
return SkStream::NewFromFile(fPath.c_str());
|
|
|
|
}
|
2013-03-22 17:21:59 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
|
|
|
SkString fPath;
|
2015-01-21 20:09:53 +00:00
|
|
|
const SkAutoTDelete<SkStreamAsset> fStream;
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
typedef SkTypeface_Custom INHERITED;
|
2008-12-17 15:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
/**
|
|
|
|
* SkFontStyleSet_Custom
|
|
|
|
*
|
|
|
|
* This class is used by SkFontMgr_Custom to hold SkTypeface_Custom families.
|
|
|
|
*/
|
|
|
|
class SkFontStyleSet_Custom : public SkFontStyleSet {
|
|
|
|
public:
|
|
|
|
explicit SkFontStyleSet_Custom(const SkString familyName) : fFamilyName(familyName) { }
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
/** Should only be called during the inital build phase. */
|
|
|
|
void appendTypeface(SkTypeface_Custom* typeface) {
|
|
|
|
fStyles.push_back().reset(typeface);
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
int count() override {
|
2013-11-11 15:53:29 +00:00
|
|
|
return fStyles.count();
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void getStyle(int index, SkFontStyle* style, SkString* name) override {
|
2013-11-11 15:53:29 +00:00
|
|
|
SkASSERT(index < fStyles.count());
|
|
|
|
bool bold = fStyles[index]->isBold();
|
|
|
|
bool italic = fStyles[index]->isItalic();
|
|
|
|
*style = SkFontStyle(bold ? SkFontStyle::kBold_Weight : SkFontStyle::kNormal_Weight,
|
|
|
|
SkFontStyle::kNormal_Width,
|
|
|
|
italic ? SkFontStyle::kItalic_Slant : SkFontStyle::kUpright_Slant);
|
|
|
|
name->reset();
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkTypeface* createTypeface(int index) override {
|
2013-11-11 15:53:29 +00:00
|
|
|
SkASSERT(index < fStyles.count());
|
|
|
|
return SkRef(fStyles[index].get());
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
static int match_score(const SkFontStyle& pattern, const SkFontStyle& candidate) {
|
|
|
|
int score = 0;
|
|
|
|
score += (pattern.width() - candidate.width()) * 100;
|
|
|
|
score += (pattern.isItalic() == candidate.isItalic()) ? 0 : 1000;
|
|
|
|
score += pattern.weight() - candidate.weight();
|
|
|
|
return score;
|
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkTypeface* matchStyle(const SkFontStyle& pattern) override {
|
2013-11-11 15:53:29 +00:00
|
|
|
if (0 == fStyles.count()) {
|
|
|
|
return NULL;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2009-09-28 16:12:48 +00:00
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
SkTypeface_Custom* closest = fStyles[0];
|
|
|
|
int minScore = std::numeric_limits<int>::max();
|
|
|
|
for (int i = 0; i < fStyles.count(); ++i) {
|
|
|
|
bool bold = fStyles[i]->isBold();
|
|
|
|
bool italic = fStyles[i]->isItalic();
|
|
|
|
SkFontStyle style = SkFontStyle(bold ? SkFontStyle::kBold_Weight
|
|
|
|
: SkFontStyle::kNormal_Weight,
|
|
|
|
SkFontStyle::kNormal_Width,
|
|
|
|
italic ? SkFontStyle::kItalic_Slant
|
|
|
|
: SkFontStyle::kUpright_Slant);
|
|
|
|
|
|
|
|
int score = match_score(pattern, style);
|
|
|
|
if (score < minScore) {
|
|
|
|
closest = fStyles[i];
|
|
|
|
minScore = score;
|
|
|
|
}
|
2010-04-15 14:04:52 +00:00
|
|
|
}
|
2013-11-11 15:53:29 +00:00
|
|
|
return SkRef(closest);
|
|
|
|
}
|
2010-04-15 14:04:52 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
SkString getFamilyName() { return fFamilyName; }
|
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
private:
|
|
|
|
SkTArray<SkAutoTUnref<SkTypeface_Custom>, true> fStyles;
|
|
|
|
SkString fFamilyName;
|
|
|
|
|
|
|
|
friend class SkFontMgr_Custom;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SkFontMgr_Custom
|
|
|
|
*
|
|
|
|
* This class is essentially a collection of SkFontStyleSet_Custom,
|
|
|
|
* one SkFontStyleSet_Custom for each family. This class may be modified
|
|
|
|
* to load fonts from any source by changing the initialization.
|
|
|
|
*/
|
|
|
|
class SkFontMgr_Custom : public SkFontMgr {
|
|
|
|
public:
|
2015-03-30 19:53:48 +00:00
|
|
|
typedef SkTArray<SkAutoTUnref<SkFontStyleSet_Custom>, true> Families;
|
|
|
|
class SystemFontLoader {
|
|
|
|
public:
|
|
|
|
virtual ~SystemFontLoader() { }
|
|
|
|
virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Families*) const = 0;
|
|
|
|
};
|
|
|
|
explicit SkFontMgr_Custom(const SystemFontLoader& loader) {
|
|
|
|
loader.loadSystemFonts(fScanner, &fFamilies);
|
|
|
|
|
|
|
|
// Try to pick a default font.
|
|
|
|
static const char* defaultNames[] = {
|
|
|
|
"Arial", "Verdana", "Times New Roman", "Droid Sans", NULL
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(defaultNames); ++i) {
|
|
|
|
SkFontStyleSet_Custom* set = this->onMatchFamily(defaultNames[i]);
|
|
|
|
if (NULL == set) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTypeface* tf = set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
|
|
|
|
SkFontStyle::kNormal_Width,
|
|
|
|
SkFontStyle::kUpright_Slant));
|
|
|
|
if (NULL == tf) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
fDefaultFamily = set;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (NULL == fDefaultFamily) {
|
|
|
|
fDefaultFamily = fFamilies[0];
|
|
|
|
}
|
2012-09-26 19:16:54 +00:00
|
|
|
}
|
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
int onCountFamilies() const override {
|
2013-11-11 15:53:29 +00:00
|
|
|
return fFamilies.count();
|
2012-09-26 19:16:54 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onGetFamilyName(int index, SkString* familyName) const override {
|
2013-11-11 15:53:29 +00:00
|
|
|
SkASSERT(index < fFamilies.count());
|
2015-03-30 19:53:48 +00:00
|
|
|
familyName->set(fFamilies[index]->getFamilyName());
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
2012-09-26 19:16:54 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkFontStyleSet_Custom* onCreateStyleSet(int index) const override {
|
2013-11-11 15:53:29 +00:00
|
|
|
SkASSERT(index < fFamilies.count());
|
|
|
|
return SkRef(fFamilies[index].get());
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2009-09-28 16:12:48 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkFontStyleSet_Custom* onMatchFamily(const char familyName[]) const override {
|
2013-11-11 15:53:29 +00:00
|
|
|
for (int i = 0; i < fFamilies.count(); ++i) {
|
2015-03-30 19:53:48 +00:00
|
|
|
if (fFamilies[i]->getFamilyName().equals(familyName)) {
|
2013-11-11 15:53:29 +00:00
|
|
|
return SkRef(fFamilies[i].get());
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2013-11-11 15:53:29 +00:00
|
|
|
return NULL;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2013-11-11 15:53:29 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
SkTypeface* onMatchFamilyStyle(const char familyName[],
|
|
|
|
const SkFontStyle& fontStyle) const override
|
2013-11-11 15:53:29 +00:00
|
|
|
{
|
|
|
|
SkAutoTUnref<SkFontStyleSet> sset(this->matchFamily(familyName));
|
|
|
|
return sset->matchStyle(fontStyle);
|
|
|
|
}
|
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
|
|
|
|
const char* bcp47[], int bcp47Count,
|
|
|
|
SkUnichar character) const override
|
2014-11-14 18:52:53 +00:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
|
|
|
|
const SkFontStyle& fontStyle) const override
|
2013-11-11 15:53:29 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < fFamilies.count(); ++i) {
|
|
|
|
for (int j = 0; j < fFamilies[i]->fStyles.count(); ++j) {
|
|
|
|
if (fFamilies[i]->fStyles[j] == familyMember) {
|
|
|
|
return fFamilies[i]->matchStyle(fontStyle);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-11-11 15:53:29 +00:00
|
|
|
return NULL;
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override {
|
2015-01-21 20:09:53 +00:00
|
|
|
return this->createFromStream(new SkMemoryStream(data), ttcIndex);
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkTypeface* onCreateFromStream(SkStreamAsset* bareStream, int ttcIndex) const override {
|
2015-01-27 13:39:10 +00:00
|
|
|
SkAutoTDelete<SkStreamAsset> stream(bareStream);
|
2013-11-11 15:53:29 +00:00
|
|
|
if (NULL == stream || stream->getLength() <= 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
bool isFixedPitch;
|
2014-10-20 20:33:19 +00:00
|
|
|
SkFontStyle style;
|
2013-11-11 15:53:29 +00:00
|
|
|
SkString name;
|
2015-05-15 18:30:41 +00:00
|
|
|
if (fScanner.scanFont(stream, ttcIndex, &name, &style, &isFixedPitch)) {
|
2014-07-11 15:52:26 +00:00
|
|
|
return SkNEW_ARGS(SkTypeface_Stream, (style, isFixedPitch, false, name,
|
2015-01-27 13:39:10 +00:00
|
|
|
stream.detach(), ttcIndex));
|
2013-11-11 15:53:29 +00:00
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override {
|
2015-01-27 13:39:10 +00:00
|
|
|
SkAutoTDelete<SkStreamAsset> stream(SkStream::NewFromFile(path));
|
2015-01-21 20:09:53 +00:00
|
|
|
return stream.get() ? this->createFromStream(stream.detach(), ttcIndex) : NULL;
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
SkTypeface* onLegacyCreateTypeface(const char familyName[], unsigned styleBits) const override {
|
2013-11-11 15:53:29 +00:00
|
|
|
SkTypeface::Style oldStyle = (SkTypeface::Style)styleBits;
|
|
|
|
SkFontStyle style = SkFontStyle(oldStyle & SkTypeface::kBold
|
|
|
|
? SkFontStyle::kBold_Weight
|
|
|
|
: SkFontStyle::kNormal_Weight,
|
|
|
|
SkFontStyle::kNormal_Width,
|
|
|
|
oldStyle & SkTypeface::kItalic
|
|
|
|
? SkFontStyle::kItalic_Slant
|
|
|
|
: SkFontStyle::kUpright_Slant);
|
|
|
|
SkTypeface* tf = NULL;
|
|
|
|
|
2014-09-05 20:34:00 +00:00
|
|
|
if (familyName) {
|
2013-11-11 15:53:29 +00:00
|
|
|
tf = this->onMatchFamilyStyle(familyName, style);
|
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
if (NULL == tf) {
|
2015-03-30 19:53:48 +00:00
|
|
|
tf = fDefaultFamily->matchStyle(style);
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
return SkSafeRef(tf);
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
private:
|
2015-03-30 19:53:48 +00:00
|
|
|
Families fFamilies;
|
|
|
|
SkFontStyleSet_Custom* fDefaultFamily;
|
|
|
|
SkTypeface_FreeType::Scanner fScanner;
|
|
|
|
};
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class DirectorySystemFontLoader : public SkFontMgr_Custom::SystemFontLoader {
|
|
|
|
public:
|
|
|
|
DirectorySystemFontLoader(const char* dir) : fBaseDirectory(dir) { }
|
|
|
|
|
|
|
|
void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner,
|
|
|
|
SkFontMgr_Custom::Families* families) const override
|
|
|
|
{
|
|
|
|
load_directory_fonts(scanner, fBaseDirectory, ".ttf", families);
|
|
|
|
load_directory_fonts(scanner, fBaseDirectory, ".ttc", families);
|
|
|
|
load_directory_fonts(scanner, fBaseDirectory, ".otf", families);
|
|
|
|
load_directory_fonts(scanner, fBaseDirectory, ".pfb", families);
|
|
|
|
|
|
|
|
if (families->empty()) {
|
|
|
|
SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
|
|
|
|
families->push_back().reset(family);
|
|
|
|
family->appendTypeface(SkNEW(SkTypeface_Empty));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static SkFontStyleSet_Custom* find_family(SkFontMgr_Custom::Families& families,
|
|
|
|
const char familyName[])
|
|
|
|
{
|
|
|
|
for (int i = 0; i < families.count(); ++i) {
|
|
|
|
if (families[i]->getFamilyName().equals(familyName)) {
|
|
|
|
return families[i].get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-11-11 15:53:29 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
static void load_directory_fonts(const SkTypeface_FreeType::Scanner& scanner,
|
|
|
|
const SkString& directory, const char* suffix,
|
|
|
|
SkFontMgr_Custom::Families* families)
|
|
|
|
{
|
2014-10-28 22:07:23 +00:00
|
|
|
SkOSFile::Iter iter(directory.c_str(), suffix);
|
2013-11-11 15:53:29 +00:00
|
|
|
SkString name;
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
while (iter.next(&name, false)) {
|
2014-10-28 22:07:23 +00:00
|
|
|
SkString filename(SkOSPath::Join(directory.c_str(), name.c_str()));
|
2015-01-21 20:09:53 +00:00
|
|
|
SkAutoTDelete<SkStream> stream(SkStream::NewFromFile(filename.c_str()));
|
2014-10-28 22:07:23 +00:00
|
|
|
if (!stream.get()) {
|
|
|
|
SkDebugf("---- failed to open <%s>\n", filename.c_str());
|
2013-11-11 15:53:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
2013-08-02 00:20:34 +00:00
|
|
|
|
2014-10-28 22:07:23 +00:00
|
|
|
int numFaces;
|
2015-03-30 19:53:48 +00:00
|
|
|
if (!scanner.recognizedFont(stream, &numFaces)) {
|
2014-10-28 22:07:23 +00:00
|
|
|
SkDebugf("---- failed to open <%s> as a font\n", filename.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
|
|
|
|
bool isFixedPitch;
|
|
|
|
SkString realname;
|
|
|
|
SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
|
2015-05-15 18:30:41 +00:00
|
|
|
if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch)) {
|
2014-10-28 22:07:23 +00:00
|
|
|
SkDebugf("---- failed to open <%s> <%d> as a font\n",
|
|
|
|
filename.c_str(), faceIndex);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_File, (
|
|
|
|
style,
|
|
|
|
isFixedPitch,
|
|
|
|
true, // system-font (cannot delete)
|
|
|
|
realname,
|
2015-03-30 19:53:48 +00:00
|
|
|
filename.c_str(),
|
|
|
|
faceIndex));
|
2014-10-28 22:07:23 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str());
|
2014-10-28 22:07:23 +00:00
|
|
|
if (NULL == addTo) {
|
|
|
|
addTo = new SkFontStyleSet_Custom(realname);
|
2015-03-30 19:53:48 +00:00
|
|
|
families->push_back().reset(addTo);
|
2014-10-28 22:07:23 +00:00
|
|
|
}
|
|
|
|
addTo->appendTypeface(tf);
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
2013-08-02 00:20:34 +00:00
|
|
|
|
2013-11-11 15:53:29 +00:00
|
|
|
SkOSFile::Iter dirIter(directory.c_str());
|
|
|
|
while (dirIter.next(&name, true)) {
|
|
|
|
if (name.startsWith(".")) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-07-29 02:26:58 +00:00
|
|
|
SkString dirname(SkOSPath::Join(directory.c_str(), name.c_str()));
|
2015-03-30 19:53:48 +00:00
|
|
|
load_directory_fonts(scanner, dirname, suffix, families);
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
2011-02-23 14:49:33 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
SkString fBaseDirectory;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SkEmbeddedResource { const uint8_t* data; size_t size; };
|
|
|
|
struct SkEmbeddedResourceHeader { const SkEmbeddedResource* entries; int count; };
|
|
|
|
|
|
|
|
class EmbeddedSystemFontLoader : public SkFontMgr_Custom::SystemFontLoader {
|
|
|
|
public:
|
|
|
|
EmbeddedSystemFontLoader(const SkEmbeddedResourceHeader* header) : fHeader(header) { }
|
|
|
|
|
|
|
|
void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner,
|
|
|
|
SkFontMgr_Custom::Families* families) const override
|
|
|
|
{
|
|
|
|
for (int i = 0; i < fHeader->count; ++i) {
|
|
|
|
const SkEmbeddedResource& fontEntry = fHeader->entries[i];
|
|
|
|
load_embedded_font(scanner, fontEntry.data, fontEntry.size, i, families);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
if (families->empty()) {
|
2013-11-11 15:53:29 +00:00
|
|
|
SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
|
2015-03-30 19:53:48 +00:00
|
|
|
families->push_back().reset(family);
|
2013-11-11 15:53:29 +00:00
|
|
|
family->appendTypeface(SkNEW(SkTypeface_Empty));
|
|
|
|
}
|
2015-03-30 19:53:48 +00:00
|
|
|
}
|
2013-03-27 20:01:49 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
private:
|
|
|
|
static SkFontStyleSet_Custom* find_family(SkFontMgr_Custom::Families& families,
|
|
|
|
const char familyName[])
|
|
|
|
{
|
|
|
|
for (int i = 0; i < families.count(); ++i) {
|
|
|
|
if (families[i]->getFamilyName().equals(familyName)) {
|
|
|
|
return families[i].get();
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
2015-03-30 19:53:48 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-08-01 15:53:39 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
static void load_embedded_font(const SkTypeface_FreeType::Scanner& scanner,
|
|
|
|
const uint8_t* data, size_t size, int index,
|
|
|
|
SkFontMgr_Custom::Families* families)
|
|
|
|
{
|
|
|
|
SkAutoTDelete<SkMemoryStream> stream(new SkMemoryStream(data, size, false));
|
2013-03-27 20:01:49 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
int numFaces;
|
|
|
|
if (!scanner.recognizedFont(stream, &numFaces)) {
|
|
|
|
SkDebugf("---- failed to open <%d> as a font\n", index);
|
|
|
|
return;
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
2015-03-30 19:53:48 +00:00
|
|
|
|
|
|
|
for (int faceIndex = 0; faceIndex < numFaces; ++faceIndex) {
|
|
|
|
bool isFixedPitch;
|
|
|
|
SkString realname;
|
|
|
|
SkFontStyle style = SkFontStyle(); // avoid uninitialized warning
|
2015-05-15 18:30:41 +00:00
|
|
|
if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch)) {
|
2015-03-30 19:53:48 +00:00
|
|
|
SkDebugf("---- failed to open <%d> <%d> as a font\n", index, faceIndex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkTypeface_Custom* tf = SkNEW_ARGS(SkTypeface_Stream, (
|
|
|
|
style,
|
|
|
|
isFixedPitch,
|
|
|
|
true, // system-font (cannot delete)
|
|
|
|
realname,
|
|
|
|
stream.detach(),
|
|
|
|
faceIndex));
|
|
|
|
|
|
|
|
SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str());
|
|
|
|
if (NULL == addTo) {
|
|
|
|
addTo = new SkFontStyleSet_Custom(realname);
|
|
|
|
families->push_back().reset(addTo);
|
|
|
|
}
|
|
|
|
addTo->appendTypeface(tf);
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
const SkEmbeddedResourceHeader* fHeader;
|
2013-11-11 15:53:29 +00:00
|
|
|
};
|
2013-03-27 20:01:49 +00:00
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
#ifdef SK_EMBEDDED_FONTS
|
|
|
|
|
|
|
|
extern "C" const SkEmbeddedResourceHeader SK_EMBEDDED_FONTS;
|
|
|
|
SkFontMgr* SkFontMgr::Factory() {
|
|
|
|
return new SkFontMgr_Custom(EmbeddedSystemFontLoader(&SK_EMBEDDED_FONTS));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#ifndef SK_FONT_FILE_PREFIX
|
|
|
|
# define SK_FONT_FILE_PREFIX "/usr/share/fonts/"
|
|
|
|
#endif
|
2013-03-27 20:01:49 +00:00
|
|
|
SkFontMgr* SkFontMgr::Factory() {
|
2015-03-30 19:53:48 +00:00
|
|
|
return new SkFontMgr_Custom(DirectorySystemFontLoader(SK_FONT_FILE_PREFIX));
|
2013-03-27 20:01:49 +00:00
|
|
|
}
|
2015-03-30 19:53:48 +00:00
|
|
|
|
|
|
|
#endif
|