SkFontMgr_FontConfigInterface.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*
  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. #include "include/core/SkFontMgr.h"
  8. #include "include/core/SkFontStyle.h"
  9. #include "include/core/SkString.h"
  10. #include "include/core/SkTypeface.h"
  11. #include "include/ports/SkFontConfigInterface.h"
  12. #include "include/ports/SkFontMgr_FontConfigInterface.h"
  13. #include "include/private/SkMutex.h"
  14. #include "src/core/SkFontDescriptor.h"
  15. #include "src/core/SkMakeUnique.h"
  16. #include "src/core/SkResourceCache.h"
  17. #include "src/core/SkTypefaceCache.h"
  18. #include "src/ports/SkFontConfigTypeface.h"
  19. #include <new>
  20. std::unique_ptr<SkStreamAsset> SkTypeface_FCI::onOpenStream(int* ttcIndex) const {
  21. *ttcIndex = this->getIdentity().fTTCIndex;
  22. if (fFontData) {
  23. SkStreamAsset* stream = fFontData->getStream();
  24. if (!stream) {
  25. return nullptr;
  26. }
  27. return stream->duplicate();
  28. }
  29. return std::unique_ptr<SkStreamAsset>(fFCI->openStream(this->getIdentity()));
  30. }
  31. std::unique_ptr<SkFontData> SkTypeface_FCI::onMakeFontData() const {
  32. if (fFontData) {
  33. return skstd::make_unique<SkFontData>(*fFontData);
  34. }
  35. const SkFontConfigInterface::FontIdentity& id = this->getIdentity();
  36. return skstd::make_unique<SkFontData>(std::unique_ptr<SkStreamAsset>(fFCI->openStream(id)),
  37. id.fTTCIndex, nullptr, 0);
  38. }
  39. void SkTypeface_FCI::onGetFontDescriptor(SkFontDescriptor* desc, bool* isLocalStream) const {
  40. SkString name;
  41. this->getFamilyName(&name);
  42. desc->setFamilyName(name.c_str());
  43. desc->setStyle(this->fontStyle());
  44. *isLocalStream = SkToBool(fFontData);
  45. }
  46. ///////////////////////////////////////////////////////////////////////////////
  47. class SkFontStyleSet_FCI : public SkFontStyleSet {
  48. public:
  49. SkFontStyleSet_FCI() {}
  50. int count() override { return 0; }
  51. void getStyle(int index, SkFontStyle*, SkString* style) override { SkASSERT(false); }
  52. SkTypeface* createTypeface(int index) override { SkASSERT(false); return nullptr; }
  53. SkTypeface* matchStyle(const SkFontStyle& pattern) override { return nullptr; }
  54. };
  55. ///////////////////////////////////////////////////////////////////////////////
  56. class SkFontRequestCache {
  57. public:
  58. struct Request : public SkResourceCache::Key {
  59. private:
  60. Request(const char* name, size_t nameLen, const SkFontStyle& style) : fStyle(style) {
  61. /** Pointer to just after the last field of this class. */
  62. char* content = const_cast<char*>(SkTAfter<const char>(&this->fStyle));
  63. // No holes.
  64. SkASSERT(SkTAddOffset<char>(this, sizeof(SkResourceCache::Key) + keySize) == content);
  65. // Has a size divisible by size of uint32_t.
  66. SkASSERT((content - reinterpret_cast<char*>(this)) % sizeof(uint32_t) == 0);
  67. size_t contentLen = SkAlign4(nameLen);
  68. sk_careful_memcpy(content, name, nameLen);
  69. sk_bzero(content + nameLen, contentLen - nameLen);
  70. this->init(nullptr, 0, keySize + contentLen);
  71. }
  72. const SkFontStyle fStyle;
  73. /** The sum of the sizes of the fields of this class. */
  74. static const size_t keySize = sizeof(fStyle);
  75. public:
  76. static Request* Create(const char* name, const SkFontStyle& style) {
  77. size_t nameLen = name ? strlen(name) : 0;
  78. size_t contentLen = SkAlign4(nameLen);
  79. char* storage = new char[sizeof(Request) + contentLen];
  80. return new (storage) Request(name, nameLen, style);
  81. }
  82. void operator delete(void* storage) {
  83. delete[] reinterpret_cast<char*>(storage);
  84. }
  85. };
  86. private:
  87. struct Result : public SkResourceCache::Rec {
  88. Result(Request* request, sk_sp<SkTypeface> typeface)
  89. : fRequest(request), fFace(std::move(typeface)) {}
  90. Result(Result&&) = default;
  91. Result& operator=(Result&&) = default;
  92. const Key& getKey() const override { return *fRequest; }
  93. size_t bytesUsed() const override { return fRequest->size() + sizeof(fFace); }
  94. const char* getCategory() const override { return "request_cache"; }
  95. SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
  96. std::unique_ptr<Request> fRequest;
  97. sk_sp<SkTypeface> fFace;
  98. };
  99. SkResourceCache fCachedResults;
  100. public:
  101. SkFontRequestCache(size_t maxSize) : fCachedResults(maxSize) {}
  102. /** Takes ownership of request. It will be deleted when no longer needed. */
  103. void add(sk_sp<SkTypeface> face, Request* request) {
  104. fCachedResults.add(new Result(request, std::move(face)));
  105. }
  106. /** Does not take ownership of request. */
  107. sk_sp<SkTypeface> findAndRef(Request* request) {
  108. sk_sp<SkTypeface> face;
  109. fCachedResults.find(*request, [](const SkResourceCache::Rec& rec, void* context) -> bool {
  110. const Result& result = static_cast<const Result&>(rec);
  111. sk_sp<SkTypeface>* face = static_cast<sk_sp<SkTypeface>*>(context);
  112. *face = result.fFace;
  113. return true;
  114. }, &face);
  115. return face;
  116. }
  117. };
  118. ///////////////////////////////////////////////////////////////////////////////
  119. static bool find_by_FontIdentity(SkTypeface* cachedTypeface, void* ctx) {
  120. typedef SkFontConfigInterface::FontIdentity FontIdentity;
  121. SkTypeface_FCI* cachedFCTypeface = static_cast<SkTypeface_FCI*>(cachedTypeface);
  122. FontIdentity* identity = static_cast<FontIdentity*>(ctx);
  123. return cachedFCTypeface->getIdentity() == *identity;
  124. }
  125. ///////////////////////////////////////////////////////////////////////////////
  126. class SkFontMgr_FCI : public SkFontMgr {
  127. sk_sp<SkFontConfigInterface> fFCI;
  128. SkTypeface_FreeType::Scanner fScanner;
  129. mutable SkMutex fMutex;
  130. mutable SkTypefaceCache fTFCache;
  131. // The value of maxSize here is a compromise between cache hits and cache size.
  132. // See https://crbug.com/424082#63 for reason for current size.
  133. static const size_t kMaxSize = 1 << 15;
  134. mutable SkFontRequestCache fCache;
  135. public:
  136. SkFontMgr_FCI(sk_sp<SkFontConfigInterface> fci)
  137. : fFCI(std::move(fci))
  138. , fCache(kMaxSize)
  139. {}
  140. protected:
  141. int onCountFamilies() const override {
  142. SK_ABORT("Not implemented.");
  143. return 0;
  144. }
  145. void onGetFamilyName(int index, SkString* familyName) const override {
  146. SK_ABORT("Not implemented.");
  147. }
  148. SkFontStyleSet* onCreateStyleSet(int index) const override {
  149. SK_ABORT("Not implemented.");
  150. return nullptr;
  151. }
  152. SkFontStyleSet* onMatchFamily(const char familyName[]) const override {
  153. SK_ABORT("Not implemented.");
  154. return new SkFontStyleSet_FCI();
  155. }
  156. SkTypeface* onMatchFamilyStyle(const char requestedFamilyName[],
  157. const SkFontStyle& requestedStyle) const override
  158. {
  159. SkAutoMutexExclusive ama(fMutex);
  160. SkFontConfigInterface::FontIdentity identity;
  161. SkString outFamilyName;
  162. SkFontStyle outStyle;
  163. if (!fFCI->matchFamilyName(requestedFamilyName, requestedStyle,
  164. &identity, &outFamilyName, &outStyle))
  165. {
  166. return nullptr;
  167. }
  168. // Check if a typeface with this FontIdentity is already in the FontIdentity cache.
  169. sk_sp<SkTypeface> face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity);
  170. if (!face) {
  171. face.reset(SkTypeface_FCI::Create(fFCI, identity, std::move(outFamilyName), outStyle));
  172. // Add this FontIdentity to the FontIdentity cache.
  173. fTFCache.add(face);
  174. }
  175. return face.release();
  176. }
  177. SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
  178. const char* bcp47[], int bcp47Count,
  179. SkUnichar character) const override {
  180. SK_ABORT("Not implemented.");
  181. return nullptr;
  182. }
  183. SkTypeface* onMatchFaceStyle(const SkTypeface*, const SkFontStyle&) const override {
  184. SK_ABORT("Not implemented.");
  185. return nullptr;
  186. }
  187. sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData> data, int ttcIndex) const override {
  188. return this->onMakeFromStreamIndex(SkMemoryStream::Make(std::move(data)), ttcIndex);
  189. }
  190. sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> stream,
  191. int ttcIndex) const override {
  192. const size_t length = stream->getLength();
  193. if (!length) {
  194. return nullptr;
  195. }
  196. if (length >= 1024 * 1024 * 1024) {
  197. return nullptr; // don't accept too large fonts (>= 1GB) for safety.
  198. }
  199. // TODO should the caller give us the style or should we get it from freetype?
  200. SkString name;
  201. SkFontStyle style;
  202. bool isFixedPitch = false;
  203. if (!fScanner.scanFont(stream.get(), 0, &name, &style, &isFixedPitch, nullptr)) {
  204. return nullptr;
  205. }
  206. auto fontData = skstd::make_unique<SkFontData>(std::move(stream), ttcIndex, nullptr, 0);
  207. return sk_sp<SkTypeface>(SkTypeface_FCI::Create(std::move(fontData), std::move(name),
  208. style, isFixedPitch));
  209. }
  210. sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> stream,
  211. const SkFontArguments& args) const override {
  212. using Scanner = SkTypeface_FreeType::Scanner;
  213. const size_t length = stream->getLength();
  214. if (!length) {
  215. return nullptr;
  216. }
  217. if (length >= 1024 * 1024 * 1024) {
  218. return nullptr; // don't accept too large fonts (>= 1GB) for safety.
  219. }
  220. bool isFixedPitch;
  221. SkFontStyle style;
  222. SkString name;
  223. Scanner::AxisDefinitions axisDefinitions;
  224. if (!fScanner.scanFont(stream.get(), args.getCollectionIndex(),
  225. &name, &style, &isFixedPitch, &axisDefinitions))
  226. {
  227. return nullptr;
  228. }
  229. SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
  230. Scanner::computeAxisValues(axisDefinitions, args.getVariationDesignPosition(),
  231. axisValues, name);
  232. auto fontData = skstd::make_unique<SkFontData>(std::move(stream),
  233. args.getCollectionIndex(),
  234. axisValues.get(),
  235. axisDefinitions.count());
  236. return sk_sp<SkTypeface>(SkTypeface_FCI::Create(std::move(fontData), std::move(name),
  237. style, isFixedPitch));
  238. }
  239. sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override {
  240. std::unique_ptr<SkStreamAsset> stream = SkStream::MakeFromFile(path);
  241. return stream ? this->makeFromStream(std::move(stream), ttcIndex) : nullptr;
  242. }
  243. sk_sp<SkTypeface> onLegacyMakeTypeface(const char requestedFamilyName[],
  244. SkFontStyle requestedStyle) const override
  245. {
  246. SkAutoMutexExclusive ama(fMutex);
  247. // Check if this request is already in the request cache.
  248. using Request = SkFontRequestCache::Request;
  249. std::unique_ptr<Request> request(Request::Create(requestedFamilyName, requestedStyle));
  250. sk_sp<SkTypeface> face = fCache.findAndRef(request.get());
  251. if (face) {
  252. return sk_sp<SkTypeface>(face);
  253. }
  254. SkFontConfigInterface::FontIdentity identity;
  255. SkString outFamilyName;
  256. SkFontStyle outStyle;
  257. if (!fFCI->matchFamilyName(requestedFamilyName, requestedStyle,
  258. &identity, &outFamilyName, &outStyle))
  259. {
  260. return nullptr;
  261. }
  262. // Check if a typeface with this FontIdentity is already in the FontIdentity cache.
  263. face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity);
  264. if (!face) {
  265. face.reset(SkTypeface_FCI::Create(fFCI, identity, std::move(outFamilyName), outStyle));
  266. // Add this FontIdentity to the FontIdentity cache.
  267. fTFCache.add(face);
  268. }
  269. // Add this request to the request cache.
  270. fCache.add(face, request.release());
  271. return face;
  272. }
  273. };
  274. SK_API sk_sp<SkFontMgr> SkFontMgr_New_FCI(sk_sp<SkFontConfigInterface> fci) {
  275. SkASSERT(fci);
  276. return sk_make_sp<SkFontMgr_FCI>(std::move(fci));
  277. }