Skip to content

Commit

Permalink
Shuffle around code in layout/base.rs to have better locality; move D…
Browse files Browse the repository at this point in the history
…ebugMethods trait to own file
  • Loading branch information
Brian J. Burg committed Sep 18, 2012
1 parent eb9a6c0 commit bcdc2ac
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 121 deletions.
27 changes: 27 additions & 0 deletions src/servo/dom/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use js::jsapi::{JSClass, JSObject, JSPropertySpec, JSContext, jsid, jsval, JSBoo
use js::rust::{bare_compartment, compartment, methods};
use js::{JSPROP_ENUMERATE, JSPROP_SHARED};
use layout::base::Box;
use layout::debug::DebugMethods;
use ptr::null;
use std::arc::ARC;
use util::tree;
Expand Down Expand Up @@ -81,6 +82,32 @@ impl NodeTree : tree::ReadMethods<Node> {
}
}


impl Node : DebugMethods {
/* Dumps the subtree rooted at this node, for debugging. */
fn dump() {
self.dump_indent(0u);
}
/* Dumps the node tree, for debugging, with indentation. */
fn dump_indent(indent: uint) {
let mut s = ~"";
for uint::range(0u, indent) |_i| {
s += ~" ";
}
s += self.debug_str();
debug!("%s", s);

for NodeTree.each_child(self) |kid| {
kid.dump_indent(indent + 1u)
}
}

fn debug_str() -> ~str {
fmt!("%?", self.read(|n| copy n.kind ))
}
}

enum NodeKind {
Doctype(DoctypeData),
Comment(~str),
Expand Down
217 changes: 96 additions & 121 deletions src/servo/layout/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use geom::size::Size2D;
use gfx::geometry::au;
use image::{Image, ImageHolder};
use layout::block::BlockFlowData;
use layout::debug::DebugMethods;
use layout::inline::InlineFlowData;
use layout::root::RootFlowData;
use layout::text::TextBoxData;
Expand All @@ -25,6 +26,20 @@ use util::color::Color;
use util::tree;
use vec::{push, push_all};

struct FlowLayoutData {
mut min_width: au,
mut pref_width: au,
mut position: Rect<au>,
}

fn FlowLayoutData() -> FlowLayoutData {
FlowLayoutData {
min_width: au(0),
pref_width: au(0),
position : au::zero_rect(),
}
}

/* The type of the formatting context, and data specific to each
context, such as lineboxes or float lists */
enum FlowContextData {
Expand Down Expand Up @@ -69,12 +84,87 @@ impl @FlowContext : cmp::Eq {
pure fn ne(&&other: @FlowContext) -> bool { !box::ptr_eq(self, other) }
}

impl @FlowContext {
fn bubble_widths() {
match self.kind {
BlockFlow(*) => self.bubble_widths_block(),
InlineFlow(*) => self.bubble_widths_inline(),
RootFlow(*) => self.bubble_widths_root(),
_ => fail fmt!("Tried to bubble_widths of flow: %?", self.kind)
}
}

fn assign_widths() {
match self.kind {
BlockFlow(*) => self.assign_widths_block(),
InlineFlow(*) => self.assign_widths_inline(),
RootFlow(*) => self.assign_widths_root(),
_ => fail fmt!("Tried to assign_widths of flow: %?", self.kind)
}
}

fn assign_height() {
match self.kind {
BlockFlow(*) => self.assign_height_block(),
InlineFlow(*) => self.assign_height_inline(),
RootFlow(*) => self.assign_height_root(),
_ => fail fmt!("Tried to assign_height of flow: %?", self.kind)
}
}
}

/* The tree holding FlowContexts */
enum FlowTree { FlowTree }

impl FlowTree : tree::ReadMethods<@FlowContext> {
fn each_child(ctx: @FlowContext, f: fn(&&@FlowContext) -> bool) {
tree::each_child(self, ctx, f)
}

fn with_tree_fields<R>(&&b: @FlowContext, f: fn(tree::Tree<@FlowContext>) -> R) -> R {
f(b.tree)
}
}

impl FlowTree : tree::WriteMethods<@FlowContext> {
fn add_child(parent: @FlowContext, child: @FlowContext) {
assert !box::ptr_eq(parent, child);
tree::add_child(self, parent, child)
}

fn with_tree_fields<R>(&&b: @FlowContext, f: fn(tree::Tree<@FlowContext>) -> R) -> R {
f(b.tree)
}
}


/* A box's kind influences how its styles are interpreted during
layout. For example, replaced content such as images are resized
differently than tables, text, or other content.
It also holds data specific to different box types, such as text.
*/

struct BoxLayoutData {
mut min_width: au,
mut pref_width: au,
mut position: Rect<au>,

mut font_size: Length,
mut background_image: Option<ImageHolder>,
}

fn BoxLayoutData() -> BoxLayoutData {
BoxLayoutData {
min_width: au(0),
pref_width: au(0),
position : au::zero_rect(),

font_size : Px(0.0),
background_image : None,
}
}

enum BoxData {
GenericBox,
ImageBox(Size2D<au>),
Expand Down Expand Up @@ -134,7 +224,9 @@ impl @Box {
// how to compute its own min and pref widths, and should
// probably cache them.
TextBox(d) => d.runs.foldl(au(0), |sum, run| {
au::max(sum, run.min_break_width())
let ret = au::max(sum, run.min_break_width());
debug!("text min width: %?px", au::to_px(ret));
ret
})
}
}
Expand All @@ -156,7 +248,9 @@ impl @Box {
// how to compute its own min and pref widths, and should
// probably cache them.
TextBox(d) => d.runs.foldl(au(0), |sum, run| {
au::max(sum, run.size().width)
let ret = au::max(sum, run.size().width);
debug!("text pref width: %?px", au::to_px(ret));
ret
})
}
}
Expand Down Expand Up @@ -199,41 +293,6 @@ impl @Box {
}
}

