/
box_.rs
1192 lines (1062 loc) · 50.8 KB
/
box_.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! The `Box` type, which represents the leaves of the layout tree.
use extra::url::Url;
use extra::arc::{MutexArc, Arc};
use geom::{Point2D, Rect, Size2D, SideOffsets2D};
use gfx::color::rgb;
use gfx::display_list::{BaseDisplayItem, BorderDisplayItem, BorderDisplayItemClass};
use gfx::display_list::{DisplayList, ImageDisplayItem, ImageDisplayItemClass};
use gfx::display_list::{SolidColorDisplayItem, SolidColorDisplayItemClass, TextDisplayItem};
use gfx::display_list::{TextDisplayItemClass, TextDisplayItemFlags, ClipDisplayItem};
use gfx::display_list::{ClipDisplayItemClass};
use gfx::font::FontStyle;
use gfx::text::text_run::TextRun;
use servo_msg::constellation_msg::{FrameRectMsg, PipelineId, SubpageId};
use servo_net::image::holder::ImageHolder;
use servo_net::local_image_cache::LocalImageCache;
use servo_util::geometry::Au;
use servo_util::geometry;
use servo_util::range::*;
use std::cast;
use std::cell::RefCell;
use std::cmp::ApproxEq;
use std::num::Zero;
use style::{ComputedValues, TElement, TNode, cascade};
use style::computed_values::{LengthOrPercentage, overflow};
use style::computed_values::{border_style, clear, font_family, line_height};
use style::computed_values::{text_align, text_decoration, vertical_align, visibility};
use css::node_style::StyledNode;
use layout::context::LayoutContext;
use layout::display_list_builder::{DisplayListBuilder, ExtraDisplayListData, ToGfxColor};
use layout::float_context::{ClearType, ClearLeft, ClearRight, ClearBoth};
use layout::flow::{Flow, FlowFlagsInfo};
use layout::flow;
use layout::model::{MaybeAuto, Auto, specified};
use layout::util::OpaqueNode;
use layout::wrapper::LayoutNode;
/// Boxes (`struct Box`) are the leaves of the layout tree. They cannot position themselves. In
/// general, boxes do not have a simple correspondence with CSS boxes in the specification:
///
/// * Several boxes may correspond to the same CSS box or DOM node. For example, a CSS text box
/// broken across two lines is represented by two boxes.
///
/// * Some CSS boxes are not created at all, such as some anonymous block boxes induced by inline
/// boxes with block-level sibling boxes. In that case, Servo uses an `InlineFlow` with
/// `BlockFlow` siblings; the `InlineFlow` is block-level, but not a block container. It is
/// positioned as if it were a block box, but its children are positioned according to inline
/// flow.
///
/// A `GenericBox` is an empty box that contributes only borders, margins, padding, and
/// backgrounds. It is analogous to a CSS nonreplaced content box.
///
/// A box's type influences how its styles are interpreted during layout. For example, replaced
/// content such as images are resized differently from tables, text, or other content. Different
/// types of boxes may also contain custom data; for example, text boxes contain text.
///
/// FIXME(pcwalton): This can be slimmed down quite a bit.
#[deriving(Clone)]
pub struct Box {
/// An opaque reference to the DOM node that this `Box` originates from.
node: OpaqueNode,
/// The CSS style of this box.
style: Arc<ComputedValues>,
/// The position of this box relative to its owning flow.
position: RefCell<Rect<Au>>,
/// The border of the content box.
///
/// FIXME(pcwalton): This need not be stored in the box.
border: RefCell<SideOffsets2D<Au>>,
/// The padding of the content box.
padding: RefCell<SideOffsets2D<Au>>,
/// The margin of the content box.
margin: RefCell<SideOffsets2D<Au>>,
/// Info specific to the kind of box. Keep this enum small.
specific: SpecificBoxInfo,
/// positioned box offsets
position_offsets: RefCell<SideOffsets2D<Au>>,
/// Inline data
inline_info: RefCell<Option<InlineInfo>>,
}
/// Info specific to the kind of box. Keep this enum small.
#[deriving(Clone)]
pub enum SpecificBoxInfo {
GenericBox,
ImageBox(ImageBoxInfo),
IframeBox(IframeBoxInfo),
ScannedTextBox(ScannedTextBoxInfo),
UnscannedTextBox(UnscannedTextBoxInfo),
}
/// A box that represents a replaced content image and its accompanying borders, shadows, etc.
#[deriving(Clone)]
pub struct ImageBoxInfo {
/// The image held within this box.
image: RefCell<ImageHolder>,
/// The width attribute supplied by the DOM, if any.
dom_width: Option<Au>,
/// The height attribute supplied by the DOM, if any.
dom_height: Option<Au>,
}
impl ImageBoxInfo {
/// Creates a new image box from the given URL and local image cache.
///
/// FIXME(pcwalton): The fact that image boxes store the cache in the box makes little sense to
/// me.
pub fn new(node: &LayoutNode, image_url: Url, local_image_cache: MutexArc<LocalImageCache>)
-> ImageBoxInfo {
fn convert_length(node: &LayoutNode, name: &str) -> Option<Au> {
node.with_element(|element| {
element.get_attr(None, name).and_then(|string| {
let n: Option<int> = FromStr::from_str(string);
n
}).and_then(|pixels| Some(Au::from_px(pixels)))
})
}
ImageBoxInfo {
image: RefCell::new(ImageHolder::new(image_url, local_image_cache)),
dom_width: convert_length(node, "width"),
dom_height: convert_length(node, "height"),
}
}
// Calculates the width of an image, accounting for the width attribute.
fn image_width(&self) -> Au {
// TODO(brson): Consult margins and borders?
self.dom_width.unwrap_or_else(|| {
let mut image_ref = self.image.borrow_mut();
Au::from_px(image_ref.get().get_size().unwrap_or(Size2D(0, 0)).width)
})
}
// Calculate the height of an image, accounting for the height attribute.
pub fn image_height(&self) -> Au {
// TODO(brson): Consult margins and borders?
self.dom_height.unwrap_or_else(|| {
let mut image_ref = self.image.borrow_mut();
Au::from_px(image_ref.get().get_size().unwrap_or(Size2D(0, 0)).height)
})
}
}
/// A box that represents an inline frame (iframe). This stores the pipeline ID so that the size
/// of this iframe can be communicated via the constellation to the iframe's own layout task.
#[deriving(Clone)]
pub struct IframeBoxInfo {
/// The pipeline ID of this iframe.
pipeline_id: PipelineId,
/// The subpage ID of this iframe.
subpage_id: SubpageId,
}
impl IframeBoxInfo {
/// Creates the information specific to an iframe box.
pub fn new(node: &LayoutNode) -> IframeBoxInfo {
let (pipeline_id, subpage_id) = node.iframe_pipeline_and_subpage_ids();
IframeBoxInfo {
pipeline_id: pipeline_id,
subpage_id: subpage_id,
}
}
}
/// A scanned text box represents a single run of text with a distinct style. A `TextBox` may be
/// split into two or more boxes across line breaks. Several `TextBox`es may correspond to a single
/// DOM text node. Split text boxes are implemented by referring to subsets of a single `TextRun`
/// object.
#[deriving(Clone)]
pub struct ScannedTextBoxInfo {
/// The text run that this represents.
run: Arc<~TextRun>,
/// The range within the above text run that this represents.
range: Range,
}
impl ScannedTextBoxInfo {
/// Creates the information specific to a scanned text box from a range and a text run.
pub fn new(run: Arc<~TextRun>, range: Range) -> ScannedTextBoxInfo {
ScannedTextBoxInfo {
run: run,
range: range,
}
}
}
/// Data for an unscanned text box. Unscanned text boxes are the results of flow construction that
/// have not yet had their width determined.
#[deriving(Clone)]
pub struct UnscannedTextBoxInfo {
/// The text inside the box.
text: ~str,
}
impl UnscannedTextBoxInfo {
/// Creates a new instance of `UnscannedTextBoxInfo` from the given DOM node.
pub fn new(node: &LayoutNode) -> UnscannedTextBoxInfo {
// FIXME(pcwalton): Don't copy text; atomically reference count it instead.
UnscannedTextBoxInfo {
text: node.text(),
}
}
}
/// Represents the outcome of attempting to split a box.
pub enum SplitBoxResult {
CannotSplit,
// in general, when splitting the left or right side can
// be zero length, due to leading/trailing trimmable whitespace
SplitDidFit(Option<Box>, Option<Box>),
SplitDidNotFit(Option<Box>, Option<Box>)
}
/// data for inline boxes
#[deriving(Clone)]
pub struct InlineInfo {
parent_info: ~[InlineParentInfo],
baseline: Au,
}
impl InlineInfo {
pub fn new() -> InlineInfo {
InlineInfo {
parent_info: ~[],
baseline: Au::new(0),
}
}
}
#[deriving(Clone)]
pub struct InlineParentInfo {
padding: SideOffsets2D<Au>,
border: SideOffsets2D<Au>,
margin: SideOffsets2D<Au>,
style: Arc<ComputedValues>,
font_ascent: Au,
font_descent: Au,
}
impl Box {
/// Constructs a new `Box` instance.
pub fn new(node: LayoutNode, specific: SpecificBoxInfo) -> Box {
// Find the nearest ancestor element and take its style. (It should be either that node or
// its immediate parent.)
// CSS 2.1 § 9.2.1.1,9.2.2.1 This is for non-inherited properties on anonymous boxes
// example:
//
// <div style="border: solid">
// <p>Foo</p>
// Bar
// <p>Baz</p>
// </div>
//
// An anonymous block box is generated around `Bar`, but it shouldn't inherit the border.
let node_style = if node.is_element() {
node.style().clone()
} else {
let mut nearest_ancestor_element = node;
while !nearest_ancestor_element.is_element() {
nearest_ancestor_element =
nearest_ancestor_element.parent_node().expect("no nearest element?!");
}
// Anonymous box: inheriting from the ancestor with no specified declarations.
Arc::new(cascade(&[Arc::new(~[])],
Some(nearest_ancestor_element.style().get())))
};
Box {
node: OpaqueNode::from_layout_node(&node),
style: node_style,
position: RefCell::new(Au::zero_rect()),
border: RefCell::new(Zero::zero()),
padding: RefCell::new(Zero::zero()),
margin: RefCell::new(Zero::zero()),
specific: specific,
position_offsets: RefCell::new(Zero::zero()),
inline_info: RefCell::new(None),
}
}
/// Returns a debug ID of this box. This ID should not be considered stable across multiple
/// layouts or box manipulations.
pub fn debug_id(&self) -> uint {
unsafe {
cast::transmute(self)
}
}
//TODO(ibnc) take into account padding.
pub fn get_y_coord_and_new_height_if_fixed(&self,
screen_height: Au,
mut height: Au,
mut y: Au,
is_fixed: bool)
-> (Au, Au) {
if is_fixed {
let position_offsets = self.position_offsets.get();
match (position_offsets.top, position_offsets.bottom) {
(Au(0), Au(0)) => {}
(Au(0), _) => {
y = screen_height - position_offsets.bottom - height;
}
(_, Au(0)) => {
y = position_offsets.top;
}
(_, _) => {
y = position_offsets.top;
match MaybeAuto::from_style(self.style().Box.height, Au(0)) {
Auto => {
height = screen_height - position_offsets.top - position_offsets.bottom;
}
_ => {}
}
}
}
}
return (y, height);
}
//TODO(ibnc) removing padding when width needs to be stretched.
pub fn get_x_coord_and_new_width_if_fixed(&self,
screen_width: Au,
screen_height: Au,
mut width: Au,
mut x: Au,
is_fixed: bool)
-> (Au, Au) {
if is_fixed {
self.compute_positioned_offsets(self.style(), screen_width, screen_height);
let position_offsets = self.position_offsets.get();
match (position_offsets.left, position_offsets.right) {
(Au(0), Au(0)) => {}
(_, Au(0)) => {
x = position_offsets.left;
}
(Au(0), _) => {
x = screen_width - position_offsets.right - width;
}
(_, _) => {
x = position_offsets.left;
match MaybeAuto::from_style(self.style().Box.width, Au(0)) {
Auto => {
width = screen_width - position_offsets.left - position_offsets.right;
}
_ => {}
}
}
}
}
return (x, width);
}
/// Transforms this box into another box of the given type, with the given size, preserving all
/// the other data.
pub fn transform(&self, size: Size2D<Au>, specific: SpecificBoxInfo) -> Box {
Box {
node: self.node,
style: self.style.clone(),
position: RefCell::new(Rect(self.position.get().origin, size)),
border: RefCell::new(self.border.get()),
padding: RefCell::new(self.padding.get()),
margin: RefCell::new(self.margin.get()),
specific: specific,
position_offsets: RefCell::new(Zero::zero()),
inline_info: self.inline_info.clone(),
}
}
/// Returns the shared part of the width for computation of minimum and preferred width per
/// CSS 2.1.
fn guess_width(&self) -> Au {
match self.specific {
GenericBox | IframeBox(_) | ImageBox(_) => {}
ScannedTextBox(_) | UnscannedTextBox(_) => return Au(0),
}
let style = self.style();
let width = MaybeAuto::from_style(style.Box.width, Au::new(0)).specified_or_zero();
let margin_left = MaybeAuto::from_style(style.Margin.margin_left,
Au::new(0)).specified_or_zero();
let margin_right = MaybeAuto::from_style(style.Margin.margin_right,
Au::new(0)).specified_or_zero();
let padding_left = self.compute_padding_length(style.Padding.padding_left, Au::new(0));
let padding_right = self.compute_padding_length(style.Padding.padding_right, Au::new(0));
width + margin_left + margin_right + padding_left + padding_right +
self.border.get().left + self.border.get().right
}
pub fn calculate_line_height(&self, font_size: Au) -> Au {
match self.line_height() {
line_height::Normal => font_size.scale_by(1.14),
line_height::Number(l) => font_size.scale_by(l),
line_height::Length(l) => l
}
}
/// Populates the box model border parameters from the given computed style.
///
/// FIXME(pcwalton): This should not be necessary. Just go to the style.
pub fn compute_borders(&self, style: &ComputedValues) {
#[inline]
fn width(width: Au, style: border_style::T) -> Au {
if style == border_style::none {
Au(0)
} else {
width
}
}
self.border.set(SideOffsets2D::new(width(style.Border.border_top_width,
style.Border.border_top_style),
width(style.Border.border_right_width,
style.Border.border_right_style),
width(style.Border.border_bottom_width,
style.Border.border_bottom_style),
width(style.Border.border_left_width,
style.Border.border_left_style)))
}
pub fn compute_positioned_offsets(&self, style: &ComputedValues, containing_width: Au, containing_height: Au) {
self.position_offsets.set(SideOffsets2D::new(
MaybeAuto::from_style(style.PositionOffsets.top, containing_height)
.specified_or_zero(),
MaybeAuto::from_style(style.PositionOffsets.right, containing_width)
.specified_or_zero(),
MaybeAuto::from_style(style.PositionOffsets.bottom, containing_height)
.specified_or_zero(),
MaybeAuto::from_style(style.PositionOffsets.left, containing_width)
.specified_or_zero()));
}
/// Populates the box model padding parameters from the given computed style.
pub fn compute_padding(&self, style: &ComputedValues, containing_block_width: Au) {
let padding = SideOffsets2D::new(self.compute_padding_length(style.Padding.padding_top,
containing_block_width),
self.compute_padding_length(style.Padding.padding_right,
containing_block_width),
self.compute_padding_length(style.Padding.padding_bottom,
containing_block_width),
self.compute_padding_length(style.Padding.padding_left,
containing_block_width));
self.padding.set(padding)
}
fn compute_padding_length(&self, padding: LengthOrPercentage, content_box_width: Au) -> Au {
specified(padding, content_box_width)
}
pub fn noncontent_width(&self) -> Au {
let left = self.margin.get().left + self.border.get().left + self.padding.get().left;
let right = self.margin.get().right + self.border.get().right + self.padding.get().right;
left + right
}
pub fn noncontent_height(&self) -> Au {
let top = self.margin.get().top + self.border.get().top + self.padding.get().top;
let bottom = self.margin.get().bottom + self.border.get().bottom +
self.padding.get().bottom;
top + bottom
}
/// Always inline for SCCP.
///
/// FIXME(pcwalton): Just replace with the clear type from the style module for speed?
#[inline(always)]
pub fn clear(&self) -> Option<ClearType> {
let style = self.style();
match style.Box.clear {
clear::none => None,
clear::left => Some(ClearLeft),
clear::right => Some(ClearRight),
clear::both => Some(ClearBoth),
}
}
/// Converts this node's computed style to a font style used for rendering.
///
/// FIXME(pcwalton): This should not be necessary; just make the font part of style sharable
/// with the display list somehow. (Perhaps we should use an ARC.)
pub fn font_style(&self) -> FontStyle {
let my_style = self.style();
debug!("(font style) start");
// FIXME: Too much allocation here.
let font_families = my_style.Font.font_family.map(|family| {
match *family {
font_family::FamilyName(ref name) => (*name).clone(),
}
});
debug!("(font style) font families: `{:?}`", font_families);
let font_size = my_style.Font.font_size.to_f64().unwrap() / 60.0;
debug!("(font style) font size: `{:f}px`", font_size);
FontStyle {
pt_size: font_size,
weight: my_style.Font.font_weight,
style: my_style.Font.font_style,
families: font_families,
}
}
#[inline(always)]
pub fn style<'a>(&'a self) -> &'a ComputedValues {
self.style.get()
}
/// Returns the text alignment of the computed style of the nearest ancestor-or-self `Element`
/// node.
pub fn text_align(&self) -> text_align::T {
self.style().Text.text_align
}
pub fn line_height(&self) -> line_height::T {
self.style().Box.line_height
}
pub fn vertical_align(&self) -> vertical_align::T {
self.style().Box.vertical_align
}
/// Returns the text decoration of this box, according to the style of the nearest ancestor
/// element.
///
/// NB: This may not be the actual text decoration, because of the override rules specified in
/// CSS 2.1 § 16.3.1. Unfortunately, computing this properly doesn't really fit into Servo's
/// model. Therefore, this is a best lower bound approximation, but the end result may actually
/// have the various decoration flags turned on afterward.
pub fn text_decoration(&self) -> text_decoration::T {
self.style().Text.text_decoration
}
/// Returns the sum of margin, border, and padding on the left.
pub fn offset(&self) -> Au {
self.margin.get().left + self.border.get().left + self.padding.get().left
}
/// Returns true if this element is replaced content. This is true for images, form elements,
/// and so on.
pub fn is_replaced(&self) -> bool {
match self.specific {
ImageBox(..) => true,
_ => false,
}
}
/// Returns true if this element can be split. This is true for text boxes.
pub fn can_split(&self) -> bool {
match self.specific {
ScannedTextBox(..) => true,
_ => false,
}
}
/// Returns the amount of left and right "fringe" used by this box. This is based on margins,
/// borders, padding, and width.
pub fn get_used_width(&self) -> (Au, Au) {
// TODO: This should actually do some computation! See CSS 2.1, Sections 10.3 and 10.4.
(Au::new(0), Au::new(0))
}
/// Returns the amount of left and right "fringe" used by this box. This should be based on
/// margins, borders, padding, and width.
pub fn get_used_height(&self) -> (Au, Au) {
// TODO: This should actually do some computation! See CSS 2.1, Sections 10.5 and 10.6.
(Au::new(0), Au::new(0))
}
/// Adds the display items necessary to paint the background of this box to the display list if
/// necessary.
pub fn paint_background_if_applicable<E:ExtraDisplayListData>(
&self,
list: &RefCell<DisplayList<E>>,
absolute_bounds: &Rect<Au>,
offset: &Point2D<Au>) {
// FIXME: This causes a lot of background colors to be displayed when they are clearly not
// needed. We could use display list optimization to clean this up, but it still seems
// inefficient. What we really want is something like "nearest ancestor element that
// doesn't have a box".
let info = self.inline_info.borrow();
match info.get() {
&Some(ref box_info) => {
let mut bg_rect = absolute_bounds.clone();
for info in box_info.parent_info.rev_iter() {
// TODO (ksh8281) compute vertical-align, line-height
bg_rect.origin.y = box_info.baseline + offset.y - info.font_ascent;
bg_rect.size.height = info.font_ascent + info.font_descent;
let background_color = info.style.get().resolve_color(
info.style.get().Background.background_color);
if !background_color.alpha.approx_eq(&0.0) {
list.with_mut(|list| {
let solid_color_display_item = ~SolidColorDisplayItem {
base: BaseDisplayItem {
bounds: bg_rect.clone(),
extra: ExtraDisplayListData::new(self),
},
color: background_color.to_gfx_color(),
};
list.append_item(SolidColorDisplayItemClass(solid_color_display_item))
});
}
}
},
&None => {}
}
let style = self.style();
let background_color = style.resolve_color(style.Background.background_color);
if !background_color.alpha.approx_eq(&0.0) {
list.with_mut(|list| {
let solid_color_display_item = ~SolidColorDisplayItem {
base: BaseDisplayItem {
bounds: *absolute_bounds,
extra: ExtraDisplayListData::new(self),
},
color: background_color.to_gfx_color(),
};
list.append_item(SolidColorDisplayItemClass(solid_color_display_item))
})
}
}
/// Adds the display items necessary to paint the borders of this box to a display list if
/// necessary.
pub fn paint_borders_if_applicable<E:ExtraDisplayListData>(
&self,
list: &RefCell<DisplayList<E>>,
abs_bounds: &Rect<Au>) {
// Fast path.
let border = self.border.get();
if border.is_zero() {
return
}
let style = self.style();
let top_color = style.resolve_color(style.Border.border_top_color);
let right_color = style.resolve_color(style.Border.border_right_color);
let bottom_color = style.resolve_color(style.Border.border_bottom_color);
let left_color = style.resolve_color(style.Border.border_left_color);
let top_style = style.Border.border_top_style;
let right_style = style.Border.border_right_style;
let bottom_style = style.Border.border_bottom_style;
let left_style = style.Border.border_left_style;
// Append the border to the display list.
list.with_mut(|list| {
let border_display_item = ~BorderDisplayItem {
base: BaseDisplayItem {
bounds: *abs_bounds,
extra: ExtraDisplayListData::new(self),
},
border: border,
color: SideOffsets2D::new(top_color.to_gfx_color(),
right_color.to_gfx_color(),
bottom_color.to_gfx_color(),
left_color.to_gfx_color()),
style: SideOffsets2D::new(top_style,
right_style,
bottom_style,
left_style)
};
list.append_item(BorderDisplayItemClass(border_display_item))
});
}
/// Adds the display items for this box to the given display list.
///
/// Arguments:
/// * `builder`: The display list builder, which manages the coordinate system and options.
/// * `dirty`: The dirty rectangle in the coordinate system of the owning flow.
/// * `origin`: The total offset from the display list root flow to the owning flow of this
/// box.
/// * `list`: The display list to which items should be appended.
///
/// TODO: To implement stacking contexts correctly, we need to create a set of display lists,
/// one per layer of the stacking context (CSS 2.1 § 9.9.1). Each box is passed the list set
/// representing the box's stacking context. When asked to construct its constituent display
/// items, each box puts its display items into the correct stack layer according to CSS 2.1
/// Appendix E. Finally, the builder flattens the list.
pub fn build_display_list<E:ExtraDisplayListData>(
&self,
builder: &DisplayListBuilder,
dirty: &Rect<Au>,
offset: Point2D<Au>,
flow: &Flow,
list: &RefCell<DisplayList<E>>) {
let box_bounds = self.position.get();
let absolute_box_bounds = box_bounds.translate(&offset);
debug!("Box::build_display_list at rel={}, abs={}: {:s}",
box_bounds, absolute_box_bounds, self.debug_str());
debug!("Box::build_display_list: dirty={}, offset={}", *dirty, offset);
if self.style().Box.visibility != visibility::visible {
return;
}
if absolute_box_bounds.intersects(dirty) {
debug!("Box::build_display_list: intersected. Adding display item...");
} else {
debug!("Box::build_display_list: Did not intersect...");
return;
}
// Add the background to the list, if applicable.
self.paint_background_if_applicable(list, &absolute_box_bounds, &offset);
match self.specific {
UnscannedTextBox(_) => fail!("Shouldn't see unscanned boxes here."),
ScannedTextBox(ref text_box) => {
list.with_mut(|list| {
let item = ~ClipDisplayItem {
base: BaseDisplayItem {
bounds: absolute_box_bounds,
extra: ExtraDisplayListData::new(self),
},
child_list: ~[],
need_clip: false
};
list.append_item(ClipDisplayItemClass(item));
});
let text_color = self.style().Color.color.to_gfx_color();
// Set the various text display item flags.
let mut flow_flags = flow::base(flow).flags_info;
let inline_info = self.inline_info.borrow();
match inline_info.get() {
&Some(ref info) => {
for data in info.parent_info.rev_iter() {
let parent_info = FlowFlagsInfo::new(data.style.get());
//FIXME(ksh8281) avoid copy
flow_flags.propagate_text_decoration_from_parent(parent_info);
}
},
&None => {}
}
let mut text_flags = TextDisplayItemFlags::new();
text_flags.set_override_underline(flow_flags.flags.override_underline());
text_flags.set_override_overline(flow_flags.flags.override_overline());
text_flags.set_override_line_through(flow_flags.flags.override_line_through());
// Create the text box.
list.with_mut(|list| {
let text_display_item = ~TextDisplayItem {
base: BaseDisplayItem {
bounds: absolute_box_bounds,
extra: ExtraDisplayListData::new(self),
},
text_run: text_box.run.clone(),
range: text_box.range,
text_color: text_color,
overline_color: flow_flags.overline_color,
underline_color: flow_flags.underline_color,
line_through_color: flow_flags.line_through_color,
flags: text_flags,
};
list.append_item(TextDisplayItemClass(text_display_item))
});
// Draw debug frames for text bounds.
//
// FIXME(pcwalton): This is a bit of an abuse of the logging infrastructure. We
// should have a real `SERVO_DEBUG` system.
debug!("{:?}", {
// Compute the text box bounds and draw a border surrounding them.
let debug_border = SideOffsets2D::new_all_same(Au::from_px(1));
list.with_mut(|list| {
let border_display_item = ~BorderDisplayItem {
base: BaseDisplayItem {
bounds: absolute_box_bounds,
extra: ExtraDisplayListData::new(self),
},
border: debug_border,
color: SideOffsets2D::new_all_same(rgb(0, 0, 200)),
style: SideOffsets2D::new_all_same(border_style::solid)
};
list.append_item(BorderDisplayItemClass(border_display_item))
});
// Draw a rectangle representing the baselines.
//
// TODO(Issue #221): Create and use a Line display item for the baseline.
let ascent = text_box.run.get().metrics_for_range(
&text_box.range).ascent;
let baseline = Rect(absolute_box_bounds.origin + Point2D(Au(0), ascent),
Size2D(absolute_box_bounds.size.width, Au(0)));
list.with_mut(|list| {
let border_display_item = ~BorderDisplayItem {
base: BaseDisplayItem {
bounds: baseline,
extra: ExtraDisplayListData::new(self),
},
border: debug_border,
color: SideOffsets2D::new_all_same(rgb(0, 200, 0)),
style: SideOffsets2D::new_all_same(border_style::dashed)
};
list.append_item(BorderDisplayItemClass(border_display_item))
});
});
},
GenericBox | IframeBox(..) => {
list.with_mut(|list| {
let item = ~ClipDisplayItem {
base: BaseDisplayItem {
bounds: absolute_box_bounds,
extra: ExtraDisplayListData::new(self),
},
child_list: ~[],
need_clip: self.needs_clip()
};
list.append_item(ClipDisplayItemClass(item));
});
// FIXME(pcwalton): This is a bit of an abuse of the logging infrastructure. We
// should have a real `SERVO_DEBUG` system.
debug!("{:?}", {
let debug_border = SideOffsets2D::new_all_same(Au::from_px(1));
list.with_mut(|list| {
let border_display_item = ~BorderDisplayItem {
base: BaseDisplayItem {
bounds: absolute_box_bounds,
extra: ExtraDisplayListData::new(self),
},
border: debug_border,
color: SideOffsets2D::new_all_same(rgb(0, 0, 200)),
style: SideOffsets2D::new_all_same(border_style::solid)
};
list.append_item(BorderDisplayItemClass(border_display_item))
});
});
},
ImageBox(ref image_box) => {
list.with_mut(|list| {
let item = ~ClipDisplayItem {
base: BaseDisplayItem {
bounds: absolute_box_bounds,
extra: ExtraDisplayListData::new(self),
},
child_list: ~[],
need_clip: false
};
list.append_item(ClipDisplayItemClass(item));
});
let mut image_ref = image_box.image.borrow_mut();
match image_ref.get().get_image() {
Some(image) => {
debug!("(building display list) building image box");
// Place the image into the display list.
list.with_mut(|list| {
let image_display_item = ~ImageDisplayItem {
base: BaseDisplayItem {
bounds: absolute_box_bounds,
extra: ExtraDisplayListData::new(self),
},
image: image.clone(),
};
list.append_item(ImageDisplayItemClass(image_display_item));
});
}
None => {
// No image data at all? Do nothing.
//
// TODO: Add some kind of placeholder image.
debug!("(building display list) no image :(");
}
}
}
}
// If this is an iframe, then send its position and size up to the constellation.
//
// FIXME(pcwalton): Doing this during display list construction seems potentially
// problematic if iframes are outside the area we're computing the display list for, since
// they won't be able to reflow at all until the user scrolls to them. Perhaps we should
// separate this into two parts: first we should send the size only to the constellation
// once that's computed during assign-heights, and second we should should send the origin
// to the constellation here during display list construction. This should work because
// layout for the iframe only needs to know size, and origin is only relevant if the
// iframe is actually going to be displayed.
match self.specific {
IframeBox(ref iframe_box) => {
self.finalize_position_and_size_of_iframe(iframe_box, offset, builder.ctx)
}
GenericBox | ImageBox(_) | ScannedTextBox(_) | UnscannedTextBox(_) => {}
}
// Add a border, if applicable.
//
// TODO: Outlines.
self.paint_borders_if_applicable(list, &absolute_box_bounds);
}
/// Returns the *minimum width* and *preferred width* of this box as defined by CSS 2.1.
pub fn minimum_and_preferred_widths(&self) -> (Au, Au) {
let guessed_width = self.guess_width();
let (additional_minimum, additional_preferred) = match self.specific {
GenericBox | IframeBox(_) => (Au(0), Au(0)),
ImageBox(ref image_box_info) => {
let image_width = image_box_info.image_width();
(image_width, image_width)
}
ScannedTextBox(ref text_box_info) => {
let range = &text_box_info.range;
let min_line_width = text_box_info.run.get().min_width_for_range(range);
let mut max_line_width = Au::new(0);
for line_range in text_box_info.run.get().iter_natural_lines_for_range(range) {
let line_metrics = text_box_info.run.get().metrics_for_range(&line_range);
max_line_width = Au::max(max_line_width, line_metrics.advance_width);
}
(min_line_width, max_line_width)
}
UnscannedTextBox(..) => fail!("Unscanned text boxes should have been scanned by now!"),
};
(guessed_width + additional_minimum, guessed_width + additional_preferred)
}
/// Returns, and computes, the height of this box.
///
/// FIXME(pcwalton): Rename to just `height`?
/// FIXME(pcwalton): This function *mutates* the height? Gross! Refactor please.
pub fn box_height(&self) -> Au {
match self.specific {
GenericBox | IframeBox(_) => Au(0),
ImageBox(ref image_box_info) => {
let mut image_ref = image_box_info.image.borrow_mut();
let size = image_ref.get().get_size();
let height = Au::from_px(size.unwrap_or(Size2D(0, 0)).height);
// Eww. Refactor this.
self.position.borrow_mut().get().size.height = height;
debug!("box_height: found image height: {}", height);
height
}
ScannedTextBox(ref text_box_info) => {
// Compute the height based on the line-height and font size.
let (range, run) = (&text_box_info.range, &text_box_info.run);
let text_bounds = run.get().metrics_for_range(range).bounding_box;
let em_size = text_bounds.size.height;
self.calculate_line_height(em_size)
}
UnscannedTextBox(_) => fail!("Unscanned text boxes should have been scanned by now!"),
}
}
/// Attempts to split this box so that its width is no more than `max_width`.
pub fn split_to_width(&self, max_width: Au, starts_line: bool) -> SplitBoxResult {
match self.specific {
GenericBox | IframeBox(_) | ImageBox(_) => CannotSplit,
UnscannedTextBox(_) => fail!("Unscanned text boxes should have been scanned by now!"),
ScannedTextBox(ref text_box_info) => {
let mut pieces_processed_count: uint = 0;
let mut remaining_width: Au = max_width;
let mut left_range = Range::new(text_box_info.range.begin(), 0);
let mut right_range: Option<Range> = None;
debug!("split_to_width: splitting text box (strlen={:u}, range={}, \
avail_width={})",
text_box_info.run.get().text.get().len(),
text_box_info.range,