File: table_painters.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (909 lines) | stat: -rw-r--r-- 34,367 bytes parent folder | download | duplicates (3)
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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/paint/table_painters.h"

#include "third_party/blink/renderer/core/layout/fragmentation_utils.h"
#include "third_party/blink/renderer/core/layout/geometry/writing_mode_converter.h"
#include "third_party/blink/renderer/core/layout/layout_box.h"
#include "third_party/blink/renderer/core/layout/physical_box_fragment.h"
#include "third_party/blink/renderer/core/layout/physical_fragment_link.h"
#include "third_party/blink/renderer/core/layout/table/layout_table.h"
#include "third_party/blink/renderer/core/layout/table/layout_table_cell.h"
#include "third_party/blink/renderer/core/layout/table/layout_table_column.h"
#include "third_party/blink/renderer/core/layout/table/table_borders.h"
#include "third_party/blink/renderer/core/paint/box_background_paint_context.h"
#include "third_party/blink/renderer/core/paint/box_border_painter.h"
#include "third_party/blink/renderer/core/paint/box_decoration_data.h"
#include "third_party/blink/renderer/core/paint/box_fragment_painter.h"
#include "third_party/blink/renderer/core/paint/box_model_object_painter.h"
#include "third_party/blink/renderer/core/paint/box_painter.h"
#include "third_party/blink/renderer/core/paint/paint_auto_dark_mode.h"
#include "third_party/blink/renderer/core/paint/paint_info.h"
#include "third_party/blink/renderer/core/paint/paint_layer.h"
#include "third_party/blink/renderer/core/paint/scoped_paint_state.h"
#include "third_party/blink/renderer/core/paint/theme_painter.h"
#include "third_party/blink/renderer/platform/graphics/graphics_context_state_saver.h"
#include "third_party/blink/renderer/platform/graphics/paint/drawing_recorder.h"

namespace blink {

namespace {

// TableCollapsedEdge represents collapsed border edge for painting.
class TableCollapsedEdge {
  STACK_ALLOCATED();

 public:
  TableCollapsedEdge(const TableBorders& borders, wtf_size_t edge_index)
      : borders_(borders) {
    edge_index_ = edge_index < borders_.EdgeCount() ? edge_index : UINT_MAX;
    InitCachedProps();
  }
  TableCollapsedEdge(const TableCollapsedEdge& source, int offset)
      : borders_(source.borders_) {
    // If edge index would have been negative.
    if (offset < 0 &&
        source.edge_index_ < static_cast<wtf_size_t>(std::abs(offset))) {
      edge_index_ = UINT_MAX;
    } else {
      edge_index_ = source.edge_index_ + offset;
      if (edge_index_ >= borders_.EdgeCount())
        edge_index_ = UINT_MAX;
    }
    InitCachedProps();
  }

  TableCollapsedEdge(const TableCollapsedEdge& edge)
      : TableCollapsedEdge(edge, 0) {}

  TableCollapsedEdge& operator=(const TableCollapsedEdge& edge) {
    edge_index_ = edge.edge_index_;
    border_width_ = edge.border_width_;
    border_style_ = edge.border_style_;
    return *this;
  }

  bool Exists() const { return edge_index_ != UINT_MAX; }

  bool CanPaint() const {
    if (!Exists())
      return false;
    if (border_style_ == EBorderStyle::kNone ||
        border_style_ == EBorderStyle::kHidden)
      return false;
    if (border_width_ == 0)
      return false;
    return true;
  }

  EBorderStyle BorderStyle() const { return border_style_; }

  LayoutUnit BorderWidth() const { return border_width_; }

  Color BorderColor() const { return borders_.BorderColor(edge_index_); }

  int CompareBoxOrder(wtf_size_t other_edge_index) const {
    wtf_size_t box_order = borders_.BoxOrder(edge_index_);
    wtf_size_t other_box_order = borders_.BoxOrder(other_edge_index);
    if (box_order < other_box_order)
      return 1;
    if (box_order > other_box_order)
      return -1;
    return 0;
  }

  bool IsInlineAxis() const {
    DCHECK(Exists());
    DCHECK_NE(edge_index_, UINT_MAX);
    return edge_index_ % borders_.EdgesPerRow() % 2 != 0;
  }

  wtf_size_t TableColumn() const {
    DCHECK(Exists());
    return edge_index_ % borders_.EdgesPerRow() / 2;
  }

  wtf_size_t TableRow() const {
    DCHECK(Exists());
    return edge_index_ / borders_.EdgesPerRow();
  }

