File: highlight_painter.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 (1253 lines) | stat: -rw-r--r-- 53,798 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
// Copyright 2017 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/highlight_painter.h"

#include "base/auto_reset.h"
#include "third_party/blink/renderer/core/dom/node.h"
#include "third_party/blink/renderer/core/editing/editor.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
#include "third_party/blink/renderer/core/editing/markers/custom_highlight_marker.h"
#include "third_party/blink/renderer/core/editing/markers/document_marker_controller.h"
#include "third_party/blink/renderer/core/editing/markers/glic_marker.h"
#include "third_party/blink/renderer/core/editing/markers/styleable_marker.h"
#include "third_party/blink/renderer/core/editing/markers/text_match_marker.h"
#include "third_party/blink/renderer/core/frame/local_dom_window.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/highlight/highlight.h"
#include "third_party/blink/renderer/core/highlight/highlight_registry.h"
#include "third_party/blink/renderer/core/highlight/highlight_style_utils.h"
#include "third_party/blink/renderer/core/layout/inline/inline_cursor.h"
#include "third_party/blink/renderer/core/layout/inline/text_offset_range.h"
#include "third_party/blink/renderer/core/layout/layout_object.h"
#include "third_party/blink/renderer/core/layout/layout_text_fragment.h"
#include "third_party/blink/renderer/core/layout/svg/layout_svg_inline_text.h"
#include "third_party/blink/renderer/core/layout/text_decoration_offset.h"
#include "third_party/blink/renderer/core/paint/highlight_overlay.h"
#include "third_party/blink/renderer/core/paint/line_relative_rect.h"
#include "third_party/blink/renderer/core/paint/marker_range_mapping_context.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/styleable_marker_painter.h"
#include "third_party/blink/renderer/core/paint/text_decoration_painter.h"
#include "third_party/blink/renderer/core/paint/text_painter.h"
#include "third_party/blink/renderer/platform/fonts/text_fragment_paint_info.h"
#include "third_party/blink/renderer/platform/graphics/graphics_context_state_saver.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"

namespace blink {

namespace {

using HighlightLayerType = HighlightOverlay::HighlightLayerType;
using HighlightRange = HighlightOverlay::HighlightRange;
using HighlightEdge = HighlightOverlay::HighlightEdge;
using HighlightDecoration = HighlightOverlay::HighlightDecoration;
using HighlightBackground = HighlightOverlay::HighlightBackground;
using HighlightTextShadow = HighlightOverlay::HighlightTextShadow;

LineRelativeRect LineRelativeLocalRect(const FragmentItem& text_fragment,
                                       StringView text,
                                       unsigned start_offset,
                                       unsigned end_offset) {
  LayoutUnit start_position, end_position;
  std::tie(start_position, end_position) =
      text_fragment.LineLeftAndRightForOffsets(text, start_offset, end_offset);

  const LayoutUnit height = text_fragment.InkOverflowRect().Height();
  return {{start_position, LayoutUnit()},
          {end_position - start_position, height}};
}

void PaintRect(GraphicsContext& context,
               const PhysicalRect& rect,
               const Color color,
               const AutoDarkMode& auto_dark_mode) {
  if (color.IsFullyTransparent()) {
    return;
  }
  if (rect.size.IsEmpty())
    return;
  const gfx::Rect pixel_snapped_rect = ToPixelSnappedRect(rect);
  if (!pixel_snapped_rect.IsEmpty())
    context.FillRect(pixel_snapped_rect, color, auto_dark_mode);
}

const LayoutSelectionStatus* GetSelectionStatus(
    const HighlightPainter::SelectionPaintState* selection) {
  if (!selection)
    return nullptr;
  return &selection->Status();
}

// Returns true if the styles for the given spelling or grammar pseudo require
// the full overlay painting algorithm.
bool HasNonTrivialSpellingGrammarStyles(const FragmentItem& fragment_item,
                                        Node* node,
                                        const ComputedStyle& originating_style,
                                        PseudoId pseudo) {
  DCHECK(pseudo == kPseudoIdSpellingError || pseudo == kPseudoIdGrammarError);
  if (const ComputedStyle* pseudo_style =
          HighlightStyleUtils::HighlightPseudoStyle(node, originating_style,
                                                    pseudo)) {
    const Document& document = node->GetDocument();
    // If the ‘color’, ‘-webkit-text-fill-color’, ‘-webkit-text-stroke-color’,
    // or ‘-webkit-text-stroke-width’ differs from the originating style.
    Color pseudo_color = HighlightStyleUtils::ResolveColor(
        document, originating_style, pseudo_style, pseudo,
        GetCSSPropertyColor(), {}, SearchTextIsActiveMatch::kNo);
    if (pseudo_color !=
        originating_style.VisitedDependentColor(GetCSSPropertyColor())) {
      return true;
    }
    if (HighlightStyleUtils::ResolveColor(document, originating_style,
                                          pseudo_style, pseudo,
                                          GetCSSPropertyWebkitTextFillColor(),
                                          {}, SearchTextIsActiveMatch::kNo) !=
        originating_style.VisitedDependentColor(
            GetCSSPropertyWebkitTextFillColor())) {
      return true;
    }
    if (HighlightStyleUtils::ResolveColor(document, originating_style,
                                          pseudo_style, pseudo,
                                          GetCSSPropertyWebkitTextStrokeColor(),
                                          {}, SearchTextIsActiveMatch::kNo) !=
        originating_style.VisitedDependentColor(
            GetCSSPropertyWebkitTextStrokeColor())) {
      return true;
    }
    if (pseudo_style->TextStrokeWidth() != originating_style.TextStrokeWidth())
      return true;
    // If there is a background color.
    if (!HighlightStyleUtils::ResolveColor(
             document, originating_style, pseudo_style, pseudo,
             GetCSSPropertyBackgroundColor(), {}, SearchTextIsActiveMatch::kNo)
             .IsFullyTransparent()) {
      return true;
    }
    // If the ‘text-shadow’ is not ‘none’.
    if (pseudo_style->TextShadow())
      return true;

    // If the ‘text-decoration-line’ is not ‘spelling-error’ or ‘grammar-error’,
    // depending on the pseudo. ‘text-decoration-color’ can vary without hurting
    // the optimisation, and for these line types, we ignore all other text
    // decoration related properties anyway.
    if (pseudo_style->TextDecorationsInEffect() !=
        (pseudo == kPseudoIdSpellingError
             ? TextDecorationLine::kSpellingError
             : TextDecorationLine::kGrammarError)) {
      return true;
    }
    // If any of the originating line decorations would need to be recolored.
    for (const AppliedTextDecoration& decoration :
         originating_style.AppliedTextDecorations()) {
      if (decoration.GetColor() != pseudo_color) {
        return true;
      }
    }
    // ‘text-emphasis-color’ should be meaningless for highlight pseudos, but
    // in our current impl, it sets the color of originating emphasis marks.
    // This means we can only use kFastSpellingGrammar if the color is the same
    // as in the originating style, or there are no emphasis marks.
    // TODO(crbug.com/1147859) clean up when spec issue is resolved again
    // https://github.com/w3c/csswg-drafts/issues/7101
    if (originating_style.GetTextEmphasisMark() != TextEmphasisMark::kNone &&
        HighlightStyleUtils::ResolveColor(document, originating_style,
                                          pseudo_style, pseudo,
                                          GetCSSPropertyTextEmphasisColor(), {},
                                          SearchTextIsActiveMatch::kNo) !=
            originating_style.VisitedDependentColor(
                GetCSSPropertyTextEmphasisColor())) {
      return true;
    }
    // If the SVG-only fill- and stroke-related properties differ from their
    // values in the originating style. These checks must be skipped outside of
    // SVG content, because the initial ‘fill’ is ‘black’, not ‘currentColor’.
    if (fragment_item.IsSvgText()) {
      // If the ‘fill’ is ‘currentColor’, assume that it differs from the
      // originating style, even if the current color actually happens to
      // match. This simplifies the logic until we know it performs poorly.
      if (pseudo_style->FillPaint().HasCurrentColor())
        return true;
      // If the ‘fill’ differs from the originating style.
      if (pseudo_style->FillPaint() != originating_style.FillPaint())
        return true;
      // If the ‘stroke’ is ‘currentColor’, assume that it differs from the
      // originating style, even if the current color actually happens to
      // match. This simplifies the logic until we know it performs poorly.
      if (pseudo_style->StrokePaint().HasCurrentColor())
        return true;
      // If the ‘stroke’ differs from the originating style.
      if (pseudo_style->StrokePaint() != originating_style.StrokePaint())
        return true;
      // If the ‘stroke-width’ differs from the originating style.
      if (pseudo_style->StrokeWidth() != originating_style.StrokeWidth())
        return true;
    }
  }
  return false;
}

TextPaintStyle TextPaintStyleForTextMatch(const TextMatchMarker& marker,
                                          const ComputedStyle& style,
                                          const Document& document,
                                          bool ignore_current_color) {
  const mojom::blink::ColorScheme color_scheme = style.UsedColorScheme();
  const Color platform_text_color =
      LayoutTheme::GetTheme().PlatformTextSearchColor(
          marker.IsActiveMatch(), document.InForcedColorsMode(), color_scheme,
          document.GetColorProviderForPainting(color_scheme),
          document.IsInWebAppScope());
  // Comparing against the value of the 'color' property doesn't always make
  // sense (for example for SVG <text> which paints using 'fill' and 'stroke').
  if (!ignore_current_color) {
    const Color text_color = style.VisitedDependentColor(GetCSSPropertyColor());
    if (platform_text_color == text_color) {
      return {};
    }
  }

  TextPaintStyle text_style;
  text_style.current_color = text_style.fill_color = text_style.stroke_color =
      text_style.emphasis_mark_color = platform_text_color;
  text_style.stroke_width = style.TextStrokeWidth();
  text_style.color_scheme = color_scheme;
  text_style.shadow = nullptr;
  text_style.paint_order = style.PaintOrder();
  return text_style;
}

// A contiguous run of parts that can have ‘background-color’ or ‘text-shadow’
// of some active overlay painted at once.
//
// These properties can often be painted a whole highlighted range at a time,
// and only need to be split into parts when affected by ‘currentColor’. By
// merging parts where possible, we avoid creating unnecessary “seams” in
// ‘background-color’, and avoid splitting ligatures in ‘text-shadow’.
//
// Inner’s operator== must return true iff the two operands come from the same
// layer and can be painted at once.
template <typename Inner>
struct MergedHighlightPart {
 public:
  struct Merged {
   public:
    const Inner& inner;
    unsigned from;
    unsigned to;
  };

