gfx: Derive line-through metrics for fonts on MacOS (#31756)

There is now platform-specific way to get metrics for `line-through` on
MacOS and currently striking through simply does not work. The correct
approach here is likely to first search for these metrics in font tables
and then falling back to deriving them. Searching the font tables is a
larger change, so this change adds the fallback mechanism first. This at
least makes sure that strike through renders at all on Mac.

In a followup change we can add support for getting metrics via HarfBuzz
in a platform-independent way, which is what Gecko does.

Fixes #942.
This commit is contained in:
Martin Robinson 2024-03-19 14:55:12 +01:00 committed by GitHub
parent d3b03a20b5
commit f175679434
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 5 deletions

View file

@ -278,6 +278,9 @@ impl FontHandleMethods for FontHandle {
}
fn metrics(&self) -> FontMetrics {
// TODO(mrobinson): Gecko first tries to get metrics from the SFNT tables via
// HarfBuzz and only afterward falls back to platform APIs. We should do something
// similar here. This will likely address issue #201 mentioned below.
let bounding_rect: CGRect = self.ctfont.bounding_box();
let ascent = self.ctfont.ascent();
let descent = self.ctfont.descent();
@ -294,18 +297,24 @@ impl FontHandleMethods for FontHandle {
.map(Au::from_f64_px)
.unwrap_or(max_advance);
let underline_size = au_from_pt(self.ctfont.underline_thickness());
let x_height = au_from_pt(self.ctfont.x_height() * scale);
let metrics = FontMetrics {
underline_size: au_from_pt(self.ctfont.underline_thickness()),
underline_size,
// TODO(Issue #201): underline metrics are not reliable. Have to pull out of font table
// directly.
//
// see also: https://bugs.webkit.org/show_bug.cgi?id=16768
// see also: https://bugreports.qt-project.org/browse/QTBUG-13364
underline_offset: au_from_pt(self.ctfont.underline_position()),
strikeout_size: Au(0), // FIXME(Issue #942)
strikeout_offset: Au(0), // FIXME(Issue #942)
// There is no way to get these from CoreText or CoreGraphics APIs, so
// derive them from the other font metrics. These should eventually be
// found in the font tables directly when #201 is fixed.
strikeout_size: underline_size,
strikeout_offset: x_height.scale_by(0.5) + underline_size.scale_by(0.5),
leading: au_from_pt(leading),
x_height: au_from_pt(self.ctfont.x_height() * scale),
x_height,
em_size,
ascent: au_from_pt(ascent * scale),
descent: au_from_pt(descent * scale),