-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
layout.rs
1972 lines (1781 loc) · 87.9 KB
/
layout.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 https://mozilla.org/MPL/2.0/. */
use std::ops::Range;
use app_units::{Au, MAX_AU};
use log::warn;
use servo_arc::Arc;
use style::computed_values::border_collapse::T as BorderCollapse;
use style::logical_geometry::WritingMode;
use style::properties::ComputedValues;
use style::values::computed::{
CSSPixelLength, Length, LengthPercentage as ComputedLengthPercentage, Percentage,
};
use style::values::generics::box_::{GenericVerticalAlign as VerticalAlign, VerticalAlignKeyword};
use style::values::generics::length::GenericLengthPercentageOrAuto::{Auto, LengthPercentage};
use style::Zero;
use super::{Table, TableSlot, TableSlotCell, TableTrackGroup};
use crate::context::LayoutContext;
use crate::formatting_contexts::{Baselines, IndependentLayout};
use crate::fragment_tree::{
BoxFragment, CollapsedBlockMargins, ExtraBackground, Fragment, PositioningFragment,
};
use crate::geom::{AuOrAuto, LengthPercentageOrAuto, LogicalRect, LogicalSides, LogicalVec2};
use crate::positioned::{PositioningContext, PositioningContextLength};
use crate::sizing::ContentSizes;
use crate::style_ext::{Clamp, ComputedValuesExt, PaddingBorderMargin};
use crate::table::TableSlotCoordinates;
use crate::ContainingBlock;
/// A result of a final or speculative layout of a single cell in
/// the table. Note that this is only done for slots that are not
/// covered by spans or empty.
struct CellLayout {
layout: IndependentLayout,
padding: LogicalSides<Length>,
border: LogicalSides<Length>,
positioning_context: PositioningContext,
}
impl CellLayout {
fn ascent(&self) -> Au {
self.layout
.baselines
.first
.unwrap_or(self.layout.content_block_size)
}
/// The block size of this laid out cell including its border and padding.
fn outer_block_size(&self) -> Au {
self.layout.content_block_size + (self.border.block_sum() + self.padding.block_sum()).into()
}
}
/// Information stored during the layout of rows.
#[derive(Clone, Debug, Default)]
struct RowLayout {
constrained: bool,
has_cell_with_span_greater_than_one: bool,
percent: Percentage,
}
/// Information stored during the layout of columns.
#[derive(Clone, Debug, Default)]
struct ColumnLayout {
constrained: bool,
has_originating_cells: bool,
}
/// A helper struct that performs the layout of the box tree version
/// of a table into the fragment tree version. This implements
/// <https://drafts.csswg.org/css-tables/#table-layout-algorithm>
struct TableLayout<'a> {
table: &'a Table,
pbm: PaddingBorderMargin,
rows: Vec<RowLayout>,
columns: Vec<ColumnLayout>,
cell_measures: Vec<Vec<LogicalVec2<CellOrTrackMeasure>>>,
assignable_width: Au,
final_table_height: Au,
column_measures: Vec<CellOrTrackMeasure>,
distributed_column_widths: Vec<Au>,
row_sizes: Vec<Au>,
row_baselines: Vec<Au>,
cells_laid_out: Vec<Vec<Option<CellLayout>>>,
basis_for_cell_padding_percentage: Au,
}
#[derive(Clone, Debug)]
struct CellOrTrackMeasure {
content_sizes: ContentSizes,
percentage: Percentage,
}
impl Zero for CellOrTrackMeasure {
fn zero() -> Self {
Self {
content_sizes: ContentSizes::zero(),
percentage: Percentage(0.),
}
}
fn is_zero(&self) -> bool {
self.content_sizes.is_zero() && self.percentage.is_zero()
}
}
impl<'a> TableLayout<'a> {
fn new(table: &'a Table) -> TableLayout {
Self {
table,
pbm: PaddingBorderMargin::zero(),
rows: Vec::new(),
columns: Vec::new(),
cell_measures: Vec::new(),
assignable_width: Au::zero(),
final_table_height: Au::zero(),
column_measures: Vec::new(),
distributed_column_widths: Vec::new(),
row_sizes: Vec::new(),
row_baselines: Vec::new(),
cells_laid_out: Vec::new(),
basis_for_cell_padding_percentage: Au::zero(),
}
}
/// This is an implementation of *Computing Cell Measures* from
/// <https://drafts.csswg.org/css-tables/#computing-cell-measures>.
pub(crate) fn compute_cell_measures(
&mut self,
layout_context: &LayoutContext,
writing_mode: WritingMode,
) {
let row_measures = vec![LogicalVec2::zero(); self.table.size.width];
self.cell_measures = vec![row_measures; self.table.size.height];
for row_index in 0..self.table.size.height {
for column_index in 0..self.table.size.width {
let cell = match self.table.slots[row_index][column_index] {
TableSlot::Cell(ref cell) => cell,
_ => continue,
};
let (size, min_size, max_size) = get_sizes_from_style(&cell.style, writing_mode);
let inline_content_sizes = cell
.contents
.contents
.inline_content_sizes(layout_context, writing_mode);
let percentage_contribution =
get_size_percentage_contribution_from_style(&cell.style, writing_mode);
// > The outer min-content width of a table-cell is max(min-width, min-content width)
// > adjusted by the cell intrinsic offsets.
let mut outer_min_content_width =
inline_content_sizes.min_content.max(min_size.inline);
let mut outer_max_content_width = if !self.columns[column_index].constrained {
// > The outer max-content width of a table-cell in a non-constrained column is
// > max(min-width, width, min-content width, min(max-width, max-content width))
// > adjusted by the cell intrinsic offsets.
min_size
.inline
.max(size.inline)
.max(inline_content_sizes.min_content)
.max(max_size.inline.min(inline_content_sizes.max_content))
} else {
// > The outer max-content width of a table-cell in a constrained column is
// > max(min-width, width, min-content width, min(max-width, width)) adjusted by the
// > cell intrinsic offsets.
min_size
.inline
.max(size.inline)
.max(inline_content_sizes.min_content)
.max(max_size.inline.min(size.inline))
};
let padding = cell
.style
.padding(writing_mode)
.percentages_relative_to(Length::zero());
let border = cell.style.border_width(writing_mode);
let inline_padding_border_sum =
Au::from(padding.inline_sum() + border.inline_sum());
outer_min_content_width += inline_padding_border_sum;
outer_max_content_width += inline_padding_border_sum;
let inline_measure = CellOrTrackMeasure {
content_sizes: ContentSizes {
min_content: outer_min_content_width,
max_content: outer_max_content_width,
},
percentage: percentage_contribution.inline,
};
// These calculations do not take into account the `min-content` and `max-content`
// sizes. These sizes are incorporated after the first row layout pass, when the
// block size of the layout is known.
//
// TODO: Is it correct to use the block size as the minimum of the `outer min
// content height` here? The specification doesn't mention this, but it does cause
// a test to pass.
let mut outer_min_content_height = min_size.block.max(size.block);
let mut outer_max_content_height = if !self.rows[row_index].constrained {
min_size.block.max(size.block)
} else {
min_size
.block
.max(size.block)
.max(max_size.block.min(size.block))
};
let block_padding_border_sum = Au::from(padding.block_sum() + border.block_sum());
outer_min_content_height += block_padding_border_sum;
outer_max_content_height += block_padding_border_sum;
let block_measure = CellOrTrackMeasure {
content_sizes: ContentSizes {
min_content: outer_min_content_height,
max_content: outer_max_content_height,
},
percentage: percentage_contribution.block,
};
self.cell_measures[row_index][column_index] = LogicalVec2 {
inline: inline_measure,
block: block_measure,
};
}
}
}
/// Compute the constrainedness of every column in the table.
///
/// > A column is constrained if its corresponding table-column-group (if any), its
/// > corresponding table-column (if any), or any of the cells spanning only that
/// > column has a computed width that is not "auto", and is not a percentage.
fn compute_track_constrainedness_and_has_originating_cells(
&mut self,
writing_mode: WritingMode,
) {
self.rows = vec![RowLayout::default(); self.table.size.height];
self.columns = vec![ColumnLayout::default(); self.table.size.width];
for column_index in 0..self.table.size.width {
if let Some(column) = self.table.columns.get(column_index) {
if !column.style.box_size(writing_mode).inline.is_auto() {
self.columns[column_index].constrained = true;
continue;
}
if let Some(column_group_index) = column.group_index {
let column_group = &self.table.column_groups[column_group_index];
if !column_group.style.box_size(writing_mode).inline.is_auto() {
self.columns[column_index].constrained = true;
continue;
}
}
self.columns[column_index].constrained = false;
}
}
for row_index in 0..self.table.size.height {
if let Some(row) = self.table.rows.get(row_index) {
if !row.style.box_size(writing_mode).block.is_auto() {
self.rows[row_index].constrained = true;
continue;
}
if let Some(row_group_index) = row.group_index {
let row_group = &self.table.row_groups[row_group_index];
if !row_group.style.box_size(writing_mode).block.is_auto() {
self.rows[row_index].constrained = true;
continue;
}
}
}
self.rows[row_index].constrained = false;
}
for column_index in 0..self.table.size.width {
for row_index in 0..self.table.size.height {
let coords = TableSlotCoordinates::new(column_index, row_index);
let cell_constrained = match self.table.resolve_first_cell(coords) {
Some(cell) if cell.colspan == 1 => cell
.style
.box_size(writing_mode)
.inline
.non_auto()
.map(|length_percentage| length_percentage.to_length().is_some())
.unwrap_or(false),
_ => false,
};
let rowspan_greater_than_1 = match self.table.slots[row_index][column_index] {
TableSlot::Cell(ref cell) => cell.rowspan > 1,
_ => false,
};
self.rows[row_index].has_cell_with_span_greater_than_one |= rowspan_greater_than_1;
self.rows[row_index].constrained |= cell_constrained;
let has_originating_cell =
matches!(self.table.get_slot(coords), Some(TableSlot::Cell(_)));
self.columns[column_index].has_originating_cells |= has_originating_cell;
self.columns[column_index].constrained |= cell_constrained;
}
}
}
/// This is an implementation of *Computing Column Measures* from
/// <https://drafts.csswg.org/css-tables/#computing-column-measures>.
fn compute_column_measures(&mut self, writing_mode: WritingMode) {
let mut column_measures = Vec::new();
// Compute the column measures only taking into account cells with colspan == 1.
// This is the base case that will be used to iteratively account for cells with
// larger colspans afterward.
//
// > min-content width of a column based on cells of span up to 1
// > The largest of:
// > - the width specified for the column:
// > - the outer min-content width of its corresponding table-column,
// > if any (and not auto)
// > - the outer min-content width of its corresponding table-column-group, if any
// > - or 0, if there is none
// > - the outer min-content width of each cell that spans the column whose colSpan
// > is 1 (or just the one in the first row in fixed mode) or 0 if there is none
// >
// > max-content width of a column based on cells of span up to 1
// > The largest of:
// > - the outer max-content width of its corresponding
// > table-column-group, if any
// > - the outer max-content width of its corresponding table-column, if any
// > - the outer max-content width of each cell that spans the column
// > whose colSpan is 1 (or just the one in the first row if in fixed mode) or 0
// > if there is no such cell
// >
// > intrinsic percentage width of a column based on cells of span up to 1
// > The largest of the percentage contributions of each cell that spans the column whose colSpan is
// > 1, of its corresponding table-column (if any), and of its corresponding table-column-group (if
// > any)
//
// TODO: Take into account `table-column` and `table-column-group` lengths.
// TODO: Take into account changes to this computation for fixed table layout.
let mut next_span_n = usize::MAX;
for column_index in 0..self.table.size.width {
let mut column_measure = self
.table
.get_column_measure_for_column_at_index(writing_mode, column_index);
for row_index in 0..self.table.size.height {
let coords = TableSlotCoordinates::new(column_index, row_index);
match self.table.resolve_first_cell(coords) {
Some(cell) if cell.colspan == 1 => cell,
Some(cell) => {
next_span_n = next_span_n.min(cell.colspan);
continue;
},
_ => continue,
};
// This takes the max of `min_content`, `max_content`, and
// intrinsic percentage width as described above.
let cell_measure = &self.cell_measures[row_index][column_index].inline;
column_measure
.content_sizes
.max_assign(cell_measure.content_sizes);
column_measure.percentage =
Percentage(column_measure.percentage.0.max(cell_measure.percentage.0));
}
column_measures.push(column_measure);
}
// Now we have the base computation complete, so iteratively take into account cells
// with higher colspan. Using `next_span_n` we can skip over span counts that don't
// correspond to any cells.
while next_span_n < usize::MAX {
(next_span_n, column_measures) = self
.compute_content_sizes_for_columns_with_span_up_to_n(next_span_n, &column_measures);
}
// > intrinsic percentage width of a column:
// > the smaller of:
// > * the intrinsic percentage width of the column based on cells of span up to N,
// > where N is the number of columns in the table
// > * 100% minus the sum of the intrinsic percentage width of all prior columns in
// > the table (further left when direction is "ltr" (right for "rtl"))
let mut total_intrinsic_percentage_width = 0.;
for column_index in 0..self.table.size.width {
let column_measure = &mut column_measures[column_index];
let final_intrinsic_percentage_width = column_measure
.percentage
.0
.min(1. - total_intrinsic_percentage_width);
total_intrinsic_percentage_width += final_intrinsic_percentage_width;
column_measure.percentage = Percentage(final_intrinsic_percentage_width);
}
self.column_measures = column_measures;
}
fn compute_content_sizes_for_columns_with_span_up_to_n(
&self,
n: usize,
old_column_measures: &[CellOrTrackMeasure],
) -> (usize, Vec<CellOrTrackMeasure>) {
let mut next_span_n = usize::MAX;
let mut new_content_sizes_for_columns = Vec::new();
let border_spacing = self.table.border_spacing();
for column_index in 0..self.table.size.width {
let old_column_measure = &old_column_measures[column_index];
let mut new_column_content_sizes = ContentSizes::zero();
let mut new_column_intrinsic_percentage_width = Percentage(0.);
for row_index in 0..self.table.size.height {
let coords = TableSlotCoordinates::new(column_index, row_index);
let resolved_coords = match self.table.resolve_first_cell_coords(coords) {
Some(resolved_coords) => resolved_coords,
None => continue,
};
let cell = match self.table.resolve_first_cell(resolved_coords) {
Some(cell) if cell.colspan <= n => cell,
Some(cell) => {
next_span_n = next_span_n.min(cell.colspan);
continue;
},
_ => continue,
};
let cell_measures =
&self.cell_measures[resolved_coords.y][resolved_coords.x].inline;
let cell_inline_content_sizes = cell_measures.content_sizes;
let columns_spanned = resolved_coords.x..resolved_coords.x + cell.colspan;
let baseline_content_sizes: ContentSizes = columns_spanned.clone().fold(
ContentSizes::zero(),
|total: ContentSizes, spanned_column_index| {
total + old_column_measures[spanned_column_index].content_sizes
},
);
let old_column_content_size = old_column_measure.content_sizes;
// > **min-content width of a column based on cells of span up to N (N > 1)**
// >
// > the largest of the min-content width of the column based on cells of span up to
// > N-1 and the contributions of the cells in the column whose colSpan is N, where
// > the contribution of a cell is the result of taking the following steps:
// >
// > 1. Define the baseline min-content width as the sum of the max-content
// > widths based on cells of span up to N-1 of all columns that the cell spans.
//
// Note: This definition is likely a typo, so we use the sum of the min-content
// widths here instead.
let baseline_min_content_width = baseline_content_sizes.min_content;
let baseline_max_content_width = baseline_content_sizes.max_content;
// > 2. Define the baseline border spacing as the sum of the horizontal
// > border-spacing for any columns spanned by the cell, other than the one in
// > which the cell originates.
let baseline_border_spacing = border_spacing.inline * (n as i32 - 1);
// > 3. The contribution of the cell is the sum of:
// > a. the min-content width of the column based on cells of span up to N-1
let a = old_column_content_size.min_content;
// > b. the product of:
// > - the ratio of:
// > - the max-content width of the column based on cells of span up
// > to N-1 of the column minus the min-content width of the
// > column based on cells of span up to N-1 of the column, to
// > - the baseline max-content width minus the baseline min-content
// > width
// > or zero if this ratio is undefined, and
// > - the outer min-content width of the cell minus the baseline
// > min-content width and the baseline border spacing, clamped to be
// > at least 0 and at most the difference between the baseline
// > max-content width and the baseline min-content width
let old_content_size_difference =
old_column_content_size.max_content - old_column_content_size.min_content;
let baseline_difference = baseline_min_content_width - baseline_max_content_width;
let mut b =
old_content_size_difference.to_f32_px() / baseline_difference.to_f32_px();
if !b.is_finite() {
b = 0.0;
}
let b = (cell_inline_content_sizes.min_content -
baseline_content_sizes.min_content -
baseline_border_spacing)
.clamp_between_extremums(Au::zero(), Some(baseline_difference))
.scale_by(b);
// > c. the product of:
// > - the ratio of the max-content width based on cells of span up to
// > N-1 of the column to the baseline max-content width
// > - the outer min-content width of the cell minus the baseline
// > max-content width and baseline border spacing, or 0 if this is
// > negative
let c = (cell_inline_content_sizes.min_content -
baseline_content_sizes.max_content -
baseline_border_spacing)
.min(Au::zero())
.scale_by(
old_column_content_size.max_content.to_f32_px() /
baseline_content_sizes.max_content.to_f32_px(),
);
let new_column_min_content_width = a + b + c;
// > **max-content width of a column based on cells of span up to N (N > 1)**
// >
// > The largest of the max-content width based on cells of span up to N-1 and the
// > contributions of the cells in the column whose colSpan is N, where the
// > contribution of a cell is the result of taking the following steps:
// > 1. Define the baseline max-content width as the sum of the max-content
// > widths based on cells of span up to N-1 of all columns that the cell spans.
//
// This is calculated above for the min-content width.
// > 2. Define the baseline border spacing as the sum of the horizontal
// > border-spacing for any columns spanned by the cell, other than the one in
// > which the cell originates.
//
// This is calculated above for min-content width.
// > 3. The contribution of the cell is the sum of:
// > a. the max-content width of the column based on cells of span up to N-1
let a = old_column_content_size.max_content;
// > b. the product of:
// > 1. the ratio of the max-content width based on cells of span up to
// > N-1 of the column to the baseline max-content width
let b_1 = old_column_content_size.max_content.to_f32_px() /
baseline_content_sizes.max_content.to_f32_px();
// > 2. the outer max-content width of the cell minus the baseline
// > max-content width and the baseline border spacing, or 0 if this
// > is negative
let b_2 = (cell_inline_content_sizes.max_content -
baseline_content_sizes.max_content -
baseline_border_spacing)
.min(Au::zero());
let b = b_2.scale_by(b_1);
let new_column_max_content_width = a + b + c;
// The computed values for the column are always the largest of any processed cell
// in that column.
new_column_content_sizes.max_assign(ContentSizes {
min_content: new_column_min_content_width,
max_content: new_column_max_content_width,
});
// > If the intrinsic percentage width of a column based on cells of span up to N-1 is
// > greater than 0%, then the intrinsic percentage width of the column based on cells
// > of span up to N is the same as the intrinsic percentage width of the column based
// > on cells of span up to N-1.
// > Otherwise, it is the largest of the contributions of the cells in the column
// > whose colSpan is N, where the contribution of a cell is the result of taking
// > the following steps:
if old_column_measure.percentage.0 <= 0. && cell_measures.percentage.0 != 0. {
// > 1. Start with the percentage contribution of the cell.
// > 2. Subtract the intrinsic percentage width of the column based on cells
// > of span up to N-1 of all columns that the cell spans. If this gives a
// > negative result, change it to 0%.
let mut spanned_columns_with_zero = 0;
let other_column_percentages_sum =
(columns_spanned).fold(0., |sum, spanned_column_index| {
let spanned_column_percentage =
old_column_measures[spanned_column_index].percentage;
if spanned_column_percentage.0 == 0. {
spanned_columns_with_zero += 1;
}
sum + spanned_column_percentage.0
});
let step_2 = (cell_measures.percentage -
Percentage(other_column_percentages_sum))
.clamp_to_non_negative();
// > Multiply by the ratio of:
// > 1. the column’s non-spanning max-content width to
// > 2. the sum of the non-spanning max-content widths of all columns
// > spanned by the cell that have an intrinsic percentage width of the column
// > based on cells of span up to N-1 equal to 0%.
// > However, if this ratio is undefined because the denominator is zero,
// > instead use the 1 divided by the number of columns spanned by the cell
// > that have an intrinsic percentage width of the column based on cells of
// > span up to N-1 equal to zero.
let step_3 = step_2.0 * (1.0 / spanned_columns_with_zero as f32);
new_column_intrinsic_percentage_width =
Percentage(new_column_intrinsic_percentage_width.0.max(step_3));
}
}
new_content_sizes_for_columns.push(CellOrTrackMeasure {
content_sizes: new_column_content_sizes,
percentage: new_column_intrinsic_percentage_width,
});
}
(next_span_n, new_content_sizes_for_columns)
}
/// Compute the GRIDMIN and GRIDMAX.
fn compute_grid_min_max(
&mut self,
layout_context: &LayoutContext,
writing_mode: WritingMode,
) -> ContentSizes {
self.compute_track_constrainedness_and_has_originating_cells(writing_mode);
self.compute_cell_measures(layout_context, writing_mode);
self.compute_column_measures(writing_mode);
// https://drafts.csswg.org/css-tables/#gridmin:
// > The row/column-grid width minimum (GRIDMIN) width is the sum of the min-content width of
// > all the columns plus cell spacing or borders.
// https://drafts.csswg.org/css-tables/#gridmax:
// > The row/column-grid width maximum (GRIDMAX) width is the sum of the max-content width of
// > all the columns plus cell spacing or borders.
let mut grid_min_max = self
.column_measures
.iter()
.fold(ContentSizes::zero(), |result, measure| {
result + measure.content_sizes
});
// TODO: GRIDMAX should never be smaller than GRIDMIN!
grid_min_max.max_content = grid_min_max.max_content.max(grid_min_max.min_content);
let inline_border_spacing = self.table.total_border_spacing().inline;
grid_min_max.min_content += inline_border_spacing;
grid_min_max.max_content += inline_border_spacing;
grid_min_max
}
fn compute_table_width(
&mut self,
containing_block_for_children: &ContainingBlock,
containing_block_for_table: &ContainingBlock,
grid_min_max: ContentSizes,
) {
let style = &self.table.style;
self.pbm = style.padding_border_margin(containing_block_for_table);
// https://drafts.csswg.org/css-tables/#resolved-table-width
// * If inline-size computes to 'auto', this is the stretch-fit size
// (https://drafts.csswg.org/css-sizing-3/#stretch-fit-size).
// * Otherwise, it's the resulting length (with percentages resolved).
// In both cases, it's clamped between min-inline-size and max-inline-size.
// This diverges a little from the specification.
let resolved_table_width = containing_block_for_children.inline_size;
// https://drafts.csswg.org/css-tables/#used-width-of-table
// * If table-root has a computed value for inline-size different than auto:
// use the maximum of the resolved table width and GRIDMIN.
// * If auto: use the resolved_table_width, clamped between GRIDMIN and GRIDMAX,
// but at least as big as min-inline-size.
// This diverges a little from the specification, but should be equivalent
// (other than using the stretch-fit size instead of the containing block width).
let used_width_of_table = match style
.content_box_size(containing_block_for_table, &self.pbm)
.inline
{
LengthPercentage(_) => resolved_table_width.max(grid_min_max.min_content),
Auto => {
let min_width: Au = style
.content_min_box_size(containing_block_for_table, &self.pbm)
.inline
.auto_is(Length::zero)
.into();
resolved_table_width
.clamp(grid_min_max.min_content, grid_min_max.max_content)
.max(min_width)
},
};
// > The assignable table width is the used width of the table minus the total horizontal
// > border spacing (if any). This is the width that we will be able to allocate to the
// > columns.
self.assignable_width = used_width_of_table - self.table.total_border_spacing().inline;
// This is the amount that we will use to resolve percentages in the padding of cells.
// It matches what Gecko and Blink do, though they disagree when there is a big caption.
self.basis_for_cell_padding_percentage =
used_width_of_table - self.table.border_spacing().inline * 2;
}
/// Distribute width to columns, performing step 2.4 of table layout from
/// <https://drafts.csswg.org/css-tables/#table-layout-algorithm>.
fn distribute_width_to_columns(&self) -> Vec<Au> {
if self.table.slots.is_empty() {
return Vec::new();
}
// > First, each column of the table is assigned a sizing type:
// > * percent-column: a column whose any constraint is defined to use a percentage only
// > (with a value different from 0%)
// > * pixel-column: column whose any constraint is defined to use a defined length only
// > (and is not a percent-column)
// > * auto-column: any other column
// >
// > Then, valid sizing methods are to be assigned to the columns by sizing type, yielding
// > the following sizing-guesses:
// >
// > * The min-content sizing-guess is the set of column width assignments where
// > each column is assigned its min-content width.
// > * The min-content-percentage sizing-guess is the set of column width assignments where:
// > * each percent-column is assigned the larger of:
// > * its intrinsic percentage width times the assignable width and
// > * its min-content width.
// > * all other columns are assigned their min-content width.
// > * The min-content-specified sizing-guess is the set of column width assignments where:
// > * each percent-column is assigned the larger of:
// > * its intrinsic percentage width times the assignable width and
// > * its min-content width
// > * any other column that is constrained is assigned its max-content width
// > * all other columns are assigned their min-content width.
// > * The max-content sizing-guess is the set of column width assignments where:
// > * each percent-column is assigned the larger of:
// > * its intrinsic percentage width times the assignable width and
// > * its min-content width
// > * all other columns are assigned their max-content width.
let mut min_content_sizing_guesses = Vec::new();
let mut min_content_percentage_sizing_guesses = Vec::new();
let mut min_content_specified_sizing_guesses = Vec::new();
let mut max_content_sizing_guesses = Vec::new();
for column_idx in 0..self.table.size.width {
let column_measure = &self.column_measures[column_idx];
let min_content_width = column_measure.content_sizes.min_content;
let max_content_width = column_measure.content_sizes.max_content;
let constrained = self.columns[column_idx].constrained;
let (
min_content_percentage_sizing_guess,
min_content_specified_sizing_guess,
max_content_sizing_guess,
) = if !column_measure.percentage.is_zero() {
let resolved = self.assignable_width.scale_by(column_measure.percentage.0);
let percent_guess = min_content_width.max(resolved);
(percent_guess, percent_guess, percent_guess)
} else if constrained {
(min_content_width, max_content_width, max_content_width)
} else {
(min_content_width, min_content_width, max_content_width)
};
min_content_sizing_guesses.push(min_content_width);
min_content_percentage_sizing_guesses.push(min_content_percentage_sizing_guess);
min_content_specified_sizing_guesses.push(min_content_specified_sizing_guess);
max_content_sizing_guesses.push(max_content_sizing_guess);
}
// > If the assignable table width is less than or equal to the max-content sizing-guess, the
// > used widths of the columns must be the linear combination (with weights adding to 1) of
// > the two consecutive sizing-guesses whose width sums bound the available width.
//
// > Otherwise, the used widths of the columns are the result of starting from the max-content
// > sizing-guess and distributing the excess width to the columns of the table according to
// > the rules for distributing excess width to columns (for used width).
fn sum(guesses: &[Au]) -> Au {
guesses.iter().fold(Au::zero(), |sum, guess| sum + *guess)
}
let max_content_sizing_sum = sum(&max_content_sizing_guesses);
if self.assignable_width >= max_content_sizing_sum {
self.distribute_extra_width_to_columns(
&mut max_content_sizing_guesses,
max_content_sizing_sum,
);
return max_content_sizing_guesses;
}
let min_content_specified_sizing_sum = sum(&min_content_specified_sizing_guesses);
if self.assignable_width == min_content_specified_sizing_sum {
return min_content_specified_sizing_guesses;
}
let min_content_percentage_sizing_sum = sum(&min_content_percentage_sizing_guesses);
if self.assignable_width == min_content_percentage_sizing_sum {
return min_content_percentage_sizing_guesses;
}
let min_content_sizes_sum = sum(&min_content_sizing_guesses);
if self.assignable_width <= min_content_sizes_sum {
return min_content_sizing_guesses;
}
let bounds = |sum_a, sum_b| self.assignable_width > sum_a && self.assignable_width < sum_b;
let blend = |a: &[Au], sum_a: Au, b: &[Au], sum_b: Au| {
// First convert the Au units to f32 in order to do floating point division.
let weight_a =
(self.assignable_width - sum_b).to_f32_px() / (sum_a - sum_b).to_f32_px();
let weight_b = 1.0 - weight_a;
a.iter()
.zip(b.iter())
.map(|(guess_a, guess_b)| {
(guess_a.scale_by(weight_a)) + (guess_b.scale_by(weight_b))
})
.collect()
};
if bounds(min_content_sizes_sum, min_content_percentage_sizing_sum) {
return blend(
&min_content_sizing_guesses,
min_content_sizes_sum,
&min_content_percentage_sizing_guesses,
min_content_percentage_sizing_sum,
);
}
if bounds(
min_content_percentage_sizing_sum,
min_content_specified_sizing_sum,
) {
return blend(
&min_content_percentage_sizing_guesses,
min_content_percentage_sizing_sum,
&min_content_specified_sizing_guesses,
min_content_specified_sizing_sum,
);
}
assert!(bounds(
min_content_specified_sizing_sum,
max_content_sizing_sum
));
blend(
&min_content_specified_sizing_guesses,
min_content_specified_sizing_sum,
&max_content_sizing_guesses,
max_content_sizing_sum,
)
}
/// This is an implementation of *Distributing excess width to columns* from
/// <https://drafts.csswg.org/css-tables/#distributing-width-to-columns>.
fn distribute_extra_width_to_columns(&self, column_sizes: &mut Vec<Au>, column_sizes_sum: Au) {
let all_columns = 0..self.table.size.width;
let extra_inline_size = self.assignable_width - column_sizes_sum;
let has_originating_cells =
|column_index: &usize| self.columns[*column_index].has_originating_cells;
let is_constrained = |column_index: &usize| self.columns[*column_index].constrained;
let is_unconstrained = |column_index: &usize| !is_constrained(column_index);
let has_percent_greater_than_zero =
|column_index: &usize| self.column_measures[*column_index].percentage.0 > 0.;
let has_percent_zero = |column_index: &usize| !has_percent_greater_than_zero(column_index);
let has_max_content = |column_index: &usize| {
self.column_measures[*column_index]
.content_sizes
.max_content !=
Au(0)
};
let max_content_sum =
|column_index: usize| self.column_measures[column_index].content_sizes.max_content;
// > If there are non-constrained columns that have originating cells with intrinsic
// > percentage width of 0% and with nonzero max-content width (aka the columns allowed to
// > grow by this rule), the distributed widths of the columns allowed to grow by this rule
// > are increased in proportion to max-content width so the total increase adds to the
// > excess width.
let unconstrained_max_content_columns = all_columns
.clone()
.filter(is_unconstrained)
.filter(has_originating_cells)
.filter(has_percent_zero)
.filter(has_max_content);
let total_max_content_width = unconstrained_max_content_columns
.clone()
.map(max_content_sum)
.fold(Au::zero(), |a, b| a + b);
if total_max_content_width != Au::zero() {
for column_index in unconstrained_max_content_columns {
column_sizes[column_index] += extra_inline_size.scale_by(
self.column_measures[column_index]
.content_sizes
.max_content
.to_f32_px() /
total_max_content_width.to_f32_px(),
);
}
return;
}
// > Otherwise, if there are non-constrained columns that have originating cells with intrinsic
// > percentage width of 0% (aka the columns allowed to grow by this rule, which thanks to the
// > previous rule must have zero max-content width), the distributed widths of the columns
// > allowed to grow by this rule are increased by equal amounts so the total increase adds to
// > the excess width.V
let unconstrained_no_percent_columns = all_columns
.clone()
.filter(is_unconstrained)
.filter(has_originating_cells)
.filter(has_percent_zero);
let total_unconstrained_no_percent = unconstrained_no_percent_columns.clone().count();
if total_unconstrained_no_percent > 0 {
let extra_space_per_column =
extra_inline_size.scale_by(1.0 / total_unconstrained_no_percent as f32);
for column_index in unconstrained_no_percent_columns {
column_sizes[column_index] += extra_space_per_column;
}
return;
}
// > Otherwise, if there are constrained columns with intrinsic percentage width of 0% and
// > with nonzero max-content width (aka the columns allowed to grow by this rule, which, due
// > to other rules, must have originating cells), the distributed widths of the columns
// > allowed to grow by this rule are increased in proportion to max-content width so the
// > total increase adds to the excess width.
let constrained_max_content_columns = all_columns
.clone()
.filter(is_constrained)
.filter(has_originating_cells)
.filter(has_percent_zero)
.filter(has_max_content);
let total_max_content_width = constrained_max_content_columns
.clone()
.map(max_content_sum)
.fold(Au::zero(), |a, b| a + b);
if total_max_content_width != Au::zero() {
for column_index in constrained_max_content_columns {
column_sizes[column_index] += extra_inline_size.scale_by(
self.column_measures[column_index]
.content_sizes
.max_content
.to_f32_px() /
total_max_content_width.to_f32_px(),
);
}
return;
}
// > Otherwise, if there are columns with intrinsic percentage width greater than 0% (aka the
// > columns allowed to grow by this rule, which, due to other rules, must have originating
// > cells), the distributed widths of the columns allowed to grow by this rule are increased
// > in proportion to intrinsic percentage width so the total increase adds to the excess
// > width.
let columns_with_percentage = all_columns.clone().filter(has_percent_greater_than_zero);
let total_percent = columns_with_percentage
.clone()
.map(|column_index| self.column_measures[column_index].percentage.0)
.sum::<f32>();
if total_percent > 0. {
for column_index in columns_with_percentage {
column_sizes[column_index] += extra_inline_size
.scale_by(self.column_measures[column_index].percentage.0 / total_percent);
}
return;
}
// > Otherwise, if there is any such column, the distributed widths of all columns that have
// > originating cells are increased by equal amounts so the total increase adds to the excess
// > width.
let has_originating_cells_columns = all_columns.clone().filter(has_originating_cells);
let total_has_originating_cells = has_originating_cells_columns.clone().count();
if total_has_originating_cells > 0 {
let extra_space_per_column =
extra_inline_size.scale_by(1.0 / total_has_originating_cells as f32);
for column_index in has_originating_cells_columns {
column_sizes[column_index] += extra_space_per_column;
}
return;
}
// > Otherwise, the distributed widths of all columns are increased by equal amounts so the
// total increase adds to the excess width.
let extra_space_for_all_columns =
extra_inline_size.scale_by(1.0 / self.table.size.width as f32);
for guess in column_sizes.iter_mut() {
*guess += extra_space_for_all_columns;
}
}
/// This is an implementation of *Row layout (first pass)* from
/// <https://drafts.csswg.org/css-tables/#row-layout>.
fn layout_cells_in_row(
&mut self,
layout_context: &LayoutContext,
containing_block_for_table: &ContainingBlock,
parent_positioning_context: &mut PositioningContext,
) {
for row_index in 0..self.table.slots.len() {
let row = &self.table.slots[row_index];
let mut cells_laid_out_row = Vec::new();
for column_index in 0..row.len() {
let cell = match &row[column_index] {
TableSlot::Cell(cell) => cell,
_ => {
cells_laid_out_row.push(None);
continue;
},
};
let mut total_width = Au::zero();
for width_index in column_index..column_index + cell.colspan {
total_width += self.distributed_column_widths[width_index];
}