1 /*
2 * Copyright 2018 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 #include "include/ports/SkFontMgr_fuchsia.h"
9
10 #include <fuchsia/fonts/cpp/fidl.h>
11 #include <lib/zx/vmar.h>
12 #include <strings.h>
13 #include <memory>
14 #include <unordered_map>
15
16 #include "src/core/SkFontDescriptor.h"
17 #include "src/ports/SkFontMgr_custom.h"
18
19 #include "include/core/SkFontMgr.h"
20 #include "include/core/SkStream.h"
21 #include "include/core/SkTypeface.h"
22 #include "src/core/SkTypefaceCache.h"
23
UnmapMemory(const void * buffer,uint64_t size)24 void UnmapMemory(const void* buffer, uint64_t size) {
25 static_assert(sizeof(void*) == sizeof(uint64_t), "pointers aren't 64-bit");
26 zx::vmar::root_self()->unmap(reinterpret_cast<uintptr_t>(buffer), size);
27 }
28
29 struct ReleaseSkDataContext {
30 uint64_t fBufferSize;
31 std::function<void()> releaseProc;
32
ReleaseSkDataContextReleaseSkDataContext33 ReleaseSkDataContext(uint64_t bufferSize, const std::function<void()>& releaseProc)
34 : fBufferSize(bufferSize), releaseProc(releaseProc) {}
35 };
36
ReleaseSkData(const void * buffer,void * context)37 void ReleaseSkData(const void* buffer, void* context) {
38 auto releaseSkDataContext = reinterpret_cast<ReleaseSkDataContext*>(context);
39 SkASSERT(releaseSkDataContext);
40 UnmapMemory(buffer, releaseSkDataContext->fBufferSize);
41 releaseSkDataContext->releaseProc();
42 delete releaseSkDataContext;
43 }
44
MakeSkDataFromBuffer(const fuchsia::mem::Buffer & data,std::function<void ()> release_proc)45 sk_sp<SkData> MakeSkDataFromBuffer(const fuchsia::mem::Buffer& data,
46 std::function<void()> release_proc) {
47 uint64_t size = data.size;
48 uintptr_t buffer = 0;
49 zx_status_t status = zx::vmar::root_self()->map(0, data.vmo, 0, size, ZX_VM_PERM_READ, &buffer);
50 if (status != ZX_OK) return nullptr;
51 auto context = new ReleaseSkDataContext(size, release_proc);
52 return SkData::MakeWithProc(reinterpret_cast<void*>(buffer), size, ReleaseSkData, context);
53 }
54
SkToFuchsiaSlant(SkFontStyle::Slant slant)55 fuchsia::fonts::Slant SkToFuchsiaSlant(SkFontStyle::Slant slant) {
56 switch (slant) {
57 case SkFontStyle::kOblique_Slant:
58 return fuchsia::fonts::Slant::OBLIQUE;
59 case SkFontStyle::kItalic_Slant:
60 return fuchsia::fonts::Slant::ITALIC;
61 case SkFontStyle::kUpright_Slant:
62 default:
63 return fuchsia::fonts::Slant::UPRIGHT;
64 }
65 }
66
FuchsiaToSkSlant(fuchsia::fonts::Slant slant)67 SkFontStyle::Slant FuchsiaToSkSlant(fuchsia::fonts::Slant slant) {
68 switch (slant) {
69 case fuchsia::fonts::Slant::OBLIQUE:
70 return SkFontStyle::kOblique_Slant;
71 case fuchsia::fonts::Slant::ITALIC:
72 return SkFontStyle::kItalic_Slant;
73 case fuchsia::fonts::Slant::UPRIGHT:
74 default:
75 return SkFontStyle::kUpright_Slant;
76 }
77 }
78
SkToFuchsiaWidth(SkFontStyle::Width width)79 fuchsia::fonts::Width SkToFuchsiaWidth(SkFontStyle::Width width) {
80 switch (width) {
81 case SkFontStyle::Width::kUltraCondensed_Width:
82 return fuchsia::fonts::Width::ULTRA_CONDENSED;
83 case SkFontStyle::Width::kExtraCondensed_Width:
84 return fuchsia::fonts::Width::EXTRA_CONDENSED;
85 case SkFontStyle::Width::kCondensed_Width:
86 return fuchsia::fonts::Width::CONDENSED;
87 case SkFontStyle::Width::kSemiCondensed_Width:
88 return fuchsia::fonts::Width::SEMI_CONDENSED;
89 case SkFontStyle::Width::kNormal_Width:
90 return fuchsia::fonts::Width::NORMAL;
91 case SkFontStyle::Width::kSemiExpanded_Width:
92 return fuchsia::fonts::Width::SEMI_EXPANDED;
93 case SkFontStyle::Width::kExpanded_Width:
94 return fuchsia::fonts::Width::EXPANDED;
95 case SkFontStyle::Width::kExtraExpanded_Width:
96 return fuchsia::fonts::Width::EXTRA_EXPANDED;
97 case SkFontStyle::Width::kUltraExpanded_Width:
98 return fuchsia::fonts::Width::ULTRA_EXPANDED;
99 }
100 }
101
102 // Tries to convert the given integer Skia style width value to the Fuchsia equivalent.
103 //
104 // On success, returns true. On failure, returns false, and `outFuchsiaWidth` is left untouched.
SkToFuchsiaWidth(int skWidth,fuchsia::fonts::Width * outFuchsiaWidth)105 bool SkToFuchsiaWidth(int skWidth, fuchsia::fonts::Width* outFuchsiaWidth) {
106 if (skWidth < SkFontStyle::Width::kUltraCondensed_Width ||
107 skWidth > SkFontStyle::Width::kUltraExpanded_Width) {
108 return false;
109 }
110 auto typedSkWidth = static_cast<SkFontStyle::Width>(skWidth);
111 *outFuchsiaWidth = SkToFuchsiaWidth(typedSkWidth);
112 return true;
113 }
114
FuchsiaToSkWidth(fuchsia::fonts::Width width)115 SkFontStyle::Width FuchsiaToSkWidth(fuchsia::fonts::Width width) {
116 switch (width) {
117 case fuchsia::fonts::Width::ULTRA_CONDENSED:
118 return SkFontStyle::Width::kUltraCondensed_Width;
119 case fuchsia::fonts::Width::EXTRA_CONDENSED:
120 return SkFontStyle::Width::kExtraCondensed_Width;
121 case fuchsia::fonts::Width::CONDENSED:
122 return SkFontStyle::Width::kCondensed_Width;
123 case fuchsia::fonts::Width::SEMI_CONDENSED:
124 return SkFontStyle::Width::kSemiCondensed_Width;
125 case fuchsia::fonts::Width::NORMAL:
126 return SkFontStyle::Width::kNormal_Width;
127 case fuchsia::fonts::Width::SEMI_EXPANDED:
128 return SkFontStyle::Width::kSemiExpanded_Width;
129 case fuchsia::fonts::Width::EXPANDED:
130 return SkFontStyle::Width::kExpanded_Width;
131 case fuchsia::fonts::Width::EXTRA_EXPANDED:
132 return SkFontStyle::Width::kExtraExpanded_Width;
133 case fuchsia::fonts::Width::ULTRA_EXPANDED:
134 return SkFontStyle::Width::kUltraExpanded_Width;
135 }
136 }
137
SkToFuchsiaStyle(const SkFontStyle & style)138 fuchsia::fonts::Style2 SkToFuchsiaStyle(const SkFontStyle& style) {
139 fuchsia::fonts::Style2 fuchsiaStyle;
140 fuchsiaStyle.set_slant(SkToFuchsiaSlant(style.slant())).set_weight(style.weight());
141
142 fuchsia::fonts::Width fuchsiaWidth = fuchsia::fonts::Width::NORMAL;
143 if (SkToFuchsiaWidth(style.width(), &fuchsiaWidth)) {
144 fuchsiaStyle.set_width(fuchsiaWidth);
145 }
146
147 return fuchsiaStyle;
148 }
149
150 constexpr struct {
151 const char* fName;
152 fuchsia::fonts::GenericFontFamily fGenericFontFamily;
153 } kGenericFontFamiliesByName[] = {{"serif", fuchsia::fonts::GenericFontFamily::SERIF},
154 {"sans", fuchsia::fonts::GenericFontFamily::SANS_SERIF},
155 {"sans-serif", fuchsia::fonts::GenericFontFamily::SANS_SERIF},
156 {"mono", fuchsia::fonts::GenericFontFamily::MONOSPACE},
157 {"monospace", fuchsia::fonts::GenericFontFamily::MONOSPACE},
158 {"cursive", fuchsia::fonts::GenericFontFamily::CURSIVE},
159 {"fantasy", fuchsia::fonts::GenericFontFamily::FANTASY},
160 {"system-ui", fuchsia::fonts::GenericFontFamily::SYSTEM_UI},
161 {"emoji", fuchsia::fonts::GenericFontFamily::EMOJI},
162 {"math", fuchsia::fonts::GenericFontFamily::MATH},
163 {"fangsong", fuchsia::fonts::GenericFontFamily::FANGSONG}};
164
165 // Tries to find a generic font family with the given name. If none is found, returns false.
GetGenericFontFamilyByName(const char * name,fuchsia::fonts::GenericFontFamily * outGenericFamily)166 bool GetGenericFontFamilyByName(const char* name,
167 fuchsia::fonts::GenericFontFamily* outGenericFamily) {
168 if (!name) return false;
169 for (auto& genericFamily : kGenericFontFamiliesByName) {
170 if (strcasecmp(genericFamily.fName, name) == 0) {
171 *outGenericFamily = genericFamily.fGenericFontFamily;
172 return true;
173 }
174 }
175 return false;
176 }
177
178 struct TypefaceId {
179 uint32_t bufferId;
180 uint32_t ttcIndex;
181
operator ==TypefaceId182 bool operator==(TypefaceId& other) {
183 return std::tie(bufferId, ttcIndex) == std::tie(other.bufferId, other.ttcIndex);
184 }
185 }
186
187 constexpr kNullTypefaceId = {0xFFFFFFFF, 0xFFFFFFFF};
188
189 class SkTypeface_Fuchsia : public SkTypeface_Stream {
190 public:
SkTypeface_Fuchsia(std::unique_ptr<SkFontData> fontData,const SkFontStyle & style,bool isFixedPitch,const SkString familyName,TypefaceId id)191 SkTypeface_Fuchsia(std::unique_ptr<SkFontData> fontData, const SkFontStyle& style,
192 bool isFixedPitch, const SkString familyName, TypefaceId id)
193 : SkTypeface_Stream(std::move(fontData), style, isFixedPitch,
194 /*sys_font=*/true, familyName)
195 , fId(id) {}
196
id()197 TypefaceId id() { return fId; }
198
199 private:
200 TypefaceId fId;
201 };
202
CreateTypefaceFromSkStream(std::unique_ptr<SkStreamAsset> stream,const SkFontArguments & args,TypefaceId id)203 sk_sp<SkTypeface> CreateTypefaceFromSkStream(std::unique_ptr<SkStreamAsset> stream,
204 const SkFontArguments& args, TypefaceId id) {
205 using Scanner = SkTypeface_FreeType::Scanner;
206 Scanner scanner;
207 bool isFixedPitch;
208 SkFontStyle style;
209 SkString name;
210 Scanner::AxisDefinitions axisDefinitions;
211 if (!scanner.scanFont(stream.get(), args.getCollectionIndex(), &name, &style, &isFixedPitch,
212 &axisDefinitions)) {
213 return nullptr;
214 }
215
216 const SkFontArguments::VariationPosition position = args.getVariationDesignPosition();
217 SkAutoSTMalloc<4, SkFixed> axisValues(axisDefinitions.count());
218 Scanner::computeAxisValues(axisDefinitions, position, axisValues, name);
219
220 auto fontData = std::make_unique<SkFontData>(std::move(stream), args.getCollectionIndex(),
221 axisValues.get(), axisDefinitions.count());
222 return sk_make_sp<SkTypeface_Fuchsia>(std::move(fontData), style, isFixedPitch, name, id);
223 }
224
CreateTypefaceFromSkData(sk_sp<SkData> data,TypefaceId id)225 sk_sp<SkTypeface> CreateTypefaceFromSkData(sk_sp<SkData> data, TypefaceId id) {
226 return CreateTypefaceFromSkStream(std::make_unique<SkMemoryStream>(std::move(data)),
227 SkFontArguments().setCollectionIndex(id.ttcIndex), id);
228 }
229
230 class SkFontMgr_Fuchsia final : public SkFontMgr {
231 public:
232 SkFontMgr_Fuchsia(fuchsia::fonts::ProviderSyncPtr provider);
233 ~SkFontMgr_Fuchsia() override;
234
235 protected:
236 // SkFontMgr overrides.
237 int onCountFamilies() const override;
238 void onGetFamilyName(int index, SkString* familyName) const override;
239 SkFontStyleSet* onMatchFamily(const char familyName[]) const override;
240 SkFontStyleSet* onCreateStyleSet(int index) const override;
241 SkTypeface* onMatchFamilyStyle(const char familyName[], const SkFontStyle&) const override;
242 SkTypeface* onMatchFamilyStyleCharacter(const char familyName[], const SkFontStyle&,
243 const char* bcp47[], int bcp47Count,
244 SkUnichar character) const override;
245 SkTypeface* onMatchFaceStyle(const SkTypeface*, const SkFontStyle&) const override;
246 sk_sp<SkTypeface> onMakeFromData(sk_sp<SkData>, int ttcIndex) const override;
247 sk_sp<SkTypeface> onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset>,
248 int ttcIndex) const override;
249 sk_sp<SkTypeface> onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset>,
250 const SkFontArguments&) const override;
251 sk_sp<SkTypeface> onMakeFromFile(const char path[], int ttcIndex) const override;
252 sk_sp<SkTypeface> onLegacyMakeTypeface(const char familyName[], SkFontStyle) const override;
253
254 private:
255 friend class SkFontStyleSet_Fuchsia;
256
257 sk_sp<SkTypeface> FetchTypeface(const char familyName[], const SkFontStyle& style,
258 const char* bcp47[], int bcp47Count, SkUnichar character,
259 bool allow_fallback, bool exact_style_match) const;
260
261 sk_sp<SkData> GetOrCreateSkData(int bufferId, const fuchsia::mem::Buffer& buffer) const;
262 void OnSkDataDeleted(int bufferId) const;
263
264 sk_sp<SkTypeface> GetOrCreateTypeface(TypefaceId id, const fuchsia::mem::Buffer& buffer) const;
265
266 mutable fuchsia::fonts::ProviderSyncPtr fFontProvider;
267
268 mutable SkMutex fCacheMutex;
269
270 // Must be accessed only with fCacheMutex acquired.
271 mutable std::unordered_map<int, SkData*> fBufferCache;
272 mutable SkTypefaceCache fTypefaceCache;
273 };
274
275 class SkFontStyleSet_Fuchsia : public SkFontStyleSet {
276 public:
SkFontStyleSet_Fuchsia(sk_sp<SkFontMgr_Fuchsia> font_manager,std::string familyName,std::vector<SkFontStyle> styles)277 SkFontStyleSet_Fuchsia(sk_sp<SkFontMgr_Fuchsia> font_manager, std::string familyName,
278 std::vector<SkFontStyle> styles)
279 : fFontManager(font_manager), fFamilyName(familyName), fStyles(styles) {}
280
281 ~SkFontStyleSet_Fuchsia() override = default;
282
count()283 int count() override { return fStyles.size(); }
284
getStyle(int index,SkFontStyle * style,SkString * styleName)285 void getStyle(int index, SkFontStyle* style, SkString* styleName) override {
286 SkASSERT(index >= 0 && index < static_cast<int>(fStyles.size()));
287 if (style) *style = fStyles[index];
288
289 // We don't have style names. Return an empty name.
290 if (styleName) styleName->reset();
291 }
292
createTypeface(int index)293 SkTypeface* createTypeface(int index) override {
294 SkASSERT(index >= 0 && index < static_cast<int>(fStyles.size()));
295
296 if (fTypefaces.empty()) fTypefaces.resize(fStyles.size());
297
298 if (!fTypefaces[index]) {
299 fTypefaces[index] = fFontManager->FetchTypeface(
300 fFamilyName.c_str(), fStyles[index], /*bcp47=*/nullptr,
301 /*bcp47Count=*/0, /*character=*/0,
302 /*allow_fallback=*/false, /*exact_style_match=*/true);
303 }
304
305 return SkSafeRef(fTypefaces[index].get());
306 }
307
matchStyle(const SkFontStyle & pattern)308 SkTypeface* matchStyle(const SkFontStyle& pattern) override { return matchStyleCSS3(pattern); }
309
310 private:
311 sk_sp<SkFontMgr_Fuchsia> fFontManager;
312 std::string fFamilyName;
313 std::vector<SkFontStyle> fStyles;
314 std::vector<sk_sp<SkTypeface>> fTypefaces;
315 };
316
SkFontMgr_Fuchsia(fuchsia::fonts::ProviderSyncPtr provider)317 SkFontMgr_Fuchsia::SkFontMgr_Fuchsia(fuchsia::fonts::ProviderSyncPtr provider)
318 : fFontProvider(std::move(provider)) {}
319
320 SkFontMgr_Fuchsia::~SkFontMgr_Fuchsia() = default;
321
onCountFamilies() const322 int SkFontMgr_Fuchsia::onCountFamilies() const {
323 // Family enumeration is not supported.
324 return 0;
325 }
326
onGetFamilyName(int index,SkString * familyName) const327 void SkFontMgr_Fuchsia::onGetFamilyName(int index, SkString* familyName) const {
328 // Family enumeration is not supported.
329 familyName->reset();
330 }
331
onCreateStyleSet(int index) const332 SkFontStyleSet* SkFontMgr_Fuchsia::onCreateStyleSet(int index) const {
333 // Family enumeration is not supported.
334 return nullptr;
335 }
336
onMatchFamily(const char familyName[]) const337 SkFontStyleSet* SkFontMgr_Fuchsia::onMatchFamily(const char familyName[]) const {
338 fuchsia::fonts::FamilyName typedFamilyName;
339 typedFamilyName.name = familyName;
340
341 fuchsia::fonts::FontFamilyInfo familyInfo;
342 int result = fFontProvider->GetFontFamilyInfo(typedFamilyName, &familyInfo);
343 if (result != ZX_OK || !familyInfo.has_styles() || familyInfo.styles().empty()) return nullptr;
344
345 std::vector<SkFontStyle> styles;
346 for (auto& style : familyInfo.styles()) {
347 styles.push_back(SkFontStyle(style.weight(), FuchsiaToSkWidth(style.width()),
348 FuchsiaToSkSlant(style.slant())));
349 }
350
351 return new SkFontStyleSet_Fuchsia(sk_ref_sp(this), familyInfo.name().name, std::move(styles));
352 }
353
onMatchFamilyStyle(const char familyName[],const SkFontStyle & style) const354 SkTypeface* SkFontMgr_Fuchsia::onMatchFamilyStyle(const char familyName[],
355 const SkFontStyle& style) const {
356 sk_sp<SkTypeface> typeface =
357 FetchTypeface(familyName, style, /*bcp47=*/nullptr,
358 /*bcp47Count=*/0, /*character=*/0,
359 /*allow_fallback=*/false, /*exact_style_match=*/false);
360 return typeface.release();
361 }
362
onMatchFamilyStyleCharacter(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character) const363 SkTypeface* SkFontMgr_Fuchsia::onMatchFamilyStyleCharacter(const char familyName[],
364 const SkFontStyle& style,
365 const char* bcp47[], int bcp47Count,
366 SkUnichar character) const {
367 sk_sp<SkTypeface> typeface =
368 FetchTypeface(familyName, style, bcp47, bcp47Count, character, /*allow_fallback=*/true,
369 /*exact_style_match=*/false);
370 return typeface.release();
371 }
372
onMatchFaceStyle(const SkTypeface *,const SkFontStyle &) const373 SkTypeface* SkFontMgr_Fuchsia::onMatchFaceStyle(const SkTypeface*, const SkFontStyle&) const {
374 return nullptr;
375 }
376
onMakeFromData(sk_sp<SkData>,int ttcIndex) const377 sk_sp<SkTypeface> SkFontMgr_Fuchsia::onMakeFromData(sk_sp<SkData>, int ttcIndex) const {
378 SkASSERT(false);
379 return nullptr;
380 }
381
onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> asset,int ttcIndex) const382 sk_sp<SkTypeface> SkFontMgr_Fuchsia::onMakeFromStreamIndex(std::unique_ptr<SkStreamAsset> asset,
383 int ttcIndex) const {
384 return makeFromStream(std::move(asset), SkFontArguments().setCollectionIndex(ttcIndex));
385 }
386
onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> asset,const SkFontArguments & args) const387 sk_sp<SkTypeface> SkFontMgr_Fuchsia::onMakeFromStreamArgs(std::unique_ptr<SkStreamAsset> asset,
388 const SkFontArguments& args) const {
389 return CreateTypefaceFromSkStream(std::move(asset), args, kNullTypefaceId);
390 }
391
onMakeFromFile(const char path[],int ttcIndex) const392 sk_sp<SkTypeface> SkFontMgr_Fuchsia::onMakeFromFile(const char path[], int ttcIndex) const {
393 return makeFromStream(std::make_unique<SkFILEStream>(path), ttcIndex);
394 }
395
onLegacyMakeTypeface(const char familyName[],SkFontStyle style) const396 sk_sp<SkTypeface> SkFontMgr_Fuchsia::onLegacyMakeTypeface(const char familyName[],
397 SkFontStyle style) const {
398 return sk_sp<SkTypeface>(matchFamilyStyle(familyName, style));
399 }
400
FetchTypeface(const char familyName[],const SkFontStyle & style,const char * bcp47[],int bcp47Count,SkUnichar character,bool allow_fallback,bool exact_style_match) const401 sk_sp<SkTypeface> SkFontMgr_Fuchsia::FetchTypeface(const char familyName[],
402 const SkFontStyle& style, const char* bcp47[],
403 int bcp47Count, SkUnichar character,
404 bool allow_fallback,
405 bool exact_style_match) const {
406 fuchsia::fonts::TypefaceQuery query;
407 query.set_style(SkToFuchsiaStyle(style));
408
409 if (bcp47Count > 0) {
410 std::vector<fuchsia::intl::LocaleId> languages{};
411 for (int i = 0; i < bcp47Count; i++) {
412 fuchsia::intl::LocaleId localeId;
413 localeId.id = bcp47[i];
414 languages.push_back(localeId);
415 }
416 query.set_languages(std::move(languages));
417 }
418
419 if (character) {
420 query.set_code_points({static_cast<uint32_t>(character)});
421 }
422
423 // If family name is not specified or is a generic family name (e.g. "serif"), then enable
424 // fallback; otherwise, pass the family name as is.
425 fuchsia::fonts::GenericFontFamily genericFontFamily =
426 fuchsia::fonts::GenericFontFamily::SANS_SERIF;
427 bool isGenericFontFamily = GetGenericFontFamilyByName(familyName, &genericFontFamily);
428 if (!familyName || *familyName == '\0' || isGenericFontFamily) {
429 if (isGenericFontFamily) {
430 query.set_fallback_family(genericFontFamily);
431 }
432 allow_fallback = true;
433 } else {
434 fuchsia::fonts::FamilyName typedFamilyName{};
435 typedFamilyName.name = familyName;
436 query.set_family(typedFamilyName);
437 }
438
439 fuchsia::fonts::TypefaceRequestFlags flags{};
440 if (!allow_fallback) flags |= fuchsia::fonts::TypefaceRequestFlags::EXACT_FAMILY;
441 if (exact_style_match) flags |= fuchsia::fonts::TypefaceRequestFlags::EXACT_STYLE;
442
443 fuchsia::fonts::TypefaceRequest request;
444 request.set_query(std::move(query));
445 request.set_flags(flags);
446
447 fuchsia::fonts::TypefaceResponse response;
448 int result = fFontProvider->GetTypeface(std::move(request), &response);
449 if (result != ZX_OK) return nullptr;
450
451 // The service may return an empty response if there is no font matching the request.
452 if (response.IsEmpty()) return nullptr;
453
454 return GetOrCreateTypeface(TypefaceId{response.buffer_id(), response.font_index()},
455 response.buffer());
456 }
457
GetOrCreateSkData(int bufferId,const fuchsia::mem::Buffer & buffer) const458 sk_sp<SkData> SkFontMgr_Fuchsia::GetOrCreateSkData(int bufferId,
459 const fuchsia::mem::Buffer& buffer) const {
460 fCacheMutex.assertHeld();
461
462 auto iter = fBufferCache.find(bufferId);
463 if (iter != fBufferCache.end()) {
464 return sk_ref_sp(iter->second);
465 }
466 auto font_mgr = sk_ref_sp(this);
467 auto data = MakeSkDataFromBuffer(
468 buffer, [font_mgr, bufferId]() { font_mgr->OnSkDataDeleted(bufferId); });
469 if (!data) {
470 return nullptr;
471 }
472 fBufferCache[bufferId] = data.get();
473 return data;
474 }
475
OnSkDataDeleted(int bufferId) const476 void SkFontMgr_Fuchsia::OnSkDataDeleted(int bufferId) const {
477 SK_UNUSED bool wasFound = fBufferCache.erase(bufferId) != 0;
478 SkASSERT(wasFound);
479 }
480
FindByTypefaceId(SkTypeface * cachedTypeface,void * ctx)481 static bool FindByTypefaceId(SkTypeface* cachedTypeface, void* ctx) {
482 SkTypeface_Fuchsia* cachedFuchsiaTypeface = static_cast<SkTypeface_Fuchsia*>(cachedTypeface);
483 TypefaceId* id = static_cast<TypefaceId*>(ctx);
484
485 return cachedFuchsiaTypeface->id() == *id;
486 }
487
GetOrCreateTypeface(TypefaceId id,const fuchsia::mem::Buffer & buffer) const488 sk_sp<SkTypeface> SkFontMgr_Fuchsia::GetOrCreateTypeface(TypefaceId id,
489 const fuchsia::mem::Buffer& buffer) const {
490 SkAutoMutexExclusive mutexLock(fCacheMutex);
491
492 sk_sp<SkTypeface> cached = fTypefaceCache.findByProcAndRef(FindByTypefaceId, &id);
493 if (cached) return cached;
494
495 sk_sp<SkData> data = GetOrCreateSkData(id.bufferId, buffer);
496 if (!data) return nullptr;
497
498 auto result = CreateTypefaceFromSkData(std::move(data), id);
499 fTypefaceCache.add(result);
500 return result;
501 }
502
SkFontMgr_New_Fuchsia(fuchsia::fonts::ProviderSyncPtr provider)503 SK_API sk_sp<SkFontMgr> SkFontMgr_New_Fuchsia(fuchsia::fonts::ProviderSyncPtr provider) {
504 return sk_make_sp<SkFontMgr_Fuchsia>(std::move(provider));
505 }
506