Flutter Engine
 
Loading...
Searching...
No Matches
font_collection.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 "font_collection.h"
6
7#include <algorithm>
8#include <list>
9#include <memory>
10#include <mutex>
11#include <set>
12#include <string>
13#include <unordered_map>
14#include <vector>
15#include "flutter/fml/logging.h"
17#include "txt/platform.h"
18#include "txt/text_style.h"
19
20namespace txt {
21
22FontCollection::FontCollection() : enable_font_fallback_(true) {}
23
24FontCollection::~FontCollection() {
25 if (skt_collection_) {
26 skt_collection_->clearCaches();
27 }
28}
29
30size_t FontCollection::GetFontManagersCount() const {
31 return GetFontManagerOrder().size();
32}
33
34void FontCollection::SetupDefaultFontManager(
35 uint32_t font_initialization_data) {
36 default_font_manager_ = GetDefaultFontManager(font_initialization_data);
37 skt_collection_.reset();
38}
39
40void FontCollection::SetDefaultFontManager(sk_sp<SkFontMgr> font_manager) {
41 default_font_manager_ = std::move(font_manager);
42 skt_collection_.reset();
43}
44
45void FontCollection::SetAssetFontManager(sk_sp<SkFontMgr> font_manager) {
46 asset_font_manager_ = std::move(font_manager);
47 skt_collection_.reset();
48}
49
50void FontCollection::SetDynamicFontManager(sk_sp<SkFontMgr> font_manager) {
51 dynamic_font_manager_ = std::move(font_manager);
52 skt_collection_.reset();
53}
54
55void FontCollection::SetTestFontManager(sk_sp<SkFontMgr> font_manager) {
56 test_font_manager_ = std::move(font_manager);
57 skt_collection_.reset();
58}
59
60// Return the available font managers in the order they should be queried.
61std::vector<sk_sp<SkFontMgr>> FontCollection::GetFontManagerOrder() const {
62 std::vector<sk_sp<SkFontMgr>> order;
63 if (dynamic_font_manager_) {
64 order.push_back(dynamic_font_manager_);
65 }
66 if (asset_font_manager_) {
67 order.push_back(asset_font_manager_);
68 }
69 if (test_font_manager_) {
70 order.push_back(test_font_manager_);
71 }
72 if (default_font_manager_) {
73 order.push_back(default_font_manager_);
74 }
75 return order;
76}
77
78void FontCollection::DisableFontFallback() {
79 enable_font_fallback_ = false;
80 if (skt_collection_) {
81 skt_collection_->disableFontFallback();
82 }
83}
84
85void FontCollection::ClearFontFamilyCache() {
86 if (skt_collection_) {
87 skt_collection_->clearCaches();
88 }
89}
90
91sk_sp<skia::textlayout::FontCollection>
92FontCollection::CreateSktFontCollection() {
93 if (!skt_collection_) {
94 skt_collection_ = sk_make_sp<skia::textlayout::FontCollection>();
95
96 std::vector<SkString> default_font_families;
97 for (const std::string& family : GetDefaultFontFamilies()) {
98 default_font_families.emplace_back(family);
99 }
100 skt_collection_->setDefaultFontManager(default_font_manager_,
101 default_font_families);
102 skt_collection_->setAssetFontManager(asset_font_manager_);
103 skt_collection_->setDynamicFontManager(dynamic_font_manager_);
104 skt_collection_->setTestFontManager(test_font_manager_);
105 if (!enable_font_fallback_) {
106 skt_collection_->disableFontFallback();
107 }
108 }
109
110 return skt_collection_;
111}
112
113} // namespace txt
std::vector< std::string > GetDefaultFontFamilies()
Definition platform.cc:13
sk_sp< SkFontMgr > GetDefaultFontManager(uint32_t font_initialization_data)
Definition platform.cc:17