struct FlowLayoutData {
mut min_width: au,
mut pref_width: au,
mut position: Rect<au>,
}


fn FlowLayoutData() -> FlowLayoutData {
FlowLayoutData {
min_width: au(0),
pref_width: au(0),
position : au::zero_rect(),
}
}

struct BoxLayoutData {
mut min_width: au,
mut pref_width: au,
mut position: Rect<au>,

mut font_size: Length,
mut background_image: Option<ImageHolder>,
}

fn BoxLayoutData() -> BoxLayoutData {
BoxLayoutData {
min_width: au(0),
pref_width: au(0),
position : au::zero_rect(),

font_size : Px(0.0),
background_image : None,
}
}

// FIXME: Why do these have to be redefined for each node type?

/* The tree holding boxes */
Expand All @@ -260,67 +319,8 @@ impl BoxTree : tree::WriteMethods<@Box> {
}
}

/* The tree holding FlowContexts */
enum FlowTree { FlowTree }

impl FlowTree : tree::ReadMethods<@FlowContext> {
fn each_child(ctx: @FlowContext, f: fn(&&@FlowContext) -> bool) {
tree::each_child(self, ctx, f)
}

fn with_tree_fields<R>(&&b: @FlowContext, f: fn(tree::Tree<@FlowContext>) -> R) -> R {
f(b.tree)
}
}

impl FlowTree : tree::WriteMethods<@FlowContext> {
fn add_child(parent: @FlowContext, child: @FlowContext) {
assert !box::ptr_eq(parent, child);
tree::add_child(self, parent, child)
}

fn with_tree_fields<R>(&&b: @FlowContext, f: fn(tree::Tree<@FlowContext>) -> R) -> R {
f(b.tree)
}
}

impl @FlowContext {
fn bubble_widths() {
match self.kind {
BlockFlow(*) => self.bubble_widths_block(),
InlineFlow(*) => self.bubble_widths_inline(),
RootFlow(*) => self.bubble_widths_root(),
_ => fail fmt!("Tried to bubble_widths of flow: %?", self.kind)
}
}

fn assign_widths() {
match self.kind {
BlockFlow(*) => self.assign_widths_block(),
InlineFlow(*) => self.assign_widths_inline(),
RootFlow(*) => self.assign_widths_root(),
_ => fail fmt!("Tried to assign_widths of flow: %?", self.kind)
}
}

fn assign_height() {
match self.kind {
BlockFlow(*) => self.assign_height_block(),
InlineFlow(*) => self.assign_height_inline(),
RootFlow(*) => self.assign_height_root(),
_ => fail fmt!("Tried to assign_height of flow: %?", self.kind)
}
}
}

// Debugging

trait DebugMethods {
fn dump();
fn dump_indent(ident: uint);
fn debug_str() -> ~str;
}

impl @FlowContext : DebugMethods {
fn dump() {
self.dump_indent(0u);
Expand Down Expand Up @@ -363,31 +363,6 @@ impl @FlowContext : DebugMethods {
}
}

impl Node : DebugMethods {
/* Dumps the subtree rooted at this node, for debugging. */
fn dump() {
self.dump_indent(0u);
}
/* Dumps the node tree, for debugging, with indentation. */
fn dump_indent(indent: uint) {
let mut s = ~"";
for uint::range(0u, indent) |_i| {
s += ~" ";
}
s += self.debug_str();
debug!("%s", s);

for NodeTree.each_child(self) |kid| {
kid.dump_indent(indent + 1u)
}
}

fn debug_str() -> ~str {
fmt!("%?", self.read(|n| copy n.kind ))
}
}

impl @Box : DebugMethods {
fn dump() {
self.dump_indent(0u);
Expand Down
5 changes: 5 additions & 0 deletions src/servo/layout/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
trait DebugMethods {
fn dump();
fn dump_indent(ident: uint);
fn debug_str() -> ~str;
}
1 change: 1 addition & 0 deletions src/servo/servo.rc
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod layout {
mod base;
mod block;
mod box_builder;
mod debug;
mod display_list_builder;
mod inline;
mod layout_task;
Expand Down

0 comments on commit bcdc2ac

Please sign in to comment.