Variable font support (#400)

* Variable font support

Here's a pretty naïve solution for variable fonts.

The iterator doesn't use the match keys' weight, but instead tries to
get the requested ideal weight, if the font is variable, otherwise it is
ignored and the actual (non-variable) weight is used. This is because I
didn't implement finding variable weight support for match keys; doing
so would be impossible without parsing TTF files when matching and I
didn't want to add that potentially expensive infrastructure if not
entirely necessary.

This is a breaking change, and I'm open for ideas on how to fix that
if it's an issue.

* cargo fmt

* Add variable font example to rich-text example
This commit is contained in:
Wren [Undefined] 2025-07-07 09:50:40 -05:00 committed by GitHub
parent d15011fba5
commit a03faa654d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 119 additions and 34 deletions

View file

@ -191,6 +191,7 @@ fn shape_fallback(
descent,
font_monospace_em_width: font.monospace_em_width(),
font_id: font.id(),
font_weight: attrs.weight,
glyph_id: info.glyph_id.try_into().expect("failed to cast glyph ID"),
//TODO: color should not be related to shaping
color_opt: attrs.color_opt,
@ -270,6 +271,7 @@ fn shape_run(
&default_families,
&scripts,
&line[start_run..end_run],
attrs.weight,
);
let font = font_iter.next().expect("no default font found");
@ -447,7 +449,14 @@ fn shape_skip(
let fonts = font_system.get_font_matches(&attrs);
let default_families = [&attrs.family];
let mut font_iter = FontFallbackIter::new(font_system, &fonts, &default_families, &[], "");
let mut font_iter = FontFallbackIter::new(
font_system,
&fonts,
&default_families,
&[],
"",
attrs.weight,
);
let font = font_iter.next().expect("no default font found");
let font_id = font.id();
@ -481,6 +490,7 @@ fn shape_skip(
descent,
font_monospace_em_width,
font_id,
font_weight: attrs.weight,
glyph_id,
color_opt: attrs.color_opt,
metadata: attrs.metadata,
@ -504,6 +514,7 @@ pub struct ShapeGlyph {
pub descent: f32,
pub font_monospace_em_width: Option<f32>,
pub font_id: fontdb::ID,
pub font_weight: fontdb::Weight,
pub glyph_id: u16,
pub color_opt: Option<Color>,
pub metadata: usize,
@ -527,6 +538,7 @@ impl ShapeGlyph {
font_size,
line_height_opt,
font_id: self.font_id,
font_weight: self.font_weight,
glyph_id: self.glyph_id,
x,
y,
@ -1445,13 +1457,7 @@ impl ShapeLine {
}
// Create the LayoutLines using the ranges inside visual lines
let align = align.unwrap_or({
if self.rtl {
Align::Right
} else {
Align::Left
}
});
let align = align.unwrap_or(if self.rtl { Align::Right } else { Align::Left });
let line_width = match width_opt {
Some(width) => width,
@ -1553,7 +1559,9 @@ impl ShapeLine {
.max(1.0)
/ glyph_to_match_factor
* font_size;
log::trace!("Adjusted glyph font size ({font_size} => {glyph_font_size})");
log::trace!(
"Adjusted glyph font size ({font_size} => {glyph_font_size})"
);
glyph_font_size
}
_ => font_size,