Skip to content

Truly independent web browser

LibGfx+LibWeb: Read the cap unit from the font’s cap height

sideshowbarker827337670c79
diff --git a/Libraries/LibGfx/Font/Font.cpp b/Libraries/LibGfx/Font/Font.cpp
index 41821b2c2b0..97bcc0dbeb4 100644
--- a/Libraries/LibGfx/Font/Font.cpp
+++ b/Libraries/LibGfx/Font/Font.cpp
@@ -104,6 +104,17 @@ FontPixelMetrics Font::compute_pixel_metrics() const
metrics.x_height = static_cast<float>(extents.y_bearing) * m_pixel_size / static_cast<float>(y_scale);
}
+ // https://drafts.csswg.org/css-values-4/#cap
+ // In the cases where it is impossible or impractical to determine the cap-height, the font's ascent must be used.
+ // NB: A font whose OS/2 table doesn't give its cap height has it measured from the top of its "H" glyph instead.
+ metrics.cap_height = metrics.ascent;
+ hb_position_t cap_height = 0;
+ if (hb_ot_metrics_get_position(m_harfbuzz_font, HB_OT_METRICS_TAG_CAP_HEIGHT, &cap_height) && cap_height > 0) {
+ metrics.cap_height = static_cast<float>(cap_height) / text_shaping_resolution;
+ } else if (y_scale > 0 && hb_font_get_nominal_glyph(m_harfbuzz_font, 'H', &glyph_id) && hb_font_get_glyph_extents(m_harfbuzz_font, glyph_id, &extents) && extents.y_bearing > 0) {
+ metrics.cap_height = static_cast<float>(extents.y_bearing) * m_pixel_size / static_cast<float>(y_scale);
+ }
+
// https://drafts.csswg.org/css-values-4/#ch
// The advance of the glyph that shaping uses for "0". In the cases where it is impossible or impractical to
// determine the measure of the "0" glyph, it must be assumed to be 0.5em wide.
@@ -114,16 +125,6 @@ FontPixelMetrics Font::compute_pixel_metrics() const
return metrics;
}
-float Font::cap_height() const
-{
- // https://drafts.csswg.org/css-values-4/#cap
- // In the cases where it is impossible or impractical to determine the cap-height, the font’s ascent must be used.
- hb_position_t cap_height = 0;
- if (hb_ot_metrics_get_position(m_harfbuzz_font, HB_OT_METRICS_TAG_CAP_HEIGHT, &cap_height) && cap_height > 0)
- return static_cast<float>(cap_height) / text_shaping_resolution;
- return m_pixel_metrics.ascent;
-}
-
Optional<float> Font::horizontal_advance_of_code_point(u32 code_point) const
{
hb_codepoint_t glyph_id = 0;
@@ -297,6 +298,7 @@ extern "C" void ladybird_gfx_font_snapshot(void const* font, Gfx::FFI::FfiFontSn
.descent = metrics.descent,
.line_gap = metrics.line_gap,
.x_height = metrics.x_height,
+ .cap_height = metrics.cap_height,
.zero_advance = metrics.advance_of_ascii_zero,
.pixel_size = typed_font.pixel_size(),
.point_size = typed_font.point_size(),
diff --git a/Libraries/LibGfx/Font/Font.h b/Libraries/LibGfx/Font/Font.h
index 4a50bb89d8c..2db478b7909 100644
--- a/Libraries/LibGfx/Font/Font.h
+++ b/Libraries/LibGfx/Font/Font.h
@@ -28,6 +28,7 @@ namespace Gfx {
struct FontPixelMetrics {
float x_height { 0 };
+ float cap_height { 0 };
float advance_of_ascii_zero { 0 };
// Number of pixels the font extends above the baseline.
@@ -64,8 +65,6 @@ public:
float point_size() const;
float pixel_size() const;
FontPixelMetrics const& pixel_metrics() const { return m_pixel_metrics; }
- // Metrics that only font-size-adjust reads, measured when asked for rather than with the pixel metrics.
- float cap_height() const;
Optional<float> horizontal_advance_of_code_point(u32 code_point) const;
Optional<float> vertical_advance_of_code_point(u32 code_point) const;
u8 slope() const { return m_style.slope; }
diff --git a/Libraries/LibGfx/Rust/src/font.rs b/Libraries/LibGfx/Rust/src/font.rs
index cc59f608e89..5565761915d 100644
--- a/Libraries/LibGfx/Rust/src/font.rs
+++ b/Libraries/LibGfx/Rust/src/font.rs
@@ -47,6 +47,7 @@ pub struct FfiFontSnapshot {
pub descent: f32,
pub line_gap: f32,
pub x_height: f32,
+ pub cap_height: f32,
pub zero_advance: f32,
pub pixel_size: f32,
pub point_size: f32,
@@ -61,6 +62,7 @@ pub struct FontFacts {
pub descent: f32,
pub line_gap: f32,
pub x_height: f32,
+ pub cap_height: f32,
pub zero_advance: f32,
pub pixel_size: f32,
pub point_size: f32,
@@ -145,6 +147,7 @@ impl FontHandle {
descent: snapshot.descent,
line_gap: snapshot.line_gap,
x_height: snapshot.x_height,
+ cap_height: snapshot.cap_height,
zero_advance: snapshot.zero_advance,
pixel_size: snapshot.pixel_size,
point_size: snapshot.point_size,
diff --git a/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp b/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp
index 5d0081736bf..186f3f156df 100644
--- a/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp
+++ b/Libraries/LibWeb/CSS/ComputedStyleWorkingSet.cpp
@@ -406,6 +406,7 @@ ComputedValuesFFI::FfiFontGroupBuildInputs ComputedStyleWorkingSet::font_group_b
.font_ascent = metrics.ascent,
.font_descent = metrics.descent,
.font_x_height = metrics.x_height,
+ .font_cap_height = metrics.cap_height,
.font_zero_advance = metrics.advance_of_ascii_zero,
.first_available_font = &first_available_font,
.font_cascade_list = font_list.ptr(),
diff --git a/Libraries/LibWeb/CSS/FontResolution.cpp b/Libraries/LibWeb/CSS/FontResolution.cpp
index f447575d1df..b96e3899476 100644
--- a/Libraries/LibWeb/CSS/FontResolution.cpp
+++ b/Libraries/LibWeb/CSS/FontResolution.cpp
@@ -311,7 +311,7 @@ static double font_size_adjust_aspect_value(Gfx::Font const& font, FontSizeAdjus
return metrics.x_height / font_size;
case FontSizeAdjustMetric::CapHeight:
// Normalize the cap-height of the fonts, using the cap-height by the font size.
- return font.cap_height() / font_size;
+ return metrics.cap_height / font_size;
case FontSizeAdjustMetric::ChWidth:
// Normalize the horizontal narrow pitch of the fonts, using the advance width of “0” (ZERO, U+0030) divided by
// the font size.
@@ -800,6 +800,7 @@ static StyleEngineFFI::FfiResolvedFont resolve_font_for_style_engine(void const*
.descent = metrics.descent,
.line_gap = metrics.line_gap,
.x_height = metrics.x_height,
+ .cap_height = metrics.cap_height,
.zero_advance = metrics.advance_of_ascii_zero,
.font_size_adjust_from_font = font_size_adjust_from_font,
};
diff --git a/Libraries/LibWeb/CSS/Length.cpp b/Libraries/LibWeb/CSS/Length.cpp
index 8c87a5e6a14..a2b03efa8e8 100644
--- a/Libraries/LibWeb/CSS/Length.cpp
+++ b/Libraries/LibWeb/CSS/Length.cpp
@@ -117,9 +117,7 @@ static GC::Ptr<DOM::Element const> get_or_compute_query_container_for_axis(Lengt
Length::FontMetrics::FontMetrics(CSSPixels font_size, Gfx::FontPixelMetrics const& pixel_metrics, CSSPixels line_height)
: font_size(font_size)
, x_height(pixel_metrics.x_height)
- // FIXME: This is only approximately the cap height. The spec suggests measuring the "O" glyph:
- // https://www.w3.org/TR/css-values-4/#cap
- , cap_height(pixel_metrics.ascent)
+ , cap_height(pixel_metrics.cap_height)
, zero_advance(pixel_metrics.advance_of_ascii_zero)
, line_height(line_height)
{
diff --git a/Libraries/LibWeb/Rust/src/css/computed_value_types.rs b/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
index 03938900487..504d51af5fa 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_value_types.rs
@@ -694,6 +694,7 @@ pub struct FontValues {
pub resolved_font_ascent: f32,
pub resolved_font_descent: f32,
pub resolved_font_x_height: f32,
+ pub resolved_font_cap_height: f32,
pub resolved_font_zero_advance: f32,
pub resolved_first_available_font: *const std::ffi::c_void,
pub font_cascade_list: libgfx_rust::font::FontCascadeListHandle,
@@ -735,6 +736,7 @@ pub struct FirstAvailableFontMetrics {
pub ascent: f32,
pub descent: f32,
pub x_height: f32,
+ pub cap_height: f32,
pub zero_advance: f32,
}
@@ -777,6 +779,7 @@ impl FontValues {
ascent: facts.ascent,
descent: facts.descent,
x_height: facts.x_height,
+ cap_height: facts.cap_height,
zero_advance: facts.zero_advance,
}
}
@@ -784,6 +787,7 @@ impl FontValues {
ascent: self.resolved_font_ascent,
descent: self.resolved_font_descent,
x_height: self.resolved_font_x_height,
+ cap_height: self.resolved_font_cap_height,
zero_advance: self.resolved_font_zero_advance,
},
}
diff --git a/Libraries/LibWeb/Rust/src/css/computed_values.rs b/Libraries/LibWeb/Rust/src/css/computed_values.rs
index 1ed96f615ae..2b581bc5457 100644
--- a/Libraries/LibWeb/Rust/src/css/computed_values.rs
+++ b/Libraries/LibWeb/Rust/src/css/computed_values.rs
@@ -348,6 +348,7 @@ impl Clone for FontValues {
resolved_font_ascent: self.resolved_font_ascent,
resolved_font_descent: self.resolved_font_descent,
resolved_font_x_height: self.resolved_font_x_height,
+ resolved_font_cap_height: self.resolved_font_cap_height,
resolved_font_zero_advance: self.resolved_font_zero_advance,
resolved_first_available_font: self.resolved_first_available_font,
font_cascade_list: self.font_cascade_list.clone(),
@@ -390,6 +391,7 @@ impl PartialEq for FontValues {
|| (self.resolved_font_ascent == other.resolved_font_ascent
&& self.resolved_font_descent == other.resolved_font_descent
&& self.resolved_font_x_height == other.resolved_font_x_height
+ && self.resolved_font_cap_height == other.resolved_font_cap_height
&& self.resolved_font_zero_advance == other.resolved_font_zero_advance
&& self.resolved_first_available_font == other.resolved_first_available_font);
first_available_fonts_equal
@@ -6621,6 +6623,7 @@ impl FontValues {
resolved_font_ascent: 0.0,
resolved_font_descent: 0.0,
resolved_font_x_height: 0.0,
+ resolved_font_cap_height: 0.0,
resolved_font_zero_advance: 0.0,
resolved_first_available_font: std::ptr::null(),
font_cascade_list: libgfx_rust::font::FontCascadeListHandle::null(),
diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
index 57060e0f553..35aea2e3148 100644
--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
@@ -447,6 +447,7 @@ pub struct FfiResolvedFont {
pub descent: f32,
pub line_gap: f32,
pub x_height: f32,
+ pub cap_height: f32,
pub zero_advance: f32,
/// For a request naming `from-font`, the number it computes to: the request's metric of the
/// first available font, divided by that font's size. Zero for other requests, and where the
diff --git a/Libraries/LibWeb/Rust/src/css/style/container_queries.rs b/Libraries/LibWeb/Rust/src/css/style/container_queries.rs
index 7e65f486d76..6db8c108bcb 100644
--- a/Libraries/LibWeb/Rust/src/css/style/container_queries.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/container_queries.rs
@@ -221,8 +221,7 @@ impl RetainedState {
font_metrics: crate::css::style_compute::FfiFontMetrics {
font_size: font.font_size.to_double(),
x_height: super::pipeline::drive::drive_font_metric(first_available.x_height),
- // The C++ metrics approximate the cap height with the ascent.
- cap_height: super::pipeline::drive::drive_font_metric(first_available.ascent),
+ cap_height: super::pipeline::drive::drive_font_metric(first_available.cap_height),
zero_advance: super::pipeline::drive::drive_font_metric(first_available.zero_advance),
line_height: font.line_height_used().to_double(),
},
diff --git a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
index 3bfb4f9e1e1..789ab8187fd 100644
--- a/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/engine_sample.rs
@@ -41,8 +41,7 @@ impl RecordFont {
metrics: FfiFontMetrics {
font_size: font.font_size.to_double(),
x_height: drive_font_metric(first_available.x_height),
- // The host's metrics approximate the cap height with the ascent.
- cap_height: drive_font_metric(first_available.ascent),
+ cap_height: drive_font_metric(first_available.cap_height),
zero_advance: drive_font_metric(first_available.zero_advance),
line_height: font.line_height_used().to_double(),
},
@@ -256,7 +255,7 @@ impl RetainedState {
Some(font) => FfiFontMetrics {
font_size: CssPixels::from_raw(font.font_size_raw).to_double(),
x_height: drive_font_metric(font.font_x_height),
- cap_height: drive_font_metric(font.font_ascent),
+ cap_height: drive_font_metric(font.font_cap_height),
zero_advance: drive_font_metric(font.font_zero_advance),
line_height: CssPixels::from_raw(font.line_height_used_raw).to_double(),
},
@@ -1507,6 +1506,7 @@ pub(super) fn font_group_build_inputs<'a>(
font_ascent: resolved.ascent,
font_descent: resolved.descent,
font_x_height: resolved.x_height,
+ font_cap_height: resolved.cap_height,
font_zero_advance: resolved.zero_advance,
first_available_font: resolved.first_available_font.as_pointer(),
font_cascade_list: resolved.font_cascade_list.as_pointer(),
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
index d536e0165f6..dca69b3fcb2 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/compute.rs
@@ -95,7 +95,7 @@ impl RootFontInputs {
metrics: [
font.font_size.to_double().to_bits(),
drive_font_metric(first_available.x_height).to_bits(),
- drive_font_metric(first_available.ascent).to_bits(),
+ drive_font_metric(first_available.cap_height).to_bits(),
drive_font_metric(first_available.zero_advance).to_bits(),
font.line_height_used().to_double().to_bits(),
],
@@ -1824,7 +1824,7 @@ impl RetainedState {
let font_metrics = crate::css::style_compute::FfiFontMetrics {
font_size: request.font_size(),
x_height: drive::drive_font_metric(resolved.x_height),
- cap_height: drive::drive_font_metric(resolved.ascent),
+ cap_height: drive::drive_font_metric(resolved.cap_height),
zero_advance: drive::drive_font_metric(resolved.zero_advance),
line_height,
};
@@ -1909,7 +1909,7 @@ impl RetainedState {
font_metrics: FfiFontMetrics {
font_size: parent_font.font_size.to_double(),
x_height: drive::drive_font_metric(first_available.x_height),
- cap_height: drive::drive_font_metric(first_available.ascent),
+ cap_height: drive::drive_font_metric(first_available.cap_height),
zero_advance: drive::drive_font_metric(first_available.zero_advance),
line_height: parent_font.line_height_used().to_double(),
},
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
index 2702f3eb20b..301b45e9418 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/drive.rs
@@ -201,7 +201,7 @@ impl RetainedState {
font_metrics: FfiFontMetrics {
font_size: inherited_size.to_double(),
x_height: drive_font_metric(resolved.x_height),
- cap_height: drive_font_metric(resolved.ascent),
+ cap_height: drive_font_metric(resolved.cap_height),
zero_advance: drive_font_metric(resolved.zero_advance),
line_height,
},
@@ -424,7 +424,7 @@ impl RetainedState {
FfiFontMetrics {
font_size: parent_font.font_size.to_double(),
x_height: drive_font_metric(first_available.x_height),
- cap_height: drive_font_metric(first_available.ascent),
+ cap_height: drive_font_metric(first_available.cap_height),
zero_advance: drive_font_metric(first_available.zero_advance),
line_height: parent_font.line_height_used().to_double(),
},
@@ -636,7 +636,7 @@ impl RetainedState {
let own_metrics = |line_height: f64| FfiFontMetrics {
font_size,
x_height: drive_font_metric(resolved.x_height),
- cap_height: drive_font_metric(resolved.ascent),
+ cap_height: drive_font_metric(resolved.cap_height),
zero_advance: drive_font_metric(resolved.zero_advance),
line_height,
};
diff --git a/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rs b/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rs
index 659ad602dcd..62456e310f8 100644
--- a/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/pipeline/test_support.rs
@@ -34,6 +34,7 @@ unsafe extern "C" fn resolve_font(
descent: 4.0,
line_gap: 0.0,
x_height: 8.0,
+ cap_height: 11.0,
zero_advance: 8.0,
font_size_adjust_from_font: 0.0,
}
diff --git a/Libraries/LibWeb/Rust/src/css/style/position_try.rs b/Libraries/LibWeb/Rust/src/css/style/position_try.rs
index 4e37f2f7bf8..df5d79c284a 100644
--- a/Libraries/LibWeb/Rust/src/css/style/position_try.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/position_try.rs
@@ -946,8 +946,7 @@ impl RetainedState {
font_metrics: FfiFontMetrics {
font_size: font.font_size.to_double(),
x_height: font_metric(first_available.x_height),
- // The C++ metrics approximate the cap height with the ascent.
- cap_height: font_metric(first_available.ascent),
+ cap_height: font_metric(first_available.cap_height),
zero_advance: font_metric(first_available.zero_advance),
line_height: font.line_height_used().to_double(),
},
diff --git a/Libraries/LibWeb/Rust/src/css/table_group_builder.rs b/Libraries/LibWeb/Rust/src/css/table_group_builder.rs
index 7f0b835f2c0..01004b18ad0 100644
--- a/Libraries/LibWeb/Rust/src/css/table_group_builder.rs
+++ b/Libraries/LibWeb/Rust/src/css/table_group_builder.rs
@@ -106,6 +106,7 @@ pub struct FfiFontGroupBuildInputs {
pub font_ascent: f32,
pub font_descent: f32,
pub font_x_height: f32,
+ pub font_cap_height: f32,
pub font_zero_advance: f32,
pub first_available_font: *const c_void,
pub font_cascade_list: *const c_void,
@@ -3406,6 +3407,7 @@ unsafe fn build_font_group(
resolved_font_ascent: inputs.font_ascent,
resolved_font_descent: inputs.font_descent,
resolved_font_x_height: inputs.font_x_height,
+ resolved_font_cap_height: inputs.font_cap_height,
resolved_font_zero_advance: inputs.font_zero_advance,
resolved_first_available_font: inputs.first_available_font,
// SAFETY: The non-null check above and the caller's guarantee make the
diff --git a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
index f6220f6332e..3fa6b3f0bd7 100644
--- a/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/svg_formatting_context.rs
@@ -3381,8 +3381,7 @@ fn font_metrics_for_length_resolution(style: StyleValues<'_>) -> crate::css::sty
crate::css::style_compute::FfiFontMetrics {
font_size: style.font_size().to_double(),
x_height: CssPixels::nearest_value_for_f32(font.x_height).to_double(),
- // FIXME: This is only approximately the cap height, exactly as Length::FontMetrics has it.
- cap_height: CssPixels::nearest_value_for_f32(font.ascent).to_double(),
+ cap_height: CssPixels::nearest_value_for_f32(font.cap_height).to_double(),
zero_advance: CssPixels::nearest_value_for_f32(font.zero_advance).to_double(),
line_height: style.line_height().to_double(),
}
diff --git a/Tests/LibWeb/Text/expected/css/cap-unit-reads-cap-height.txt b/Tests/LibWeb/Text/expected/css/cap-unit-reads-cap-height.txt
new file mode 100644
index 00000000000..7c039df91a3
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/cap-unit-reads-cap-height.txt
@@ -0,0 +1,4 @@
+plus-ligatures: 70px
+serenity-sans: 75.78125px
+lato-bold: 72.296875px
+root-relative: 12.125px
diff --git a/Tests/LibWeb/Text/input/css/cap-unit-reads-cap-height.html b/Tests/LibWeb/Text/input/css/cap-unit-reads-cap-height.html
new file mode 100644
index 00000000000..a9e2223c38f
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/cap-unit-reads-cap-height.html
@@ -0,0 +1,21 @@
+<!DOCTYPE html>
+<style>
+ @font-face { font-family: PlusLigatures; src: url(../../../Assets/PlusLigatures.ttf); }
+ @font-face { font-family: SerenitySans; src: url(../../../Assets/SerenitySans-with-line-gap.ttf); }
+ @font-face { font-family: LatoBold; src: url(../../../Assets/Lato-Bold.ttf); }
+ div { font-size: 100px; height: 1px; }
+</style>
+<!-- The OS/2 table of PlusLigatures gives no cap height, so its "H" glyph decides it. -->
+<div id="plus-ligatures" style="font-family: PlusLigatures; width: 1cap">H</div>
+<div id="serenity-sans" style="font-family: SerenitySans; width: 1cap">H</div>
+<div id="lato-bold" style="font-family: LatoBold; width: 1cap">H</div>
+<div id="root-relative" style="font-family: SerenitySans; width: 1rcap">H</div>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ await document.fonts.ready;
+ for (const id of ["plus-ligatures", "serenity-sans", "lato-bold", "root-relative"])
+ println(`${id}: ${getComputedStyle(document.getElementById(id)).width}`);
+ done();
+ });
+</script>