Skip to content

Commit

Permalink
Merge pull request #651 from Sharktheone/layout/update-taffy-0.6.0
Browse files Browse the repository at this point in the history
upgrade taffy
  • Loading branch information
Sharktheone authored Oct 26, 2024
2 parents 8a01d75 + 76cd043 commit 839e29b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 8 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/gosub_taffy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions crates/gosub_taffy/src/compute/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ pub fn compute_inline_layout<LT: LayoutTree<TaffyLayouter>>(
},
order: 0,
padding: Rect::ZERO,
margin: Rect::ZERO,
},
);
}
Expand All @@ -492,6 +493,7 @@ pub fn compute_inline_layout<LT: LayoutTree<TaffyLayouter>>(
},
order: 0,
padding: Rect::ZERO,
margin: Rect::ZERO, //TODO: we currently handle margins in the text layout, but we should handle them here
},
);
}
Expand Down
78 changes: 76 additions & 2 deletions crates/gosub_taffy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -230,7 +246,14 @@ impl<LT: LayoutTree<TaffyLayouter>> LayoutDocument<'_, LT> {
}

impl<LT: LayoutTree<TaffyLayouter>> 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()))
}

Expand Down Expand Up @@ -273,3 +296,54 @@ impl<LT: LayoutTree<TaffyLayouter>> LayoutPartialTree for LayoutDocument<'_, LT>
})
}
}

impl<LT: LayoutTree<TaffyLayouter>> 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<LT: LayoutTree<TaffyLayouter>> 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<LT: LayoutTree<TaffyLayouter>> 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()))
}
}
6 changes: 6 additions & 0 deletions crates/gosub_taffy/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
)
Expand Down
35 changes: 34 additions & 1 deletion crates/gosub_taffy/src/style/parse_properties.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -337,3 +337,36 @@ pub fn parse_grid_column(node: &mut impl Node) -> Line<GridPlacement> {
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
}
}

0 comments on commit 839e29b

Please sign in to comment.