  // Which edge gets to paint the joint intersection?
  // Returns -1 if this edge wins, 1 if other edge wins, 0 if tie.
  static int CompareForPaint(const TableCollapsedEdge& lhs,
                             const TableCollapsedEdge& rhs) {
    if (lhs.edge_index_ == rhs.edge_index_)
      return 0;
    bool lhs_paints = lhs.CanPaint();
    bool rhs_paints = rhs.CanPaint();
    if (lhs_paints && rhs_paints) {
      // Compare widths.
      if (lhs.border_width_ > rhs.border_width_) {
        return 1;
      } else if (lhs.border_width_ < rhs.border_width_) {
        return -1;
      } else {  // Compare styles.
        // Paint border style comparison for paint has different
        // rules than for winning edge border (hidden does not win).
        if (lhs.border_style_ == rhs.border_style_)
          return lhs.CompareBoxOrder(rhs.edge_index_);
        if (rhs.border_style_ == EBorderStyle::kHidden)
          return 1;
        if (lhs.border_style_ == EBorderStyle::kHidden)
          return -1;
        if (lhs.border_style_ > rhs.border_style_)
          return 1;
        return -1;
      }
    }
    if (!lhs_paints && !rhs_paints)
      return 0;
    if (!lhs_paints)
      return -1;
    DCHECK(!rhs_paints);
    return 1;
  }

  // Returns logical neighbor edges around edge intersections.
  TableCollapsedEdge EdgeBeforeStartIntersection() const {
    if (IsInlineAxis()) {
      return TableCollapsedEdge(*this, -2);
    } else {
      return TableCollapsedEdge(*this, -1);
    }
  }
  TableCollapsedEdge EdgeAfterStartIntersection() const {
    if (IsInlineAxis()) {
      return TableCollapsedEdge(*this, 0);
    } else {
      return TableCollapsedEdge(*this, 1);
    }
  }
  TableCollapsedEdge EdgeOverStartIntersection() const {
    if (IsInlineAxis()) {
      return TableCollapsedEdge(*this, -(borders_.EdgesPerRow() + 1));
    } else {
      return TableCollapsedEdge(*this, -borders_.EdgesPerRow());
    }
  }
  TableCollapsedEdge EdgeUnderStartIntersection() const {
    if (IsInlineAxis()) {
      return TableCollapsedEdge(*this, -1);
    } else {
      return TableCollapsedEdge(*this, 0);
    }
  }
  TableCollapsedEdge EdgeBeforeEndIntersection() const {
    if (IsInlineAxis()) {
      return TableCollapsedEdge(*this, 0);
    } else {
      return TableCollapsedEdge(*this, borders_.EdgesPerRow() - 1);
    }
  }
  TableCollapsedEdge EdgeAfterEndIntersection() const {
    if (IsInlineAxis()) {
      return TableCollapsedEdge(*this, 2);
    } else {
      return TableCollapsedEdge(*this, borders_.EdgesPerRow() + 1);
    }
  }
  TableCollapsedEdge EdgeOverEndIntersection() const {
    if (IsInlineAxis()) {
      return TableCollapsedEdge(*this, -(borders_.EdgesPerRow() - 1));
    } else {
      return TableCollapsedEdge(*this, 0);
    }
  }
  TableCollapsedEdge EdgeUnderEndIntersection() const {
    if (IsInlineAxis()) {
      return TableCollapsedEdge(*this, 1);
    } else {
      return TableCollapsedEdge(*this, borders_.EdgesPerRow());
    }
  }
  TableCollapsedEdge EmptyEdge() const {
    return TableCollapsedEdge(borders_, UINT_MAX);
  }

  TableCollapsedEdge& operator++() {
    DCHECK_NE(edge_index_, UINT_MAX);
    if (++edge_index_ >= borders_.EdgeCount())
      edge_index_ = UINT_MAX;
    InitCachedProps();
    return *this;
  }
  bool operator==(const TableCollapsedEdge& rhs) const {
    return edge_index_ == rhs.edge_index_;
  }
  bool operator!=(const TableCollapsedEdge& rhs) const {
    return !(*this == rhs);
  }

 private:
  void InitCachedProps() {
    if (edge_index_ == UINT_MAX) {
      border_width_ = LayoutUnit();
      border_style_ = EBorderStyle::kNone;
    } else {
      border_width_ = borders_.BorderWidth(edge_index_);
      border_style_ = borders_.BorderStyle(edge_index_);
    }
  }

