From d979a0d91658f242e34f2ad95b5942095212a8ed Mon Sep 17 00:00:00 2001 From: Shark Date: Thu, 19 Sep 2024 11:40:01 +0200 Subject: [PATCH] implement text variation --- crates/gosub_taffy/src/compute/inline.rs | 58 ++++++++++++++++++++++-- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/crates/gosub_taffy/src/compute/inline.rs b/crates/gosub_taffy/src/compute/inline.rs index 0d62d8f4..994da90e 100644 --- a/crates/gosub_taffy/src/compute/inline.rs +++ b/crates/gosub_taffy/src/compute/inline.rs @@ -588,10 +588,60 @@ fn parse_font_style(node: &mut impl Node) -> FontStyle { } } -fn parse_font_axes(p: &mut impl Node) -> Vec { - _ = p; +fn parse_font_axes(n: &mut impl Node) -> Vec { + let prop = n.get_property("font-variation-settings"); - //TODO + let Some(s) = prop else { + return Vec::new(); + }; + + dbg!(&s); + + // we don't need to care about things other than a list, since you always need two values for a variation + let Some(vars) = s.as_list() else { + return Vec::new(); + }; + + let mut slice = vars.as_slice(); + + let mut vars = Vec::with_capacity((slice.len() as f32 / 3.0).ceil() as usize); + + loop { + let Some((candidates, new_slice)) = slice.split_at_checked(2) else { + break; + }; + + let axis = &candidates[0]; + + if axis.is_comma() { + slice = &slice[1..]; // we can guarantee that this won't panic since we know that we have at least 2 elements because of the first check + continue; + } + + let value = &candidates[1]; + + slice = new_slice; // we can now update the slice, since no matter if we have a comma or not, we need to move to the next pair + + if value.is_comma() { + continue; + } + + let Some(axis) = axis.as_string() else { + continue; + }; + + let Ok(tag_bytes): Result<[u8; 4], _> = axis.as_bytes().try_into() else { + continue; + }; + + let tag = u32::from_be_bytes(tag_bytes); + + let Some(value) = value.as_number() else { + continue; + }; + + vars.push(FontVariation { tag, value }); + } - Vec::new() + vars }