Case-insensitive matching of family names for custom fonts (flutter/engine#6519)
This commit is contained in:
@@ -1001,6 +1001,7 @@ FILE: ../../../flutter/third_party/txt/src/minikin/WordBreaker.cpp
|
||||
FILE: ../../../flutter/third_party/txt/src/minikin/WordBreaker.h
|
||||
FILE: ../../../flutter/third_party/txt/src/txt/asset_font_manager.cc
|
||||
FILE: ../../../flutter/third_party/txt/src/txt/asset_font_manager.h
|
||||
FILE: ../../../flutter/third_party/txt/src/txt/font_asset_provider.cc
|
||||
FILE: ../../../flutter/third_party/txt/src/txt/font_asset_provider.h
|
||||
FILE: ../../../flutter/third_party/txt/src/txt/font_collection.cc
|
||||
FILE: ../../../flutter/third_party/txt/src/txt/font_collection.h
|
||||
|
||||
@@ -40,7 +40,7 @@ std::string AssetManagerFontProvider::GetFamilyName(int index) const {
|
||||
// |FontAssetProvider|
|
||||
SkFontStyleSet* AssetManagerFontProvider::MatchFamily(
|
||||
const std::string& family_name) {
|
||||
auto found = registered_families_.find(family_name);
|
||||
auto found = registered_families_.find(CanonicalFamilyName(family_name));
|
||||
if (found == registered_families_.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -49,13 +49,14 @@ SkFontStyleSet* AssetManagerFontProvider::MatchFamily(
|
||||
|
||||
void AssetManagerFontProvider::RegisterAsset(std::string family_name,
|
||||
std::string asset) {
|
||||
auto family_it = registered_families_.find(family_name);
|
||||
std::string canonical_name = CanonicalFamilyName(family_name);
|
||||
auto family_it = registered_families_.find(canonical_name);
|
||||
|
||||
if (family_it == registered_families_.end()) {
|
||||
family_names_.push_back(family_name);
|
||||
family_it = registered_families_
|
||||
.emplace(std::piecewise_construct,
|
||||
std::forward_as_tuple(family_name),
|
||||
std::forward_as_tuple(canonical_name),
|
||||
std::forward_as_tuple(asset_manager_))
|
||||
.first;
|
||||
}
|
||||
|
||||
1
engine/src/flutter/third_party/txt/BUILD.gn
vendored
1
engine/src/flutter/third_party/txt/BUILD.gn
vendored
@@ -66,6 +66,7 @@ source_set("txt") {
|
||||
"src/minikin/WordBreaker.h",
|
||||
"src/txt/asset_font_manager.cc",
|
||||
"src/txt/asset_font_manager.h",
|
||||
"src/txt/font_asset_provider.cc",
|
||||
"src/txt/font_asset_provider.h",
|
||||
"src/txt/font_collection.cc",
|
||||
"src/txt/font_collection.h",
|
||||
|
||||
36
engine/src/flutter/third_party/txt/src/txt/font_asset_provider.cc
vendored
Normal file
36
engine/src/flutter/third_party/txt/src/txt/font_asset_provider.cc
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright 2018 Google Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
#include "txt/font_asset_provider.h"
|
||||
|
||||
namespace txt {
|
||||
|
||||
// Return a canonicalized version of a family name that is suitable for
|
||||
// matching.
|
||||
std::string FontAssetProvider::CanonicalFamilyName(std::string family_name) {
|
||||
std::string result(family_name.length(), 0);
|
||||
|
||||
// Convert ASCII characters to lower case.
|
||||
std::transform(family_name.begin(), family_name.end(), result.begin(),
|
||||
[](char c) { return (c & 0x80) ? c : ::tolower(c); });
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace txt
|
||||
@@ -28,6 +28,9 @@ class FontAssetProvider {
|
||||
virtual size_t GetFamilyCount() const = 0;
|
||||
virtual std::string GetFamilyName(int index) const = 0;
|
||||
virtual SkFontStyleSet* MatchFamily(const std::string& family_name) = 0;
|
||||
|
||||
protected:
|
||||
static std::string CanonicalFamilyName(std::string family_name);
|
||||
};
|
||||
|
||||
} // namespace txt
|
||||
|
||||
@@ -39,7 +39,7 @@ std::string TypefaceFontAssetProvider::GetFamilyName(int index) const {
|
||||
// |FontAssetProvider|
|
||||
SkFontStyleSet* TypefaceFontAssetProvider::MatchFamily(
|
||||
const std::string& family_name) {
|
||||
auto found = registered_families_.find(family_name);
|
||||
auto found = registered_families_.find(CanonicalFamilyName(family_name));
|
||||
if (found == registered_families_.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
@@ -65,12 +65,13 @@ void TypefaceFontAssetProvider::RegisterTypeface(
|
||||
return;
|
||||
}
|
||||
|
||||
auto family_it = registered_families_.find(family_name_alias);
|
||||
std::string canonical_name = CanonicalFamilyName(family_name_alias);
|
||||
auto family_it = registered_families_.find(canonical_name);
|
||||
if (family_it == registered_families_.end()) {
|
||||
family_names_.push_back(family_name_alias);
|
||||
family_it = registered_families_
|
||||
.emplace(std::piecewise_construct,
|
||||
std::forward_as_tuple(family_name_alias),
|
||||
std::forward_as_tuple(canonical_name),
|
||||
std::forward_as_tuple())
|
||||
.first;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user