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
|
|
|
*/
|
|
|
|
|
2012-05-31 19:55:08 +00:00
|
|
|
#include "SkFontDescriptor.h"
|
2015-07-29 18:49:40 +00:00
|
|
|
#include "SkFontHost_FreeType_common.h"
|
2013-11-11 15:53:29 +00:00
|
|
|
#include "SkFontMgr.h"
|
2015-06-15 22:17:21 +00:00
|
|
|
#include "SkFontMgr_custom.h"
|
2015-07-29 18:49:40 +00:00
|
|
|
#include "SkFontStyle.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkOSFile.h"
|
2015-07-29 18:49:40 +00:00
|
|
|
#include "SkRefCnt.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkStream.h"
|
2015-07-29 18:49:40 +00:00
|
|
|
#include "SkString.h"
|
2013-11-11 15:53:29 +00:00
|
|
|
#include "SkTArray.h"
|
2015-07-29 18:49:40 +00:00
|
|
|
#include "SkTemplates.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
#include "SkTypefaceCache.h"
|
|
|
|
#include "SkTypes.h"
|
2013-11-11 15:53:29 +00:00
|
|
|
|
|
|
|
#include <limits>
|
2016-06-10 11:14:51 +00:00
|
|
|
#include <memory>
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2015-07-29 18:49:40 +00:00
|
|
|
class SkData;
|
|
|
|
|
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)
|
2016-07-13 12:16:58 +00:00
|
|
|
: INHERITED(style, 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());
|
|
|
|
*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-08-27 14:41:13 +00:00
|
|
|
SkStreamAsset* onOpenStream(int*) const override { return nullptr; }
|
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:
|
2016-06-10 11:14:51 +00:00
|
|
|
SkTypeface_Stream(std::unique_ptr<SkFontData> fontData,
|
|
|
|
const SkFontStyle& style, bool isFixedPitch, bool sysFont,
|
|
|
|
const SkString familyName)
|
|
|
|
: INHERITED(style, isFixedPitch, sysFont, familyName, fontData->getIndex())
|
|
|
|
, fData(std::move(fontData))
|
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 {
|
2016-06-10 11:14:51 +00:00
|
|
|
*ttcIndex = fData->getIndex();
|
|
|
|
return fData->duplicateStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkFontData* onCreateFontData() const override {
|
|
|
|
return new SkFontData(*fData.get());
|
2013-03-22 17:21:59 +00:00
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
private:
|
2016-06-10 11:14:51 +00:00
|
|
|
std::unique_ptr<const SkFontData> fData;
|
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
|
|
|
/** 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)
|
|
|
|
{ }
|
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();
|
2015-06-15 22:17:21 +00:00
|
|
|
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;
|
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. */
|
2016-06-10 11:14:51 +00:00
|
|
|
void appendTypeface(sk_sp<SkTypeface_Custom> typeface) {
|
|
|
|
fStyles.emplace_back(std::move(typeface));
|
2015-03-30 19:53:48 +00:00
|
|
|
}
|
|
|
|
|
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());
|
2016-04-27 19:30:25 +00:00
|
|
|
if (style) {
|
|
|
|
*style = fStyles[index]->fontStyle();
|
|
|
|
}
|
|
|
|
if (name) {
|
|
|
|
name->reset();
|
|
|
|
}
|
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* 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
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkTypeface* matchStyle(const SkFontStyle& pattern) override {
|
2016-04-27 19:30:25 +00:00
|
|
|
return this->matchStyleCSS3(pattern);
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
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:
|
2016-06-10 11:14:51 +00:00
|
|
|
SkTArray<sk_sp<SkTypeface_Custom>> fStyles;
|
2013-11-11 15:53:29 +00:00
|
|
|
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:
|
2016-06-10 11:14:51 +00:00
|
|
|
typedef SkTArray<sk_sp<SkFontStyleSet_Custom>> Families;
|
2015-03-30 19:53:48 +00:00
|
|
|
class SystemFontLoader {
|
|
|
|
public:
|
|
|
|
virtual ~SystemFontLoader() { }
|
|
|
|
virtual void loadSystemFonts(const SkTypeface_FreeType::Scanner&, Families*) const = 0;
|
|
|
|
};
|
2015-08-27 14:41:13 +00:00
|
|
|
explicit SkFontMgr_Custom(const SystemFontLoader& loader) : fDefaultFamily(nullptr) {
|
2015-03-30 19:53:48 +00:00
|
|
|
loader.loadSystemFonts(fScanner, &fFamilies);
|
|
|
|
|
|
|
|
// Try to pick a default font.
|
|
|
|
static const char* defaultNames[] = {
|
2015-08-27 14:41:13 +00:00
|
|
|
"Arial", "Verdana", "Times New Roman", "Droid Sans", nullptr
|
2015-03-30 19:53:48 +00:00
|
|
|
};
|
|
|
|
for (size_t i = 0; i < SK_ARRAY_COUNT(defaultNames); ++i) {
|
2016-06-10 11:14:51 +00:00
|
|
|
sk_sp<SkFontStyleSet_Custom> set(this->onMatchFamily(defaultNames[i]));
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == set) {
|
2015-03-30 19:53:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-10 11:14:51 +00:00
|
|
|
sk_sp<SkTypeface> tf(set->matchStyle(SkFontStyle(SkFontStyle::kNormal_Weight,
|
|
|
|
SkFontStyle::kNormal_Width,
|
|
|
|
SkFontStyle::kUpright_Slant)));
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == tf) {
|
2015-03-30 19:53:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-06-10 11:14:51 +00:00
|
|
|
fDefaultFamily = set.get();
|
2015-03-30 19:53:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == fDefaultFamily) {
|
2016-06-10 11:14:51 +00:00
|
|
|
fDefaultFamily = fFamilies[0].get();
|
2015-03-30 19:53:48 +00:00
|
|
|
}
|
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
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
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
|
|
|
{
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2014-11-14 18:52:53 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-06-10 11:14:51 +00:00
|
|
|
if (fFamilies[i]->fStyles[j].get() == familyMember) {
|
2013-11-11 15:53:29 +00:00
|
|
|
return fFamilies[i]->matchStyle(fontStyle);
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
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 {
|
2016-06-10 11:14:51 +00:00
|
|
|
return this->createFromStream(bareStream, FontParameters().setCollectionIndex(ttcIndex));
|
|
|
|
}
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2016-06-10 11:14:51 +00:00
|
|
|
SkTypeface* onCreateFromStream(SkStreamAsset* s, const FontParameters& params) const override {
|
|
|
|
using Scanner = SkTypeface_FreeType::Scanner;
|
|
|
|
SkAutoTDelete<SkStreamAsset> stream(s);
|
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;
|
2016-06-10 11:14:51 +00:00
|
|
|
Scanner::AxisDefinitions axisDefinitions;
|
|
|
|
if (!fScanner.scanFont(stream, params.getCollectionIndex(), &name, &style, &isFixedPitch,
|
|
|
|
&axisDefinitions))
|
|
|
|
{
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
2016-06-10 11:14:51 +00:00
|
|
|
|
|
|
|
int paramAxisCount;
|
|
|
|
const FontParameters::Axis* paramAxes = params.getAxes(¶mAxisCount);
|
|
|
|
SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
|
|
|
|
Scanner::computeAxisValues(axisDefinitions, paramAxes, paramAxisCount, axisValues, name);
|
|
|
|
|
|
|
|
std::unique_ptr<SkFontData> data(new SkFontData(stream.release(),
|
|
|
|
params.getCollectionIndex(),
|
|
|
|
axisValues.get(), axisDefinitions.count()));
|
|
|
|
return new SkTypeface_Stream(std::move(data), style, isFixedPitch, false, name);
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2016-06-10 19:43:17 +00:00
|
|
|
SkTypeface* onCreateFromFontData(SkFontData* data) const override {
|
|
|
|
bool isFixedPitch;
|
|
|
|
SkFontStyle style;
|
|
|
|
SkString name;
|
|
|
|
if (!fScanner.scanFont(data->getStream(), data->getIndex(),
|
|
|
|
&name, &style, &isFixedPitch, nullptr))
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
std::unique_ptr<SkFontData> unique_data(data);
|
|
|
|
return new SkTypeface_Stream(std::move(unique_data), style, isFixedPitch, false, name);
|
|
|
|
}
|
|
|
|
|
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));
|
2016-03-16 20:53:35 +00:00
|
|
|
return stream.get() ? this->createFromStream(stream.release(), ttcIndex) : nullptr;
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
2012-07-30 12:59:12 +00:00
|
|
|
|
2016-04-12 20:45:06 +00:00
|
|
|
SkTypeface* onLegacyCreateTypeface(const char familyName[], SkFontStyle style) const override {
|
2015-08-27 14:41:13 +00:00
|
|
|
SkTypeface* tf = nullptr;
|
2013-11-11 15:53:29 +00:00
|
|
|
|
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
|
|
|
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == 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
|
|
|
|
2016-06-10 11:14:51 +00:00
|
|
|
return 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);
|
2016-06-10 11:14:51 +00:00
|
|
|
family->appendTypeface(sk_make_sp<SkTypeface_Empty>());
|
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();
|
|
|
|
}
|
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2015-03-30 19:53:48 +00:00
|
|
|
}
|
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-08-27 14:41:13 +00:00
|
|
|
if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch, nullptr)) {
|
2014-10-28 22:07:23 +00:00
|
|
|
SkDebugf("---- failed to open <%s> <%d> as a font\n",
|
|
|
|
filename.c_str(), faceIndex);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str());
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == addTo) {
|
2014-10-28 22:07:23 +00:00
|
|
|
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
|
|
|
}
|
2016-06-10 11:14:51 +00:00
|
|
|
addTo->appendTypeface(sk_make_sp<SkTypeface_File>(style, isFixedPitch, true,
|
|
|
|
realname, filename.c_str(),
|
|
|
|
faceIndex));
|
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;
|
|
|
|
};
|
|
|
|
|
2015-06-15 22:17:21 +00:00
|
|
|
SK_API SkFontMgr* SkFontMgr_New_Custom_Directory(const char* dir) {
|
|
|
|
return new SkFontMgr_Custom(DirectorySystemFontLoader(dir));
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-03-30 19:53:48 +00:00
|
|
|
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);
|
2016-06-10 11:14:51 +00:00
|
|
|
family->appendTypeface(sk_make_sp<SkTypeface_Empty>());
|
2013-11-11 15:53:29 +00:00
|
|
|
}
|
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
|
|
|
}
|
2015-08-27 14:41:13 +00:00
|
|
|
return nullptr;
|
2015-03-30 19:53:48 +00:00
|
|
|
}
|
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-08-27 14:41:13 +00:00
|
|
|
if (!scanner.scanFont(stream, faceIndex, &realname, &style, &isFixedPitch, nullptr)) {
|
2015-03-30 19:53:48 +00:00
|
|
|
SkDebugf("---- failed to open <%d> <%d> as a font\n", index, faceIndex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkFontStyleSet_Custom* addTo = find_family(*families, realname.c_str());
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == addTo) {
|
2015-03-30 19:53:48 +00:00
|
|
|
addTo = new SkFontStyleSet_Custom(realname);
|
|
|
|
families->push_back().reset(addTo);
|
|
|
|
}
|
2016-06-10 11:14:51 +00:00
|
|
|
std::unique_ptr<SkFontData> data(
|
|
|
|
new SkFontData(stream.release(), faceIndex, nullptr, 0));
|
|
|
|
addTo->appendTypeface(sk_make_sp<SkTypeface_Stream>(std::move(data),
|
|
|
|
style, isFixedPitch,
|
|
|
|
true, realname));
|
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-06-15 22:17:21 +00:00
|
|
|
SkFontMgr* SkFontMgr_New_Custom_Embedded(const SkEmbeddedResourceHeader* header) {
|
|
|
|
return new SkFontMgr_Custom(EmbeddedSystemFontLoader(header));
|
2013-03-27 20:01:49 +00:00
|
|
|
}
|
2016-03-21 13:55:52 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class EmptyFontLoader : public SkFontMgr_Custom::SystemFontLoader {
|
|
|
|
public:
|
|
|
|
EmptyFontLoader() { }
|
|
|
|
|
|
|
|
void loadSystemFonts(const SkTypeface_FreeType::Scanner& scanner,
|
|
|
|
SkFontMgr_Custom::Families* families) const override
|
|
|
|
{
|
|
|
|
SkFontStyleSet_Custom* family = new SkFontStyleSet_Custom(SkString());
|
|
|
|
families->push_back().reset(family);
|
2016-06-10 11:14:51 +00:00
|
|
|
family->appendTypeface(sk_make_sp<SkTypeface_Empty>());
|
2016-03-21 13:55:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
SK_API SkFontMgr* SkFontMgr_New_Custom_Empty() {
|
|
|
|
return new SkFontMgr_Custom(EmptyFontLoader());
|
|
|
|
}
|