blob: 04e903ed1445708b878f542483567378f36fd45d [file] [log] [blame]
bungeman@google.com72cf4fc2014-03-21 22:48:32 +00001/*
2 * Copyright 2014 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_indirect_DEFINED
9#define SkFontMgr_indirect_DEFINED
10
bungemanbf521ff2016-02-17 13:13:44 -080011#include "../private/SkMutex.h"
mtkleind9dd4282016-04-18 08:09:11 -070012#include "../private/SkOnce.h"
bungemanbf521ff2016-02-17 13:13:44 -080013#include "../private/SkTArray.h"
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000014#include "SkFontMgr.h"
bungemanf20488b2015-07-29 11:49:40 -070015#include "SkRefCnt.h"
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000016#include "SkRemotableFontMgr.h"
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000017#include "SkTypeface.h"
bungemanf20488b2015-07-29 11:49:40 -070018#include "SkTypes.h"
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000019
20class SkData;
bungemanf20488b2015-07-29 11:49:40 -070021class SkFontStyle;
22class SkStreamAsset;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000023class SkString;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000024
25class SK_API SkFontMgr_Indirect : public SkFontMgr {
26public:
27 // TODO: The SkFontMgr is only used for createFromStream/File/Data.
28 // In the future these calls should be broken out into their own interface
29 // with a name like SkFontRenderer.
bungeman6bd52842016-10-27 09:30:08 -070030 SkFontMgr_Indirect(sk_sp<SkFontMgr> impl, sk_sp<SkRemotableFontMgr> proxy)
31 : fImpl(std::move(impl)), fProxy(std::move(proxy))
bungeman@google.com1ba62622014-03-24 18:38:25 +000032 { }
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000033
34protected:
mtklein36352bf2015-03-25 18:17:31 -070035 int onCountFamilies() const override;
36 void onGetFamilyName(int index, SkString* familyName) const override;
37 SkFontStyleSet* onCreateStyleSet(int index) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000038
mtklein36352bf2015-03-25 18:17:31 -070039 SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000040
bungeman11a77c62016-04-12 13:45:06 -070041 SkTypeface* onMatchFamilyStyle(const char familyName[],
42 const SkFontStyle& fontStyle) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000043
bungeman11a77c62016-04-12 13:45:06 -070044 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
45 const SkFontStyle&,
46 const char* bcp47[],
47 int bcp47Count,
48 SkUnichar character) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000049
bungeman11a77c62016-04-12 13:45:06 -070050 SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
51 const SkFontStyle& fontStyle) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000052
Mike Reed59227392017-09-26 09:46:08 -040053 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>, int ttcIndex) const override;
54 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override;
55 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override;
56 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000057
58private:
59 SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const;
60
bungeman6bd52842016-10-27 09:30:08 -070061 sk_sp<SkFontMgr> fImpl;
62 sk_sp<SkRemotableFontMgr> fProxy;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000063
64 struct DataEntry {
bungeman@google.com3d21f212014-03-21 23:00:41 +000065 uint32_t fDataId; // key1
bungeman@google.comf76cbb82014-03-21 23:07:24 +000066 uint32_t fTtcIndex; // key2
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000067 SkTypeface* fTypeface; // value: weak ref to typeface
68
69 DataEntry() { }
70
bungeman918090c2016-02-09 09:14:28 -080071 DataEntry(DataEntry&& that)
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000072 : fDataId(that.fDataId)
73 , fTtcIndex(that.fTtcIndex)
74 , fTypeface(that.fTypeface)
75 {
bungeman@google.comc8f0d602014-03-22 00:25:34 +000076 SkDEBUGCODE(that.fDataId = SkFontIdentity::kInvalidDataId;)
77 SkDEBUGCODE(that.fTtcIndex = 0xbbadbeef;)
Ben Wagnera93a14a2017-08-28 10:34:05 -040078 that.fTypeface = nullptr;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000079 }
80
81 ~DataEntry() {
82 if (fTypeface) {
83 fTypeface->weak_unref();
84 }
85 }
86 };
87 /**
88 * This cache is essentially { dataId: { ttcIndex: typeface } }
89 * For data caching we want a mapping from data id to weak references to
90 * typefaces with that data id. By storing the index next to the typeface,
91 * this data cache also acts as a typeface cache.
92 */
93 mutable SkTArray<DataEntry> fDataCache;
94 mutable SkMutex fDataCacheMutex;
95
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000096 friend class SkStyleSet_Indirect;
97};
98
99#endif