  const TableBorders& borders_;
  wtf_size_t edge_index_;  // UINT_MAX means end.
  // cache paint properties
  LayoutUnit border_width_;
  EBorderStyle border_style_;
};

// Computes a rectangle for start/end joint.
// start/end_wins is set to true if examined edge won.
// Examined edge should shrink/expand its size to fill the joints.
void ComputeEdgeJoints(const TableBorders& collapsed_borders,
                       const TableCollapsedEdge& edge,
                       bool is_over_edge_fragmentation_boundary,
                       bool is_under_edge_fragmentation_boundary,
                       LogicalSize& start_joint,
                       LogicalSize& end_joint,
                       bool& start_wins,
                       bool& end_wins) {
  // Interesting question:
  // Should multiple edges ever paint inside the same joint?
  // - if one edge clearly wins, it should occupy the entire joint.
  // - if edge equals another edge, we have a choice:
  //   a) both edges can win.
  //      If edges are transparent, multiple paint will be visible.
  //   b) pick winners by edge orders. This results in ugly staggered borders.
  //  I've picked a), which is how Legacy does it.

  // Border precedence around the joint. Highest priority is after, then
  // clockwise: after, under, before, over.
  start_wins = false;
  end_wins = false;
  // Find winner for the start of the inline edge.
  TableCollapsedEdge before_edge = edge.EdgeBeforeStartIntersection();
  TableCollapsedEdge after_edge = edge.EdgeAfterStartIntersection();
  TableCollapsedEdge over_edge = is_over_edge_fragmentation_boundary
                                     ? edge.EmptyEdge()
                                     : edge.EdgeOverStartIntersection();
  TableCollapsedEdge under_edge =
      is_under_edge_fragmentation_boundary && edge.IsInlineAxis()
          ? edge.EmptyEdge()
          : edge.EdgeUnderStartIntersection();

  int inline_compare =
      TableCollapsedEdge::CompareForPaint(before_edge, after_edge);
  start_joint.block_size = inline_compare == 1 ? before_edge.BorderWidth()
                                               : after_edge.BorderWidth();
  if (is_over_edge_fragmentation_boundary ||
      (is_under_edge_fragmentation_boundary && edge.IsInlineAxis())) {
    start_joint.block_size = LayoutUnit();
  }

  // Compare over and under edges.
  int block_compare =
      TableCollapsedEdge::CompareForPaint(over_edge, under_edge);
  start_joint.inline_size =
      block_compare == 1 ? over_edge.BorderWidth() : under_edge.BorderWidth();
  int inline_vs_block = TableCollapsedEdge::CompareForPaint(
      inline_compare == 1 ? before_edge : after_edge,
      block_compare == 1 ? over_edge : under_edge);

  if (edge.IsInlineAxis()) {
    if (inline_vs_block != -1 && inline_compare != 1)
      start_wins = true;
  } else {
    if (inline_vs_block != 1 && block_compare != 1)
      start_wins = true;
  }
  // Find the winner for the end joint of the inline edge.
  before_edge = edge.EdgeBeforeEndIntersection();
  after_edge = edge.EdgeAfterEndIntersection();
  over_edge = is_over_edge_fragmentation_boundary && edge.IsInlineAxis()
                  ? edge.EmptyEdge()
                  : edge.EdgeOverEndIntersection();
  under_edge = is_under_edge_fragmentation_boundary
                   ? edge.EmptyEdge()
                   : edge.EdgeUnderEndIntersection();

  inline_compare = TableCollapsedEdge::CompareForPaint(before_edge, after_edge);
  end_joint.block_size = inline_compare == 1 ? before_edge.BorderWidth()
                                             : after_edge.BorderWidth();
  if ((is_over_edge_fragmentation_boundary && edge.IsInlineAxis()) ||
      is_under_edge_fragmentation_boundary) {
    end_joint.block_size = LayoutUnit();
  }

  block_compare = TableCollapsedEdge::CompareForPaint(over_edge, under_edge);
  end_joint.inline_size =
      block_compare == 1 ? over_edge.BorderWidth() : under_edge.BorderWidth();
  inline_vs_block = TableCollapsedEdge::CompareForPaint(
      inline_compare == 1 ? before_edge : after_edge,
      block_compare == 1 ? over_edge : under_edge);

  if (edge.IsInlineAxis()) {
    if (inline_vs_block != -1 && inline_compare != -1)
      end_wins = true;
  } else {
    if (inline_vs_block != 1 && block_compare != -1)
      end_wins = true;
  }
}

// Computes the stitched columns-rect relative to the current fragment.
// The columns-rect is the union of all the sections in the table.
PhysicalRect ComputeColumnsRect(const PhysicalBoxFragment& fragment) {
  const auto writing_direction = fragment.Style().GetWritingDirection();
  LogicalRect columns_rect;
  LayoutUnit stitched_block_size;
  LayoutUnit fragment_block_offset;

  bool is_first_section = true;
  for (const PhysicalBoxFragment& walker :
       To<LayoutBox>(fragment.GetLayoutObject())->PhysicalFragments()) {
    if (&walker == &fragment)
      fragment_block_offset = stitched_block_size;

    WritingModeConverter converter(writing_direction, walker.Size());
    for (const auto& child : walker.Children()) {
      if (!child->IsTableSection()) {
        continue;
      }

      LogicalRect section_rect =
          converter.ToLogical({child.offset, child->Size()});
      section_rect.offset.block_offset += stitched_block_size;

      if (is_first_section) {
        columns_rect = section_rect;
        is_first_section = false;
      } else {
        columns_rect.UniteEvenIfEmpty(section_rect);
      }
    }

    stitched_block_size +=
        LogicalFragment(writing_direction, walker).BlockSize();
  }

  // Make the rect relative to the fragment we are currently painting.
  columns_rect.offset.block_offset -= fragment_block_offset;

  WritingModeConverter converter(writing_direction, fragment.Size());
  return converter.ToPhysical(columns_rect);
}

// When painting background in a cell (for the cell or its ancestor table part),
// if any ancestor table part has a layer and the table collapses borders, the
// background is painted after the collapsed borders. We need to clip the
// background to prevent it from covering the collapsed borders around the cell.
// TODO(crbug.com/1181813): Investigate other methods.
class TableCellBackgroundClipper {
  STACK_ALLOCATED();

