Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c0d2b39

Browse files
committed
Case-insensitive matching of family names for custom fonts
1 parent f2e7441 commit c0d2b39

5 files changed

Lines changed: 47 additions & 5 deletions

File tree

lib/ui/text/asset_manager_font_provider.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ std::string AssetManagerFontProvider::GetFamilyName(int index) const {
4040
// |FontAssetProvider|
4141
SkFontStyleSet* AssetManagerFontProvider::MatchFamily(
4242
const std::string& family_name) {
43-
auto found = registered_families_.find(family_name);
43+
auto found = registered_families_.find(CanonicalFamilyName(family_name));
4444
if (found == registered_families_.end()) {
4545
return nullptr;
4646
}
@@ -49,13 +49,14 @@ SkFontStyleSet* AssetManagerFontProvider::MatchFamily(
4949

5050
void AssetManagerFontProvider::RegisterAsset(std::string family_name,
5151
std::string asset) {
52-
auto family_it = registered_families_.find(family_name);
52+
std::string canonical_name = CanonicalFamilyName(family_name);
53+
auto family_it = registered_families_.find(canonical_name);
5354

5455
if (family_it == registered_families_.end()) {
5556
family_names_.push_back(family_name);
5657
family_it = registered_families_
5758
.emplace(std::piecewise_construct,
58-
std::forward_as_tuple(family_name),
59+
std::forward_as_tuple(canonical_name),
5960
std::forward_as_tuple(asset_manager_))
6061
.first;
6162
}

third_party/txt/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ source_set("txt") {
6666
"src/minikin/WordBreaker.h",
6767
"src/txt/asset_font_manager.cc",
6868
"src/txt/asset_font_manager.h",
69+
"src/txt/font_asset_provider.cc",
6970
"src/txt/font_asset_provider.h",
7071
"src/txt/font_collection.cc",
7172
"src/txt/font_collection.h",
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
17+
#include <algorithm>
18+
#include <string>
19+
20+
#include "txt/font_asset_provider.h"
21+
22+
namespace txt {
23+
24+
// Return a canonicalized version of a family name that is suitable for
25+
// matching.
26+
std::string FontAssetProvider::CanonicalFamilyName(std::string family_name) {
27+
std::string result(family_name.length(), 0);
28+
29+
// Convert ASCII characters to lower case.
30+
std::transform(family_name.begin(), family_name.end(), result.begin(),
31+
[](char c) { return (c & 0x80) ? c : ::tolower(c); });
32+
33+
return result;
34+
}
35+
36+
} // namespace txt

third_party/txt/src/txt/font_asset_provider.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class FontAssetProvider {
2828
virtual size_t GetFamilyCount() const = 0;
2929
virtual std::string GetFamilyName(int index) const = 0;
3030
virtual SkFontStyleSet* MatchFamily(const std::string& family_name) = 0;
31+
32+
protected:
33+
static std::string CanonicalFamilyName(std::string family_name);
3134
};
3235

3336
} // namespace txt

third_party/txt/src/txt/typeface_font_asset_provider.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ void TypefaceFontAssetProvider::RegisterTypeface(
6565
return;
6666
}
6767

68-
auto family_it = registered_families_.find(family_name_alias);
68+
std::string canonical_name = CanonicalFamilyName(family_name_alias);
69+
auto family_it = registered_families_.find(canonical_name);
6970
if (family_it == registered_families_.end()) {
7071
family_names_.push_back(family_name_alias);
7172
family_it = registered_families_
7273
.emplace(std::piecewise_construct,
73-
std::forward_as_tuple(family_name_alias),
74+
std::forward_as_tuple(canonical_name),
7475
std::forward_as_tuple())
7576
.first;
7677
}

0 commit comments

Comments
 (0)