Flutter Engine
typeface_font_asset_provider.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2018 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 
18 
19 #include "flutter/fml/logging.h"
20 #include "third_party/skia/include/core/SkString.h"
21 #include "third_party/skia/include/core/SkTypeface.h"
22 
23 namespace txt {
24 
26 
28 
29 // |FontAssetProvider|
31  return family_names_.size();
32 }
33 
34 // |FontAssetProvider|
35 std::string TypefaceFontAssetProvider::GetFamilyName(int index) const {
36  return family_names_[index];
37 }
38 
39 // |FontAssetProvider|
41  const std::string& family_name) {
42  auto found = registered_families_.find(CanonicalFamilyName(family_name));
43  if (found == registered_families_.end()) {
44  return nullptr;
45  }
46  sk_sp<TypefaceFontStyleSet> font_style_set = found->second;
47  return font_style_set.release();
48 }
49 
50 void TypefaceFontAssetProvider::RegisterTypeface(sk_sp<SkTypeface> typeface) {
51  if (typeface == nullptr) {
52  return;
53  }
54 
55  SkString sk_family_name;
56  typeface->getFamilyName(&sk_family_name);
57 
58  std::string family_name(sk_family_name.c_str(), sk_family_name.size());
59  RegisterTypeface(std::move(typeface), std::move(family_name));
60 }
61 
63  sk_sp<SkTypeface> typeface,
64  std::string family_name_alias) {
65  if (family_name_alias.empty()) {
66  return;
67  }
68 
69  std::string canonical_name = CanonicalFamilyName(family_name_alias);
70  auto family_it = registered_families_.find(canonical_name);
71  if (family_it == registered_families_.end()) {
72  family_names_.push_back(family_name_alias);
73  auto value =
74  std::make_pair(canonical_name, sk_make_sp<TypefaceFontStyleSet>());
75  family_it = registered_families_.emplace(value).first;
76  }
77  family_it->second->registerTypeface(std::move(typeface));
78 }
79 
81 
83 
84 void TypefaceFontStyleSet::registerTypeface(sk_sp<SkTypeface> typeface) {
85  if (typeface == nullptr) {
86  return;
87  }
88  typefaces_.emplace_back(std::move(typeface));
89 }
90 
92  return typefaces_.size();
93 }
94 
96  SkFontStyle* style,
97  SkString* name) {
98  FML_DCHECK(static_cast<size_t>(index) < typefaces_.size());
99  if (style) {
100  *style = typefaces_[index]->fontStyle();
101  }
102  if (name) {
103  name->reset();
104  }
105 }
106 
108  size_t index = i;
109  if (index >= typefaces_.size()) {
110  return nullptr;
111  }
112  return SkRef(typefaces_[index].get());
113 }
114 
115 SkTypeface* TypefaceFontStyleSet::matchStyle(const SkFontStyle& pattern) {
116  return matchStyleCSS3(pattern);
117 }
118 
119 } // namespace txt
SkFontStyleSet * MatchFamily(const std::string &family_name) override
void getStyle(int index, SkFontStyle *style, SkString *name) override
#define FML_DCHECK(condition)
Definition: logging.h:86
SkTypeface * matchStyle(const SkFontStyle &pattern) override
~TypefaceFontStyleSet() override
uint8_t value
SkTypeface * createTypeface(int index) override
static std::string CanonicalFamilyName(std::string family_name)
void registerTypeface(sk_sp< SkTypeface > typeface)
std::string GetFamilyName(int index) const override
const char * name
Definition: fuchsia.cc:50
void RegisterTypeface(sk_sp< SkTypeface > typeface)