 public:
  TableCellBackgroundClipper(
      GraphicsContext& context,
      const LayoutTableCell& table_cell,
      const PhysicalRect& cell_rect,
      bool is_painting_background_in_contents_space = false)
      : context_(context),
        needs_clip_(!is_painting_background_in_contents_space &&
                    (table_cell.HasLayer() || table_cell.Parent()->HasLayer() ||
                     table_cell.Parent()->Parent()->HasLayer()) &&
                    table_cell.Table()->HasCollapsedBorders()) {
    if (!needs_clip_)
      return;

    PhysicalRect clip_rect = cell_rect;
    clip_rect.Contract(table_cell.BorderOutsets());
    context.Save();
    context.Clip(ToPixelSnappedRect(clip_rect));
  }

  ~TableCellBackgroundClipper() {
    if (needs_clip_)
      context_.Restore();
  }

 private:
  GraphicsContext& context_;
  bool needs_clip_;
};

}  // namespace

bool TablePainter::WillCheckColumnBackgrounds() {
  return fragment_.TableColumnGeometries();
}

void TablePainter::PaintBoxDecorationBackground(
    const PaintInfo& paint_info,
    const PhysicalRect& paint_rect,
    const BoxDecorationData& box_decoration_data) {
  WritingModeConverter converter(fragment_.Style().GetWritingDirection(),
                                 fragment_.Size());
  PhysicalRect grid_paint_rect =
      converter.ToPhysical(fragment_.TableGridRect());
  grid_paint_rect.offset += paint_rect.offset;

  // Paint the table background on the grid-rect.
  if (box_decoration_data.ShouldPaint()) {
    BoxFragmentPainter(fragment_).PaintBoxDecorationBackgroundWithRectImpl(
        paint_info, grid_paint_rect, box_decoration_data);
  }

  // Optimization: only traverse colgroups with backgrounds.
  const GCedTableColumnGeometries* column_geometries_original =
      fragment_.TableColumnGeometries();
  TableColumnGeometries column_geometries_with_background;
  if (column_geometries_original) {
    for (const auto& column_geometry : *column_geometries_original) {
      if (column_geometry.node.Style().HasBoxDecorationBackground()) {
        column_geometries_with_background.push_back(column_geometry);
      }
    }
  }

  if (column_geometries_with_background.empty())
    return;

  // Paint <colgroup>/<col> backgrounds.
  PhysicalRect columns_paint_rect = ComputeColumnsRect(fragment_);
  columns_paint_rect.offset += paint_rect.offset;
  for (const PhysicalFragmentLink& child : fragment_.Children()) {
    if (!child.fragment->IsTableSection()) {
      continue;
    }
    TableSectionPainter(To<PhysicalBoxFragment>(*child.fragment))
        .PaintColumnsBackground(paint_info, paint_rect.offset + child.offset,
                                columns_paint_rect,
                                column_geometries_with_background);
  }
}

namespace {

const PhysicalFragment* StartSection(const PhysicalBoxFragment& table) {
  for (const auto& child : table.Children()) {
    if (!child->IsTableSection()) {
      continue;
    }
    return child.get();
  }
  return nullptr;
}

const PhysicalFragment* EndSection(const PhysicalBoxFragment& table) {
  const auto children = table.Children();
  for (auto it = children.rbegin(); it != children.rend(); ++it) {
    const auto& child = *it;
    if (!child->IsTableSection()) {
      continue;
    }
    return child.get();
  }
  return nullptr;
}

bool IsStartRowFragmented(const PhysicalBoxFragment& section) {
  for (const auto& child : section.Children()) {
    if (!child->IsTableRow()) {
      continue;
    }

    return IsBreakInside(
        FindPreviousBreakToken(To<PhysicalBoxFragment>(*child)));
  }

  return false;
}

bool IsEndRowFragmented(const PhysicalBoxFragment& section) {
  const auto children = section.Children();
  for (auto it = children.rbegin(); it != children.rend(); ++it) {
    const auto& child = *it;
    if (!child->IsTableRow()) {
      continue;
    }
    const auto* break_token = To<BlockBreakToken>(child->GetBreakToken());
    return IsBreakInside(break_token) && !break_token->IsAtBlockEnd();
  }
  return false;
}

}  // namespace

void TablePainter::PaintCollapsedBorders(const PaintInfo& paint_info,
                                         const PhysicalOffset& paint_offset,
                                         const gfx::Rect& visual_rect) {
  const TableBorders* collapsed_borders = fragment_.TableCollapsedBorders();
  if (!collapsed_borders)
    return;
  const CollapsedTableBordersGeometry* collapsed_borders_geometry =
      fragment_.TableCollapsedBordersGeometry();
  CHECK(collapsed_borders_geometry);

  const auto& layout_table = *To<LayoutTable>(fragment_.GetLayoutObject());
  if (DrawingRecorder::UseCachedDrawingIfPossible(
          paint_info.context, layout_table, paint_info.phase))
    return;
  DrawingRecorder recorder(paint_info.context, layout_table, paint_info.phase,
                           visual_rect);
  AutoDarkMode auto_dark_mode(PaintAutoDarkMode(
      fragment_.Style(), DarkModeFilter::ElementRole::kBorder));

  const wtf_size_t edges_per_row = collapsed_borders->EdgesPerRow();
  const wtf_size_t total_row_count =
      collapsed_borders->EdgeCount() / edges_per_row;

  const auto* start_section = StartSection(fragment_);
  const auto* end_section = EndSection(fragment_);

  // We paint collapsed-borders section-by-section for fragmentation purposes.
  // This means that we need to track the final row we've painted in each
  // section to avoid double painting.
  std::optional<wtf_size_t> previous_painted_row_index;

  for (const auto& child : fragment_.Children()) {
    if (!child->IsTableSection()) {
      continue;
    }

    const auto& section = To<PhysicalBoxFragment>(*child);
    const std::optional<wtf_size_t> section_start_row_index =
        section.TableSectionStartRowIndex();
    if (!section_start_row_index)
      continue;

    const auto& section_row_offsets = *section.TableSectionRowOffsets();
    const wtf_size_t start_edge_index =
        *section_start_row_index * edges_per_row;

    // Determine if we have (table) content in the next/previous fragmentainer.
    // We'll use this information to paint "half" borders if required.
    bool has_content_in_previous_fragmentainer =
        (start_section == &section) && (*section_start_row_index > 0u);
    bool has_content_in_next_fragmentainer =
        (end_section == &section) &&
        (*section_start_row_index + section_row_offsets.size() <
         total_row_count);

    // If our row was fragmented we skip painting the borders at that edge.
    bool is_start_row_fragmented = IsStartRowFragmented(section);
    bool is_end_row_fragmented = IsEndRowFragmented(section);

    WritingModeConverter converter(fragment_.Style().GetWritingDirection(),
                                   section.Size());

    for (auto edge = TableCollapsedEdge(*collapsed_borders, start_edge_index);
         edge.Exists(); ++edge) {
      const wtf_size_t table_row = edge.TableRow();
      const wtf_size_t table_column = edge.TableColumn();
      const wtf_size_t fragment_table_row =
          table_row - *section_start_row_index;

      // Check if we've exhausted the rows in this section.
      if (fragment_table_row >= section_row_offsets.size()) {
        // Store the final row which we painted (if it wasn't fragmented).
        if (is_end_row_fragmented)
          previous_painted_row_index = std::nullopt;
        else
          previous_painted_row_index = table_row - 1;
        break;
      }

      if (!edge.CanPaint())
        continue;

      bool is_start_row = fragment_table_row == 0u;
      bool is_start_fragmented = is_start_row && is_start_row_fragmented;
      bool is_start_at_fragmentation_boundary =
          is_start_row && has_content_in_previous_fragmentainer;

      const LayoutUnit row_start_offset =
          section_row_offsets[fragment_table_row];
      const LayoutUnit column_start_offset =
          collapsed_borders_geometry->columns[table_column];

      LayoutUnit inline_start;
      LayoutUnit block_start;
      LayoutUnit inline_size;
      LayoutUnit block_size;

      if (edge.IsInlineAxis()) {
        // NOTE: This crash has been observed, but we aren't able to find a
        // reproducible testcase. See: crbug.com/1179369.
        if (table_column + 1 >= collapsed_borders_geometry->columns.size()) {
          NOTREACHED();
        }

        // Check if we have painted this inline border in a previous section.
        if (previous_painted_row_index &&
            *previous_painted_row_index == table_row) {
          continue;
        }

        bool is_end_row = fragment_table_row == section_row_offsets.size() - 1u;
        bool is_end_fragmented = is_end_row && is_end_row_fragmented;
        bool is_end_at_fragmentation_boundary =
            is_end_row && has_content_in_next_fragmentainer;

        // If the current row has been fragmented, omit the inline border.
        if (is_start_fragmented || is_end_fragmented)
          continue;

        inline_start = column_start_offset;
        inline_size = collapsed_borders_geometry->columns[table_column + 1] -
                      column_start_offset;
        block_start = is_start_at_fragmentation_boundary
                          ? row_start_offset
                          : row_start_offset - edge.BorderWidth() / 2;
        block_size = is_start_at_fragmentation_boundary ||
                             is_end_at_fragmentation_boundary
                         ? edge.BorderWidth() / 2
                         : edge.BorderWidth();

        LogicalSize start_joint;
        LogicalSize end_joint;
        bool start_wins;
        bool end_wins;
        ComputeEdgeJoints(*collapsed_borders, edge,
                          is_start_at_fragmentation_boundary,
                          is_end_at_fragmentation_boundary, start_joint,
                          end_joint, start_wins, end_wins);
        if (start_wins) {
          inline_start -= start_joint.inline_size / 2;
          inline_size += start_joint.inline_size / 2;
        } else {
          inline_start += start_joint.inline_size / 2;
          inline_size -= start_joint.inline_size / 2;
        }
        if (end_wins) {
          inline_size += end_joint.inline_size / 2;
        } else {
          inline_size -= end_joint.inline_size / 2;
        }
      } else {  // block_axis
        // Check if this block border exists in this section.
        if (fragment_table_row + 1 >= section_row_offsets.size())
          continue;

        bool is_end_row =
            fragment_table_row + 1u == section_row_offsets.size() - 1u;
        bool is_end_fragmented = is_end_row && is_end_row_fragmented;
        bool is_end_at_fragmentation_boundary =
            is_end_row && has_content_in_next_fragmentainer;

        block_start = row_start_offset;
        block_size =
            section_row_offsets[fragment_table_row + 1] - row_start_offset;
        inline_start = column_start_offset - edge.BorderWidth() / 2;
        inline_size = edge.BorderWidth();

        LogicalSize start_joint;
        LogicalSize end_joint;
        bool start_wins;
        bool end_wins;
        ComputeEdgeJoints(*collapsed_borders, edge,
                          is_start_at_fragmentation_boundary,
                          is_end_at_fragmentation_boundary, start_joint,
                          end_joint, start_wins, end_wins);
        if (is_start_fragmented) {
          // We don't need to perform any adjustment if we've been start
          // fragmented as there isn't a joint here.
        } else if (start_wins) {
          block_start -= start_joint.block_size / 2;
          block_size += start_joint.block_size / 2;
        } else {
          block_start += start_joint.block_size / 2;
          block_size -= start_joint.block_size / 2;
        }
        if (is_end_fragmented) {
          // We don't need to perform any adjustment if we've been end
          // fragmented as there isn't a joint here.
        } else if (end_wins) {
          block_size += end_joint.block_size / 2;
        } else {
          block_size -= end_joint.block_size / 2;
        }
      }
      const LogicalRect logical_border_rect(inline_start, block_start,
                                            inline_size, block_size);
      PhysicalRect physical_border_rect =
          converter.ToPhysical(logical_border_rect);
      physical_border_rect.offset += child.offset + paint_offset;

      BoxSide box_side;
      if (fragment_.Style().IsHorizontalWritingMode()) {
        box_side = edge.IsInlineAxis() ? BoxSide::kTop : BoxSide::kLeft;
      } else {
        box_side = edge.IsInlineAxis() ? BoxSide::kLeft : BoxSide::kTop;
      }
      BoxBorderPainter::DrawBoxSide(
          paint_info.context, ToPixelSnappedRect(physical_border_rect),
          box_side, edge.BorderColor(), edge.BorderStyle(), auto_dark_mode);
    }
  }
}

void TableSectionPainter::PaintBoxDecorationBackground(
    const PaintInfo& paint_info,
    const PhysicalRect& paint_rect,
    const BoxDecorationData& box_decoration_data) {
  DCHECK(box_decoration_data.ShouldPaint());
  if (box_decoration_data.ShouldPaintShadow()) {
    BoxPainterBase::PaintNormalBoxShadow(
        paint_info, paint_rect, fragment_.Style(), PhysicalBoxSides(),
        !box_decoration_data.ShouldPaintBackground());
  }

  // If we are fragmented - determine the total part size, relative to the
  // current fragment.
  PhysicalRect part_rect = paint_rect;
  if (!fragment_.IsOnlyForNode())
    part_rect.offset -= OffsetInStitchedFragments(fragment_, &part_rect.size);

  for (const PhysicalFragmentLink& child : fragment_.Children()) {
    const auto& child_fragment = *child;
    DCHECK(child_fragment.IsBox());
    if (!child_fragment.IsTableRow()) {
      continue;
    }
    TableRowPainter(To<PhysicalBoxFragment>(child_fragment))
        .PaintTablePartBackgroundIntoCells(
            paint_info, *To<LayoutBox>(fragment_.GetLayoutObject()), part_rect,
            paint_rect.offset + child.offset);
  }
  if (box_decoration_data.ShouldPaintShadow()) {
    BoxPainterBase::PaintInsetBoxShadowWithInnerRect(paint_info, paint_rect,
                                                     fragment_.Style());
  }
}

void TableSectionPainter::PaintColumnsBackground(
    const PaintInfo& paint_info,
    const PhysicalOffset& section_paint_offset,
    const PhysicalRect& columns_paint_rect,
    const TableColumnGeometries& column_geometries) {
  for (const PhysicalFragmentLink& row : fragment_.Children()) {
    if (!row.fragment->IsTableRow()) {
      continue;
    }
    TableRowPainter(To<PhysicalBoxFragment>(*row.fragment))
        .PaintColumnsBackground(paint_info, section_paint_offset + row.offset,
                                columns_paint_rect, column_geometries);
  }
}

void TableRowPainter::PaintBoxDecorationBackground(
    const PaintInfo& paint_info,
    const PhysicalRect& paint_rect,
    const BoxDecorationData& box_decoration_data) {
  DCHECK(box_decoration_data.ShouldPaint());
  if (box_decoration_data.ShouldPaintShadow()) {
    BoxPainterBase::PaintNormalBoxShadow(
        paint_info, paint_rect, fragment_.Style(), PhysicalBoxSides(),
        !box_decoration_data.ShouldPaintBackground());
  }

  // If we are fragmented - determine the total part size, relative to the
  // current fragment.
  PhysicalRect part_rect = paint_rect;
  if (!fragment_.IsOnlyForNode())
    part_rect.offset -= OffsetInStitchedFragments(fragment_, &part_rect.size);

  PaintTablePartBackgroundIntoCells(paint_info,
                                    *To<LayoutBox>(fragment_.GetLayoutObject()),
                                    part_rect, paint_rect.offset);
  if (box_decoration_data.ShouldPaintShadow()) {
    BoxPainterBase::PaintInsetBoxShadowWithInnerRect(paint_info, paint_rect,
                                                     fragment_.Style());
  }
}

void TableRowPainter::PaintTablePartBackgroundIntoCells(
    const PaintInfo& paint_info,
    const LayoutBox& table_part,
    const PhysicalRect& table_part_paint_rect,
    const PhysicalOffset& row_paint_offset) {
  for (const PhysicalFragmentLink& child : fragment_.Children()) {
    DCHECK(child.fragment->IsBox());
    DCHECK(child.fragment->GetLayoutObject()->IsTableCell() ||
           child.fragment->GetLayoutObject()->IsOutOfFlowPositioned());
    const auto& child_fragment = *child;
    if (!child_fragment.IsTableCell()) {
      continue;
    }
    TableCellPainter(To<PhysicalBoxFragment>(child_fragment))
        .PaintBackgroundForTablePart(paint_info, table_part,
                                     table_part_paint_rect,
                                     row_paint_offset + child.offset);
  }
}

void TableRowPainter::PaintColumnsBackground(
    const PaintInfo& paint_info,
    const PhysicalOffset& row_paint_offset,
    const PhysicalRect& columns_paint_rect,
    const TableColumnGeometries& column_geometries) {
  WritingModeConverter converter(fragment_.Style().GetWritingDirection(),
                                 columns_paint_rect.size);
  for (const PhysicalFragmentLink& child : fragment_.Children()) {
    if (!child.fragment->IsTableCell()) {
      continue;
    }
    const wtf_size_t cell_column =
        To<PhysicalBoxFragment>(child.fragment.Get())->TableCellColumnIndex();
    for (const auto& column_geometry : column_geometries) {
      wtf_size_t current_start = column_geometry.start_column;
      wtf_size_t current_end =
          column_geometry.start_column + column_geometry.span - 1;
      if (cell_column < current_start || cell_column > current_end)
        continue;

      LogicalSize column_size = converter.ToLogical(columns_paint_rect.size);
      column_size.inline_size = column_geometry.inline_size;

      PhysicalRect column_paint_rect;
      column_paint_rect.size = converter.ToPhysical(column_size);
      column_paint_rect.offset =
          columns_paint_rect.offset +
          converter.ToPhysical({column_geometry.inline_offset, LayoutUnit()},
                               column_paint_rect.size);

      TableCellPainter(To<PhysicalBoxFragment>(*child.fragment))
          .PaintBackgroundForTablePart(
              paint_info, *column_geometry.node.GetLayoutBox(),
              column_paint_rect, row_paint_offset + child.offset);
    }
  }
}

void TableCellPainter::PaintBoxDecorationBackground(
    const PaintInfo& paint_info,
    const PhysicalRect& paint_rect,
    const BoxDecorationData& box_decoration_data) {
  DCHECK(box_decoration_data.ShouldPaint());
  TableCellBackgroundClipper clipper(
      paint_info.context, *To<LayoutTableCell>(fragment_.GetLayoutObject()),
      paint_rect, box_decoration_data.IsPaintingBackgroundInContentsSpace());
  BoxFragmentPainter(fragment_).PaintBoxDecorationBackgroundWithRectImpl(
      paint_info, paint_rect, box_decoration_data);
}

// Inspired by TableCellPainter::PaintBackground.
void TableCellPainter::PaintBackgroundForTablePart(
    const PaintInfo& paint_info,
    const LayoutBox& table_part,
    const PhysicalRect& table_part_paint_rect,
    const PhysicalOffset& table_cell_paint_offset) {
  if (fragment_.Style().Visibility() != EVisibility::kVisible) {
    return;
  }
  const auto& layout_table_cell =
      *To<LayoutTableCell>(fragment_.GetLayoutObject());
  if (layout_table_cell.BackgroundTransfersToView())
    return;  // cargo-culted from other painters.

  Color color = table_part.StyleRef().VisitedDependentColor(
      GetCSSPropertyBackgroundColor());
  const FillLayer& background_layers = table_part.StyleRef().BackgroundLayers();
  if (background_layers.AnyLayerHasImage() || !color.IsFullyTransparent()) {
    PhysicalRect cell_paint_rect(table_cell_paint_offset, fragment_.Size());
    TableCellBackgroundClipper clipper(paint_info.context, layout_table_cell,
                                       cell_paint_rect);
    BoxBackgroundPaintContext bg_paint_context(
        layout_table_cell,
        table_cell_paint_offset - table_part_paint_rect.offset, table_part,
        table_part_paint_rect.size);
    BoxFragmentPainter(fragment_).PaintFillLayers(
        paint_info, color, background_layers, cell_paint_rect,
        bg_paint_context);
  }
}

}  // namespace blink