From 6340a4a0bdd8feac7ec77b6b188373bf040eebb0 Mon Sep 17 00:00:00 2001 From: Patrick Walton Date: Thu, 10 May 2012 12:30:07 -0700 Subject: [PATCH] Stub inline layout computation; move reflow to impls --- src/servo/layout/base.rs | 81 ++++++++++++++++++++++++-------------- src/servo/layout/inline.rs | 34 ++++++++++++++++ src/servo/layout/layout.rs | 3 +- src/servo/servo.rc | 3 +- 4 files changed, 89 insertions(+), 32 deletions(-) create mode 100644 src/servo/layout/inline.rs diff --git a/src/servo/layout/base.rs b/src/servo/layout/base.rs index e0316ec190c..7b4ea6835ca 100644 --- a/src/servo/layout/base.rs +++ b/src/servo/layout/base.rs @@ -1,13 +1,20 @@ import dom::base::{nk_div, nk_img, node_data, node_kind, node}; import dom::rcu; import dom::rcu::reader_methods; +import inline::inline_layout_methods; import gfx::geom; import gfx::geom::{size, rect, point, au}; import util::{tree}; +enum display { + di_block, + di_inline +} + enum box = { tree: tree::fields<@box>, node: node, + mut display: display, mut bounds: geom::rect }; @@ -46,6 +53,7 @@ impl of tree::wr_tree_ops<@box> for btree { fn new_box(n: node) -> @box { @box({tree: tree::empty(), node: n, + mut display: di_block, mut bounds: geom::zero_rect_au()}) } @@ -64,38 +72,51 @@ fn linked_subtree(p: node) -> @box { ret p_box; } -fn reflow_block(root: @box, available_width: au) { - // Root here is the root of the reflow, not necessarily the doc as - // a whole. - // - // This routine: - // - generates root.bounds.size - // - generates root.bounds.origin for each child - // - and recursively computes the bounds for each child - - let k = root.node.rd() { |r| r.kind }; - alt k { - nk_img(size) { - root.bounds.size = size; - ret; - } - - nk_div { /* fallthrough */ } +impl block_layout_methods for @box { + #[doc="The main reflow routine."] + fn reflow(available_width: au) { + alt self.display { + di_block { self.reflow_block(available_width) } + di_inline { self.reflow_inline(available_width) } + } } - let mut current_height = 0; - for tree::each_child(btree, root) {|c| - let mut blk_available_width = available_width; - // FIXME subtract borders, margins, etc - c.bounds.origin = {mut x: au(0), mut y: au(current_height)}; - reflow_block(c, blk_available_width); - current_height += *c.bounds.size.height; + #[doc="The main reflow routine for block layout."] + fn reflow_block(available_width: au) { + assert self.display == di_block; + + // Root here is the root of the reflow, not necessarily the doc as + // a whole. + // + // This routine: + // - generates root.bounds.size + // - generates root.bounds.origin for each child + // - and recursively computes the bounds for each child + + let k = self.node.rd() { |r| r.kind }; + alt k { + nk_img(size) { + self.bounds.size = size; + ret; + } + + nk_div { /* fallthrough */ } + } + + let mut current_height = 0; + for tree::each_child(btree, self) {|c| + let mut blk_available_width = available_width; + // FIXME subtract borders, margins, etc + c.bounds.origin = {mut x: au(0), mut y: au(current_height)}; + c.reflow(blk_available_width); + current_height += *c.bounds.size.height; + } + + self.bounds.size = {mut width: available_width, // FIXME + mut height: au(current_height)}; + + #debug["reflow_block root=%? size=%?", k, self.bounds]; } - - root.bounds.size = {mut width: available_width, // FIXME - mut height: au(current_height)}; - - #debug["reflow_block root=%? size=%?", k, root.bounds]; } #[cfg(test)] @@ -150,7 +171,7 @@ mod test { tree::add_child(btree, b3, b1); tree::add_child(btree, b3, b2); - reflow_block(b3, au(100)); + b3.reflow_block(au(100)); let fb = flat_bounds(b3); #debug["fb=%?", fb]; assert fb == [geom::box(au(0), au(0), au(10), au(10)), // n0 diff --git a/src/servo/layout/inline.rs b/src/servo/layout/inline.rs new file mode 100644 index 00000000000..b614ad6389f --- /dev/null +++ b/src/servo/layout/inline.rs @@ -0,0 +1,34 @@ +// Inline layout. + +import base::*; // FIXME: Can't get around import * due to resolve bug. +import dom::rcu; +import dom::rcu::reader_methods; +import gfx::geom::au; +import util::tree; + +#[doc="The main reflow routine for inline layout."] +impl inline_layout_methods for @box { + fn reflow_inline(available_width: au) { + assert self.display == di_inline; + + // FIXME: This is clownshoes inline layout and is not even close to + // correct. + let y = self.bounds.origin.y; + let mut x = 0, inline_available_width = *available_width; + let mut current_height = 0; + for tree::each_child(btree, self) { + |kid| + kid.bounds.origin = { mut x: au(x), mut y: y }; + kid.reflow(au(inline_available_width)); + inline_available_width -= *kid.bounds.size.width; + x += *kid.bounds.size.width; + current_height = int::max(current_height, *kid.bounds.size.height); + } + + self.bounds.size = { mut width: available_width, + mut height: au(current_height) }; + + #debug["reflow_inline root=%? size=%?", self, self.bounds]; + } +} + diff --git a/src/servo/layout/layout.rs b/src/servo/layout/layout.rs index db26b0ee3ad..2d13c3e8251 100644 --- a/src/servo/layout/layout.rs +++ b/src/servo/layout/layout.rs @@ -12,6 +12,7 @@ import gfx::renderer; import dom::base::node; import dom::rcu::scope; import base::{btree, rd_tree_ops, wr_tree_ops, linked_subtree}; +import base::block_layout_methods; import dl = display_list; enum msg { @@ -29,7 +30,7 @@ fn layout(to_renderer: chan) -> chan { build(node) { #debug("layout: received layout request"); let box = linked_subtree(node); - base::reflow_block(box, geom::px_to_au(800)); + box.reflow(geom::px_to_au(800)); let dlist = build_display_list(box); to_renderer.send(renderer::render(dlist)); } diff --git a/src/servo/servo.rc b/src/servo/servo.rc index c86bc2be860..eb47d9990e2 100755 --- a/src/servo/servo.rc +++ b/src/servo/servo.rc @@ -32,6 +32,7 @@ mod image { mod layout { mod base; mod display_list; + mod inline; mod layout; } @@ -51,4 +52,4 @@ mod util { mod content; mod opts; -mod engine; \ No newline at end of file +mod engine;