  // Merge |next| and |next_part| into |merged| if possible, otherwise start a
  // new run and return the old |merged| if any.
  std::optional<Merged> Merge(const Inner& next,
                              const HighlightPart& next_part) {
    std::optional<Merged> result{};
    if (merged.has_value()) {
      if (merged->inner == next && merged->to == next_part.range.from) {
        merged->to = next_part.range.to;
        return {};
      } else {
        result.emplace(*merged);
      }
    }
    merged.emplace(Merged{next, next_part.range.from, next_part.range.to});
    return result;
  }

  // Take and return the last |merged| if any, leaving it empty.
  std::optional<Merged> Take() {
    if (!merged.has_value()) {
      return {};
    }
    std::optional<Merged> result{};
    result.emplace(*merged);
    merged.reset();
    return result;
  }

  std::optional<Merged> merged;
};

}  // namespace

HighlightPainter::SelectionPaintState::SelectionPaintState(
    const InlineCursor& containing_block,
    const PhysicalOffset& box_offset,
    const std::optional<AffineTransform> writing_mode_rotation)
    : SelectionPaintState(containing_block,
                          box_offset,
                          writing_mode_rotation,
                          containing_block.Current()
                              .GetLayoutObject()
                              ->GetDocument()
                              .GetFrame()
                              ->Selection()) {}
HighlightPainter::SelectionPaintState::SelectionPaintState(
    const InlineCursor& containing_block,
    const PhysicalOffset& box_offset,
    const std::optional<AffineTransform> writing_mode_rotation,
    const FrameSelection& frame_selection)
    : selection_status_(
          frame_selection.ComputeLayoutSelectionStatus(containing_block)),
      state_(frame_selection.ComputePaintingSelectionStateForCursor(
          containing_block.Current())),
      containing_block_(containing_block),
      box_offset_(box_offset),
      writing_mode_rotation_(writing_mode_rotation) {}

void HighlightPainter::SelectionPaintState::ComputeSelectionStyle(
    const Document& document,
    const ComputedStyle& style,
    Node* node,
    const PaintInfo& paint_info,
    const TextPaintStyle& text_style) {
  const ComputedStyle* pseudo_style = HighlightStyleUtils::HighlightPseudoStyle(
      node, style, kPseudoIdSelection);
  selection_style_ = HighlightStyleUtils::HighlightPaintingStyle(
      document, style, pseudo_style, node, kPseudoIdSelection, text_style,
      paint_info, SearchTextIsActiveMatch::kNo);
  paint_selected_text_only_ =
      (paint_info.phase == PaintPhase::kSelectionDragImage);
}

void HighlightPainter::SelectionPaintState::ComputeSelectionRectIfNeeded() {
  if (!selection_rect_) {
    PhysicalRect physical =
        containing_block_.CurrentLocalSelectionRectForText(selection_status_);
    physical.offset += box_offset_;
    LineRelativeRect rotated =
        LineRelativeRect::Create(physical, writing_mode_rotation_);
    selection_rect_.emplace(SelectionRect{physical, rotated});
  }
}

const PhysicalRect&
HighlightPainter::SelectionPaintState::PhysicalSelectionRect() {
  ComputeSelectionRectIfNeeded();
  return selection_rect_->physical;
}

const LineRelativeRect&
HighlightPainter::SelectionPaintState::LineRelativeSelectionRect() {
  ComputeSelectionRectIfNeeded();
  return selection_rect_->rotated;
}

// |selection_start| and |selection_end| should be between
// [text_fragment.StartOffset(), text_fragment.EndOffset()].
void HighlightPainter::SelectionPaintState::PaintSelectionBackground(
    GraphicsContext& context,
    Node* node,
    const Document& document,
    const ComputedStyle& style,
    const std::optional<AffineTransform>& rotation) {
  const Color color = HighlightStyleUtils::HighlightBackgroundColor(
      document, style, node, selection_style_.style.current_color,
      kPseudoIdSelection, SearchTextIsActiveMatch::kNo);
  HighlightPainter::PaintHighlightBackground(context, style, color,
                                             PhysicalSelectionRect(), rotation);
}

// Paint the selected text only.
void HighlightPainter::SelectionPaintState::PaintSelectedText(
    TextPainter& text_painter,
    const TextFragmentPaintInfo& fragment_paint_info,
    const TextPaintStyle& text_style,
    DOMNodeId node_id,
    const AutoDarkMode& auto_dark_mode) {
  text_painter.PaintSelectedText(
      fragment_paint_info, selection_status_.start, selection_status_.end,
      text_style, selection_style_.style, LineRelativeSelectionRect(), node_id,
      auto_dark_mode);
}

// Paint the given text range in the given style, suppressing the text proper
// (painting shadows only) where selected.
void HighlightPainter::SelectionPaintState::
    PaintSuppressingTextProperWhereSelected(
        TextPainter& text_painter,
        const TextFragmentPaintInfo& fragment_paint_info,
        const TextPaintStyle& text_style,
        DOMNodeId node_id,
        const AutoDarkMode& auto_dark_mode) {
  // First paint the shadows for the whole range.
  if (text_style.shadow) {
    text_painter.Paint(fragment_paint_info, text_style, node_id, auto_dark_mode,
                       TextPainter::kShadowsOnly);
  }

  // Then paint the text proper for any unselected parts in storage order, so
  // that they’re always on top of the shadows.
  if (fragment_paint_info.from < selection_status_.start) {
    text_painter.Paint(
        fragment_paint_info.WithEndOffset(selection_status_.start), text_style,
        node_id, auto_dark_mode, TextPainter::kTextProperOnly);
  }
  if (selection_status_.end < fragment_paint_info.to) {
    text_painter.Paint(
        fragment_paint_info.WithStartOffset(selection_status_.end), text_style,
        node_id, auto_dark_mode, TextPainter::kTextProperOnly);
  }
}

// GetNode() for first-letter fragment returns null because it is anonymous.
// Use AssociatedTextNode() of LayoutTextFragment to get the associated node.
static Node* AssociatedNode(const LayoutObject* layout_object) {
  if (RuntimeEnabledFeatures::PaintHighlightsForFirstLetterEnabled()) {
    if (auto* layout_text_fragment =
            DynamicTo<LayoutTextFragment>(layout_object)) {
      return layout_text_fragment->AssociatedTextNode();
    }
  }
  return layout_object->GetNode();
}

HighlightPainter::HighlightPainter(
    const TextFragmentPaintInfo& fragment_paint_info,
    TextPainter& text_painter,
    TextDecorationPainter& decoration_painter,
    const PaintInfo& paint_info,
    const InlineCursor& cursor,
    const FragmentItem& fragment_item,
    const PhysicalOffset& box_origin,
    const ComputedStyle& style,
    const TextPaintStyle& text_style,
    SelectionPaintState* selection)
    : fragment_paint_info_(fragment_paint_info),
      text_painter_(text_painter),
      decoration_painter_(decoration_painter),
      paint_info_(paint_info),
      cursor_(cursor),
      root_inline_cursor_(cursor),
      fragment_item_(fragment_item),
      box_origin_(box_origin),
      originating_style_(style),
      originating_text_style_(text_style),
      selection_(selection),
      layout_object_(fragment_item_.GetLayoutObject()),
      node_(AssociatedNode(layout_object_)),
      foreground_auto_dark_mode_(
          PaintAutoDarkMode(originating_style_,
                            DarkModeFilter::ElementRole::kForeground)),
      background_auto_dark_mode_(
          PaintAutoDarkMode(originating_style_,
                            DarkModeFilter::ElementRole::kBackground)) {
  root_inline_cursor_.ExpandRootToContainingBlock();

  // Custom highlights and marker-based highlights are defined in terms of
  // DOM ranges in a Text node. Generated text either has no Text node or does
  // not derive its content from the Text node (e.g. ellipsis, soft hyphens).
  // TODO(crbug.com/17528) handle ::first-letter
  if (!fragment_item_.IsGeneratedText()) {
    const auto* text_node = DynamicTo<Text>(node_);
    if (text_node) {
      DocumentMarkerController& controller = node_->GetDocument().Markers();
      if (controller.HasAnyMarkersForText(*text_node)) {
        fragment_dom_offsets_ = GetFragmentDOMOffsets(
            *text_node, fragment_paint_info_.from, fragment_paint_info_.to);
        DCHECK(fragment_dom_offsets_);
        markers_ = controller.ComputeMarkersToPaint(*text_node);
        if (RuntimeEnabledFeatures::SearchTextHighlightPseudoEnabled() &&
            !fragment_item_.IsSvgText()) {
          search_ = controller.MarkersFor(
              *text_node, DocumentMarker::kTextMatch,
              fragment_dom_offsets_->start, fragment_dom_offsets_->end);
        }
        target_ = controller.MarkersFor(
            *text_node, DocumentMarker::kTextFragment,
            fragment_dom_offsets_->start, fragment_dom_offsets_->end);
        spelling_ = controller.MarkersFor(*text_node, DocumentMarker::kSpelling,
                                          fragment_dom_offsets_->start,
                                          fragment_dom_offsets_->end);
        grammar_ = controller.MarkersFor(*text_node, DocumentMarker::kGrammar,
                                         fragment_dom_offsets_->start,
                                         fragment_dom_offsets_->end);
        custom_ = controller.MarkersFor(
            *text_node, DocumentMarker::kCustomHighlight,
            fragment_dom_offsets_->start, fragment_dom_offsets_->end);
      } else if (selection) {
        fragment_dom_offsets_ = GetFragmentDOMOffsets(
            *text_node, fragment_paint_info_.from, fragment_paint_info_.to);
      }
    }
  }

  paint_case_ = ComputePaintCase();

  // |layers_| and |parts_| are only needed when using the full overlay
  // painting algorithm, otherwise we can leave them empty.
  if (paint_case_ == kOverlay) {
    auto* selection_status = GetSelectionStatus(selection_);
    layers_ = HighlightOverlay::ComputeLayers(
        layout_object_->GetDocument(), node_, originating_style_,
        originating_text_style_, paint_info_, selection_status, custom_,
        grammar_, spelling_, target_, search_);
    Vector<HighlightEdge> edges = HighlightOverlay::ComputeEdges(
        node_, fragment_item_.IsGeneratedText(), fragment_dom_offsets_, layers_,
        selection_status, custom_, grammar_, spelling_, target_, search_);
    parts_ =
        HighlightOverlay::ComputeParts(fragment_paint_info_, layers_, edges);

    if (!parts_.empty()) {
      if (const ShapeResultView* shape_result_view =
              fragment_item_->TextShapeResult()) {
        const ShapeResult* shape_result =
            shape_result_view->CreateShapeResult();
        unsigned start_offset = fragment_item_->StartOffset();
        edges_info_.push_back(HighlightEdgeInfo{
            parts_[0].range.from,
            shape_result->CaretPositionForOffset(
                parts_[0].range.from - start_offset, cursor_.CurrentText())});
        for (const HighlightPart& part : parts_) {
          edges_info_.push_back(HighlightEdgeInfo{
              part.range.to,
              shape_result->CaretPositionForOffset(part.range.to - start_offset,
                                                   cursor_.CurrentText())});
        }
      } else {
        edges_info_.push_back(HighlightEdgeInfo{
            parts_[0].range.from,
            fragment_item_
                .CaretInlinePositionForOffset(cursor_.CurrentText(),
                                              parts_[0].range.from)
                .ToFloat()});
        for (const HighlightPart& part : parts_) {
          edges_info_.push_back(HighlightEdgeInfo{
              part.range.to, fragment_item_
                                 .CaretInlinePositionForOffset(
                                     cursor_.CurrentText(), part.range.to)
                                 .ToFloat()});
        }
      }
    }
  }
}

void HighlightPainter::PaintNonCssMarkers(Phase phase) {
  if (markers_.empty())
    return;

  CHECK(node_);
  const StringView text = cursor_.CurrentText();

  const auto* text_node = DynamicTo<Text>(node_);
  const MarkerRangeMappingContext mapping_context(*text_node,
                                                  *fragment_dom_offsets_);
  for (const DocumentMarker* marker : markers_) {
    std::optional<TextOffsetRange> marker_offsets =
        mapping_context.GetTextContentOffsets(*marker);
    if (!marker_offsets || (marker_offsets->start == marker_offsets->end)) {
      continue;
    }
    const unsigned paint_start_offset = marker_offsets->start;
    const unsigned paint_end_offset = marker_offsets->end;

    DCHECK(!DocumentMarker::MarkerTypes::HighlightPseudos().Contains(
        marker->GetType()));

    switch (marker->GetType()) {
      case DocumentMarker::kTextMatch: {
        if (RuntimeEnabledFeatures::SearchTextHighlightPseudoEnabled() &&
            !fragment_item_->IsSvgText()) {
          break;
        }
        const Document& document = node_->GetDocument();
        const auto& text_match_marker = To<TextMatchMarker>(*marker);
        if (phase == kBackground) {
          Color color =
              LayoutTheme::GetTheme().PlatformTextSearchHighlightColor(
                  text_match_marker.IsActiveMatch(),
                  document.InForcedColorsMode(),
                  originating_style_.UsedColorScheme(),
                  document.GetColorProviderForPainting(
                      originating_style_.UsedColorScheme()),
                  document.IsInWebAppScope());
          PaintRect(
              paint_info_.context,
              ComputeBackgroundRect(text, paint_start_offset, paint_end_offset),
              color, background_auto_dark_mode_);
          break;
        }

        const TextPaintStyle text_style =
            TextPaintStyleForTextMatch(text_match_marker, originating_style_,
                                       document, fragment_item_->IsSvgText());
        if (fragment_item_->IsSvgText()) {
          text_painter_.SetSvgState(
              *To<LayoutSVGInlineText>(fragment_item_->GetLayoutObject()),
              originating_style_, text_style.fill_color);
        }
        text_painter_.Paint(
            fragment_paint_info_.Slice(paint_start_offset, paint_end_offset),
            text_style, kInvalidDOMNodeId, foreground_auto_dark_mode_);
      } break;

      case DocumentMarker::kComposition:
      case DocumentMarker::kActiveSuggestion:
      case DocumentMarker::kSuggestion: {
        const auto& styleable_marker = To<StyleableMarker>(*marker);
        if (phase == kBackground) {
          PaintRect(
              paint_info_.context,
              ComputeBackgroundRect(text, paint_start_offset, paint_end_offset),
              styleable_marker.BackgroundColor(), background_auto_dark_mode_);
          break;
        }
        if (StyleableMarkerPainter::ShouldPaintUnderline(styleable_marker)) {
          const SimpleFontData* font_data =
              originating_style_.GetFont()->PrimaryFont();
          StyleableMarkerPainter::PaintUnderline(
              styleable_marker, paint_info_.context, box_origin_,
              originating_style_,
              LineRelativeLocalRect(fragment_item_, text, paint_start_offset,
                                    paint_end_offset),
              LayoutUnit(font_data->GetFontMetrics().Height()),
              node_->GetDocument().InDarkMode());
        }
        if (marker->GetType() == DocumentMarker::kComposition &&
            !styleable_marker.TextColor().IsFullyTransparent() &&
            RuntimeEnabledFeatures::CompositionForegroundMarkersEnabled()) {
          PaintTextForCompositionMarker(text, styleable_marker.TextColor(),
                                        paint_start_offset, paint_end_offset);
        }
        break;
      }
      case DocumentMarker::kGlic: {
        if (phase == kBackground) {
          PaintBackgroundForGlicMarker(marker, text, paint_start_offset,
                                       paint_end_offset);
        }
      } break;
      case DocumentMarker::kSpelling:
      case DocumentMarker::kGrammar:
      case DocumentMarker::kTextFragment:
      case DocumentMarker::kCustomHighlight:
        NOTREACHED();
    }
  }
}

HighlightPainter::Case HighlightPainter::PaintCase() const {
  return paint_case_;
}

HighlightPainter::Case HighlightPainter::ComputePaintCase() const {
  if (selection_ && selection_->ShouldPaintSelectedTextOnly())
    return kSelectionOnly;

  // This can yield false positives (weakening the optimisations below) if all
  // non-spelling/grammar/selection highlights are outside the text fragment.
  if (!target_.empty() || !search_.empty() || !custom_.empty()) {
    return kOverlay;
  }

  if (selection_ && spelling_.empty() && grammar_.empty()) {
    const ComputedStyle* pseudo_style =
        HighlightStyleUtils::HighlightPseudoStyle(node_, originating_style_,
                                                  kPseudoIdSelection);

    // If we only have a selection, and there are no selection or originating
    // decorations, we don’t need the expense of overlay painting.
    return !originating_style_.HasAppliedTextDecorations() &&
                   (!pseudo_style || !pseudo_style->HasAppliedTextDecorations())
               ? kFastSelection
               : kOverlay;
  }

  if (!spelling_.empty() || !grammar_.empty()) {
    // If there is a selection too, we must use the overlay painting algorithm.
    if (selection_)
      return kOverlay;

    // If there are only spelling and/or grammar highlights, and they use the
    // default style that only adds decorations without adding a background or
    // changing the text color, we don’t need the expense of overlay painting.
    bool spelling_ok =
        spelling_.empty() ||
        !HasNonTrivialSpellingGrammarStyles(
            fragment_item_, node_, originating_style_, kPseudoIdSpellingError);
    bool grammar_ok =
        grammar_.empty() ||
        !HasNonTrivialSpellingGrammarStyles(
            fragment_item_, node_, originating_style_, kPseudoIdGrammarError);
    return spelling_ok && grammar_ok ? kFastSpellingGrammar : kOverlay;
  }

  DCHECK(!selection_ && target_.empty() && spelling_.empty() &&
         grammar_.empty() && custom_.empty());
  return kNoHighlights;
}

void HighlightPainter::FastPaintSpellingGrammarDecorations() {
  DCHECK_EQ(paint_case_, kFastSpellingGrammar);
  CHECK(node_);
  const auto& text_node = To<Text>(*node_);
  const StringView text = cursor_.CurrentText();

  // ::spelling-error overlay is drawn on top of ::grammar-error overlay.
  // https://drafts.csswg.org/css-pseudo-4/#highlight-backgrounds
  FastPaintSpellingGrammarDecorations(text_node, text, grammar_);
  FastPaintSpellingGrammarDecorations(text_node, text, spelling_);
}

void HighlightPainter::FastPaintSpellingGrammarDecorations(
    const Text& text_node,
    const StringView& text,
    const DocumentMarkerVector& markers) {
  const MarkerRangeMappingContext mapping_context(text_node,
                                                  *fragment_dom_offsets_);
  for (const DocumentMarker* marker : markers) {
    std::optional<TextOffsetRange> marker_offsets =
        mapping_context.GetTextContentOffsets(*marker);
    if (!marker_offsets || (marker_offsets->start == marker_offsets->end)) {
      continue;
    }
    PaintOneSpellingGrammarDecoration(
        marker->GetType(), text, marker_offsets->start, marker_offsets->end);
  }
}

void HighlightPainter::PaintOneSpellingGrammarDecoration(
    DocumentMarker::MarkerType type,
    const StringView& text,
    unsigned paint_start_offset,
    unsigned paint_end_offset) {
  if (node_->GetDocument().Printing()) {
    return;
  }

  if (!text_painter_.GetSvgState()) {
    if (const auto* pseudo_style = HighlightStyleUtils::HighlightPseudoStyle(
            node_, originating_style_, PseudoFor(type))) {
      const TextPaintStyle text_style =
          HighlightStyleUtils::HighlightPaintingStyle(
              node_->GetDocument(), originating_style_, pseudo_style, node_,
              PseudoFor(type), originating_text_style_, paint_info_,
              SearchTextIsActiveMatch::kNo)
              .style;
      PaintOneSpellingGrammarDecoration(type, text, paint_start_offset,
                                        paint_end_offset, *pseudo_style,
                                        text_style, nullptr);
      return;
    }
  }

  // If they are not yet implemented (as is the case for SVG), or they have no
  // styles (as there can be for non-HTML content or for HTML content with the
  // wrong root), use the originating style with the decorations override set
  // to a synthesised AppliedTextDecoration.
  //
  // For the synthesised decoration, just like with our real spelling/grammar
  // decorations, the ‘text-decoration-style’, ‘text-decoration-thickness’, and
  // ‘text-underline-offset’ are irrelevant.
  //
  // SVG painting currently ignores ::selection styles, and will malfunction
  // or crash if asked to paint decorations introduced by highlight pseudos.
  // TODO(crbug.com/1147859) is SVG spec ready for highlight decorations?
  // TODO(crbug.com/1147859) https://github.com/w3c/svgwg/issues/894
  const AppliedTextDecoration synthesised{
      LineFor(type), {}, ColorFor(type), {}, {}};
  PaintOneSpellingGrammarDecoration(type, text, paint_start_offset,
                                    paint_end_offset, originating_style_,
                                    originating_text_style_, &synthesised);
}

void HighlightPainter::PaintOneSpellingGrammarDecoration(
    DocumentMarker::MarkerType marker_type,
    const StringView& text,
    unsigned paint_start_offset,
    unsigned paint_end_offset,
    const ComputedStyle& style,
    const TextPaintStyle& text_style,
    const AppliedTextDecoration* decoration_override) {
  // When painting decorations on the spelling/grammar fast path, the part and
  // the decoration have the same range, so we can use the same rect for both
  // clipping the canvas and painting the decoration.
  const HighlightRange range{paint_start_offset, paint_end_offset};
  const LineRelativeRect rect = LineRelativeWorldRect(range);

  std::optional<TextDecorationInfo> decoration_info{};
  decoration_painter_.UpdateDecorationInfo(decoration_info, fragment_item_,
                                           style, rect, decoration_override);

  GraphicsContextStateSaver saver{paint_info_.context};
  ClipToPartRect(rect);

  decoration_painter_.PaintExceptLineThrough(
      *decoration_info, text_style,
      fragment_paint_info_.Slice(paint_start_offset, paint_end_offset),
      LineFor(marker_type));
}

void HighlightPainter::PaintOriginatingShadow(const TextPaintStyle& text_style,
                                              DOMNodeId node_id) {
  // Paint the shadows for the whole range.
  if (text_style.shadow) {
    text_painter_.Paint(fragment_paint_info_, text_style, node_id,
                        foreground_auto_dark_mode_, TextPainter::kShadowsOnly);
  }
}

TextOffsetRange HighlightPainter::GetFragmentDOMOffsets(const Text& text,
                                                        unsigned from,
                                                        unsigned to) {
  const OffsetMapping* mapping = OffsetMapping::GetFor(text.GetLayoutObject());
  unsigned last_from = mapping->GetLastPosition(from).OffsetInContainerNode();
  unsigned first_to = mapping->GetFirstPosition(to).OffsetInContainerNode();
  return {last_from, first_to};
}

const PhysicalRect HighlightPainter::ComputeBackgroundRect(
    StringView text,
    unsigned start_offset,
    unsigned end_offset) {
  return fragment_item_.LocalRect(text, start_offset, end_offset) + box_origin_;
}

const PhysicalRect HighlightPainter::ComputeBackgroundRectForSelection(
    unsigned start_offset,
    unsigned end_offset) {
  LayoutSelectionStatus selection_status{selection_->Status()};
  selection_status.start = start_offset;
  selection_status.end = end_offset;
  return root_inline_cursor_.CurrentLocalSelectionRectForText(
             selection_status) +
         box_origin_;
}

void HighlightPainter::PaintHighlightOverlays(
    const TextPaintStyle& originating_text_style,
    DOMNodeId node_id,
    bool paint_marker_backgrounds,
    std::optional<AffineTransform> rotation) {
  DCHECK_EQ(paint_case_, kOverlay);

  // |node_| might not be a Text node (e.g. <br>), or it might be nullptr (e.g.
  // ::first-letter). In both cases, we should still try to paint kOriginating
  // and kSelection if necessary, but we can’t paint marker-based highlights,
  // because GetTextContentOffset requires a Text node. Markers are defined and
  // stored in terms of Text nodes anyway, so this should never be a problem.

  // For each overlay, paint ‘background-color’ and ‘text-shadow’ one part at a
  // time, since both of them may vary in color when ‘color’ is ‘currentColor’.
  for (wtf_size_t i = 0; i < layers_.size(); i++) {
    const HighlightLayer& layer = layers_[i];

    if (layer.type == HighlightLayerType::kOriginating) {
      continue;
    }

    if (layer.type == HighlightLayerType::kSelection &&
        !paint_marker_backgrounds) {
      continue;
    }

    // Paint ‘background-color’ while trying to merge parts if possible,
    // to avoid creating unnecessary “seams”.
    MergedHighlightPart<HighlightBackground> merged_background{};
    const auto& paint_background =
        [&](const MergedHighlightPart<HighlightBackground>::Merged& merged) {
          if (merged.inner.color.IsFullyTransparent()) {
            return;
          }
          // TODO(crbug.com/40281215) ComputeBackgroundRect should use the same
          // logic as ComputeBackgroundRectForSelection, that is, it should
          // expand selection to the line height and extend for line breaks.
          PhysicalRect part_rect =
              layer.type == HighlightLayerType::kSelection
                  ? ComputeBackgroundRectForSelection(merged.from, merged.to)
                  : ComputeBackgroundRect(cursor_.CurrentText(), merged.from,
                                          merged.to);
          PaintHighlightBackground(paint_info_.context, originating_style_,
                                   merged.inner.color, part_rect, rotation);
        };
    for (const HighlightPart& part : parts_) {
      for (const HighlightBackground& background : part.backgrounds) {
        if (background.layer_index == i) {
          if (const auto& merged = merged_background.Merge(background, part)) {
            paint_background(*merged);
          }
          break;
        }
      }
    }
    if (const auto& merged = merged_background.Take()) {
      paint_background(*merged);
    }

    // Paint ‘text-shadow’ while trying to merge parts if possible,
    // to avoid unnecessarily splitting ligatures.
    MergedHighlightPart<HighlightTextShadow> merged_text_shadow{};
    const auto& paint_text_shadow =
        [&](const MergedHighlightPart<HighlightTextShadow>::Merged& merged) {
          if (!layer.text_style.style.shadow) {
            return;
          }
          TextPaintStyle text_shadow_style{};
          text_shadow_style.shadow = layer.text_style.style.shadow;
          text_shadow_style.current_color = merged.inner.current_color;
          text_painter_.Paint(
              fragment_paint_info_.Slice(merged.from, merged.to),
              text_shadow_style, node_id, foreground_auto_dark_mode_,
              TextPainter::kShadowsOnly);
        };
    for (const HighlightPart& part : parts_) {
      for (const HighlightTextShadow& text_shadow : part.text_shadows) {
        if (text_shadow.layer_index == i) {
          if (const auto& merged =
                  merged_text_shadow.Merge(text_shadow, part)) {
            paint_text_shadow(*merged);
          }
          break;
        }
      }
    }
    if (const auto& merged = merged_text_shadow.Take()) {
      paint_text_shadow(*merged);
    }
  }

  // For each part, paint the text proper over every highlighted range,
  for (auto& part : parts_) {
    LineRelativeRect part_rect = LineRelativeWorldRect(part.range);

    PaintDecorationsExceptLineThrough(part, part_rect);

    // Only paint text if we have a shape result. See TextPainter::Paint().
    if (fragment_paint_info_.shape_result) {
      std::optional<base::AutoReset<bool>> is_painting_selection_reset;
      GraphicsContextStateSaver state_saver(paint_info_.context);
      // SVG text may have transforms that defeat clipping. The clipping
      // is only required for ligatures, so we will accept potential
      // double painting of ligatures for SVG so as to correctly handle
      // transformed text (include text paths). This might be fixable by
      // transforming the ink overflow before using it to expamd the clip.
      TextPainter::SvgTextPaintState* svg_state = text_painter_.GetSvgState();
      if (svg_state && part.type == HighlightLayerType::kSelection)
          [[unlikely]] {
        // SVG text painting needs to know it is painting selection.
        is_painting_selection_reset.emplace(&svg_state->is_painting_selection_,
                                            true);
      } else {
        LineRelativeRect clip_rect = part_rect;
        if (part.stroke_width > 0) {
          clip_rect.Inflate(
              LayoutUnit::FromFloatCeil(part.stroke_width / 2.0f));
        }
        // If we're at the far left or right end of a fragment, expand the clip
        // to avoid clipping characters (italics and some antialiasing).
        if (part.range.from == fragment_paint_info_.from) {
          clip_rect.AdjustLineStartToInkOverflow(fragment_item_);
        }
        if (part.range.to == fragment_paint_info_.to) {
          clip_rect.AdjustLineEndToInkOverflow(fragment_item_);
        }
        ClipToPartRect(clip_rect.EnclosingLineRelativeRect());
      }

      // Adjust start/end offset when they are in the middle of a ligature.
      // e.g., when |start_offset| is between a ligature of "fi", it needs to
      // be adjusted to before "f".
      unsigned start = part.range.from;
      unsigned end = part.range.to;
      fragment_paint_info_.shape_result->ExpandRangeToIncludePartialGlyphs(
          &start, &end);

      text_painter_.Paint(fragment_paint_info_.Slice(start, end), part.style,
                          node_id, foreground_auto_dark_mode_,
                          TextPainter::kTextProperOnly);
    }

    PaintDecorationsOnlyLineThrough(part, part_rect);
  }
}

void HighlightPainter::PaintHighlightBackground(
    GraphicsContext& context,
    const ComputedStyle& style,
    Color color,
    const PhysicalRect& rect,
    const std::optional<AffineTransform>& rotation) {
  AutoDarkMode auto_dark_mode(
      PaintAutoDarkMode(style, DarkModeFilter::ElementRole::kSelection));

  if (!rotation) {
    PaintRect(context, rect, color, auto_dark_mode);
    return;
  }

  // PaintRect tries to pixel-snap the given rect, but if we’re painting in a
  // non-horizontal writing mode, our context has been transformed, regressing
  // tests like <paint/invalidation/repaint-across-writing-mode-boundary>. To
  // fix this, we undo the transformation temporarily, then use the original
  // physical coordinates (before MapSelectionRectIntoRotatedSpace).
  context.ConcatCTM(rotation->Inverse());
  PaintRect(context, rect, color, auto_dark_mode);
  context.ConcatCTM(*rotation);
}

PseudoId HighlightPainter::PseudoFor(DocumentMarker::MarkerType type) {
  switch (type) {
    case DocumentMarker::kSpelling:
      return kPseudoIdSpellingError;
    case DocumentMarker::kGrammar:
      return kPseudoIdGrammarError;
    case DocumentMarker::kTextFragment:
      return kPseudoIdTargetText;
    default:
      NOTREACHED();
  }
}

TextDecorationLine HighlightPainter::LineFor(DocumentMarker::MarkerType type) {
  switch (type) {
    case DocumentMarker::kSpelling:
      return TextDecorationLine::kSpellingError;
    case DocumentMarker::kGrammar:
      return TextDecorationLine::kGrammarError;
    default:
      NOTREACHED();
  }
}

Color HighlightPainter::ColorFor(DocumentMarker::MarkerType type) {
  switch (type) {
    case DocumentMarker::kSpelling:
      return LayoutTheme::GetTheme().PlatformSpellingMarkerUnderlineColor();
    case DocumentMarker::kGrammar:
      return LayoutTheme::GetTheme().PlatformGrammarMarkerUnderlineColor();
    default:
      NOTREACHED();
  }
}

LineRelativeRect HighlightPainter::LineRelativeWorldRect(
    const HighlightOverlay::HighlightRange& range) {
  return LocalRectInWritingModeSpace(range.from, range.to) +
         LineRelativeOffset::CreateFromBoxOrigin(box_origin_);
}

LineRelativeRect HighlightPainter::LocalRectInWritingModeSpace(
    unsigned from,
    unsigned to) const {
  if (paint_case_ != kOverlay) {
    const StringView text = cursor_.CurrentText();
    return LineRelativeLocalRect(fragment_item_, text, from, to);
  }

  auto from_info =
      std::lower_bound(edges_info_.begin(), edges_info_.end(), from,
                       [](const HighlightEdgeInfo& info, unsigned offset) {
                         return info.offset < offset;
                       });
  auto to_info =
      std::lower_bound(from_info, edges_info_.end(), to,
                       [](const HighlightEdgeInfo& info, unsigned offset) {
                         return info.offset < offset;
                       });
  CHECK_NE(from_info, edges_info_.end());
  CHECK_NE(to_info, edges_info_.end());

  // This rect is used for 2 purposes: To set the offset and width for
  // text decoration painting, and the set the clip. The former uses the
  // offset and width, but not height, and the offset should be the
  // fragment offset. The latter uses offset and size, but the offset should
  // be the corner of the painted region. Return the origin for text decorations
  // and the height for clipping, then update the offset for clipping in the
  // calling code.
  const LayoutUnit height = fragment_item_.InkOverflowRect().Height();
  LayoutUnit left;
  LayoutUnit right;
  if (from_info->x > to_info->x) {
    left = LayoutUnit::FromFloatFloor(to_info->x);
    right = LayoutUnit::FromFloatCeil(from_info->x);
  } else {
    left = LayoutUnit::FromFloatFloor(from_info->x);
    right = LayoutUnit::FromFloatCeil(to_info->x);
  }
  return {{left, LayoutUnit{}}, {right - left, height}};
}

void HighlightPainter::ClipToPartRect(const LineRelativeRect& part_rect) {
  gfx::RectF clip_rect{part_rect};
  if (fragment_item_.IsSvgText()) [[unlikely]] {
    clip_rect = TextDecorationPainter::ExpandRectForSVGDecorations(part_rect);
  } else {
    clip_rect.Offset(0, fragment_item_.InkOverflowRect().Y());
  }
  paint_info_.context.Clip(clip_rect);
}

void HighlightPainter::PaintDecorationsExceptLineThrough(
    const HighlightPart& part,
    const LineRelativeRect& part_rect) {
  // Line decorations in highlight pseudos are ordered first by the kind of line
  // (underlines before overlines), then by the highlight layer they came from.
  // https://github.com/w3c/csswg-drafts/issues/6022
  PaintDecorationsExceptLineThrough(part, part_rect,
                                    TextDecorationLine::kUnderline);
  PaintDecorationsExceptLineThrough(part, part_rect,
                                    TextDecorationLine::kOverline);
  PaintDecorationsExceptLineThrough(
      part, part_rect,
      TextDecorationLine::kSpellingError | TextDecorationLine::kGrammarError);
}

void HighlightPainter::PaintDecorationsExceptLineThrough(
    const HighlightPart& part,
    const LineRelativeRect& part_rect,
    TextDecorationLine lines_to_paint) {
  GraphicsContextStateSaver state_saver(paint_info_.context, false);
  for (const HighlightDecoration& decoration : part.decorations) {
    HighlightLayer& decoration_layer = layers_[decoration.layer_index];

    // Clipping the canvas unnecessarily is expensive, so avoid doing it if
    // there are no decorations of the given |lines_to_paint|.
    if (!EnumHasFlags(decoration_layer.decorations_in_effect, lines_to_paint)) {
      continue;
    }

    // SVG painting currently ignores ::selection styles, and will malfunction
    // or crash if asked to paint decorations introduced by highlight pseudos.
    // TODO(crbug.com/1147859) is SVG spec ready for highlight decorations?
    // TODO(crbug.com/1147859) https://github.com/w3c/svgwg/issues/894
    if (text_painter_.GetSvgState() &&
        decoration.type != HighlightLayerType::kOriginating) {
      continue;
    }

    // Paint the decoration over the range of the originating fragment or active
    // highlight, but clip it to the range of the part.
    const LineRelativeRect decoration_rect =
        LineRelativeWorldRect(decoration.range);

    std::optional<TextDecorationInfo> decoration_info{};
    decoration_painter_.UpdateDecorationInfo(decoration_info, fragment_item_,
                                             *decoration_layer.style,
                                             decoration_rect);

    if (part.type != HighlightLayerType::kOriginating) {
      if (decoration.type == HighlightLayerType::kOriginating) {
        decoration_info->SetHighlightOverrideColor(part.style.current_color);
      } else {
        decoration_info->SetHighlightOverrideColor(
            decoration.highlight_override_color);
      }
    }

    if (!state_saver.Saved()) {
      state_saver.Save();
      const LineRelativeRect clip_rect =
          part.range != decoration.range ? part_rect : decoration_rect;
      ClipToPartRect(clip_rect);
    }

    decoration_painter_.PaintExceptLineThrough(
        *decoration_info, decoration_layer.text_style.style,
        fragment_paint_info_.Slice(part.range.from, part.range.to),
        lines_to_paint);
  }
}

void HighlightPainter::PaintDecorationsOnlyLineThrough(
    const HighlightPart& part,
    const LineRelativeRect& part_rect) {
  GraphicsContextStateSaver state_saver(paint_info_.context, false);
  for (const HighlightDecoration& decoration : part.decorations) {
    HighlightLayer& decoration_layer = layers_[decoration.layer_index];

    // Clipping the canvas unnecessarily is expensive, so avoid doing it if
    // there are no ‘line-through’ decorations.
    if (!EnumHasFlags(decoration_layer.decorations_in_effect,
                      TextDecorationLine::kLineThrough)) {
      continue;
    }

    // SVG painting currently ignores ::selection styles, and will malfunction
    // or crash if asked to paint decorations introduced by highlight pseudos.
    // TODO(crbug.com/1147859) is SVG spec ready for highlight decorations?
    // TODO(crbug.com/1147859) https://github.com/w3c/svgwg/issues/894
    if (text_painter_.GetSvgState() &&
        decoration.type != HighlightLayerType::kOriginating) {
      continue;
    }

    // Paint the decoration over the range of the originating fragment or active
    // highlight, but clip it to the range of the part.
    const LineRelativeRect decoration_rect =
        LineRelativeWorldRect(decoration.range);

    std::optional<TextDecorationInfo> decoration_info{};
    decoration_painter_.UpdateDecorationInfo(decoration_info, fragment_item_,
                                             *decoration_layer.style,
                                             decoration_rect);

    if (part.type != HighlightLayerType::kOriginating) {
      if (decoration.type == HighlightLayerType::kOriginating) {
        decoration_info->SetHighlightOverrideColor(part.style.current_color);
      } else {
        decoration_info->SetHighlightOverrideColor(
            decoration.highlight_override_color);
      }
    }

    if (!state_saver.Saved()) {
      state_saver.Save();
      const LineRelativeRect clip_rect =
          part.range != decoration.range ? part_rect : decoration_rect;
      ClipToPartRect(clip_rect);
    }

    decoration_painter_.PaintOnlyLineThrough(*decoration_info,
                                             decoration_layer.text_style.style);
  }
}

void HighlightPainter::PaintTextForCompositionMarker(
    const StringView& text,
    const Color& text_color,
    unsigned paint_start_offset,
    unsigned paint_end_offset) {
  TextPaintStyle text_style;
  text_style.current_color = text_style.fill_color = text_style.stroke_color =
      text_style.emphasis_mark_color = text_color;
  text_style.stroke_width = originating_style_.TextStrokeWidth();
  text_style.color_scheme = originating_style_.UsedColorScheme();
  text_style.shadow = nullptr;
  text_style.paint_order = originating_style_.PaintOrder();

  LineRelativeRect decoration_rect = LineRelativeLocalRect(
      fragment_item_, text, paint_start_offset, paint_end_offset);
  decoration_rect.Move(LineRelativeOffset::CreateFromBoxOrigin(box_origin_));
  TextDecorationPainter decoration_painter(
      text_painter_, decoration_painter_.InlineContext(), paint_info_,
      originating_style_, text_style, decoration_rect, selection_);

  decoration_painter.Begin(fragment_item_, TextDecorationPainter::kOriginating);
  decoration_painter.PaintExceptLineThrough(
      fragment_paint_info_.Slice(paint_start_offset, paint_end_offset));

  text_painter_.Paint(
      fragment_paint_info_.Slice(paint_start_offset, paint_end_offset),
      text_style, kInvalidDOMNodeId, foreground_auto_dark_mode_);

  decoration_painter.PaintOnlyLineThrough();
}

void HighlightPainter::PaintBackgroundForGlicMarker(
    const DocumentMarker* marker,
    const StringView& text,
    unsigned paint_start_offset,
    unsigned paint_end_offset) {
  const auto& glic_highlight = To<GlicMarker>(*marker);
  gfx::RectF text_box(
      ComputeBackgroundRect(text, paint_start_offset, paint_end_offset));
  cc::PaintFlags flags;
  flags.setAntiAlias(true);
  flags.setColor(glic_highlight.BackgroundColor().toSkColor4f());
  paint_info_.context.Canvas()->drawRect(RectFToSkRect(text_box), flags);
}

}  // namespace blink