Truly independent web browser
LibGfx: Apply letter-spacing once per cluster, not once per glyph
sideshowbarker75420163548a
diff --git a/Libraries/LibGfx/TextLayout.cpp b/Libraries/LibGfx/TextLayout.cppindex b0fc2e35fcb..5b2892e130a 100644--- a/Libraries/LibGfx/TextLayout.cpp+++ b/Libraries/LibGfx/TextLayout.cpp@@ -566,10 +566,20 @@ static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font co- FloatPoint { 0, metrics.ascent }+ FloatPoint { positions[i].x_offset, -positions[i].y_offset } / text_shaping_resolution;- auto 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;+ // https://drafts.csswg.org/css-text-3/#letter-spacing-property+ // This property specifies additional spacing (commonly called tracking) between adjacent typographic character+ // units.+ // NB: A cluster's glyphs (a base and its marks, or a conjunct's parts) are one typographic character unit, so+ // the spacing goes after the cluster's last glyph only. Spacing after any other glyph of the cluster would+ // push the glyphs after it away from the ones they attach to.+ auto 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 (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;+ }glyphs.unchecked_append({.position = position,@@ -604,14 +614,20 @@ float measure_text_width(Utf16View const& string, Font const& font, float letterauto* buffer = setup_text_shaping(string, font, FFI::TextType::Common);u32 glyph_count;+ auto 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;- for (size_t i = 0; i < glyph_count; ++i)+ 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)+ ++cluster_count;+ }hb_buffer_destroy(buffer);- return static_cast<float>(point_x / text_shaping_resolution + glyph_count * letter_spacing);+ return static_cast<float>(point_x / text_shaping_resolution + cluster_count * letter_spacing);}}diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-combining-mark-001-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-combining-mark-001-ref.htmlnew file mode 100644index 00000000000..07a6ffa5478--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-combining-mark-001-ref.html@@ -0,0 +1,35 @@+<!DOCTYPE html>+<html lang="en">+<meta charset="utf-8">+<title>CSS Text reference: letter-spacing does not separate a combining mark from its base</title>+<link rel="author" title="Brent Fulgham" href="mailto:[email protected]">++<style>+/* Same font, size and letter-spacing as the test, but drawing each cluster's+ bars with a single precomposed glyph instead of a base plus marks, so no GPOS+ mark positioning is involved. 'B' is the bottom and middle bars, 'C' is all+ three. Each glyph is one typographic character unit, exactly as each+ base-plus-marks cluster in the test is, so the two get the same number of+ letter-spacing insertions -- which also makes this reference insensitive to+ whether an implementation adds letter-spacing before or after each unit. */+@font-face {+ font-family: "mark-anchor-test";+ src: url("../../../../fonts/mark-anchor-test.ttf") format("truetype");+}+div {+ font-family: "mark-anchor-test";+ font-size: 100px;+ line-height: 150px;+ letter-spacing: 100px;+ white-space: nowrap;+}+</style>++<p>Test passes if every black bar below is exactly as wide as, and left-aligned+with, the bars above and below it in the same group — two groups in the+middle row — and no bar is offset to the right of the others.</p>++<div>B</div>+<div>BB</div>+<div>C</div>+</html>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-combining-mark-002-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-combining-mark-002-ref.htmlnew file mode 100644index 00000000000..766d68c1dda--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-combining-mark-002-ref.html@@ -0,0 +1,34 @@+<!DOCTYPE html>+<html lang="en">+<meta charset="utf-8">+<title>CSS Text reference: letter-spacing does not separate a combining mark from its base in RTL</title>+<link rel="author" title="Brent Fulgham" href="mailto:[email protected]">++<style>+/* Same font, size, letter-spacing and direction as the test, but drawing each+ cluster's bars with one precomposed glyph instead of a base plus marks, so no+ GPOS mark positioning is involved. Each glyph is one typographic character+ unit, as each cluster in the test is, so both get the same number of+ letter-spacing insertions. */+@font-face {+ font-family: "mark-anchor-test";+ src: url("../../../../fonts/mark-anchor-test.ttf") format("truetype");+}+div {+ font-family: "mark-anchor-test";+ font-size: 100px;+ line-height: 150px;+ letter-spacing: 100px;+ direction: rtl;+ white-space: nowrap;+}+</style>++<p>Test passes if every black bar below is exactly as wide as, and right-aligned+with, the bars above and below it in the same group — two groups in the+middle row — and no bar is offset to the left of the others.</p>++<div>ב</div>+<div>בב</div>+<div>ג</div>+</html>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-combining-mark-001.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-combining-mark-001.htmlnew file mode 100644index 00000000000..83b75f86c8a--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-combining-mark-001.html@@ -0,0 +1,44 @@+<!DOCTYPE html>+<html lang="en">+<meta charset="utf-8">+<title>CSS Text test: letter-spacing does not separate a combining mark from its base</title>+<link rel="author" title="Brent Fulgham" href="mailto:[email protected]">+<link rel="help" href="https://drafts.csswg.org/css-text-3/#letter-spacing-property">+<link rel="match" href="../../../../../expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-combining-mark-001-ref.html">+<meta name="assert" content="Letter spacing is inserted between typographic character units. A grapheme cluster base and the combining marks following it are one typographic character unit, so letter-spacing must neither be inserted inside the cluster nor displace a mark relative to its base, even when the font positions the mark with a GPOS mark-to-base anchor rather than with glyph advances.">++<style>+/* ../../../fonts/mark-anchor-test.ttf positions U+0301 and U+0300 on 'A' with GPOS+ mark-to-base anchors. Each glyph is a full-width bar in its own horizontal+ band: 'A' is the bottom bar, U+0301 the middle bar, U+0300 the top bar, and+ the marks are zero-advance glyphs whose anchors place them directly above+ their base. 'B' draws the bottom and middle bars as one glyph and 'C' draws+ all three, so a correctly positioned cluster paints exactly what the+ reference paints. Letter-spacing that displaces a mark slides that mark's+ bar out from over the base's bar instead.+ Generated by css/css-text/tools/generate-mark-anchor-font.py. */+@font-face {+ font-family: "mark-anchor-test";+ src: url("../../../fonts/mark-anchor-test.ttf") format("truetype");+}+div {+ font-family: "mark-anchor-test";+ font-size: 100px;+ line-height: 150px;+ /* One em, so a displaced mark lands clear of the base it belongs on. */+ letter-spacing: 100px;+ white-space: nowrap;+}+</style>++<p>Test passes if every black bar below is exactly as wide as, and left-aligned+with, the bars above and below it in the same group — two groups in the+middle row — and no bar is offset to the right of the others.</p>++<!-- One base, one mark. -->+<div>Á</div>+<!-- Two clusters, so any displacement would accumulate across them. -->+<div>ÁÁ</div>+<!-- Two marks on one base; each takes its position from the same base. -->+<div>Á̀</div>+</html>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-combining-mark-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-combining-mark-002.htmlnew file mode 100644index 00000000000..c0ddf67a5ca--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-combining-mark-002.html@@ -0,0 +1,42 @@+<!DOCTYPE html>+<html lang="en">+<meta charset="utf-8">+<title>CSS Text test: letter-spacing does not separate a combining mark from its base in RTL</title>+<link rel="author" title="Brent Fulgham" href="mailto:[email protected]">+<link rel="help" href="https://drafts.csswg.org/css-text-3/#letter-spacing-property">+<link rel="match" href="../../../../../expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-combining-mark-002-ref.html">+<meta name="assert" content="Letter spacing is inserted between typographic character units, in a right-to-left run as much as in a left-to-right one. A grapheme cluster base and the combining marks following it are one typographic character unit, so letter-spacing must neither be inserted inside the cluster nor displace a mark relative to its base.">++<style>+/* The RTL counterpart of letter-spacing-combining-mark-001, on Hebrew bases (a+ genuine RTL run rather than a bidi-override, which would skip bidi rule W1):+ ALEF is the bottom bar, BET the bottom and middle bars as one glyph, and GIMEL+ all three. U+0301 and U+0300 are zero-advance marks anchored above their base+ with a GPOS mark-to-base anchor.+ Generated by css/css-text/tools/generate-mark-anchor-font.py. */+@font-face {+ font-family: "mark-anchor-test";+ src: url("../../../fonts/mark-anchor-test.ttf") format("truetype");+}+div {+ font-family: "mark-anchor-test";+ font-size: 100px;+ line-height: 150px;+ /* One em, so a displaced mark lands clear of the base it belongs on. */+ letter-spacing: 100px;+ direction: rtl;+ white-space: nowrap;+}+</style>++<p>Test passes if every black bar below is exactly as wide as, and right-aligned+with, the bars above and below it in the same group — two groups in the+middle row — and no bar is offset to the left of the others.</p>++<!-- One base, one mark. -->+<div>א́</div>+<!-- Two clusters, so any displacement would accumulate across them. -->+<div>א́א́</div>+<!-- Two marks on one base; each takes its position from the same base. -->+<div>א́̀</div>+</html>