File: text_painter.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 (155 lines) | stat: -rw-r--r-- 6,006 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
// 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_TEXT_PAINTER_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_TEXT_PAINTER_H_

#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/layout/style_variant.h"
#include "third_party/blink/renderer/core/paint/line_relative_rect.h"
#include "third_party/blink/renderer/core/paint/paint_flags.h"
#include "third_party/blink/renderer/core/style/computed_style_constants.h"
#include "third_party/blink/renderer/platform/graphics/color.h"
#include "third_party/blink/renderer/platform/graphics/dom_node_id.h"

namespace blink {

class ComputedStyle;
class Document;
class Font;
class GraphicsContext;
class LayoutObject;
class LayoutSVGInlineText;
class TextDecorationInfo;
enum class TextEmphasisPosition : unsigned;
struct AutoDarkMode;
struct PaintInfo;
struct SvgContextPaints;
struct TextFragmentPaintInfo;
struct TextPaintStyle;

// Base class for text painting. Operates on PhysicalTextFragments and only
// paints text and decorations. Border painting etc is handled by the
// TextFragmentPainter class.
// TODO(layout-dev): Does this distinction make sense?
class CORE_EXPORT TextPainter {
  STACK_ALLOCATED();

 public:
  class SvgTextPaintState final {
    STACK_ALLOCATED();

   public:
    SvgTextPaintState(const LayoutSVGInlineText&,
                      const ComputedStyle&,
                      StyleVariant style_variant,
                      PaintFlags paint_flags);
    SvgTextPaintState(const LayoutSVGInlineText&,
                      const ComputedStyle&,
                      Color text_match_color);

    const LayoutSVGInlineText& InlineText() const;
    const LayoutObject& TextDecorationObject() const;
    const ComputedStyle& Style() const;
    bool IsPaintingSelection() const;
    PaintFlags GetPaintFlags() const;
    bool IsRenderingClipPathAsMaskImage() const;
    bool IsPaintingTextMatch() const;
    // This is callable only if IsPaintingTextMatch().
    Color TextMatchColor() const;

    AffineTransform& EnsureShaderTransform();
    const AffineTransform* GetShaderTransform() const;

   private:
    const LayoutSVGInlineText& layout_svg_inline_text_;
    const ComputedStyle& style_;
    std::optional<AffineTransform> shader_transform_;
    std::optional<Color> text_match_color_;
    StyleVariant style_variant_ = StyleVariant::kStandard;
    PaintFlags paint_flags_ = PaintFlag::kNoFlag;
    bool is_painting_selection_ = false;
    friend class TextPainter;
    friend class HighlightPainter;
  };

  TextPainter(GraphicsContext& context,
              const SvgContextPaints* svg_context_paints,
              const Font& font,
              const gfx::Rect& visual_rect,
              const LineRelativeOffset& text_origin)
      : graphics_context_(context),
        svg_context_paints_(svg_context_paints),
        font_(font),
        visual_rect_(visual_rect),
        text_origin_(text_origin) {}
  ~TextPainter() = default;

  enum ShadowMode { kBothShadowsAndTextProper, kShadowsOnly, kTextProperOnly };
  void Paint(const TextFragmentPaintInfo& fragment_paint_info,
             const TextPaintStyle&,
             DOMNodeId,
             const AutoDarkMode& auto_dark_mode,
             ShadowMode = kBothShadowsAndTextProper);

  void PaintSelectedText(const TextFragmentPaintInfo& fragment_paint_info,
                         unsigned selection_start,
                         unsigned selection_end,
                         const TextPaintStyle& text_style,
                         const TextPaintStyle& selection_style,
                         const LineRelativeRect& selection_rect,
                         DOMNodeId node_id,
                         const AutoDarkMode& auto_dark_mode);

  void PaintDecorationLine(const TextDecorationInfo& decoration_info,
                           const Color& line_color,
                           const TextFragmentPaintInfo* fragment_paint_info);

  SvgTextPaintState& SetSvgState(const LayoutSVGInlineText&,
                                 const ComputedStyle&,
                                 StyleVariant style_variant,
                                 PaintFlags paint_flags);
  SvgTextPaintState& SetSvgState(const LayoutSVGInlineText& svg_inline_text,
                                 const ComputedStyle& style,
                                 Color text_match_color);
  SvgTextPaintState* GetSvgState();

  static Color TextColorForWhiteBackground(Color);

  static TextPaintStyle TextPaintingStyle(const Document&,
                                          const ComputedStyle&,
                                          const PaintInfo&);

  void SetEmphasisMark(const AtomicString&, LineLogicalSide);

 protected:
  const Font& font() const { return font_; }
  const LineRelativeOffset& text_origin() const { return text_origin_; }
  const AtomicString& emphasis_mark() const { return emphasis_mark_; }
  int emphasis_mark_offset() const { return emphasis_mark_offset_; }
  GraphicsContext& graphics_context() const { return graphics_context_; }

 private:
  void PaintSvgTextFragment(const TextFragmentPaintInfo&,
                            DOMNodeId node_id,
                            const AutoDarkMode& auto_dark_mode);

  virtual void ClipDecorationsStripe(const TextFragmentPaintInfo&,
                                     float upper,
                                     float stripe_width,
                                     float dilation);

  GraphicsContext& graphics_context_;
  const SvgContextPaints* svg_context_paints_;
  const Font& font_;
  const gfx::Rect visual_rect_;
  const LineRelativeOffset text_origin_;
  std::optional<SvgTextPaintState> svg_text_paint_state_;
  AtomicString emphasis_mark_;
  int emphasis_mark_offset_ = 0;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_PAINT_TEXT_PAINTER_H_