Truly independent web browser
LibGfx: Turn off optional ligatures under a non-zero letter-spacing
sideshowbarker9803f2d5d888
diff --git a/Libraries/LibGfx/FontCascadeList.cpp b/Libraries/LibGfx/FontCascadeList.cppindex e3da52d96b9..f83d5800e1e 100644--- a/Libraries/LibGfx/FontCascadeList.cpp+++ b/Libraries/LibGfx/FontCascadeList.cpp@@ -583,8 +583,9 @@ extern "C" void ladybird_gfx_cascade_snapshot_header(void const* handle, Gfx::FF}// NB: Rust reads these as its FfiShapeFeature.-static_assert(sizeof(Gfx::ShapeFeature) == 8);+static_assert(sizeof(Gfx::ShapeFeature) == 12);static_assert(offsetof(Gfx::ShapeFeature, value) == 4);+static_assert(offsetof(Gfx::ShapeFeature, set_by_feature_settings) == 8);extern "C" void ladybird_gfx_cascade_snapshot_system_fallback_features(void const* handle, Gfx::ShapeFeature* out_features){diff --git a/Libraries/LibGfx/Rust/src/font.rs b/Libraries/LibGfx/Rust/src/font.rsindex c6b3ecbb3e6..37593d71912 100644--- a/Libraries/LibGfx/Rust/src/font.rs+++ b/Libraries/LibGfx/Rust/src/font.rs@@ -453,12 +453,13 @@ struct FfiCascadeSnapshotHeader {system_fallback_feature_count: usize,}-/// A `Gfx::ShapeFeature`: an OpenType feature tag and its value.+/// A `Gfx::ShapeFeature`: an OpenType feature tag, its value, and whether font-feature-settings set it.#[repr(C)]#[derive(Clone, Copy, Default, Debug, PartialEq)]struct FfiShapeFeature {tag: [u8; 4],value: u32,+ set_by_feature_settings: bool,}#[repr(C)]diff --git a/Libraries/LibGfx/ShapeFeature.cpp b/Libraries/LibGfx/ShapeFeature.cppindex 2b9753bcdfb..77dc18bd65a 100644--- a/Libraries/LibGfx/ShapeFeature.cpp+++ b/Libraries/LibGfx/ShapeFeature.cpp@@ -15,6 +15,7 @@ ErrorOr<void> encode(Encoder& encoder, Gfx::ShapeFeature const& feature){TRY(encoder.append(reinterpret_cast<u8 const*>(feature.tag), sizeof(feature.tag)));TRY(encoder.encode(feature.value));+ TRY(encoder.encode(feature.set_by_feature_settings));return {};}@@ -24,6 +25,7 @@ ErrorOr<Gfx::ShapeFeature> decode(Decoder& decoder)Gfx::ShapeFeature feature {};TRY(decoder.decode_into(Bytes { reinterpret_cast<u8*>(feature.tag), sizeof(feature.tag) }));feature.value = TRY(decoder.decode<u32>());+ feature.set_by_feature_settings = TRY(decoder.decode<bool>());return feature;}diff --git a/Libraries/LibGfx/ShapeFeature.h b/Libraries/LibGfx/ShapeFeature.hindex 11dba9b2b95..0c48bd98066 100644--- a/Libraries/LibGfx/ShapeFeature.h+++ b/Libraries/LibGfx/ShapeFeature.h@@ -14,6 +14,9 @@ namespace Gfx {struct ShapeFeature {char tag[4];u32 value;+ // Whether font-feature-settings set this feature, which keeps it in place over the optional ligatures that a+ // non-zero letter-spacing turns off: https://drafts.csswg.org/css-fonts-4/#feature-variation-precedence+ bool set_by_feature_settings { false };bool operator==(ShapeFeature const&) const = default;bool operator!=(ShapeFeature const&) const = default;@@ -47,6 +50,7 @@ struct Traits<Gfx::ShapeFeatures> : public DefaultTraits<Gfx::ShapeFeatures> {hash = pair_int_hash(hash, feature.tag[2]);hash = pair_int_hash(hash, feature.tag[3]);hash = pair_int_hash(hash, feature.value);+ hash = pair_int_hash(hash, feature.set_by_feature_settings);}return hash;diff --git a/Libraries/LibGfx/TextLayout.cpp b/Libraries/LibGfx/TextLayout.cppindex cb25bce3747..3fb3da94e6d 100644--- a/Libraries/LibGfx/TextLayout.cpp+++ b/Libraries/LibGfx/TextLayout.cpp@@ -409,7 +409,7 @@ Vector<float> glyph_run_glyph_intercepts(Font const& font, ReadonlySpan<ShapedGlreturn intervals;}-static hb_buffer_t* setup_text_shaping(Utf16View const& string, Font const& font, FFI::TextType text_type, FFI::JoiningContext joining_context = {})+static hb_buffer_t* setup_text_shaping(Utf16View const& string, Font const& font, FFI::TextType text_type, float letter_spacing, FFI::JoiningContext joining_context = {}){hb_buffer_t* buffer = hb_buffer_create();@@ -445,22 +445,35 @@ static hb_buffer_t* setup_text_shaping(Utf16View const& string, Font const& fonthb_buffer_add_codepoints(buffer, &ZERO_WIDTH_JOINER, 1, 0, 0);auto* hb_font = font.harfbuzz_font();- hb_feature_t const* hb_features_data = nullptr;- Vector<hb_feature_t, 4> hb_features;- if (!font.features().is_empty()) {- hb_features.ensure_capacity(font.features().size());- for (auto const& feature : font.features()) {- hb_features.unchecked_append({- .tag = HB_TAG(feature.tag[0], feature.tag[1], feature.tag[2], feature.tag[3]),- .value = feature.value,- .start = 0,- .end = HB_FEATURE_GLOBAL_END,+ Vector<hb_feature_t, 8> hb_features;+ hb_features.ensure_capacity(font.features().size());+ for (auto const& feature : font.features()) {+ hb_features.unchecked_append({+ .tag = HB_TAG(feature.tag[0], feature.tag[1], feature.tag[2], feature.tag[3]),+ .value = feature.value,+ .start = 0,+ .end = HB_FEATURE_GLOBAL_END,+ });+ }++ // https://drafts.csswg.org/css-fonts-4/#feature-variation-precedence+ // 11. Feature settings determined by properties other than font-variant or font-feature-settings are applied. For+ // example, setting a non-default value for the letter-spacing property disables optional ligatures.+ // NB: The optional ligatures are the ones CSS Text names: those not required for fundamentally correct glyph+ // shaping, which leaves rlig on. A feature that font-feature-settings set stays as it is, since step 13+ // applies font-feature-settings after this step. And HarfBuzz applies the last of the features with one tag,+ // so these go after the font's own.+ if (letter_spacing != 0) {+ for (auto tag : { HB_TAG('l', 'i', 'g', 'a'), HB_TAG('c', 'l', 'i', 'g'), HB_TAG('d', 'l', 'i', 'g'), HB_TAG('h', 'l', 'i', 'g') }) {+ auto set_by_feature_settings = any_of(font.features(), [&](auto const& feature) {+ return feature.set_by_feature_settings && HB_TAG(feature.tag[0], feature.tag[1], feature.tag[2], feature.tag[3]) == tag;});+ if (!set_by_feature_settings)+ hb_features.append({ .tag = tag, .value = 0, .start = 0, .end = HB_FEATURE_GLOBAL_END });}- hb_features_data = hb_features.data();}- hb_shape(hb_font, buffer, hb_features_data, font.features().size());+ hb_shape(hb_font, buffer, hb_features.data(), hb_features.size());return buffer;}@@ -545,7 +558,7 @@ struct ShapedGlyphs {static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font const& font, FFI::TextType text_type, float letter_spacing, float word_spacing, FFI::JoiningContext joining_context){auto const& metrics = font.pixel_metrics();- auto* buffer = setup_text_shaping(string, font, text_type, joining_context);+ auto* buffer = setup_text_shaping(string, font, text_type, letter_spacing, joining_context);u32 glyph_count;auto const* glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);@@ -658,7 +671,7 @@ static ShapedGlyphs build_origin_relative_shape(Utf16View const& string, Font cofloat measure_text_width(Utf16View const& string, Font const& font, float letter_spacing){- auto* buffer = setup_text_shaping(string, font, FFI::TextType::Common);+ auto* buffer = setup_text_shaping(string, font, FFI::TextType::Common, letter_spacing);u32 glyph_count;auto const* glyph_info = hb_buffer_get_glyph_infos(buffer, &glyph_count);diff --git a/Libraries/LibWeb/CSS/FontResolution.cpp b/Libraries/LibWeb/CSS/FontResolution.cppindex 1dcb23a5980..f447575d1df 100644--- a/Libraries/LibWeb/CSS/FontResolution.cpp+++ b/Libraries/LibWeb/CSS/FontResolution.cpp@@ -289,9 +289,9 @@ static Gfx::ShapeFeatures shape_features_for(ComputedFontCacheKey const& key, Foreturn values->data();},&features,- [](void* features, u32 tag, u32 value) {+ [](void* features, u32 tag, u32 value, bool set_by_feature_settings) {auto four_cc = Gfx::FourCC::from_u32(tag);- static_cast<Gfx::ShapeFeatures*>(features)->append({ { four_cc.cc[0], four_cc.cc[1], four_cc.cc[2], four_cc.cc[3] }, value });+ static_cast<Gfx::ShapeFeatures*>(features)->append({ { four_cc.cc[0], four_cc.cc[1], four_cc.cc[2], four_cc.cc[3] }, value, set_by_feature_settings });});return features;}diff --git a/Libraries/LibWeb/Rust/src/css/font_feature_values.rs b/Libraries/LibWeb/Rust/src/css/font_feature_values.rsindex f70ecb44e4d..a525a83cae4 100644--- a/Libraries/LibWeb/Rust/src/css/font_feature_values.rs+++ b/Libraries/LibWeb/Rust/src/css/font_feature_values.rs@@ -279,14 +279,19 @@ pub extern "C" fn rust_font_feature_values_clear(owner: &FontFeatureValuesRule,/// The OpenType features a style asks of a font, by tag, the later setting of a tag winning.#[derive(Default)]-struct ShapeFeatures(Vec<(u32, u32)>);+/// Each feature's tag, its value, and whether font-feature-settings set it.+struct ShapeFeatures(Vec<(u32, u32, bool)>);impl ShapeFeatures {fn set(&mut self, tag: &[u8; 4], value: u32) {+ self.set_from(tag, value, false);+ }++ fn set_from(&mut self, tag: &[u8; 4], value: u32, set_by_feature_settings: bool) {let tag = u32::from_be_bytes(*tag);- match self.0.iter_mut().find(|(existing, _)| *existing == tag) {- Some(feature) => feature.1 = value,- None => self.0.push((tag, value)),+ match self.0.iter_mut().find(|(existing, ..)| *existing == tag) {+ Some(feature) => (feature.1, feature.2) = (value, set_by_feature_settings),+ None => self.0.push((tag, value, set_by_feature_settings)),}}}@@ -323,7 +328,8 @@ fn integer_of(value: &StyleValueData) -> Option<i32> {/// The OpenType features the computed values of a font resolution request ask for, by/// `FontResolutionFeatureInput` and null for a property with its initial value. The names in/// `font-variant-alternates` are looked up in a family's `@font-feature-values` through `lookup`,-/// when `family_values` is not null. Each feature goes to `append` as a big-endian tag and a value.+/// when `family_values` is not null. Each feature goes to `append` as a big-endian tag, a value,+/// and whether font-feature-settings set it./// https://drafts.csswg.org/css-fonts-4/#feature-variation-precedence////// # Safety@@ -336,7 +342,7 @@ pub unsafe extern "C" fn rust_font_shape_features(family_values: *const c_void,lookup: unsafe extern "C" fn(*const c_void, FontFeatureValuesRuleKind, FfiUtf16View, &mut usize) -> *const u32,features: *mut c_void,- append: unsafe extern "C" fn(*mut c_void, u32, u32),+ append: unsafe extern "C" fn(*mut c_void, u32, u32, bool),) {use crate::css::css_enums::keyword::*;use crate::css::style::bridge::{FONT_RESOLUTION_FEATURE_INPUT_COUNT, FontResolutionFeatureInput as Input};@@ -507,14 +513,14 @@ pub unsafe extern "C" fn rust_font_shape_features({let packed_tag = open_type_tagged.packed_tag;// NB: A feature setting is kept as one byte.- shape.set(&packed_tag.to_be_bytes(), u32::from(setting as u8));+ shape.set_from(&packed_tag.to_be_bytes(), u32::from(setting as u8), true);}}}- for (tag, value) in shape.0 {+ for (tag, value, set_by_feature_settings) in shape.0 {// SAFETY: Guaranteed by the caller.- unsafe { append(features, tag, value) };+ unsafe { append(features, tag, value, set_by_feature_settings) };}}diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-ligatures-002-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-ligatures-002-ref.htmlnew file mode 100644index 00000000000..1e6447dbf0b--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-text/letter-spacing/reference/letter-spacing-ligatures-002-ref.html@@ -0,0 +1,29 @@+<!DOCTYPE html>+<html lang="en" >+<meta charset="utf-8">+<title>letter spacing and ligatures</title>+<link rel='author' title='Florian Rivoal' href='https://florian.rivoal.net'>+<style>+@font-face {+ font-family: 'mplus';+ src: url('../../../../fonts/mplus-1p-regular.woff');+}+div {+ font-size: 2em;+ font-family: mplus;+ width: 12ch;+ font-variant-ligatures: none;+ letter-spacing: 0;+ font-kerning: none;+}+i {+ font-style: normal;+ padding-left: 20px;+}+.ref {+ color: blue;+}+</style>+<p>Test passes if the black text looks like the blue one.+<div>o<i></i>f<i></i>f<i></i>i<i></i>c<i></i>e</div>+<div class=ref>o<i></i>f<i></i>f<i></i>i<i></i>c<i></i>e</div>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-ligatures-002.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-ligatures-002.htmlnew file mode 100644index 00000000000..0e6d9aec127--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-text/letter-spacing/letter-spacing-ligatures-002.html@@ -0,0 +1,34 @@+<!DOCTYPE html>+<html lang="en" >+<meta charset="utf-8">+<title>letter spacing and ligatures</title>+<link rel='author' title='Florian Rivoal' href='https://florian.rivoal.net'>+<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-ligatures-002-ref.html'>+<meta name=flags content="should">+<meta name="assert" content="When the effective spacing between two characters is not zero (due to […] letter spacing), user agents should not apply optional ligatures">+<style>+@font-face {+ font-family: 'mplus';+ src: url('../../../fonts/mplus-1p-regular.woff');+}+div {+ font-size: 2em;+ font-family: mplus;+ width: 12ch;+ letter-spacing: 20px;+ font-kerning: none;+}+i {+ font-style: normal;+ padding-left: 20px;+}+.ref {+ color: blue;+ letter-spacing: 0;+ font-variant-ligatures: none;+}+</style>+<p>Test passes if the black text looks like the blue one.+<div>office</div>+<div class=ref>o<i></i>f<i></i>f<i></i>i<i></i>c<i></i>e</div>diff --git a/Tests/LibWeb/Text/expected/css/letter-spacing-disables-optional-ligatures.txt b/Tests/LibWeb/Text/expected/css/letter-spacing-disables-optional-ligatures.txtnew file mode 100644index 00000000000..27b6801a3e4--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/letter-spacing-disables-optional-ligatures.txt@@ -0,0 +1,5 @@+letter-spacing: 12+letter-spacing, font-variant-ligatures: common-ligatures: 12+letter-spacing, font-variant-ligatures: discretionary-ligatures: 12+letter-spacing, font-feature-settings: "liga": 7+letter-spacing, font-feature-settings: "liga" 0: 12diff --git a/Tests/LibWeb/Text/input/css/letter-spacing-disables-optional-ligatures.html b/Tests/LibWeb/Text/input/css/letter-spacing-disables-optional-ligatures.htmlnew file mode 100644index 00000000000..7294df3992b--- /dev/null+++ b/Tests/LibWeb/Text/input/css/letter-spacing-disables-optional-ligatures.html@@ -0,0 +1,47 @@+<!DOCTYPE html>+<meta charset="utf-8">+<style>+ @font-face {+ font-family: "mplus";+ src: url("../wpt-import/fonts/mplus-1p-regular.woff");+ }+ span {+ font: 32px mplus;+ font-kerning: none;+ white-space: pre;+ }+ .spaced {+ letter-spacing: 20px;+ }+ .unligated {+ font-variant-ligatures: none;+ }+</style>+<div><span class="unligated">office affix</span></div>+ <div><span class="spaced" style=''>office affix</span></div>+ <div><span class="spaced" style='font-variant-ligatures: common-ligatures;'>office affix</span></div>+ <div><span class="spaced" style='font-variant-ligatures: discretionary-ligatures;'>office affix</span></div>+ <div><span class="spaced" style='font-feature-settings: "liga";'>office affix</span></div>+ <div><span class="spaced" style='font-feature-settings: "liga" 0;'>office affix</span></div>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ // How many times the spacing went into each sample: what letter-spacing adds to the width of the text without+ // ligatures, over the spacing. A ligature the spacing leaves in place joins two or three letters into one glyph,+ // which takes the spacing once.+ const names = [+ 'letter-spacing',+ 'letter-spacing, font-variant-ligatures: common-ligatures',+ 'letter-spacing, font-variant-ligatures: discretionary-ligatures',+ 'letter-spacing, font-feature-settings: "liga"',+ 'letter-spacing, font-feature-settings: "liga" 0',+ ];+ await document.fonts.ready;+ const [unligated, ...samples] = document.querySelectorAll("span");+ const base = unligated.getBoundingClientRect().width;+ samples.forEach((sample, index) => {+ const count = Math.round((sample.getBoundingClientRect().width - base) / 20);+ println(`${names[index]}: ${count}`);+ });+ });+</script>