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