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