tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 1117HTML 1044Markdown 12TOML 94797 Definitions

implement basic rounding

d9e860909ffe428383e01976a9ee56ea02cd64a1 · by Emil Sjölander · (7 years ago) · parent 2db4ee59f · browse the tree at this commit

Semantic delta · 2

src/algo.rs

1 files · +24 −1

diff --git a/src/algo.rs b/src/algo.rs
index 3cfb8e92d..a563f6eb5 100644
--- a/src/algo.rs
+++ b/src/algo.rs
@@ -112,12 +112,35 @@ pub fn compute(root: &style::Node) -> layout::Node {
         SizeConstraint::undefined(),
     );
 
-    layout::Node {
+    round_layout(&layout::Node {
         width: result.size.width,
         height: result.size.height,
         x: 0.0,
         y: 0.0,
         children: result.children,
+    }, 0.0, 0.0)
+}
+
+fn round_layout(layout: &layout::Node, abs_x: f32, abs_y: f32) -> layout::Node {
+    let abs_x = abs_x + layout.x;
+    let abs_y = abs_y + layout.y;
+
+    let rounded_x = layout.x.round();
+    let rounded_y = layout.y.round();
+
+    let rounded_width = (abs_x + layout.width).round() - abs_x.round();
+    let rounded_height = (abs_y + layout.height).round() - abs_y.round();
+
+    let rounded_children = layout.children.iter().map(|child| {
+        round_layout(child, abs_x, abs_y)
+    }).collect();
+
+    layout::Node {
+        width: rounded_width,
+        height: rounded_height,
+        x: rounded_x,
+        y: rounded_y,
+        children: rounded_children,
     }
 }