blob: 0977eea26ace12c5185bb00940f72b329de2c6e8 [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
11#include "SkDataTable.h"
12#include "SkFontMgr.h"
13#include "SkFontStyle.h"
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000014#include "SkRemotableFontMgr.h"
15#include "SkTArray.h"
16#include "SkTypeface.h"
17
18class SkData;
19class SkStream;
20class SkString;
21class SkTypeface;
22
23class SK_API SkFontMgr_Indirect : public SkFontMgr {
24public:
25 // TODO: The SkFontMgr is only used for createFromStream/File/Data.
26 // In the future these calls should be broken out into their own interface
27 // with a name like SkFontRenderer.
28 SkFontMgr_Indirect(SkFontMgr* impl, SkRemotableFontMgr* proxy)
bungeman@google.com1ba62622014-03-24 18:38:25 +000029 : fImpl(SkRef(impl)), fProxy(SkRef(proxy)), fFamilyNamesInited(false)
30 { }
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000031
32protected:
mtklein36352bf2015-03-25 18:17:31 -070033 int onCountFamilies() const override;
34 void onGetFamilyName(int index, SkString* familyName) const override;
35 SkFontStyleSet* onCreateStyleSet(int index) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000036
mtklein36352bf2015-03-25 18:17:31 -070037 SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000038
39 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
mtklein36352bf2015-03-25 18:17:31 -070040 const SkFontStyle& fontStyle) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000041
42 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
43 const SkFontStyle&,
bungemanc20386e2014-10-23 07:08:05 -070044 const char* bcp47[],
45 int bcp47Count,
mtklein36352bf2015-03-25 18:17:31 -070046 SkUnichar character) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000047
48 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
mtklein36352bf2015-03-25 18:17:31 -070049 const SkFontStyle& fontStyle) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000050
mtklein36352bf2015-03-25 18:17:31 -070051 SkTypeface* onCreateFromStream(SkStreamAsset* stream, int ttcIndex) const override;
52 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override;
53 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000054
55 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
mtklein36352bf2015-03-25 18:17:31 -070056 unsigned styleBits) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000057
58private:
59 SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const;
60
61 SkAutoTUnref<SkFontMgr> fImpl;
62 SkAutoTUnref<SkRemotableFontMgr> fProxy;
63
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
71 // This is a move!!!
72 DataEntry(DataEntry& that)
73 : fDataId(that.fDataId)
74 , fTtcIndex(that.fTtcIndex)
75 , fTypeface(that.fTypeface)
76 {
bungeman@google.comc8f0d602014-03-22 00:25:34 +000077 SkDEBUGCODE(that.fDataId = SkFontIdentity::kInvalidDataId;)
78 SkDEBUGCODE(that.fTtcIndex = 0xbbadbeef;)
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000079 that.fTypeface = NULL;
80 }
81
82 ~DataEntry() {
83 if (fTypeface) {
84 fTypeface->weak_unref();
85 }
86 }
87 };
88 /**
89 * This cache is essentially { dataId: { ttcIndex: typeface } }
90 * For data caching we want a mapping from data id to weak references to
91 * typefaces with that data id. By storing the index next to the typeface,
92 * this data cache also acts as a typeface cache.
93 */
94 mutable SkTArray<DataEntry> fDataCache;
95 mutable SkMutex fDataCacheMutex;
96
97 mutable SkAutoTUnref<SkDataTable> fFamilyNames;
bungeman@google.com1ba62622014-03-24 18:38:25 +000098 mutable bool fFamilyNamesInited;
99 mutable SkMutex fFamilyNamesMutex;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000100 static void set_up_family_names(const SkFontMgr_Indirect* self);
101
102 friend class SkStyleSet_Indirect;
103};
104
105#endif