From 76cd0435d8be90bda7dd81d5f3b6d844b0190e94 Mon Sep 17 00:00:00 2001 From: Shark Date: Thu, 24 Oct 2024 10:59:19 +0200 Subject: [PATCH] upgrade taffy --- Cargo.lock | 8 +- crates/gosub_taffy/Cargo.toml | 2 +- crates/gosub_taffy/src/compute/inline.rs | 2 + crates/gosub_taffy/src/lib.rs | 78 ++++++++++++++++++- crates/gosub_taffy/src/style.rs | 6 ++ .../gosub_taffy/src/style/parse_properties.rs | 35 ++++++++- 6 files changed, 123 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 322a1bef9..d61717e94 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1880,9 +1880,9 @@ dependencies = [ [[package]] name = "grid" -version = "0.14.0" +version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be136d9dacc2a13cc70bb6c8f902b414fb2641f8db1314637c6b7933411a8f82" +checksum = "36119f3a540b086b4e436bb2b588cf98a68863470e0e880f4d0842f112a3183a" [[package]] name = "guillotiere" @@ -4058,9 +4058,9 @@ dependencies = [ [[package]] name = "taffy" -version = "0.5.2" +version = "0.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9cb893bff0f80ae17d3a57e030622a967b8dbc90e38284d9b4b1442e23873c94" +checksum = "d60b6947c5b788a8eef50637201746ca5dcfa670b47ac714cf9e0a5898a5de08" dependencies = [ "arrayvec 0.7.6", "grid", diff --git a/crates/gosub_taffy/Cargo.toml b/crates/gosub_taffy/Cargo.toml index 2b8488388..26c4a08fd 100644 --- a/crates/gosub_taffy/Cargo.toml +++ b/crates/gosub_taffy/Cargo.toml @@ -9,7 +9,7 @@ license = "MIT" gosub_shared = { path = "../gosub_shared" } gosub_render_backend = { path = "../gosub_render_backend" } gosub_typeface = { path = "../gosub_typeface" } -taffy = "0.5.2" +taffy = "0.6.1" anyhow = "1.0.90" regex = "1.10.5" log = "0.4.22" diff --git a/crates/gosub_taffy/src/compute/inline.rs b/crates/gosub_taffy/src/compute/inline.rs index b2a3852f3..f48a808b4 100644 --- a/crates/gosub_taffy/src/compute/inline.rs +++ b/crates/gosub_taffy/src/compute/inline.rs @@ -468,6 +468,7 @@ pub fn compute_inline_layout>( }, order: 0, padding: Rect::ZERO, + margin: Rect::ZERO, }, ); } @@ -492,6 +493,7 @@ pub fn compute_inline_layout>( }, order: 0, padding: Rect::ZERO, + margin: Rect::ZERO, //TODO: we currently handle margins in the text layout, but we should handle them here }, ); } diff --git a/crates/gosub_taffy/src/lib.rs b/crates/gosub_taffy/src/lib.rs index a067e7261..9ffb92525 100644 --- a/crates/gosub_taffy/src/lib.rs +++ b/crates/gosub_taffy/src/lib.rs @@ -1,9 +1,11 @@ +use std::ops::{Deref, DerefMut}; use std::vec::IntoIter; use taffy::{ compute_block_layout, compute_cached_layout, compute_flexbox_layout, compute_grid_layout, compute_hidden_layout, compute_root_layout, AvailableSpace, Cache as TaffyCache, Display as TaffyDisplay, Layout as TaffyLayout, - LayoutInput, LayoutOutput, LayoutPartialTree, NodeId as TaffyId, Style, TraversePartialTree, + LayoutBlockContainer, LayoutFlexboxContainer, LayoutGridContainer, LayoutInput, LayoutOutput, LayoutPartialTree, + NodeId as TaffyId, Style, TraversePartialTree, }; use gosub_render_backend::geo::{Point, Rect, Size, SizeU32}; @@ -108,6 +110,20 @@ pub struct Cache { display: Display, } +impl Deref for Cache { + type Target = TaffyCache; + + fn deref(&self) -> &Self::Target { + &self.taffy + } +} + +impl DerefMut for Cache { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.taffy + } +} + impl Layouter for TaffyLayouter { type Cache = Cache; type Layout = Layout; @@ -230,7 +246,14 @@ impl> LayoutDocument<'_, LT> { } impl> LayoutPartialTree for LayoutDocument<'_, LT> { - fn get_style(&self, node_id: TaffyId) -> &Style { + type CoreContainerStyle<'a> = &'a Style + where + Self: 'a; + type CacheMut<'b> = &'b mut TaffyCache + where + Self: 'b; + + fn get_core_container_style(&self, node_id: TaffyId) -> Self::CoreContainerStyle<'_> { self.get_taffy_style_no_update(LT::NodeId::from(node_id.into())) } @@ -273,3 +296,54 @@ impl> LayoutPartialTree for LayoutDocument<'_, LT> }) } } + +impl> LayoutBlockContainer for LayoutDocument<'_, LT> { + type BlockContainerStyle<'a> = &'a Style + where + Self: 'a; + type BlockItemStyle<'a> = &'a Style + where + Self: 'a; + + fn get_block_container_style(&self, node_id: TaffyId) -> Self::BlockContainerStyle<'_> { + self.get_taffy_style_no_update(LT::NodeId::from(node_id.into())) + } + + fn get_block_child_style(&self, child_node_id: TaffyId) -> Self::BlockItemStyle<'_> { + self.get_taffy_style_no_update(LT::NodeId::from(child_node_id.into())) + } +} + +impl> LayoutFlexboxContainer for LayoutDocument<'_, LT> { + type FlexboxContainerStyle<'a> = &'a Style + where + Self: 'a; + type FlexboxItemStyle<'a> = &'a Style + where + Self: 'a; + + fn get_flexbox_container_style(&self, node_id: TaffyId) -> Self::FlexboxContainerStyle<'_> { + self.get_taffy_style_no_update(LT::NodeId::from(node_id.into())) + } + + fn get_flexbox_child_style(&self, child_node_id: TaffyId) -> Self::FlexboxItemStyle<'_> { + self.get_taffy_style_no_update(LT::NodeId::from(child_node_id.into())) + } +} + +impl> LayoutGridContainer for LayoutDocument<'_, LT> { + type GridContainerStyle<'a> = &'a Style + where + Self: 'a; + type GridItemStyle<'a> = &'a Style + where + Self: 'a; + + fn get_grid_container_style(&self, node_id: TaffyId) -> Self::GridContainerStyle<'_> { + self.get_taffy_style_no_update(LT::NodeId::from(node_id.into())) + } + + fn get_grid_child_style(&self, child_node_id: TaffyId) -> Self::GridItemStyle<'_> { + self.get_taffy_style_no_update(LT::NodeId::from(child_node_id.into())) + } +} diff --git a/crates/gosub_taffy/src/style.rs b/crates/gosub_taffy/src/style.rs index 686c35df9..2083f8d09 100644 --- a/crates/gosub_taffy/src/style.rs +++ b/crates/gosub_taffy/src/style.rs @@ -40,6 +40,8 @@ pub fn get_style_from_node(node: &mut impl Node) -> (Style, Display) { let grid_auto_flow = parse_properties::parse_grid_auto_flow(node); let grid_row = parse_properties::parse_grid_row(node); let grid_column = parse_properties::parse_grid_column(node); + let box_sizing = parse_properties::parse_box_sizing(node); + let text_align = parse_properties::parse_text_align(node); ( Style { @@ -74,6 +76,10 @@ pub fn get_style_from_node(node: &mut impl Node) -> (Style, Display) { grid_auto_flow, grid_row, grid_column, + // item_is_table: disp == Display::Table, + item_is_table: false, + box_sizing, + text_align, }, disp, ) diff --git a/crates/gosub_taffy/src/style/parse_properties.rs b/crates/gosub_taffy/src/style/parse_properties.rs index 546ce6890..1d345e633 100644 --- a/crates/gosub_taffy/src/style/parse_properties.rs +++ b/crates/gosub_taffy/src/style/parse_properties.rs @@ -1,6 +1,6 @@ use regex::Regex; use taffy::prelude::*; -use taffy::{Overflow, Point}; +use taffy::{Overflow, Point, TextAlign}; use crate::style::parse::{ parse_align_c, parse_align_i, parse_dimension, parse_grid_auto, parse_grid_placement, parse_len, parse_len_auto, @@ -337,3 +337,36 @@ pub fn parse_grid_column(node: &mut impl Node) -> Line { end: parse_grid_placement(node, "grid-column-end"), } } + +pub fn parse_box_sizing(node: &mut impl Node) -> BoxSizing { + let Some(property) = node.get_property("box-sizing") else { + return BoxSizing::ContentBox; + }; + + if let Some(value) = property.as_string() { + match value { + "content-box" => BoxSizing::ContentBox, + "border-box" => BoxSizing::BorderBox, + _ => BoxSizing::ContentBox, + } + } else { + BoxSizing::ContentBox + } +} + +pub fn parse_text_align(node: &mut impl Node) -> TextAlign { + let Some(property) = node.get_property("text-align") else { + return TextAlign::Auto; + }; + + if let Some(value) = property.as_string() { + match value { + "-webkit-left" | "-moz-left" => TextAlign::LegacyLeft, + "-webkit-right" | "-moz-right" => TextAlign::LegacyRight, + "-webkit-center" | "-moz-center" => TextAlign::LegacyCenter, + _ => TextAlign::Auto, + } + } else { + TextAlign::Auto + } +}