File: highlight_overlay.h

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 (276 lines) | stat: -rw-r--r-- 9,734 bytes parent folder | download | duplicates (4)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_HIGHLIGHT_OVERLAY_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_HIGHLIGHT_OVERLAY_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/editing/markers/document_marker.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/text_offset_range.h"
#include "third_party/blink/renderer/core/style/computed_style_constants.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"

namespace blink {

class HighlightRegistry;
struct LayoutSelectionStatus;
struct TextFragmentPaintInfo;

class CORE_EXPORT HighlightOverlay {
  STATIC_ONLY(HighlightOverlay);

 public:
  enum class HighlightLayerType : uint8_t {
    kOriginating,
    kCustom,
    kGrammar,
    kSpelling,
    kTargetText,
    kSearchText,
    kSearchTextActiveMatch,
    kSelection,
  };

  enum class HighlightEdgeType : uint8_t { kStart, kEnd };

  // Identifies a highlight layer, such as the originating content, one of the
  // highlight pseudos, or a custom highlight (name unique within a registry).
  struct CORE_EXPORT HighlightLayer {
    DISALLOW_NEW();

   public:
    explicit HighlightLayer(HighlightLayerType type,
                            const AtomicString& name = g_null_atom);

    void Trace(Visitor* visitor) const {
      visitor->Trace(style);
      visitor->Trace(text_style);
    }

    String ToString() const;
    enum PseudoId PseudoId() const;
    const AtomicString& PseudoArgument() const;

    int8_t ComparePaintOrder(const HighlightLayer&,
                             const HighlightRegistry*) const;
    bool operator==(const HighlightLayer&) const;
    bool operator!=(const HighlightLayer&) const;

    HighlightLayerType type;
    Member<const ComputedStyle> style;
    HighlightStyleUtils::HighlightTextPaintStyle text_style;
    TextDecorationLine decorations_in_effect;
    // Constructed from the highlight markers name reference, and the
    // marker always outlives the painter that owns the layers.
    AtomicString name;
  };

  // Represents a range of the fragment, as offsets in canonical text space.
  // More details about canonical text offsets: <https://goo.gl/CJbxky>
  struct CORE_EXPORT HighlightRange {
    DISALLOW_NEW();

   public:
    HighlightRange(unsigned from, unsigned to);

    String ToString() const;

    bool operator==(const HighlightRange&) const;
    bool operator!=(const HighlightRange&) const;

    unsigned from;
    unsigned to;
  };

  // Represents the start or end (indicated by |type|) of a highlighted |range|
  // for the given |layer|. Storing both offsets of the range, rather than just
  // the offset of this edge, allows decorations added by highlights to recover
  // the original range for the purposes of decoration phase and wavelength.
  struct CORE_EXPORT HighlightEdge {
    DISALLOW_NEW();

   public:
    HighlightEdge(HighlightRange range,
                  HighlightLayerType layer_type,
                  uint16_t layer_index,
                  HighlightEdgeType edge_type)
        : range(range),
          layer_index(layer_index),
          layer_type(layer_type),
          edge_type(edge_type) {}

    String ToString() const;
    unsigned Offset() const;

    // Order by offset asc, then “end” edges first, then by layer paint order.
    // ComputeParts requires “end” edges first in case two ranges of the same
    // highlight are immediately adjacent. The opposite would be required for
    // empty highlight ranges, but they’re illegal as per DocumentMarker ctor.
    bool LessThan(const HighlightEdge&,
                  const HeapVector<HighlightLayer>& layers,
                  const HighlightRegistry*) const;
    bool operator==(const HighlightEdge&) const;
    bool operator!=(const HighlightEdge&) const;

    HighlightRange range;
    uint16_t layer_index;
    HighlightLayerType layer_type;
    HighlightEdgeType edge_type;
  };

  // Represents a potential decoration for the given layer type and index that
  // would need to be painted over the given |range|.
  //
  // Note that decorations are painted with this range, but clipped to the range
  // in each HighlightPart, ensuring that decoration phase and wavelength are
  // maintained while allowing them to be recolored or split across layers.
  struct CORE_EXPORT HighlightDecoration {
    DISALLOW_NEW();

   public:
    HighlightDecoration(HighlightLayerType type,
                        uint16_t layer_index,
                        HighlightRange range,
                        Color override_color);

    String ToString() const;

    bool operator==(const HighlightDecoration&) const;
    bool operator!=(const HighlightDecoration&) const;

