From 596c66dfef1cb0ab8d959cb95a11bf8aef80e9f5 Mon Sep 17 00:00:00 2001 From: Shark Date: Tue, 10 Sep 2024 04:26:03 +0200 Subject: [PATCH] fix inline layout with only inline-boxes --- crates/gosub_taffy/src/compute/inline.rs | 234 ++++++++++++----------- 1 file changed, 118 insertions(+), 116 deletions(-) diff --git a/crates/gosub_taffy/src/compute/inline.rs b/crates/gosub_taffy/src/compute/inline.rs index 4de4171e0..9272da0ab 100644 --- a/crates/gosub_taffy/src/compute/inline.rs +++ b/crates/gosub_taffy/src/compute/inline.rs @@ -272,144 +272,146 @@ pub fn compute_inline_layout>( height: layout.height().ceil(), }; - if let Some(first) = text_node_data.first() { - let mut current_node_idx = 0; - let mut current_node_id = LT::NodeId::from(first.id.into()); - let mut current_to = first.to; + let mut current_node_idx = 0; + let mut current_node_id = LT::NodeId::from(0); + let mut current_to = 0; - let mut current_glyph_idx = 0; + let mut current_glyph_idx = 0; + if let Some(first) = text_node_data.first() { + current_node_id = LT::NodeId::from(first.id.into()); + current_to = first.to; + } - 'lines: for line in layout.lines() { - let metrics = line.metrics(); + 'lines: for line in layout.lines() { + let metrics = line.metrics(); - let height = metrics.line_height; + let height = metrics.line_height; - for item in line.items() { - match item { - PositionedLayoutItem::GlyphRun(run) => { - let mut offset = 0.0; + for item in line.items() { + match item { + PositionedLayoutItem::GlyphRun(run) => { + let mut offset = 0.0; - let grun = run.run(); - let fs = grun.font_size(); + let grun = run.run(); + let fs = grun.font_size(); - let glyphs = run - .glyphs() - .map(|g| { - let gl = Glyph { - id: g.id, - x: g.x + offset, - y: g.y, - }; + let glyphs = run + .glyphs() + .map(|g| { + let gl = Glyph { + id: g.id, + x: g.x + offset, + y: g.y, + }; - offset += g.advance; + offset += g.advance; - gl - }) - .collect::>(); + gl + }) + .collect::>(); - let run_y = run.baseline(); + let run_y = run.baseline(); - if current_node_id.into() == 161 || current_node_id.into() == 163 { - println!("current_node_id: {:?}", current_node_id.into()); - println!("first glyph: {:?}", glyphs.get(2)); - } + if current_node_id.into() == 161 || current_node_id.into() == 163 { + println!("current_node_id: {:?}", current_node_id.into()); + println!("first glyph: {:?}", glyphs.get(2)); + } - current_glyph_idx += glyphs.len(); + current_glyph_idx += glyphs.len(); - if current_glyph_idx > current_to { - current_node_idx += 1; + if current_glyph_idx > current_to { + current_node_idx += 1; - if let Some(next) = text_node_data.get(current_node_idx) { - current_to = next.to; - current_node_id = LT::NodeId::from(next.id.into()); - } else { - break 'lines; - } + if let Some(next) = text_node_data.get(current_node_idx) { + current_to = next.to; + current_node_id = LT::NodeId::from(next.id.into()); + } else { + break 'lines; } + } - let size = geo::Size { - width: run.advance(), - height, - }; - - let coords = grun.normalized_coords().to_owned(); + let size = geo::Size { + width: run.advance(), + height, + }; + + let coords = grun.normalized_coords().to_owned(); + + let text_layout = TextLayout { + size, + font_size: fs, + font: Font(grun.font().clone()), + glyphs, + coords, + }; + + let Some(node) = tree.0.get_node(current_node_id) else { + continue; + }; + + node.set_text_layout(text_layout); + + let size = Size { + width: size.width, + height: size.height, + }; + + if current_node_id.into() == 160 { + println!("current_node_id: {:?}", current_node_id.into()); + println!("size: {:?}", size); + + println!( + "location: {:?}", + Point { + x: run.offset(), + y: run_y, + } + ); + } - let text_layout = TextLayout { + tree.set_unrounded_layout( + NodeId::new(current_node_id.into()), + &Layout { size, - font_size: fs, - font: Font(grun.font().clone()), - glyphs, - coords, - }; - - let Some(node) = tree.0.get_node(current_node_id) else { - continue; - }; - - node.set_text_layout(text_layout); - - let size = Size { - width: size.width, - height: size.height, - }; - - if current_node_id.into() == 160 { - println!("current_node_id: {:?}", current_node_id.into()); - println!("size: {:?}", size); - - println!( - "location: {:?}", - Point { - x: run.offset(), - y: run_y, - } - ); - } - - tree.set_unrounded_layout( - NodeId::new(current_node_id.into()), - &Layout { - size, - content_size: size, - scrollbar_size: Size::ZERO, - border: Rect::ZERO, - location: Point { - x: run.offset(), - y: run_y, - }, - order: 0, - padding: Rect::ZERO, + content_size: size, + scrollbar_size: Size::ZERO, + border: Rect::ZERO, + location: Point { + x: run.offset(), + y: run_y, }, - ); + order: 0, + padding: Rect::ZERO, + }, + ); + } + PositionedLayoutItem::InlineBox(inline_box) => { + let id = NodeId::from(inline_box.id); + + if inline_box.id == 137 { + println!("inline_box: {:?}", inline_box); } - PositionedLayoutItem::InlineBox(inline_box) => { - let id = NodeId::from(inline_box.id); - if inline_box.id == 162 { - println!("inline_box: {:?}", inline_box); - } + let size = Size { + width: inline_box.width, + height: inline_box.height, + }; - let size = Size { - width: inline_box.width, - height: inline_box.height, - }; - - tree.set_unrounded_layout( - id, - &Layout { - size, - content_size: size, - scrollbar_size: Size::ZERO, - border: Rect::ZERO, - location: Point { - x: inline_box.x, - y: 0.0, - }, - order: 0, - padding: Rect::ZERO, + tree.set_unrounded_layout( + id, + &Layout { + size, + content_size: size, + scrollbar_size: Size::ZERO, + border: Rect::ZERO, + location: Point { + x: inline_box.x, + y: inline_box.y, }, - ); - } + order: 0, + padding: Rect::ZERO, + }, + ); } } }