Skip to content

Truly independent web browser

LibGfx+LibWebView: Don’t match a symbol face ahead of a text face

sideshowbarkera8747160f5a6
diff --git a/Libraries/LibGfx/Font/FaceDescription.cpp b/Libraries/LibGfx/Font/FaceDescription.cpp
index c83f0bfaf3a..312286c7245 100644
--- a/Libraries/LibGfx/Font/FaceDescription.cpp
+++ b/Libraries/LibGfx/Font/FaceDescription.cpp
@@ -64,6 +64,24 @@ static Optional<String> read_family(hb_face_t* face)
return {};
}
+// https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#windows-platform-platform-id--3
+static bool has_windows_symbol_encoding(hb_face_t* face)
+{
+ static constexpr u16 windows_platform_id = 3;
+ static constexpr u16 symbol_encoding_id = 0;
+
+ FontTable cmap { face, FourCC { "cmap" } };
+ if (!cmap.contains(0, 4))
+ return false;
+ size_t record_count = cmap.u16_at(2);
+ for (size_t index = 0; index < record_count && cmap.contains(4 + 8 * index, 4); ++index) {
+ auto record = 4 + 8 * index;
+ if (cmap.u16_at(record) == windows_platform_id && cmap.u16_at(record + 2) == symbol_encoding_id)
+ return true;
+ }
+ return false;
+}
+
// https://learn.microsoft.com/en-us/typography/opentype/spec/os2#fsselection
static constexpr u16 italic_selection_bit = 1 << 0;
static constexpr u16 oblique_selection_bit = 1 << 9;
@@ -142,6 +160,7 @@ FaceDescription FaceDescription::read(hb_face_t* face)
FaceDescription description;
if (auto family = read_family(face); family.has_value())
description.m_family = FlyString { family.release_value() };
+ description.m_is_symbol_font = has_windows_symbol_encoding(face);
description.m_declared_style = read_declared_style(face);
description.m_vertical_metrics = read_vertical_metrics(face);
diff --git a/Libraries/LibGfx/Font/FaceDescription.h b/Libraries/LibGfx/Font/FaceDescription.h
index 42426429c71..892aa276b92 100644
--- a/Libraries/LibGfx/Font/FaceDescription.h
+++ b/Libraries/LibGfx/Font/FaceDescription.h
@@ -42,6 +42,10 @@ public:
FaceStyle style() const { return m_style; }
FaceVerticalMetrics const& vertical_metrics() const { return m_vertical_metrics; }
+ // Whether the face has a cmap subtable of the Windows symbol encoding, which marks a font of symbols rather than of
+ // text, even where its other subtables map letters (to ornaments, e.g.).
+ bool is_symbol_font() const { return m_is_symbol_font; }
+
// The style of the face with these variations applied.
FaceStyle style_for_variations(ReadonlySpan<FontVariationAxis>) const;
@@ -61,6 +65,7 @@ private:
};
FlyString m_family;
+ bool m_is_symbol_font { false };
// The style that the OS/2 and head tables give, which the axes of a variable face can change.
FaceStyle m_declared_style;
FaceStyle m_style;
diff --git a/Libraries/LibGfx/Font/PathFontProvider.cpp b/Libraries/LibGfx/Font/PathFontProvider.cpp
index 7e11fc635f4..ffba6c2f393 100644
--- a/Libraries/LibGfx/Font/PathFontProvider.cpp
+++ b/Libraries/LibGfx/Font/PathFontProvider.cpp
@@ -47,6 +47,15 @@ void PathFontProvider::load_all_fonts_from_uri(StringView uri)
auto& family = m_typeface_by_family.ensure(font->family(), [] {
return Vector<NonnullRefPtr<Typeface>> {};
});
+ // A face of text goes ahead of the faces of symbol fonts in its family, so that it's the one found for its
+ // weight, width and slope.
+ if (!font->description().is_symbol_font()) {
+ auto index = family.find_first_index_if([](auto const& typeface) { return typeface->description().is_symbol_font(); });
+ if (index.has_value()) {
+ family.insert(*index, move(font));
+ return;
+ }
+ }
family.append(move(font));
});
}
diff --git a/Libraries/LibWebView/FontService.cpp b/Libraries/LibWebView/FontService.cpp
index 33a2526c128..e396d21c42c 100644
--- a/Libraries/LibWebView/FontService.cpp
+++ b/Libraries/LibWebView/FontService.cpp
@@ -84,6 +84,18 @@ ErrorOr<void> FontService::build_catalog()
Optional<Error> callback_error;
u64 next_face_id = 1;
+ struct CollectedFace {
+ FlyString family;
+ u64 face_id { 0 };
+ u32 ttc_index { 0 };
+ u16 weight { 0 };
+ u16 width { 0 };
+ u8 slope { 0 };
+ Gfx::FontFileFormat format { Gfx::FontFileFormat::OpenType };
+ bool is_symbol_font { false };
+ };
+ Vector<CollectedFace> collected_faces;
+
auto directories = TRY(Gfx::FontDatabase::font_directories());
directories.extend(m_additional_font_directories);
for (auto const& directory : directories) {
@@ -114,19 +126,16 @@ ErrorOr<void> FontService::build_catalog()
}
m_local_font_names.set(folded_name.release_value(), face_id, AK::HashSetExistingEntryBehavior::Keep);
}
- auto result = builder->add_face({
- .family = typeface->family().bytes_as_string_view(),
+ collected_faces.append({
+ .family = typeface->family(),
.face_id = face_id,
.ttc_index = ttc_index,
.weight = typeface->weight(),
.width = typeface->width(),
.slope = typeface->slope(),
.format = format,
+ .is_symbol_font = typeface->description().is_symbol_font(),
});
- if (result.is_error()) {
- callback_error = result.release_error();
- return;
- }
FontSource source { .path = path, .ttc_index = ttc_index, .format = format };
m_face_id_by_source.set(source, face_id);
m_font_sources.set(face_id, move(source));
@@ -136,6 +145,24 @@ ErrorOr<void> FontService::build_catalog()
return callback_error.release_value();
}
+ // The catalog matches the faces of a family in the order they're added. So, the faces of symbol fonts go last, and a
+ // face of ornaments in a family of text is never matched in place of the text face of the same style.
+ for (auto symbol_fonts : { false, true }) {
+ for (auto const& face : collected_faces) {
+ if (face.is_symbol_font != symbol_fonts)
+ continue;
+ TRY(builder->add_face({
+ .family = face.family.bytes_as_string_view(),
+ .face_id = face.face_id,
+ .ttc_index = face.ttc_index,
+ .weight = face.weight,
+ .width = face.width,
+ .slope = face.slope,
+ .format = face.format,
+ }));
+ }
+ }
+
auto serialized = TRY(builder->serialize());
m_catalog_size = serialized.size();
m_catalog_file = TRY(create_immutable_font_data(serialized.bytes()));
diff --git a/Tests/LibGfx/TestFont.cpp b/Tests/LibGfx/TestFont.cpp
index 4f405d090b8..f3465a325c7 100644
--- a/Tests/LibGfx/TestFont.cpp
+++ b/Tests/LibGfx/TestFont.cpp
@@ -405,6 +405,20 @@ TEST_CASE(font_collection_preserves_each_face_style)
EXPECT(Gfx::Typeface::try_load_from_temporary_memory(file->bytes(), 3).is_error());
}
+TEST_CASE(face_with_a_symbol_cmap_subtable_is_a_symbol_font)
+{
+ auto file = MUST(Core::MappedFile::map(TEST_INPUT("fonts/symbol-face.ttc"sv)));
+ auto symbol_face = MUST(Gfx::Typeface::try_load_from_temporary_memory(file->bytes(), 0));
+ auto text_face = MUST(Gfx::Typeface::try_load_from_temporary_memory(file->bytes(), 1));
+ EXPECT_EQ(symbol_face->family(), "Symbol Face Test"sv);
+ EXPECT_EQ(text_face->family(), "Symbol Face Test"sv);
+ EXPECT(symbol_face->description().is_symbol_font());
+ EXPECT(!text_face->description().is_symbol_font());
+
+ // The symbol face still maps a letter through its Unicode subtable.
+ EXPECT_NE(symbol_face->font(12)->glyph_id_for_code_point('a'), 0u);
+}
+
// The answer depends on the installed font set alone, so every thread must reach the same one and
// the memo must match a code point once however many threads ask at the same moment.
TEST_CASE(system_fallback_fonts_can_be_matched_on_several_threads)
diff --git a/Tests/LibGfx/TestSharedFontProvider.cpp b/Tests/LibGfx/TestSharedFontProvider.cpp
index ccb968d7f49..5af59c7a13e 100644
--- a/Tests/LibGfx/TestSharedFontProvider.cpp
+++ b/Tests/LibGfx/TestSharedFontProvider.cpp
@@ -340,3 +340,22 @@ TEST_CASE(file_collection_faces_share_the_same_mapping)
faces.append(move(typeface)); });
EXPECT_EQ(faces.size(), 3u);
}
+
+TEST_CASE(path_provider_finds_a_symbol_face_last)
+{
+ // The first face of the collection has a symbol cmap subtable, and the second doesn't. Both are of one family, at
+ // the same weight, width and slope.
+ Gfx::PathFontProvider provider;
+ provider.load_all_fonts_from_uri(MUST(String::formatted("file://{}/Tests/LibGfx/test-inputs/fonts", LADYBIRD_SOURCE_DIR)));
+
+ auto font = provider.get_font("Symbol Face Test"_fly_string, 12, 400, 5, 0);
+ EXPECT(font);
+ if (font)
+ EXPECT_EQ(font->typeface().collection_index(), 1u);
+
+ Vector<u32> indices;
+ provider.for_each_typeface_with_family_name("Symbol Face Test"_fly_string, [&](Gfx::Typeface const& typeface) {
+ indices.append(typeface.collection_index());
+ });
+ EXPECT_EQ(indices, (Vector<u32> { 1, 0 }));
+}
diff --git a/Tests/LibGfx/test-inputs/fonts/symbol-face.ttc b/Tests/LibGfx/test-inputs/fonts/symbol-face.ttc
new file mode 100644
index 00000000000..1d065c20279
Binary files /dev/null and b/Tests/LibGfx/test-inputs/fonts/symbol-face.ttc differ
diff --git a/Tests/LibWebView/TestFontService.cpp b/Tests/LibWebView/TestFontService.cpp
index 2a301fb97c2..1a6f7d7c390 100644
--- a/Tests/LibWebView/TestFontService.cpp
+++ b/Tests/LibWebView/TestFontService.cpp
@@ -6,6 +6,7 @@
#include <LibCore/MappedFile.h>
#include <LibFileSystem/FileSystem.h>
+#include <LibGfx/Font/FontCatalog.h>
#include <LibTest/TestCase.h>
#include <LibWebView/FontService.h>
#include <sys/mman.h>
@@ -97,3 +98,23 @@ TEST_CASE(dynamic_matches_of_a_catalog_face_use_its_catalog_id)
auto match = WebView::FontServiceTestAccess::materialize(*service, test_font_file("text.ttf"sv), "catalog-face"_string);
EXPECT_EQ(match.face_id, *catalog_face_id);
}
+
+TEST_CASE(catalog_matches_a_symbol_face_last)
+{
+ // The first face of the collection has a symbol cmap subtable, and the second doesn't. Both are of one family, at
+ // the same weight, width and slope.
+ auto font_directory = MUST(FileSystem::real_path("../LibGfx/test-inputs/fonts"sv));
+ auto service = WebView::FontService::create({ MUST(String::from_byte_string(font_directory)) });
+ auto catalog = MUST(service->clone_catalog());
+ auto mapping = MUST(Core::MappedFile::map_from_fd_range_and_close(catalog.file.take_fd(), "font catalog"sv, 0, catalog.size));
+ auto parsed_catalog = MUST(Gfx::FontCatalog::parse(mapping->bytes(), catalog.generation));
+
+ auto match = parsed_catalog->match_style("Symbol Face Test"sv, 400, 5, 0);
+ EXPECT(match.has_value());
+ EXPECT_EQ(match->ttc_index, 1u);
+
+ EXPECT_EQ(parsed_catalog->family_face_count("Symbol Face Test"sv), 2u);
+ auto first_face = parsed_catalog->family_face_at("Symbol Face Test"sv, 0);
+ EXPECT(first_face.has_value());
+ EXPECT_EQ(first_face->ttc_index, 1u);
+}