Skip to content

Commit

Permalink
Added width and height parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
mmeyerho committed Jul 19, 2012
1 parent 0c5aefa commit ed99449
Show file tree
Hide file tree
Showing 14 changed files with 545 additions and 357 deletions.
1 change: 0 additions & 1 deletion src/servo/content.rs
Expand Up @@ -20,7 +20,6 @@ import dom::style;
import dom::style::Stylesheet;
import gfx::renderer::Sink;
import parser::html_lexer::spawn_html_lexer_task;
import parser::css_builder::build_stylesheet;
import parser::html_builder::build_dom;
import layout::layout_task;
import layout_task::{Layout, BuildMsg};
Expand Down
29 changes: 25 additions & 4 deletions src/servo/dom/style.rs
@@ -1,16 +1,37 @@
import util::color::Color;

enum DisplayType{
#[doc = "
Defines how css rules, both selectors and style specifications, are
stored. CSS selector-matching rules, as presented by
http://www.w3.org/TR/CSS2/selector.html are represented by nested, structural types,
"]

enum DisplayType {
DisBlock,
DisInline,
DisNone
}

enum StyleDeclaration{
FontSize(uint), // Currently assumes format '# pt'
enum Unit {
Auto,
Percent(float),
In(float),
Mm(float),
Cm(float),
Em(float),
Ex(float),
Pt(float),
Pc(float),
Px(float)
}

enum StyleDeclaration {
BackgroundColor(Color),
Display(DisplayType),
FontSize(Unit),
Height(Unit),
TextColor(Color),
BackgroundColor(Color)
Width(Unit)
}

enum Attr{
Expand Down
10 changes: 5 additions & 5 deletions src/servo/layout/base.rs
Expand Up @@ -13,9 +13,9 @@ import image::base::image;
import layout::block::block_layout_methods;
import layout::inline::inline_layout_methods;
import util::tree;
import util::color::Color;
import util::color::{Color, css_colors};
import text::text_box;
import style::style::computed_style;
import style::style::SpecifiedStyle;
import text::text_layout_methods;
import vec::{push, push_all};

Expand All @@ -28,11 +28,11 @@ enum BoxKind {

class Appearance {
let mut background_image: option<@image>;
let mut background_color: option<Color>;
let mut background_color: Color;

new() {
self.background_image = none;
self.background_color = none;
self.background_color = css_colors::black();
}
}

Expand All @@ -53,7 +53,7 @@ class Box {
}

enum LayoutData = {
mut computed_style: ~computed_style,
mut specified_style: ~SpecifiedStyle,
mut box: option<@Box>
};

Expand Down
139 changes: 72 additions & 67 deletions src/servo/layout/box_builder.rs
Expand Up @@ -39,81 +39,84 @@ fn create_context(parent_node: Node, parent_box: @Box) -> ctxt {

impl methods for ctxt {
#[doc="
Constructs boxes for the parent's children, when the parent's 'display'
attribute is 'block'.
"]
Constructs boxes for the parent's children, when the parent's 'display' attribute is 'block'.
"]
fn construct_boxes_for_block_children() {
for NTree.each_child(self.parent_node) |kid| {

// Create boxes for the child. Get its primary box.
let kid_box = kid.construct_boxes();

// Determine the child's display.
let disp = kid.get_computed_style().display;
if disp != DisInline {
let disp = kid.get_specified_style().display_type;
if disp != some(DisInline) {
self.finish_anonymous_box_if_necessary();
}

// Add the child's box to the current enclosing box or the current anonymous box.
alt kid.get_computed_style().display {
DisBlock {
BTree.add_child(self.parent_box, kid_box);
}
DisInline {
let anon_box = alt self.anon_box {
none {
//
// The anonymous box inherits the attributes of its parents for now, so
// that properties of intrinsic boxes are not spread to their parenting
// anonymous box.
//
// TODO: check what CSS actually specifies
//

let b = @Box(self.parent_node, InlineBox);
self.anon_box = some(b);
b
}
some(b) { b }
};
BTree.add_child(anon_box, kid_box);
}
DisNone {
// Nothing to do.
}
alt kid.get_specified_style().display_type {
some(DisBlock) {
BTree.add_child(self.parent_box, kid_box);
}
some(DisInline) {
let anon_box = alt self.anon_box {
none {
//
// The anonymous box inherits the attributes of its parents for now, so
// that properties of intrinsic boxes are not spread to their parenting
// anonymous box.
//
// TODO: check what CSS actually specifies
//

let b = @Box(self.parent_node, InlineBox);
self.anon_box = some(b);
b
}
some(b) { b }
};
BTree.add_child(anon_box, kid_box);
}
some(DisNone) {
// Nothing to do.
}
_ { //hack for now
}
}
}
}

#[doc="
Constructs boxes for the parent's children, when the parent's 'display'
attribute is 'inline'.
"]
Constructs boxes for the parent's children, when the parent's 'display'
attribute is 'inline'.
"]
fn construct_boxes_for_inline_children() {
for NTree.each_child(self.parent_node) |kid| {

// Construct boxes for the child. Get its primary box.
let kid_box = kid.construct_boxes();

// Determine the child's display.
let disp = kid.get_computed_style().display;
if disp != DisInline {
let disp = kid.get_specified_style().display_type;
if disp != some(DisInline) {
// TODO
}

// Add the child's box to the current enclosing box.
alt kid.get_computed_style().display {
DisBlock {
alt kid.get_specified_style().display_type {
some(DisBlock) {
// TODO
#warn("TODO: non-inline display found inside inline box");
BTree.add_child(self.parent_box, kid_box);
}
DisInline {
some(DisInline) {
BTree.add_child(self.parent_box, kid_box);
}
DisNone {
some(DisNone) {
// Nothing to do.
}
_ { //hack for now
}
}
}
}
Expand All @@ -123,24 +126,26 @@ impl methods for ctxt {
#debug("parent node:");
self.parent_node.dump();

alt self.parent_node.get_computed_style().display {
DisBlock { self.construct_boxes_for_block_children(); }
DisInline { self.construct_boxes_for_inline_children(); }
DisNone { /* Nothing to do. */ }
alt self.parent_node.get_specified_style().display_type {
some(DisBlock) { self.construct_boxes_for_block_children(); }
some(DisInline) { self.construct_boxes_for_inline_children(); }
some(DisNone) { /* Nothing to do. */ }
_ { //hack for now
}
}

self.finish_anonymous_box_if_necessary();
assert is_none(self.anon_box);
}

#[doc="
Flushes the anonymous box we're creating if it exists. This appends the
anonymous box to the block.
Flushes the anonymous box we're creating if it exists. This appends the
anonymous box to the block.
"]
fn finish_anonymous_box_if_necessary() {
alt copy self.anon_box {
none { /* Nothing to do. */ }
some(b) { BTree.add_child(self.parent_box, b); }
none { /* Nothing to do. */ }
some(b) { BTree.add_child(self.parent_box, b); }
}
self.anon_box = none;
}
Expand All @@ -152,21 +157,21 @@ trait box_builder_priv {

impl box_builder_priv of box_builder_priv for Node {
#[doc="
Determines the kind of box that this node needs. Also, for images, computes the intrinsic
size.
"]
Determines the kind of box that this node needs. Also, for images, computes the intrinsic
size.
"]
fn determine_box_kind() -> BoxKind {
alt self.read(|n| copy n.kind) {
~Text(string) {
TextBox(@text_box(copy string))
}
~Element(element) {
alt *element.kind {
HTMLDivElement { BlockBox }
HTMLImageElement({size}) { IntrinsicBox(@size) }
UnknownElement { InlineBox }
}
~Text(string) {
TextBox(@text_box(copy string))
}
~Element(element) {
alt *element.kind {
HTMLDivElement { BlockBox }
HTMLImageElement({size}) { IntrinsicBox(@size) }
UnknownElement { InlineBox }
}
}
}
}
}
Expand All @@ -181,13 +186,13 @@ impl box_builder_methods of box_builder_methods for Node {
let box_kind = self.determine_box_kind();
let my_box = @Box(self, box_kind);
alt box_kind {
BlockBox | InlineBox {
let cx = create_context(self, my_box);
cx.construct_boxes_for_children();
}
_ {
// Nothing to do.
}
BlockBox | InlineBox {
let cx = create_context(self, my_box);
cx.construct_boxes_for_children();
}
_ {
// Nothing to do.
}
}
ret my_box;
}
Expand Down
19 changes: 6 additions & 13 deletions src/servo/layout/display_list_builder.rs
Expand Up @@ -70,9 +70,10 @@ fn box_to_display_items(box: @Box, origin: Point2D<au>) -> ~[dl::display_item] {
#debug("request to display a box from origin %?", origin);

let bounds = Rect(origin, copy box.bounds.size);
let col = box.appearance.background_color;

alt (box.kind, box.appearance.background_image, box.appearance.background_color) {
(TextBox(subbox), _, _) {
alt (box.kind, box.appearance.background_image) {
(TextBox(subbox), _) {
let run = copy subbox.run;
assert run.is_some();
push(items, dl::display_item({
Expand All @@ -84,28 +85,20 @@ fn box_to_display_items(box: @Box, origin: Point2D<au>) -> ~[dl::display_item] {
bounds: bounds
}));
}
(_, some(image), some(*)) | (_, some(image), none) {
(_, some(image)) {
push(items, dl::display_item({
item_type: dl::display_item_image(~copy *image),
bounds: bounds
}));
}
(_, none, some(col)) {
(_, none) {
#debug("Assigning color %? to box with bounds %?", col, bounds);
let col = box.appearance.background_color;
push(items, dl::display_item({
item_type: dl::display_item_solid_color(col.red, col.green, col.blue),
bounds: bounds
}));
}
(_, none, none) {
let r = rand::rng();
push(items, dl::display_item({
item_type: dl::display_item_solid_color(r.next() as u8,
r.next() as u8,
r.next() as u8),
bounds: bounds
}));
}
}

#debug("layout: display items: %?", items);
Expand Down
19 changes: 14 additions & 5 deletions src/servo/layout/style/apply.rs
Expand Up @@ -3,8 +3,8 @@
import dom::base::{Element, HTMLImageElement, Node};
import dom::rcu::ReaderMethods;
import image::base::load;
import base::{Box, BTree, NTree, LayoutData, BoxTreeReadMethods};
import style::style_methods;
import base::{Box, BTree, NTree, LayoutData, BoxTreeReadMethods, SpecifiedStyle};
import style::{default_style_methods, style_methods};

trait ApplyStyleBoxMethods {
fn apply_style_for_subtree();
Expand All @@ -19,15 +19,24 @@ impl ApplyStyleBoxMethods of ApplyStyleBoxMethods for @Box {
}
}

#[doc="Applies CSS style."]
#[doc="Applies CSS style to a layout box.
Get the specified style and apply the existing traits to a
layout box. If a trait does not exist, calculate the default
value for the given type of element and use that instead.
"]
fn apply_style() {
// Right now, we only handle images.
self.node.read(|node| {
alt node.kind {
~Element(element) {
let style = self.node.get_computed_style();
let style = self.node.get_specified_style();

self.appearance.background_color = some(style.back_color);
self.appearance.background_color = alt style.background_color {
some(col) { col }
none { node.kind.default_color() }
};

alt element.kind {
~HTMLImageElement(*) {
Expand Down

0 comments on commit ed99449

Please sign in to comment.