/
display_list_builder.rs
188 lines (151 loc) · 5.29 KB
/
display_list_builder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
export build_display_list;
import dl = display_list;
import dom::rcu::Scope;
import dom::base::{Text, NodeScope};
import gfx::geometry::{au, au_to_px, box, px_to_au};
import gfx::renderer;
import layout::base::*;
import util::color::methods;
import util::tree;
import box_builder::box_builder_methods;
import text::text_layout_methods;
import geom::size::Size2D;
import geom::point::Point2D;
import geom::rect::Rect;
#[doc = "
Builds a display list for a box and all its children
"]
fn build_display_list(box : @Box) -> dl::display_list {
ret build_display_list_from_origin(box, Point2D(au(0), au(0)));
}
#[doc="
Builds a display list for a box and all its children.
# Arguments
* `box` - The box to build the display list for.
* `origin` - The coordinates of upper-left corner of the box containing the
passed-in box.
"]
fn build_display_list_from_origin(box: @Box, origin: Point2D<au>)
-> dl::display_list {
let box_origin = Point2D(
px_to_au(au_to_px(origin.x) + au_to_px(box.bounds.origin.x)),
px_to_au(au_to_px(origin.y) + au_to_px(box.bounds.origin.y)));
#debug("Handed origin %?, box has bounds %?, starting with origin %?", origin, copy box.bounds, box_origin);
let mut list = box_to_display_items(box, box_origin);
for BTree.each_child(box) |c| {
#debug("Recursively building display list with origin %?", box_origin);
list += build_display_list_from_origin(c, box_origin);
}
#debug("display_list: %?", list);
ret list;
}
#[doc="
Creates a display list item for a single block.
# Arguments
* `box` - The box to build the display list for
* `origin` - The coordinates of upper-left corner of the passed in box.
"]
#[warn(no_non_implicitly_copyable_typarams)]
fn box_to_display_items(box: @Box, origin: Point2D<au>) -> [dl::display_item] {
let mut items = [];
#debug("request to display a box from origin %?", origin);
let bounds = Rect(origin, copy box.bounds.size);
alt (box.kind, box.appearance.background_image, box.appearance.background_color) {
(TextBox(subbox), _, _) {
let run = copy subbox.run;
assert run.is_some();
items += [
dl::display_item({
item_type: dl::display_item_solid_color(255u8, 255u8, 255u8),
bounds: bounds
}),
dl::display_item({
item_type: dl::display_item_text(run.get()),
bounds: bounds
})
];
}
(_, some(image), some(*)) | (_, some(image), none) {
items += [dl::display_item({
item_type: dl::display_item_image(~copy *image),
bounds: bounds
})];
}
(_, none, some(col)) {
#debug("Assigning color %? to box with bounds %?", col, bounds);
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();
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);
ret items;
}
fn should_convert_text_boxes_to_solid_color_background_items() {
#[test];
#[ignore(reason = "crashy")];
let s = Scope();
let n = s.new_node(Text("firecracker"));
let b = n.construct_boxes();
let subbox = alt check b.kind { TextBox(subbox) { subbox } };
b.reflow_text(px_to_au(800), subbox);
let di = box_to_display_items(b, Point2D(px_to_au(0), px_to_au(0)));
alt di[0].item_type {
dl::display_item_solid_color(*) { }
_ { fail }
}
}
fn should_convert_text_boxes_to_text_items() {
#[test];
#[ignore(reason = "crashy")];
let s = Scope();
let n = s.new_node(Text("firecracker"));
let b = n.construct_boxes();
let subbox = alt check b.kind { TextBox(subbox) { subbox } };
b.reflow_text(px_to_au(800), subbox);
let di = box_to_display_items(b, Point2D(px_to_au(0), px_to_au(0)));
alt di[1].item_type {
dl::display_item_text(_) { }
_ { fail }
}
}
fn should_calculate_the_bounds_of_the_text_box_background_color() {
#[test];
#[ignore];
let s = Scope();
let n = s.new_node(Text("firecracker"));
let b = n.construct_boxes();
let subbox = alt check b.kind { TextBox(subbox) { subbox } };
b.reflow_text(px_to_au(800), subbox);
let di = box_to_display_items(b, Point2D(px_to_au(0), px_to_au(0)));
let expected = Rect(
Point2D(px_to_au(0), px_to_au(0)),
Size2D(px_to_au(84), px_to_au(20))
);
assert di[0].bounds == expected;
}
fn should_calculate_the_bounds_of_the_text_items() {
#[test];
#[ignore];
let s = Scope();
let n = s.new_node(Text("firecracker"));
let b = n.construct_boxes();
let subbox = alt check b.kind { TextBox(subbox) { subbox } };
b.reflow_text(px_to_au(800), subbox);
let di = box_to_display_items(b, Point2D(px_to_au(0), px_to_au(0)));
let expected = Rect(
Point2D(px_to_au(0), px_to_au(0)),
Size2D(px_to_au(84), px_to_au(20))
);
assert di[1].bounds == expected;
}