SkTypeface* onLegacyCreateTypeface(const char requestedFamilyName[], SkFontStyle requestedStyle) const override { SkAutoMutexAcquire ama(fMutex); // Check if this request is already in the request cache. using Request = SkFontRequestCache::Request; SkAutoTDelete<Request> request(Request::Create(requestedFamilyName, requestedStyle)); SkTypeface* face = fCache.findAndRef(request); if (face) { return face; } SkFontConfigInterface::FontIdentity identity; SkString outFamilyName; SkFontStyle outStyle; if (!fFCI->matchFamilyName(requestedFamilyName, requestedStyle, &identity, &outFamilyName, &outStyle)) { return nullptr; } // Check if a typeface with this FontIdentity is already in the FontIdentity cache. face = fTFCache.findByProcAndRef(find_by_FontIdentity, &identity); if (!face) { face = SkTypeface_FCI::Create(fFCI, identity, outFamilyName, outStyle); // Add this FontIdentity to the FontIdentity cache. fTFCache.add(face); } // Add this request to the request cache. fCache.add(face, request.release()); return face; }
DEF_TEST(TypefaceCache, reporter) { sk_sp<SkTypeface> t1(SkEmptyTypeface::Create()); { SkTypefaceCache cache; REPORTER_ASSERT(reporter, count(reporter, cache) == 0); { sk_sp<SkTypeface> t0(SkEmptyTypeface::Create()); cache.add(t0.get()); REPORTER_ASSERT(reporter, count(reporter, cache) == 1); cache.add(t1.get()); REPORTER_ASSERT(reporter, count(reporter, cache) == 2); cache.purgeAll(); REPORTER_ASSERT(reporter, count(reporter, cache) == 2); } REPORTER_ASSERT(reporter, count(reporter, cache) == 2); cache.purgeAll(); REPORTER_ASSERT(reporter, count(reporter, cache) == 1); } REPORTER_ASSERT(reporter, t1->unique()); }
SkTypeface* SkFontMgr_DirectWrite::createTypefaceFromDWriteFont( IDWriteFontFace* fontFace, IDWriteFont* font, IDWriteFontFamily* fontFamily) const { SkAutoMutexAcquire ama(fTFCacheMutex); ProtoDWriteTypeface spec = { fontFace, font, fontFamily }; SkTypeface* face = fTFCache.findByProcAndRef(FindByDWriteFont, &spec); if (nullptr == face) { face = DWriteFontTypeface::Create(fFactory.get(), fontFace, font, fontFamily); if (face) { fTFCache.add(face); } } return face; }