Stub inline layout computation; move reflow to impls

This commit is contained in:
Patrick Walton 2012-05-10 12:30:07 -07:00
parent 1e686a16d9
commit 6340a4a0bd
4 changed files with 89 additions and 32 deletions

View file

@ -1,13 +1,20 @@
import dom::base::{nk_div, nk_img, node_data, node_kind, node}; import dom::base::{nk_div, nk_img, node_data, node_kind, node};
import dom::rcu; import dom::rcu;
import dom::rcu::reader_methods; import dom::rcu::reader_methods;
import inline::inline_layout_methods;
import gfx::geom; import gfx::geom;
import gfx::geom::{size, rect, point, au}; import gfx::geom::{size, rect, point, au};
import util::{tree}; import util::{tree};
enum display {
di_block,
di_inline
}
enum box = { enum box = {
tree: tree::fields<@box>, tree: tree::fields<@box>,
node: node, node: node,
mut display: display,
mut bounds: geom::rect<au> mut bounds: geom::rect<au>
}; };
@ -46,6 +53,7 @@ impl of tree::wr_tree_ops<@box> for btree {
fn new_box(n: node) -> @box { fn new_box(n: node) -> @box {
@box({tree: tree::empty(), @box({tree: tree::empty(),
node: n, node: n,
mut display: di_block,
mut bounds: geom::zero_rect_au()}) mut bounds: geom::zero_rect_au()})
} }
@ -64,38 +72,51 @@ fn linked_subtree(p: node) -> @box {
ret p_box; ret p_box;
} }
fn reflow_block(root: @box, available_width: au) { impl block_layout_methods for @box {
// Root here is the root of the reflow, not necessarily the doc as #[doc="The main reflow routine."]
// a whole. fn reflow(available_width: au) {
// alt self.display {
// This routine: di_block { self.reflow_block(available_width) }
// - generates root.bounds.size di_inline { self.reflow_inline(available_width) }
// - 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 */ }
} }
let mut current_height = 0; #[doc="The main reflow routine for block layout."]
for tree::each_child(btree, root) {|c| fn reflow_block(available_width: au) {
let mut blk_available_width = available_width; assert self.display == di_block;
// FIXME subtract borders, margins, etc
c.bounds.origin = {mut x: au(0), mut y: au(current_height)}; // Root here is the root of the reflow, not necessarily the doc as
reflow_block(c, blk_available_width); // a whole.
current_height += *c.bounds.size.height; //
// 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)] #[cfg(test)]
@ -150,7 +171,7 @@ mod test {
tree::add_child(btree, b3, b1); tree::add_child(btree, b3, b1);
tree::add_child(btree, b3, b2); tree::add_child(btree, b3, b2);
reflow_block(b3, au(100)); b3.reflow_block(au(100));
let fb = flat_bounds(b3); let fb = flat_bounds(b3);
#debug["fb=%?", fb]; #debug["fb=%?", fb];
assert fb == [geom::box(au(0), au(0), au(10), au(10)), // n0 assert fb == [geom::box(au(0), au(0), au(10), au(10)), // n0

View file

@ -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];
}
}

View file

@ -12,6 +12,7 @@ import gfx::renderer;
import dom::base::node; import dom::base::node;
import dom::rcu::scope; import dom::rcu::scope;
import base::{btree, rd_tree_ops, wr_tree_ops, linked_subtree}; import base::{btree, rd_tree_ops, wr_tree_ops, linked_subtree};
import base::block_layout_methods;
import dl = display_list; import dl = display_list;
enum msg { enum msg {
@ -29,7 +30,7 @@ fn layout(to_renderer: chan<renderer::msg>) -> chan<msg> {
build(node) { build(node) {
#debug("layout: received layout request"); #debug("layout: received layout request");
let box = linked_subtree(node); 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); let dlist = build_display_list(box);
to_renderer.send(renderer::render(dlist)); to_renderer.send(renderer::render(dlist));
} }

View file

@ -32,6 +32,7 @@ mod image {
mod layout { mod layout {
mod base; mod base;
mod display_list; mod display_list;
mod inline;
mod layout; mod layout;
} }
@ -51,4 +52,4 @@ mod util {
mod content; mod content;
mod opts; mod opts;
mod engine; mod engine;