    HighlightLayerType type;
    uint16_t layer_index;
    HighlightRange range;
    Color highlight_override_color;
  };

  struct CORE_EXPORT HighlightBackground {
    DISALLOW_NEW();

   public:
    String ToString() const;

    bool operator==(const HighlightBackground&) const;
    bool operator!=(const HighlightBackground&) const;

    HighlightLayerType type;
    uint16_t layer_index;
    Color color;
  };

  struct CORE_EXPORT HighlightTextShadow {
    DISALLOW_NEW();

   public:
    String ToString() const;

    bool operator==(const HighlightTextShadow&) const;
    bool operator!=(const HighlightTextShadow&) const;

    HighlightLayerType type;
    uint16_t layer_index;
    Color current_color;
  };

  // Represents a |range| of the fragment that needs its text proper painted in
  // the style of the given topmost layer with the given |decorations|.
  //
  // Note that decorations are clipped to this range, but painted with the range
  // in each HighlightDecoration, ensuring that decoration phase and wavelength
  // are maintained while allowing them to be recolored or split across layers.
  struct CORE_EXPORT HighlightPart {
    DISALLOW_NEW();

   public:
    HighlightPart(HighlightLayerType,
                  uint16_t,
                  HighlightRange,
                  TextPaintStyle,
                  float,
                  Vector<HighlightDecoration>,
                  Vector<HighlightBackground>,
                  Vector<HighlightTextShadow>);
    HighlightPart(HighlightLayerType,
                  uint16_t,
                  HighlightRange,
                  TextPaintStyle,
                  float,
                  Vector<HighlightDecoration>);

    void Trace(Visitor* visitor) const { visitor->Trace(style); }

    String ToString() const;

    bool operator==(const HighlightPart&) const;
    bool operator!=(const HighlightPart&) const;

    HighlightLayerType type;
    uint16_t layer_index;
    HighlightRange range;
    TextPaintStyle style;
    float stroke_width;
    Vector<HighlightDecoration> decorations;
    Vector<HighlightBackground> backgrounds;
    Vector<HighlightTextShadow> text_shadows;
  };

  // Given details of a fragment and how it is highlighted, returns the layers
  // that need to be painted, in overlay painting order.
  static HeapVector<HighlightLayer> ComputeLayers(
      const Document& document,
      Node* node,
      const ComputedStyle& originating_style,
      const TextPaintStyle& originating_text_style,
      const PaintInfo& paint_info,
      const LayoutSelectionStatus* selection,
      const DocumentMarkerVector& custom,
      const DocumentMarkerVector& grammar,
      const DocumentMarkerVector& spelling,
      const DocumentMarkerVector& target,
      const DocumentMarkerVector& search);

  // Given details of a fragment and how it is highlighted, returns the start
  // and end transitions (edges) of the layers, in offset and layer order.
  static Vector<HighlightEdge> ComputeEdges(
      const Node*,
      bool is_generated_text_fragment,
      std::optional<TextOffsetRange> dom_offsets,
      const HeapVector<HighlightLayer>& layers,
      const LayoutSelectionStatus* selection,
      const DocumentMarkerVector& custom,
      const DocumentMarkerVector& grammar,
      const DocumentMarkerVector& spelling,
      const DocumentMarkerVector& target,
      const DocumentMarkerVector& search);

  // Given highlight |layers| and |edges|, returns the ranges of text that can
  // be painted in the same layer with the same decorations, clamping the result
  // to the given |originating| fragment.
  //
  // The edges must not represent overlapping ranges. If the highlight is active
  // in overlapping ranges, those ranges must be merged before ComputeEdges.
  static HeapVector<HighlightPart> ComputeParts(
      const TextFragmentPaintInfo& originating,
      const HeapVector<HighlightLayer>& layers,
      const Vector<HighlightEdge>& edges);
};

CORE_EXPORT std::ostream& operator<<(std::ostream&,
                                     const HighlightOverlay::HighlightLayer&);
CORE_EXPORT std::ostream& operator<<(std::ostream&,
                                     const HighlightOverlay::HighlightEdge&);
CORE_EXPORT std::ostream& operator<<(std::ostream&,
                                     const HighlightOverlay::HighlightPart&);

}  // namespace blink

WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(
    blink::HighlightOverlay::HighlightLayer)
WTF_ALLOW_CLEAR_UNUSED_SLOTS_WITH_MEM_FUNCTIONS(
    blink::HighlightOverlay::HighlightPart)

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_HIGHLIGHT_OVERLAY_H_