1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkFontMgr.h"
9 #include "SkLazyPtr.h"
10 #include "SkStream.h"
11 #include "SkTypes.h"
12
13 class SkFontStyle;
14 class SkTypeface;
15
16 class SkEmptyFontStyleSet : public SkFontStyleSet {
17 public:
count()18 int count() override { return 0; }
getStyle(int,SkFontStyle *,SkString *)19 void getStyle(int, SkFontStyle*, SkString*) override {
20 SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
21 }
createTypeface(int index)22 SkTypeface* createTypeface(int index) override {
23 SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
24 return NULL;
25 }
matchStyle(const SkFontStyle &)26 SkTypeface* matchStyle(const SkFontStyle&) override {
27 return NULL;
28 }
29 };
30
CreateEmpty()31 SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
32 return SkNEW(SkEmptyFontStyleSet);
33 }
34
35 ///////////////////////////////////////////////////////////////////////////////
36
37 class SkEmptyFontMgr : public SkFontMgr {
38 protected:
onCountFamilies() const39 int onCountFamilies() const override {
40 return 0;
41 }
onGetFamilyName(int index,SkString * familyName) const42 void onGetFamilyName(int index, SkString* familyName) const override {
43 SkDEBUGFAIL("onGetFamilyName called with bad index");
44 }
onCreateStyleSet(int index) const45 SkFontStyleSet* onCreateStyleSet(int index) const override {
46 SkDEBUGFAIL("onCreateStyleSet called with bad index");
47 return NULL;
48 }
onMatchFamily(const char[]) const49 SkFontStyleSet* onMatchFamily(const char[]) const override {
50 return SkFontStyleSet::CreateEmpty();
51 }
52
onMatchFamilyStyle(const char[],const SkFontStyle &) const53 virtual SkTypeface* onMatchFamilyStyle(const char[],
54 const SkFontStyle&) const override {
55 return NULL;
56 }
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const57 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
58 const SkFontStyle& style,
59 const char* bcp47[],
60 int bcp47Count,
61 SkUnichar character) const override {
62 return NULL;
63 }
onMatchFaceStyle(const SkTypeface *,const SkFontStyle &) const64 virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
65 const SkFontStyle&) const override {
66 return NULL;
67 }
onCreateFromData(SkData *,int) const68 SkTypeface* onCreateFromData(SkData*, int) const override {
69 return NULL;
70 }
onCreateFromStream(SkStreamAsset * stream,int) const71 SkTypeface* onCreateFromStream(SkStreamAsset* stream, int) const override {
72 SkDELETE(stream);
73 return NULL;
74 }
onCreateFromFile(const char[],int) const75 SkTypeface* onCreateFromFile(const char[], int) const override {
76 return NULL;
77 }
onLegacyCreateTypeface(const char[],unsigned) const78 SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const override {
79 return NULL;
80 }
81 };
82
emptyOnNull(SkFontStyleSet * fsset)83 static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
84 if (NULL == fsset) {
85 fsset = SkFontStyleSet::CreateEmpty();
86 }
87 return fsset;
88 }
89
countFamilies() const90 int SkFontMgr::countFamilies() const {
91 return this->onCountFamilies();
92 }
93
getFamilyName(int index,SkString * familyName) const94 void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
95 this->onGetFamilyName(index, familyName);
96 }
97
createStyleSet(int index) const98 SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
99 return emptyOnNull(this->onCreateStyleSet(index));
100 }
101
matchFamily(const char familyName[]) const102 SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
103 return emptyOnNull(this->onMatchFamily(familyName));
104 }
105
matchFamilyStyle(const char familyName[],const SkFontStyle & fs) const106 SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
107 const SkFontStyle& fs) const {
108 return this->onMatchFamilyStyle(familyName, fs);
109 }
110
matchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const111 SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
112 const char* bcp47[], int bcp47Count,
113 SkUnichar character) const {
114 return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, character);
115 }
116
matchFaceStyle(const SkTypeface * face,const SkFontStyle & fs) const117 SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
118 const SkFontStyle& fs) const {
119 return this->onMatchFaceStyle(face, fs);
120 }
121
createFromData(SkData * data,int ttcIndex) const122 SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
123 if (NULL == data) {
124 return NULL;
125 }
126 return this->onCreateFromData(data, ttcIndex);
127 }
128
createFromStream(SkStreamAsset * stream,int ttcIndex) const129 SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, int ttcIndex) const {
130 if (NULL == stream) {
131 return NULL;
132 }
133 return this->onCreateFromStream(stream, ttcIndex);
134 }
135
createFromFile(const char path[],int ttcIndex) const136 SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
137 if (NULL == path) {
138 return NULL;
139 }
140 return this->onCreateFromFile(path, ttcIndex);
141 }
142
legacyCreateTypeface(const char familyName[],unsigned styleBits) const143 SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
144 unsigned styleBits) const {
145 return this->onLegacyCreateTypeface(familyName, styleBits);
146 }
147
148 // As a template argument this must have external linkage.
sk_fontmgr_create_default()149 SkFontMgr* sk_fontmgr_create_default() {
150 SkFontMgr* fm = SkFontMgr::Factory();
151 return fm ? fm : SkNEW(SkEmptyFontMgr);
152 }
153
154 SK_DECLARE_STATIC_LAZY_PTR(SkFontMgr, singleton, sk_fontmgr_create_default);
155
RefDefault()156 SkFontMgr* SkFontMgr::RefDefault() {
157 return SkRef(singleton.get());
158 }
159