Truly independent web browser
LibGfx: Don’t letter-space the letters of cursive scripts
sideshowbarker7d43d1f6aeab
diff --git a/Libraries/LibGfx/TextLayout.cpp b/Libraries/LibGfx/TextLayout.cppindex 5b2892e130a..cb25bce3747 100644--- a/Libraries/LibGfx/TextLayout.cpp+++ b/Libraries/LibGfx/TextLayout.cpp@@ -7,6 +7,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/AnyOf.h>#include <AK/Array.h>#include <AK/BitCast.h>#include <AK/HashFunctions.h>@@ -476,6 +477,51 @@ static bool is_word_separator(u32 code_point)return code_point == 0x0020 || code_point == 0x00A0 || code_point == 0x000A;}+// https://drafts.csswg.org/css-text-3/#cursive-script+static bool is_cursive_script(u32 code_point)+{+ switch (hb_unicode_script(hb_unicode_funcs_get_default(), code_point)) {+ case HB_SCRIPT_ARABIC:+ case HB_SCRIPT_HANIFI_ROHINGYA:+ case HB_SCRIPT_MANDAIC:+ case HB_SCRIPT_MONGOLIAN:+ case HB_SCRIPT_NKO:+ case HB_SCRIPT_PHAGS_PA:+ case HB_SCRIPT_SYRIAC:+ return true;+ case HB_SCRIPT_COMMON:+ case HB_SCRIPT_INHERITED: {+ // https://drafts.csswg.org/css-text-3/#character-properties+ // script property: Defined in Unicode Standard Annex #24 [UAX24] and given as the Script property in the+ // Unicode Character Database [UAX44]. (UAs must include any ScriptExtensions.txt assignments in this mapping.)+ // NB: So a character that serves several scripts, as the tatweel (U+0640) does, belongs to a cursive script+ // when one of its script extensions is one.+ static auto const cursive_scripts = [] {+ constexpr Array names { "Arab"sv, "Rohg"sv, "Mand"sv, "Mong"sv, "Nkoo"sv, "Phag"sv, "Syrc"sv };+ Array<Unicode::Script, names.size()> scripts;+ for (size_t i = 0; i < names.size(); ++i)+ scripts[i] = Unicode::script_from_string(names[i]).release_value();+ return scripts;+ }();+ return any_of(cursive_scripts, [&](auto script) { return Unicode::code_point_has_script_extension(code_point, script); });+ }+ default:+ return false;+ }+}++// Whether letter-spacing goes after the last glyph of the cluster starting at cluster_start: Every cluster takes it but+// one that starts with a character of a cursive script.+static bool cluster_takes_letter_spacing(Utf16View const& string, u32 cluster_start)+{+ // https://drafts.csswg.org/css-text-3/#cursive-tracking+ // Otherwise, if the UA cannot expand text from a cursive script without breaking its cursive connections, it must+ // not apply spacing between any pair of that script's typographic letter units at all (effectively treating each+ // word as a single typographic letter unit for the purpose of letter-spacing).+ // NB: Spaces still take it: Their script is Common, with no extensions.+ return cluster_start >= string.length_in_code_units() || !is_cursive_script(string.code_point_at(cluster_start));+}+static size_t length_of_trailing_whitespace_run(Utf16View const& string){size_t length = 0;@@ -575,7 +621,8 @@ static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font coauto is_last_glyph_of_cluster = i + 1 == glyph_count || glyph_info[i + 1].cluster != glyph_info[i].cluster;float extra_advance = 0;if (is_last_glyph_of_cluster) {- extra_advance = letter_spacing;+ if (letter_spacing != 0 && cluster_takes_letter_spacing(string, glyph_info[i].cluster))+ extra_advance = letter_spacing;if (auto starting_offset = glyph_info[i].cluster; starting_offset < string.length_in_code_units()&& is_word_separator(string.code_point_at(starting_offset)))extra_advance += word_spacing;@@ -617,12 +664,12 @@ float measure_text_width(Utf16View const& string, Font const& font, float letterauto const* glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);auto const* positions = hb_buffer_get_glyph_positions(buffer, &glyph_count);- // Letter-spacing goes once per cluster; see build_origin_relative_shape().double point_x = 0;size_t cluster_count = 0;for (size_t i = 0; i < glyph_count; ++i) {point_x += positions[i].x_advance;- if (i + 1 == glyph_count || glyph_info[i + 1].cluster != glyph_info[i].cluster)+ if (letter_spacing != 0 && (i + 1 == glyph_count || glyph_info[i + 1].cluster != glyph_info[i].cluster)+ && cluster_takes_letter_spacing(string, glyph_info[i].cluster))++cluster_count;}diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-cursive-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-cursive-001-ref.htmlnew file mode 100644index 00000000000..720c9491b4d--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-cursive-001-ref.html@@ -0,0 +1,32 @@+<!DOCTYPE html>+<head>+<meta charset="utf-8">+<title>+CSS Text Test - Letter-spacing should not be applied for Cursive Scripts.+</title>+<link rel="author" title="Sejal Anand" href="mailto:[email protected]">+<style>+div {+ margin: 1em;+ font: 24px serif;+ white-space: nowrap;+ display: inline-block;+ outline: 1px solid gray;+}+</style>+</head>+<body>+<p>+The following test will pass if no spacing appears between letters within+Arabic words. Both Arabic texts should display identically.+</p>+<div>+<span>مرحباً</span>+</div>+<br>+<div>+<span>مرحباً</span>+</div>+<br>+</body>+</html>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-cursive-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-cursive-001.htmlnew file mode 100644index 00000000000..dc2eb7917e1--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-cursive-001.html@@ -0,0 +1,39 @@+<!DOCTYPE html>+<head>+<meta charset="utf-8">+<title>+CSS Text Test - Letter-spacing should not be applied for Cursive Scripts.+</title>+<link rel="author" title="Sejal Anand" href="mailto:[email protected]">+<link rel="help" href="https://www.w3.org/TR/css-text-3/#cursive-tracking">+<link rel="match" href="../../../../../expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-cursive-001-ref.html">+<meta name="assert" content="Letter-spacing must not be applied to Arabic text,+ preserving cursive connections.">+<style>+div {+ margin: 1em;+ font: 24px serif;+ white-space: nowrap;+ display: inline-block;+ outline: 1px solid gray;+}+.letterspacing {+ letter-spacing: 10px; /* Should NOT affect Arabic text */+}+</style>+</head>+<body>+<p>+The following test will pass if no spacing appears between letters within+Arabic words. Both Arabic texts should display identically.+</p>+<div class="letterspacing">+<span>مرحباً</span>+</div>+<br>+<div>+<span>مرحباً</span>+</div>+<br>+</body>+</html>diff --git a/Tests/LibWeb/Text/expected/css/letter-spacing-cursive-scripts.txt b/Tests/LibWeb/Text/expected/css/letter-spacing-cursive-scripts.txtnew file mode 100644index 00000000000..9e59ebc7a09--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/letter-spacing-cursive-scripts.txt@@ -0,0 +1,10 @@+Arabic word: 0+Arabic with harakat: 0+Arabic with tatweel: 0+Arabic with Arabic-Indic digits: 0+Arabic words with a space: 1+Syriac word: 0+N'Ko word: 0+Mongolian word: 0+Latin word: 3+Hebrew word: 4diff --git a/Tests/LibWeb/Text/input/css/letter-spacing-cursive-scripts.html b/Tests/LibWeb/Text/input/css/letter-spacing-cursive-scripts.htmlnew file mode 100644index 00000000000..0cee675441c--- /dev/null+++ b/Tests/LibWeb/Text/input/css/letter-spacing-cursive-scripts.html@@ -0,0 +1,44 @@+<!DOCTYPE html>+<meta charset="utf-8">+<style>+ span {+ font-size: 20px;+ white-space: pre;+ }+ .spaced {+ letter-spacing: 10px;+ }+</style>+ <div><span>مرحبا</span><span class="spaced">مرحبا</span></div>+ <div><span>بَب</span><span class="spaced">بَب</span></div>+ <div><span>بـب</span><span class="spaced">بـب</span></div>+ <div><span>ب١٢ب</span><span class="spaced">ب١٢ب</span></div>+ <div><span>بب بب</span><span class="spaced">بب بب</span></div>+ <div><span>ܫܠܡܐ</span><span class="spaced">ܫܠܡܐ</span></div>+ <div><span>ߒߞߏ</span><span class="spaced">ߒߞߏ</span></div>+ <div><span>ᠮᠣᠩᠭᠤᠯ</span><span class="spaced">ᠮᠣᠩᠭᠤᠯ</span></div>+ <div><span>abc</span><span class="spaced">abc</span></div>+ <div><span>שלום</span><span class="spaced">שלום</span></div>+<script src="../include.js"></script>+<script>+ test(() => {+ // How many times the spacing went into each sample: what letter-spacing adds to its width, over the spacing.+ const names = [+ "Arabic word",+ "Arabic with harakat",+ "Arabic with tatweel",+ "Arabic with Arabic-Indic digits",+ "Arabic words with a space",+ "Syriac word",+ "N'Ko word",+ "Mongolian word",+ "Latin word",+ "Hebrew word",+ ];+ document.querySelectorAll("div").forEach((div, index) => {+ const [plain, spaced] = div.querySelectorAll("span");+ const count = Math.round((spaced.getBoundingClientRect().width - plain.getBoundingClientRect().width) / 10);+ println(`${names[index]}: ${count}`);+ });+ });+</script>