blob: 33da1db37626dcf2c6b2d9ddebf9d7be8127eedd [file] [log] [blame]
bungemane1a828c2015-01-20 11:15:37 -08001/*
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
bungeman05773ed2015-05-13 10:57:09 -07008#include "SkFontDescriptor.h"
bungemane1a828c2015-01-20 11:15:37 -08009#include "SkFontMgr.h"
10#include "SkLazyPtr.h"
scroggoa1193e42015-01-21 12:09:53 -080011#include "SkStream.h"
bungemane1a828c2015-01-20 11:15:37 -080012#include "SkTypes.h"
13
14class SkFontStyle;
bungemane1a828c2015-01-20 11:15:37 -080015class SkTypeface;
16
17class SkEmptyFontStyleSet : public SkFontStyleSet {
18public:
mtklein36352bf2015-03-25 18:17:31 -070019 int count() override { return 0; }
20 void getStyle(int, SkFontStyle*, SkString*) override {
bungemane1a828c2015-01-20 11:15:37 -080021 SkDEBUGFAIL("SkFontStyleSet::getStyle called on empty set");
22 }
mtklein36352bf2015-03-25 18:17:31 -070023 SkTypeface* createTypeface(int index) override {
bungemane1a828c2015-01-20 11:15:37 -080024 SkDEBUGFAIL("SkFontStyleSet::createTypeface called on empty set");
25 return NULL;
26 }
mtklein36352bf2015-03-25 18:17:31 -070027 SkTypeface* matchStyle(const SkFontStyle&) override {
bungemane1a828c2015-01-20 11:15:37 -080028 return NULL;
29 }
30};
31
32SkFontStyleSet* SkFontStyleSet::CreateEmpty() {
33 return SkNEW(SkEmptyFontStyleSet);
34}
35
36///////////////////////////////////////////////////////////////////////////////
37
38class SkEmptyFontMgr : public SkFontMgr {
39protected:
mtklein36352bf2015-03-25 18:17:31 -070040 int onCountFamilies() const override {
bungemane1a828c2015-01-20 11:15:37 -080041 return 0;
42 }
mtklein36352bf2015-03-25 18:17:31 -070043 void onGetFamilyName(int index, SkString* familyName) const override {
bungemane1a828c2015-01-20 11:15:37 -080044 SkDEBUGFAIL("onGetFamilyName called with bad index");
45 }
mtklein36352bf2015-03-25 18:17:31 -070046 SkFontStyleSet* onCreateStyleSet(int index) const override {
bungemane1a828c2015-01-20 11:15:37 -080047 SkDEBUGFAIL("onCreateStyleSet called with bad index");
48 return NULL;
49 }
mtklein36352bf2015-03-25 18:17:31 -070050 SkFontStyleSet* onMatchFamily(const char[]) const override {
bungemane1a828c2015-01-20 11:15:37 -080051 return SkFontStyleSet::CreateEmpty();
52 }
53
54 virtual SkTypeface* onMatchFamilyStyle(const char[],
mtklein36352bf2015-03-25 18:17:31 -070055 const SkFontStyle&) const override {
bungemane1a828c2015-01-20 11:15:37 -080056 return NULL;
57 }
58 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
59 const SkFontStyle& style,
60 const char* bcp47[],
61 int bcp47Count,
mtklein36352bf2015-03-25 18:17:31 -070062 SkUnichar character) const override {
bungemane1a828c2015-01-20 11:15:37 -080063 return NULL;
64 }
65 virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
mtklein36352bf2015-03-25 18:17:31 -070066 const SkFontStyle&) const override {
bungemane1a828c2015-01-20 11:15:37 -080067 return NULL;
68 }
mtklein36352bf2015-03-25 18:17:31 -070069 SkTypeface* onCreateFromData(SkData*, int) const override {
bungemane1a828c2015-01-20 11:15:37 -080070 return NULL;
71 }
mtklein36352bf2015-03-25 18:17:31 -070072 SkTypeface* onCreateFromStream(SkStreamAsset* stream, int) const override {
scroggoa1193e42015-01-21 12:09:53 -080073 SkDELETE(stream);
bungemane1a828c2015-01-20 11:15:37 -080074 return NULL;
75 }
mtklein36352bf2015-03-25 18:17:31 -070076 SkTypeface* onCreateFromFile(const char[], int) const override {
bungemane1a828c2015-01-20 11:15:37 -080077 return NULL;
78 }
mtklein36352bf2015-03-25 18:17:31 -070079 SkTypeface* onLegacyCreateTypeface(const char [], unsigned) const override {
bungemane1a828c2015-01-20 11:15:37 -080080 return NULL;
81 }
82};
83
84static SkFontStyleSet* emptyOnNull(SkFontStyleSet* fsset) {
85 if (NULL == fsset) {
86 fsset = SkFontStyleSet::CreateEmpty();
87 }
88 return fsset;
89}
90
91int SkFontMgr::countFamilies() const {
92 return this->onCountFamilies();
93}
94
95void SkFontMgr::getFamilyName(int index, SkString* familyName) const {
96 this->onGetFamilyName(index, familyName);
97}
98
99SkFontStyleSet* SkFontMgr::createStyleSet(int index) const {
100 return emptyOnNull(this->onCreateStyleSet(index));
101}
102
103SkFontStyleSet* SkFontMgr::matchFamily(const char familyName[]) const {
104 return emptyOnNull(this->onMatchFamily(familyName));
105}
106
107SkTypeface* SkFontMgr::matchFamilyStyle(const char familyName[],
108 const SkFontStyle& fs) const {
109 return this->onMatchFamilyStyle(familyName, fs);
110}
111
112SkTypeface* SkFontMgr::matchFamilyStyleCharacter(const char familyName[], const SkFontStyle& style,
113 const char* bcp47[], int bcp47Count,
114 SkUnichar character) const {
115 return this->onMatchFamilyStyleCharacter(familyName, style, bcp47, bcp47Count, character);
116}
117
118SkTypeface* SkFontMgr::matchFaceStyle(const SkTypeface* face,
119 const SkFontStyle& fs) const {
120 return this->onMatchFaceStyle(face, fs);
121}
122
123SkTypeface* SkFontMgr::createFromData(SkData* data, int ttcIndex) const {
124 if (NULL == data) {
125 return NULL;
126 }
127 return this->onCreateFromData(data, ttcIndex);
128}
129
bungeman5f213d92015-01-27 05:39:10 -0800130SkTypeface* SkFontMgr::createFromStream(SkStreamAsset* stream, int ttcIndex) const {
bungemane1a828c2015-01-20 11:15:37 -0800131 if (NULL == stream) {
132 return NULL;
133 }
134 return this->onCreateFromStream(stream, ttcIndex);
135}
136
bungeman05773ed2015-05-13 10:57:09 -0700137SkTypeface* SkFontMgr::createFromFontData(SkFontData* data) const {
138 if (NULL == data) {
139 return NULL;
140 }
141 return this->onCreateFromFontData(data);
142}
143
144// This implementation is temporary until it can be made pure virtual.
145SkTypeface* SkFontMgr::onCreateFromFontData(SkFontData* data) const {
146 SkTypeface* ret = this->createFromStream(data->detachStream(), data->getIndex());
147 delete data;
148 return ret;
149}
150
bungemane1a828c2015-01-20 11:15:37 -0800151SkTypeface* SkFontMgr::createFromFile(const char path[], int ttcIndex) const {
152 if (NULL == path) {
153 return NULL;
154 }
155 return this->onCreateFromFile(path, ttcIndex);
156}
157
158SkTypeface* SkFontMgr::legacyCreateTypeface(const char familyName[],
159 unsigned styleBits) const {
160 return this->onLegacyCreateTypeface(familyName, styleBits);
161}
162
163// As a template argument this must have external linkage.
164SkFontMgr* sk_fontmgr_create_default() {
165 SkFontMgr* fm = SkFontMgr::Factory();
166 return fm ? fm : SkNEW(SkEmptyFontMgr);
167}
168
169SK_DECLARE_STATIC_LAZY_PTR(SkFontMgr, singleton, sk_fontmgr_create_default);
170
171SkFontMgr* SkFontMgr::RefDefault() {
172 return SkRef(singleton.get());
173}