blob: 721018124a365085ec9733bbb35794e02d5bcb62 [file] [log] [blame]
reed@google.comc452d822013-03-25 18:44:17 +00001/*
2 * Copyright 2013 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#ifndef SkFontMgr_DEFINED
9#define SkFontMgr_DEFINED
10
11#include "SkRefCnt.h"
12#include "SkFontStyle.h"
13
14class SkData;
15class SkStream;
16class SkString;
reed@google.com0b983262013-04-11 18:27:26 +000017class SkTypeface;
reed@google.comc452d822013-03-25 18:44:17 +000018
reed@google.comaae71ba2013-04-10 13:10:40 +000019class SK_API SkFontStyleSet : public SkRefCnt {
reed@google.comc452d822013-03-25 18:44:17 +000020public:
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000021 SK_DECLARE_INST_COUNT(SkFontStyleSet)
22
reed@google.com83787c52013-03-26 17:19:15 +000023 virtual int count() = 0;
24 virtual void getStyle(int index, SkFontStyle*, SkString* style) = 0;
25 virtual SkTypeface* createTypeface(int index) = 0;
reed@google.com964988f2013-03-29 14:57:22 +000026 virtual SkTypeface* matchStyle(const SkFontStyle& pattern) = 0;
27
28 static SkFontStyleSet* CreateEmpty();
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000029
30private:
31 typedef SkRefCnt INHERITED;
reed@google.comc452d822013-03-25 18:44:17 +000032};
33
reed@google.com30ddd612013-07-30 17:47:39 +000034class SkTypeface;
35
reed@google.comaae71ba2013-04-10 13:10:40 +000036class SK_API SkFontMgr : public SkRefCnt {
reed@google.comc452d822013-03-25 18:44:17 +000037public:
commit-bot@chromium.orgef284a82013-07-11 22:29:29 +000038 SK_DECLARE_INST_COUNT(SkFontMgr)
39
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000040 int countFamilies() const;
41 void getFamilyName(int index, SkString* familyName) const;
42 SkFontStyleSet* createStyleSet(int index) const;
reed@google.comc452d822013-03-25 18:44:17 +000043
bungeman@google.com9fc5c682013-11-12 15:25:29 +000044 /**
45 * The caller must call unref() on the returned object.
46 * Never returns NULL; will return an empty set if the name is not found.
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000047 *
48 * It is possible that this will return a style set not accessible from
49 * createStyleSet(int) due to hidden or auto-activated fonts.
bungeman@google.com9fc5c682013-11-12 15:25:29 +000050 */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000051 SkFontStyleSet* matchFamily(const char familyName[]) const;
reed@google.com964988f2013-03-29 14:57:22 +000052
reed@google.comc452d822013-03-25 18:44:17 +000053 /**
54 * Find the closest matching typeface to the specified familyName and style
55 * and return a ref to it. The caller must call unref() on the returned
56 * object. Will never return NULL, as it will return the default font if
57 * no matching font is found.
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000058 *
59 * It is possible that this will return a style set not accessible from
60 * createStyleSet(int) or matchFamily(const char[]) due to hidden or
61 * auto-activated fonts.
reed@google.comc452d822013-03-25 18:44:17 +000062 */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000063 SkTypeface* matchFamilyStyle(const char familyName[], const SkFontStyle&) const;
reed@google.comc452d822013-03-25 18:44:17 +000064
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000065 /**
66 * Use the system fallback to find a typeface for the given character.
bungemanc20386e2014-10-23 07:08:05 -070067 * Note that bcp47 is a combination of ISO 639, 15924, and 3166-1 codes,
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000068 * so it is fine to just pass a ISO 639 here.
69 *
70 * Will return NULL if no family can be found for the character
71 * in the system fallback.
bungemanc20386e2014-10-23 07:08:05 -070072 *
73 * bcp47[0] is the least significant fallback, bcp47[bcp47Count-1] is the
74 * most significant. If no specified bcp47 codes match, any font with the
75 * requested character will be matched.
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000076 */
bungemanc20386e2014-10-23 07:08:05 -070077#ifdef SK_FM_NEW_MATCH_FAMILY_STYLE_CHARACTER
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000078 SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
bungemanc20386e2014-10-23 07:08:05 -070079 const char* bcp47[], int bcp47Count,
80 SkUnichar character) const;
81#else
82 SkTypeface* matchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
83 const char bcp47[], SkUnichar character) const;
84#endif
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000085
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000086 SkTypeface* matchFaceStyle(const SkTypeface*, const SkFontStyle&) const;
reed@google.com95625db2013-03-25 20:44:02 +000087
reed@google.comc452d822013-03-25 18:44:17 +000088 /**
89 * Create a typeface for the specified data and TTC index (pass 0 for none)
90 * or NULL if the data is not recognized. The caller must call unref() on
91 * the returned object if it is not null.
92 */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +000093 SkTypeface* createFromData(SkData*, int ttcIndex = 0) const;
reed@google.comc452d822013-03-25 18:44:17 +000094
95 /**
96 * Create a typeface for the specified stream and TTC index
97 * (pass 0 for none) or NULL if the stream is not recognized. The caller
98 * must call unref() on the returned object if it is not null.
99 */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000100 SkTypeface* createFromStream(SkStream*, int ttcIndex = 0) const;
reed@google.comc452d822013-03-25 18:44:17 +0000101
102 /**
103 * Create a typeface for the specified fileName and TTC index
104 * (pass 0 for none) or NULL if the file is not found, or its contents are
105 * not recognized. The caller must call unref() on the returned object
106 * if it is not null.
107 */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000108 SkTypeface* createFromFile(const char path[], int ttcIndex = 0) const;
reed@google.comc452d822013-03-25 18:44:17 +0000109
reed@google.com30ddd612013-07-30 17:47:39 +0000110 SkTypeface* legacyCreateTypeface(const char familyName[],
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000111 unsigned typefaceStyleBits) const;
reed@google.com30ddd612013-07-30 17:47:39 +0000112
reed@google.com95625db2013-03-25 20:44:02 +0000113 /**
114 * Return a ref to the default fontmgr. The caller must call unref() on
115 * the returned object.
116 */
117 static SkFontMgr* RefDefault();
118
119protected:
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000120 virtual int onCountFamilies() const = 0;
121 virtual void onGetFamilyName(int index, SkString* familyName) const = 0;
122 virtual SkFontStyleSet* onCreateStyleSet(int index)const = 0;
reed@google.com95625db2013-03-25 20:44:02 +0000123
bungeman@google.com9fc5c682013-11-12 15:25:29 +0000124 /** May return NULL if the name is not found. */
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000125 virtual SkFontStyleSet* onMatchFamily(const char familyName[]) const = 0;
reed@google.com964988f2013-03-29 14:57:22 +0000126
reed@google.com95625db2013-03-25 20:44:02 +0000127 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000128 const SkFontStyle&) const = 0;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000129 // TODO: pure virtual, implement on all impls.
bungemanc20386e2014-10-23 07:08:05 -0700130#ifdef SK_FM_NEW_MATCH_FAMILY_STYLE_CHARACTER
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000131 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
bungemanc20386e2014-10-23 07:08:05 -0700132 const char* bcp47[], int bcp47Count,
133 SkUnichar character) const
134#else
135 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
136 const char bcp47[], SkUnichar character) const
137#endif
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000138 { return NULL; }
reed@google.com95625db2013-03-25 20:44:02 +0000139 virtual SkTypeface* onMatchFaceStyle(const SkTypeface*,
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000140 const SkFontStyle&) const = 0;
reed@google.com95625db2013-03-25 20:44:02 +0000141
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000142 virtual SkTypeface* onCreateFromData(SkData*, int ttcIndex) const = 0;
143 virtual SkTypeface* onCreateFromStream(SkStream*, int ttcIndex) const = 0;
144 virtual SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const = 0;
bungeman@google.comfb1663a2013-10-24 22:32:43 +0000145
bungeman@google.comfb1663a2013-10-24 22:32:43 +0000146 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
commit-bot@chromium.org967dee32014-02-04 22:35:01 +0000147 unsigned styleBits) const = 0;
reed@google.comc452d822013-03-25 18:44:17 +0000148private:
reed@google.com95625db2013-03-25 20:44:02 +0000149 static SkFontMgr* Factory(); // implemented by porting layer
mtklein148ec592014-10-13 13:17:56 -0700150 friend SkFontMgr* sk_fontmgr_create_default();
skia.committer@gmail.come60ed082013-03-26 07:01:04 +0000151
reed@google.comc452d822013-03-25 18:44:17 +0000152 typedef SkRefCnt INHERITED;
153};
154
155#endif