/
box_.rs
1680 lines (1511 loc) · 72.6 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::{ImageDisplayItem, ImageDisplayItemClass};
use gfx::display_list::{SolidColorDisplayItem, SolidColorDisplayItemClass, TextDisplayItem};
use gfx::display_list::{TextDisplayItemClass, TextDisplayItemFlags, ClipDisplayItem};
use gfx::display_list::{ClipDisplayItemClass, DisplayListCollection};
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 servo_util::namespace;
use std::cast;
use std::cell::RefCell;
use std::cmp::ApproxEq;
use std::num::Zero;
use style::{ComputedValues, TElement, TNode};
use style::computed_values::{LengthOrPercentage, LengthOrPercentageOrAuto, overflow, LPA_Auto};
use style::computed_values::{border_style, clear, font_family, line_height, position};
use style::computed_values::{text_align, text_decoration, vertical_align, visibility, white_space};
use css::node_style::StyledNode;
use layout::construct::FlowConstructor;
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, specified, Auto, Specified};
use layout::util::OpaqueNode;
use layout::wrapper::{TLayoutNode, ThreadSafeLayoutNode};
/// 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>>,
/// New-line chracter(\n)'s positions(relative, not absolute)
new_line_pos: ~[uint],
}
/// 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>,
computed_width: RefCell<Option<Au>>,
computed_height: RefCell<Option<Au>>,
dom_width: Option<Au>,
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: &ThreadSafeLayoutNode,
image_url: Url,
local_image_cache: MutexArc<LocalImageCache>)
-> ImageBoxInfo {
fn convert_length(node: &ThreadSafeLayoutNode, name: &str) -> Option<Au> {
node.with_element(|element| {
element.get_attr(&namespace::Null, 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)),
computed_width: RefCell::new(None),
computed_height: RefCell::new(None),
dom_width: convert_length(node,"width"),
dom_height: convert_length(node,"height"),
}
}
/// Returns the calculated width of the image, accounting for the width attribute.
pub fn computed_width(&self) -> Au {
match self.computed_width.borrow().get() {
&Some(width) => {
width
},
&None => {
fail!("image width is not computed yet!");
}
}
}
/// Returns width of image(just original width)
pub fn image_width(&self) -> Au {
let mut image_ref = self.image.borrow_mut();
Au::from_px(image_ref.get().get_size().unwrap_or(Size2D(0,0)).width)
}
pub fn style_length(style_length: LengthOrPercentageOrAuto,
dom_length: Option<Au>,
container_width: Au) -> MaybeAuto {
match (MaybeAuto::from_style(style_length,container_width),dom_length) {
(Specified(length),_) => {
Specified(length)
},
(Auto,Some(length)) => {
Specified(length)
},
(Auto,None) => {
Auto
}
}
}
/// Returns the calculated height of the image, accounting for the height attribute.
pub fn computed_height(&self) -> Au {
match self.computed_height.borrow().get() {
&Some(height) => {
height
},
&None => {
fail!("image height is not computed yet!");
}
}
}
/// Returns height of image(just original height)
pub fn image_height(&self) -> Au {
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: &ThreadSafeLayoutNode) -> 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: &ThreadSafeLayoutNode) -> UnscannedTextBoxInfo {
// FIXME(pcwalton): Don't copy text; atomically reference count it instead.
UnscannedTextBoxInfo {
text: node.text(),
}
}
/// Creates a new instance of `UnscannedTextBoxInfo` from the given text.
#[inline]
pub fn from_text(text: ~str) -> UnscannedTextBoxInfo {
UnscannedTextBoxInfo {
text: 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,
node: OpaqueNode,
}
impl Box {
/// Constructs a new `Box` instance.
pub fn new(constructor: &mut FlowConstructor, node: ThreadSafeLayoutNode) -> Box {
Box {
node: OpaqueNode::from_thread_safe_layout_node(&node),
style: node.style().clone(),
position: RefCell::new(Au::zero_rect()),
border: RefCell::new(Zero::zero()),
padding: RefCell::new(Zero::zero()),
margin: RefCell::new(Zero::zero()),
specific: constructor.build_specific_box_info_for_node(node),
position_offsets: RefCell::new(Zero::zero()),
inline_info: RefCell::new(None),
new_line_pos: ~[],
}
}
/// Constructs a new `Box` instance from an opaque node.
pub fn from_opaque_node_and_style(node: OpaqueNode,
style: Arc<ComputedValues>,
specific: SpecificBoxInfo)
-> Box {
Box {
node: node,
style: 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),
new_line_pos: ~[],
}
}
/// 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)
}
}
// CSS Section 10.6.4
// We have to solve the constraint equation:
// top + bottom + height + (vertical border + padding) = height of
// containing block (`screen_height`)
//
// `y`: static position of the element
//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.get().height, Au(0)) {
Auto => {
height = screen_height - position_offsets.top - position_offsets.bottom;
}
_ => {}
}
}
}
}
return (y, height);
}
// CSS Section 10.3.7
//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.get().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(),
new_line_pos: self.new_line_pos.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.get().width, Au::new(0)).specified_or_zero();
let margin_left = MaybeAuto::from_style(style.Margin.get().margin_left,
Au::new(0)).specified_or_zero();
let margin_right = MaybeAuto::from_style(style.Margin.get().margin_right,
Au::new(0)).specified_or_zero();
let padding_left = self.compute_padding_length(style.Padding.get().padding_left, Au(0));
let padding_right = self.compute_padding_length(style.Padding.get().padding_right, Au(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.get().border_top_width,
style.Border.get().border_top_style),
width(style.Border.get().border_right_width,
style.Border.get().border_right_style),
width(style.Border.get().border_bottom_width,
style.Border.get().border_bottom_style),
width(style.Border.get().border_left_width,
style.Border.get().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.get().top, containing_height)
.specified_or_zero(),
MaybeAuto::from_style(style.PositionOffsets.get().right, containing_width)
.specified_or_zero(),
MaybeAuto::from_style(style.PositionOffsets.get().bottom, containing_height)
.specified_or_zero(),
MaybeAuto::from_style(style.PositionOffsets.get().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
.get()
.padding_top,
containing_block_width),
self.compute_padding_length(style.Padding
.get()
.padding_right,
containing_block_width),
self.compute_padding_length(style.Padding
.get()
.padding_bottom,
containing_block_width),
self.compute_padding_length(style.Padding
.get()
.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 {
self.noncontent_left() + self.noncontent_right()
}
pub fn noncontent_height(&self) -> Au {
self.noncontent_top() + self.noncontent_bottom()
}
pub fn noncontent_left(&self) -> Au {
self.margin.get().left + self.border.get().left + self.padding.get().left
}
pub fn noncontent_right(&self) -> Au {
self.margin.get().right + self.border.get().right + self.padding.get().right
}
pub fn noncontent_top(&self) -> Au {
self.margin.get().top + self.border.get().top + self.padding.get().top
}
pub fn noncontent_bottom(&self) -> Au {
self.margin.get().bottom + self.border.get().bottom + self.padding.get().bottom
}
pub fn noncontent_inline_left(&self) -> Au {
let mut left = Au::new(0);
let info = self.inline_info.borrow();
match info.get() {
&Some(ref info) => {
for info in info.parent_info.iter() {
left = left + info.margin.left + info.border.left + info.padding.left;
}
},
&None => {}
}
left
}
pub fn noncontent_inline_right(&self) -> Au {
let mut right = Au::new(0);
let info = self.inline_info.borrow();
match info.get() {
&Some(ref info) => {
for info in info.parent_info.iter() {
right = right + info.margin.right + info.border.right + info.padding.right;
}
},
&None => {}
}
right
}
pub fn noncontent_inline_top(&self) -> Au {
let mut top = Au::new(0);
let info = self.inline_info.borrow();
match info.get() {
&Some(ref info) => {
for info in info.parent_info.iter() {
top = top + info.margin.top + info.border.top + info.padding.top;
}
},
&None => {}
}
top
}
pub fn noncontent_inline_bottom(&self) -> Au {
let mut bottom = Au::new(0);
let info = self.inline_info.borrow();
match info.get() {
&Some(ref info) => {
for info in info.parent_info.iter() {
bottom = bottom + info.margin.bottom + info.border.bottom + info.padding.bottom;
}
},
&None => {}
}
bottom
}
pub fn merge_noncontent_inline_right(&self, other_box: &Box) {
let mut info = self.inline_info.borrow_mut();
let other_info = other_box.inline_info.borrow();
match other_info.get() {
&Some(ref other_info) => {
match info.get() {
&Some(ref mut info) => {
for other_item in other_info.parent_info.iter() {
for item in info.parent_info.mut_iter() {
if item.node == other_item.node {
item.border.right = other_item.border.right;
item.padding.right = other_item.padding.right;
item.margin.right = other_item.margin.right;
break;
}
}
}
},
&None => {}
}
},
&None => {}
}
}
pub fn merge_noncontent_inline_left(&self, other_box: &Box) {
let mut info = self.inline_info.borrow_mut();
let other_info = other_box.inline_info.borrow();
match other_info.get() {
&Some(ref other_info) => {
match info.get() {
&Some(ref mut info) => {
for other_item in other_info.parent_info.iter() {
for item in info.parent_info.mut_iter() {
if item.node == other_item.node {
item.border.left = other_item.border.left;
item.padding.left = other_item.padding.left;
item.margin.left = other_item.margin.left;
break;
}
}
}
},
&None => {}
}
},
&None => {}
}
}
pub fn clear_noncontent_inline_right(&self) {
let mut info = self.inline_info.borrow_mut();
match info.get() {
&Some(ref mut info) => {
for item in info.parent_info.mut_iter() {
item.border.right = Au::new(0);
item.padding.right = Au::new(0);
item.margin.right = Au::new(0);
}
},
&None => {}
}
}
pub fn clear_noncontent_inline_left(&self) {
let mut info = self.inline_info.borrow_mut();
match info.get() {
&Some(ref mut info) => {
for item in info.parent_info.mut_iter() {
item.border.left = Au::new(0);
item.padding.left = Au::new(0);
item.margin.left = Au::new(0);
}
},
&None => {}
}
}
pub fn relative_position(&self, container_block_size: &Size2D<Au>) -> Point2D<Au> {
fn left_right(style: &ComputedValues, block_width: Au) -> Au {
// TODO(ksh8281) : consider RTL(right-to-left) culture
match (style.PositionOffsets.get().left, style.PositionOffsets.get().right) {
(LPA_Auto, _) => {
-MaybeAuto::from_style(style.PositionOffsets.get().right, block_width)
.specified_or_zero()
}
(_, _) => {
MaybeAuto::from_style(style.PositionOffsets.get().left, block_width)
.specified_or_zero()
}
}
}
fn top_bottom(style: &ComputedValues,block_height: Au) -> Au {
match (style.PositionOffsets.get().top, style.PositionOffsets.get().bottom) {
(LPA_Auto, _) => {
-MaybeAuto::from_style(style.PositionOffsets.get().bottom, block_height)
.specified_or_zero()
}
(_, _) => {
MaybeAuto::from_style(style.PositionOffsets.get().top, block_height)
.specified_or_zero()
}
}
}
let mut rel_pos: Point2D<Au> = Point2D {
x: Au::new(0),
y: Au::new(0),
};
if self.style().Box.get().position == position::relative {
rel_pos.x = rel_pos.x + left_right(self.style(), container_block_size.width);
rel_pos.y = rel_pos.y + top_bottom(self.style(), container_block_size.height);
}
let info = self.inline_info.borrow();
match info.get() {
&Some(ref info) => {
for info in info.parent_info.iter() {
if info.style.get().Box.get().position == position::relative {
rel_pos.x = rel_pos.x + left_right(info.style.get(),
container_block_size.width);
rel_pos.y = rel_pos.y + top_bottom(info.style.get(),
container_block_size.height);
}
}
},
&None => {}
}
rel_pos
}
/// 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.get().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.get().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.get().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.get().font_weight,
style: my_style.Font.get().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().InheritedText.get().text_align
}
pub fn line_height(&self) -> line_height::T {
self.style().InheritedBox.get().line_height
}
pub fn vertical_align(&self) -> vertical_align::T {
self.style().Box.get().vertical_align
}
pub fn white_space(&self) -> white_space::T {
self.style().InheritedText.get().white_space
}
/// 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.get().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))
}
pub fn paint_inline_background_border_if_applicable<E:ExtraDisplayListData>(
&self,
index: uint,
lists: &RefCell<DisplayListCollection<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.get().background_color);
if !background_color.alpha.approx_eq(&0.0) {
lists.with_mut(|lists| {
let solid_color_display_item = ~SolidColorDisplayItem {
base: BaseDisplayItem {
bounds: bg_rect.clone(),
extra: ExtraDisplayListData::new(self),
},
color: background_color.to_gfx_color(),
};
lists.lists[index].append_item(SolidColorDisplayItemClass(solid_color_display_item))
});
}
let border = &info.border;
// Fast path.
if border.is_zero() {
continue;
}
bg_rect.origin.y = bg_rect.origin.y - border.top;
bg_rect.size.height = bg_rect.size.height + border.top + border.bottom;
let style = info.style.get();
let top_color = style.resolve_color(style.Border.get().border_top_color);
let right_color = style.resolve_color(style.Border.get().border_right_color);
let bottom_color = style.resolve_color(style.Border.get().border_bottom_color);
let left_color = style.resolve_color(style.Border.get().border_left_color);
let top_style = style.Border.get().border_top_style;
let right_style = style.Border.get().border_right_style;
let bottom_style = style.Border.get().border_bottom_style;
let left_style = style.Border.get().border_left_style;
lists.with_mut(|lists| {
let border_display_item = ~BorderDisplayItem {
base: BaseDisplayItem {
bounds: bg_rect,
extra: ExtraDisplayListData::new(self),
},
border: border.clone(),
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)
};
lists.lists[index].append_item(BorderDisplayItemClass(border_display_item))
});
bg_rect.origin.x = bg_rect.origin.x + border.left;
bg_rect.size.width = bg_rect.size.width - border.left - border.right;
}
},
&None => {}
}
}
/// 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,
builder: &DisplayListBuilder,
index: uint,
lists: &RefCell<DisplayListCollection<E>>,
absolute_bounds: &Rect<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 style = self.style();
let background_color = style.resolve_color(style.Background.get().background_color);
if !background_color.alpha.approx_eq(&0.0) {
lists.with_mut(|lists| {
let solid_color_display_item = ~SolidColorDisplayItem {
base: BaseDisplayItem {
bounds: *absolute_bounds,
extra: ExtraDisplayListData::new(self),
},
color: background_color.to_gfx_color(),
};
lists.lists[index].append_item(SolidColorDisplayItemClass(solid_color_display_item))
});
}
// The background image is painted on top of the background color.
// Implements background image, per spec:
// http://www.w3.org/TR/CSS21/colors.html#background
match style.Background.get().background_image {
Some(ref image_url) => {
let mut holder = ImageHolder::new(image_url.clone(), builder.ctx.image_cache.clone());
match holder.get_image() {
Some(image) => {
debug!("(building display list) building background image");
// Place the image into the display list.
lists.with_mut(|lists| {
let image_display_item = ~ImageDisplayItem {
base: BaseDisplayItem {
bounds: *absolute_bounds,
extra: ExtraDisplayListData::new(self),
},
image: image.clone(),
};
lists.lists[index].append_item(ImageDisplayItemClass(image_display_item));
});
}
None => {
// No image data at all? Do nothing.
//
// TODO: Add some kind of placeholder background image.
debug!("(building display list) no background image :(");
}
}
}
None => {}
}
}
/// Adds the display items necessary to paint the borders of this box to a display list if
/// necessary.