Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbopack next/font/google: Use capsize's xWidthAvg to compute fallbacks #50878

Merged
merged 6 commits into from
Jun 8, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ use turbopack_binding::turbo::tasks::{

pub(crate) struct DefaultFallbackFont {
pub name: String,
pub x_width_avg: f64,
pub capsize_key: String,
pub az_avg_width: f64,
pub units_per_em: u32,
}

// From https://github.com/vercel/next.js/blob/a3893bf69c83fb08e88c87bf8a21d987a0448c8e/packages/font/src/utils.ts#L4
pub(crate) static DEFAULT_SANS_SERIF_FONT: Lazy<DefaultFallbackFont> =
Lazy::new(|| DefaultFallbackFont {
name: "Arial".to_owned(),
x_width_avg: 934.5116279069767,
capsize_key: "arial".to_owned(),
az_avg_width: 934.5116279069767,
units_per_em: 2048,
});

pub(crate) static DEFAULT_SERIF_FONT: Lazy<DefaultFallbackFont> =
Lazy::new(|| DefaultFallbackFont {
name: "Times New Roman".to_owned(),
x_width_avg: 854.3953488372093,
capsize_key: "timesNewRoman".to_owned(),
az_avg_width: 854.3953488372093,
units_per_em: 2048,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@
// Derived from
// https://github.com/vercel/next.js/blob/7bfd5829999b1d203e447d30de7e29108c31934a/packages/next/src/server/font-utils.ts#L131
let main_font_avg_width = metrics.x_width_avg / metrics.units_per_em as f64;
let fallback_font_avg_width = fallback.x_width_avg / fallback.units_per_em as f64;
let fallback_metrics = font_metrics_map.0.get(&fallback.capsize_key).unwrap();

Check failure on line 146 in packages/next-swc/crates/next-core/src/next_font/google/font_fallback.rs

View workflow job for this annotation

GitHub Actions / test cargo unit / build

called `Option::unwrap()` on a `None` value

Check failure on line 146 in packages/next-swc/crates/next-core/src/next_font/google/font_fallback.rs

View workflow job for this annotation

GitHub Actions / test cargo unit / build

called `Option::unwrap()` on a `None` value
let fallback_font_avg_width = fallback_metrics.x_width_avg / fallback.units_per_em as f64;
let size_adjust = main_font_avg_width / fallback_font_avg_width;

let ascent = metrics.ascent as f64 / (metrics.units_per_em as f64 * size_adjust);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ async fn get_font_adjustment(
))?
.units_per_em as f64;

let fallback_avg_width = fallback_font.x_width_avg / fallback_font.units_per_em as f64;
let fallback_avg_width = fallback_font.az_avg_width / fallback_font.units_per_em as f64;
// TODO: Use xWidthAvg like next/google.
// JS implementation: https://github.com/seek-oss/capsize/blob/42d6dc39d58247bc6b9e013a4b1c4463bf287dca/packages/unpack/src/index.ts#L7-L83
let size_adjust = match az_avg_width {
Some(az_avg_width) => az_avg_width as f64 / units_per_em / fallback_avg_width,
None => 1.0,
Expand Down