blob: 96ba3632d5f5c413644756464fafb61855729672 [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"
mtklein1b249332015-07-07 12:21:21 -070014#include "SkMutex.h"
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000015#include "SkRemotableFontMgr.h"
16#include "SkTArray.h"
17#include "SkTypeface.h"
18
19class SkData;
20class SkStream;
21class SkString;
22class SkTypeface;
23
24class SK_API SkFontMgr_Indirect : public SkFontMgr {
25public:
26 // TODO: The SkFontMgr is only used for createFromStream/File/Data.
27 // In the future these calls should be broken out into their own interface
28 // with a name like SkFontRenderer.
29 SkFontMgr_Indirect(SkFontMgr* impl, SkRemotableFontMgr* proxy)
bungeman@google.com1ba62622014-03-24 18:38:25 +000030 : fImpl(SkRef(impl)), fProxy(SkRef(proxy)), fFamilyNamesInited(false)
31 { }
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000032
33protected:
mtklein36352bf2015-03-25 18:17:31 -070034 int onCountFamilies() const override;
35 void onGetFamilyName(int index, SkString* familyName) const override;
36 SkFontStyleSet* onCreateStyleSet(int index) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000037
mtklein36352bf2015-03-25 18:17:31 -070038 SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000039
40 virtual SkTypeface* onMatchFamilyStyle(const char familyName[],
mtklein36352bf2015-03-25 18:17:31 -070041 const SkFontStyle& fontStyle) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000042
43 virtual SkTypeface* onMatchFamilyStyleCharacter(const char familyName[],
44 const SkFontStyle&,
bungemanc20386e2014-10-23 07:08:05 -070045 const char* bcp47[],
46 int bcp47Count,
mtklein36352bf2015-03-25 18:17:31 -070047 SkUnichar character) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000048
49 virtual SkTypeface* onMatchFaceStyle(const SkTypeface* familyMember,
mtklein36352bf2015-03-25 18:17:31 -070050 const SkFontStyle& fontStyle) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000051
mtklein36352bf2015-03-25 18:17:31 -070052 SkTypeface* onCreateFromStream(SkStreamAsset* stream, int ttcIndex) const override;
53 SkTypeface* onCreateFromFile(const char path[], int ttcIndex) const override;
54 SkTypeface* onCreateFromData(SkData* data, int ttcIndex) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000055
56 virtual SkTypeface* onLegacyCreateTypeface(const char familyName[],
mtklein36352bf2015-03-25 18:17:31 -070057 unsigned styleBits) const override;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000058
59private:
60 SkTypeface* createTypefaceFromFontId(const SkFontIdentity& fontId) const;
61
62 SkAutoTUnref<SkFontMgr> fImpl;
63 SkAutoTUnref<SkRemotableFontMgr> fProxy;
64
65 struct DataEntry {
bungeman@google.com3d21f212014-03-21 23:00:41 +000066 uint32_t fDataId; // key1
bungeman@google.comf76cbb82014-03-21 23:07:24 +000067 uint32_t fTtcIndex; // key2
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000068 SkTypeface* fTypeface; // value: weak ref to typeface
69
70 DataEntry() { }
71
72 // This is a move!!!
73 DataEntry(DataEntry& that)
74 : fDataId(that.fDataId)
75 , fTtcIndex(that.fTtcIndex)
76 , fTypeface(that.fTypeface)
77 {
bungeman@google.comc8f0d602014-03-22 00:25:34 +000078 SkDEBUGCODE(that.fDataId = SkFontIdentity::kInvalidDataId;)
79 SkDEBUGCODE(that.fTtcIndex = 0xbbadbeef;)
bungeman@google.com72cf4fc2014-03-21 22:48:32 +000080 that.fTypeface = NULL;
81 }
82
83 ~DataEntry() {
84 if (fTypeface) {
85 fTypeface->weak_unref();
86 }
87 }
88 };
89 /**
90 * This cache is essentially { dataId: { ttcIndex: typeface } }
91 * For data caching we want a mapping from data id to weak references to
92 * typefaces with that data id. By storing the index next to the typeface,
93 * this data cache also acts as a typeface cache.
94 */
95 mutable SkTArray<DataEntry> fDataCache;
96 mutable SkMutex fDataCacheMutex;
97
98 mutable SkAutoTUnref<SkDataTable> fFamilyNames;
bungeman@google.com1ba62622014-03-24 18:38:25 +000099 mutable bool fFamilyNamesInited;
100 mutable SkMutex fFamilyNamesMutex;
bungeman@google.com72cf4fc2014-03-21 22:48:32 +0000101 static void set_up_family_names(const SkFontMgr_Indirect* self);
102
103 friend class SkStyleSet_Indirect;
104};
105
106#endif