tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

HTML 231Rust 10TOML 3CSS 1350 Definitions

implement basic rounding

6276af09d829a2a28c9e0034f8c0e367dc878227 · by Emil Sjölander · (7 years ago) · parent ba1f724e1 · 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,
     }
 }