| Flutter Impeller |

typography_context.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/toolkit/interop/typography_context.h"
6
7 #include <mutex>
8
9 #include "flutter/fml/icu_util.h"
10 #include "flutter/txt/src/txt/platform.h"
11 #include "impeller/base/validation.h"
12 #include "impeller/toolkit/interop/embedded_icu_data.h"
13
14 namespace impeller::interop {
15
16 TypographyContext::TypographyContext()
17 : collection_(std::make_shared<txt::FontCollection>()) {
18 static std::once_flag sICUInitOnceFlag;
19 std::call_once(sICUInitOnceFlag, []() {
20 auto icu_data = std::make_unique<fml::NonOwnedMapping>(
21 impeller_embedded_icu_data_data, impeller_embedded_icu_data_length);
22 fml::icu::InitializeICUFromMapping(std::move(icu_data));
23 });
24 // The fallback for all fonts. Looks in platform specific locations.
25 collection_->SetupDefaultFontManager(0u);
26
27 // Looks for fonts in user supplied blobs.
28 asset_font_manager_ = sk_make_sp<skia::textlayout::TypefaceFontProvider>();
29 collection_->SetAssetFontManager(asset_font_manager_);
30 }
31
32 TypographyContext::~TypographyContext() = default;
33
34 bool TypographyContext::IsValid() const {
35 return !!collection_;
36 }
37
38 const std::shared_ptr<txt::FontCollection>&
39 TypographyContext::GetFontCollection() const {
40 return collection_;
41 }
42
43 static sk_sp<SkTypeface> CreateTypefaceFromFontData(
44 std::unique_ptr<fml::Mapping> font_data) {
45 if (!font_data) {
46 VALIDATION_LOG << "Invalid font data.";
47 return nullptr;
48 }
49 auto sk_data_context = font_data.release();
50 auto sk_data = SkData::MakeWithProc(
51 sk_data_context->GetMapping(), // data ptr
52 sk_data_context->GetSize(), // data size
53 [](const void*, void* context) {
54 delete reinterpret_cast<decltype(sk_data_context)>(context);
55 }, // release callback
56 sk_data_context // release callback context
57 );
58 auto sk_data_stream = SkMemoryStream::Make(sk_data);
59 auto sk_typeface =
60 txt::GetDefaultFontManager()->makeFromStream(std::move(sk_data_stream));
61 if (!sk_typeface) {
62 VALIDATION_LOG << "Could not create typeface with data.";
63 return nullptr;
64 }
65 return sk_typeface;
66 }
67
68 bool TypographyContext::RegisterFont(std::unique_ptr<fml::Mapping> font_data,
69 const char* family_name_alias) {
70 auto typeface = CreateTypefaceFromFontData(std::move(font_data));
71 if (typeface == nullptr) {
72 return false;
73 }
74 size_t result = 0u;
75 if (family_name_alias == nullptr) {
76 result = asset_font_manager_->registerTypeface(std::move(typeface));
77 } else {
78 result = asset_font_manager_->registerTypeface(std::move(typeface),
79 SkString{family_name_alias});
80 }
81 return result != 0;
82 }
83
84 } // namespace impeller::interop
impeller::interop::TypographyContext::RegisterFont
bool RegisterFont(std::unique_ptr< fml::Mapping > font_data, const char *family_name_alias)
Registers custom font data. If an alias for the family name is provided, subsequent lookups will need...
Definition: typography_context.cc:68
impeller::interop::TypographyContext::IsValid
bool IsValid() const
Definition: typography_context.cc:34
impeller::interop::TypographyContext::TypographyContext
TypographyContext()
Definition: typography_context.cc:16
impeller::interop::TypographyContext::GetFontCollection
const std::shared_ptr< txt::FontCollection > & GetFontCollection() const
Definition: typography_context.cc:39
impeller::interop::TypographyContext::~TypographyContext
~TypographyContext() override
impeller::interop
Definition: context_gles.cc:12
impeller::interop::CreateTypefaceFromFontData
static sk_sp< SkTypeface > CreateTypefaceFromFontData(std::unique_ptr< fml::Mapping > font_data)
Definition: typography_context.cc:43
std
Definition: comparable.h:95
typography_context.h
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91