Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
font_collection.cc
Go to the documentation of this file.
1/*
2 * Copyright 2017 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "font_collection.h"
18
19#include <algorithm>
20#include <list>
21#include <memory>
22#include <mutex>
23#include <set>
24#include <string>
25#include <unordered_map>
26#include <vector>
27#include "flutter/fml/logging.h"
28#include "flutter/fml/trace_event.h"
29#include "txt/platform.h"
30#include "txt/text_style.h"
31
32namespace txt {
33
34FontCollection::FontCollection() : enable_font_fallback_(true) {}
35
36FontCollection::~FontCollection() {
37 if (skt_collection_) {
38 skt_collection_->clearCaches();
39 }
40}
41
42size_t FontCollection::GetFontManagersCount() const {
43 return GetFontManagerOrder().size();
44}
45
46void FontCollection::SetupDefaultFontManager(
47 uint32_t font_initialization_data) {
48 default_font_manager_ = GetDefaultFontManager(font_initialization_data);
49 skt_collection_.reset();
50}
51
52void FontCollection::SetDefaultFontManager(sk_sp<SkFontMgr> font_manager) {
53 default_font_manager_ = font_manager;
54 skt_collection_.reset();
55}
56
57void FontCollection::SetAssetFontManager(sk_sp<SkFontMgr> font_manager) {
58 asset_font_manager_ = font_manager;
59 skt_collection_.reset();
60}
61
62void FontCollection::SetDynamicFontManager(sk_sp<SkFontMgr> font_manager) {
63 dynamic_font_manager_ = font_manager;
64 skt_collection_.reset();
65}
66
67void FontCollection::SetTestFontManager(sk_sp<SkFontMgr> font_manager) {
68 test_font_manager_ = font_manager;
69 skt_collection_.reset();
70}
71
72// Return the available font managers in the order they should be queried.
73std::vector<sk_sp<SkFontMgr>> FontCollection::GetFontManagerOrder() const {
74 std::vector<sk_sp<SkFontMgr>> order;
75 if (dynamic_font_manager_)
76 order.push_back(dynamic_font_manager_);
77 if (asset_font_manager_)
78 order.push_back(asset_font_manager_);
79 if (test_font_manager_)
80 order.push_back(test_font_manager_);
81 if (default_font_manager_)
82 order.push_back(default_font_manager_);
83 return order;
84}
85
86void FontCollection::DisableFontFallback() {
87 enable_font_fallback_ = false;
88 if (skt_collection_) {
89 skt_collection_->disableFontFallback();
90 }
91}
92
93void FontCollection::ClearFontFamilyCache() {
94 if (skt_collection_) {
95 skt_collection_->clearCaches();
96 }
97}
98
100FontCollection::CreateSktFontCollection() {
101 if (!skt_collection_) {
102 skt_collection_ = sk_make_sp<skia::textlayout::FontCollection>();
103
104 std::vector<SkString> default_font_families;
105 for (const std::string& family : GetDefaultFontFamilies()) {
106 default_font_families.emplace_back(family);
107 }
108 skt_collection_->setDefaultFontManager(default_font_manager_,
109 default_font_families);
110 skt_collection_->setAssetFontManager(asset_font_manager_);
111 skt_collection_->setDynamicFontManager(dynamic_font_manager_);
112 skt_collection_->setTestFontManager(test_font_manager_);
113 if (!enable_font_fallback_) {
114 skt_collection_->disableFontFallback();
115 }
116 }
117
118 return skt_collection_;
119}
120
121} // namespace txt
void reset(T *ptr=nullptr)
Definition SkRefCnt.h:310
std::vector< std::string > GetDefaultFontFamilies()
Definition platform.cc:13
sk_sp< SkFontMgr > GetDefaultFontManager(uint32_t font_initialization_data)
Definition platform.cc:17