Flutter Impeller
text_frame.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_TYPOGRAPHER_TEXT_FRAME_H_
6 #define FLUTTER_IMPELLER_TYPOGRAPHER_TEXT_FRAME_H_
7 
8 #include <cstdint>
11 
12 namespace impeller {
13 
14 //------------------------------------------------------------------------------
15 /// @brief Represents a collection of shaped text runs.
16 ///
17 /// This object is typically the entrypoint in the Impeller type
18 /// rendering subsystem.
19 ///
20 /// A text frame should not be reused in multiple places within a single frame,
21 /// as internally it is used as a cache for various glyph properties.
22 class TextFrame {
23  public:
25 
26  TextFrame(std::vector<TextRun>& runs, Rect bounds, bool has_color);
27 
29 
31  const TextRun::GlyphPosition& glyph_position,
32  AxisAlignment alignment,
33  const Matrix& transform);
34 
36 
37  //----------------------------------------------------------------------------
38  /// @brief The conservative bounding box for this text frame.
39  ///
40  /// @return The bounds rectangle. If there are no glyphs in this text
41  /// frame and empty Rectangle is returned instead.
42  ///
43  Rect GetBounds() const;
44 
45  //----------------------------------------------------------------------------
46  /// @brief The number of runs in this text frame.
47  ///
48  /// @return The run count.
49  ///
50  size_t GetRunCount() const;
51 
52  //----------------------------------------------------------------------------
53  /// @brief Returns a reference to all the text runs in this frame.
54  ///
55  /// @return The runs in this frame.
56  ///
57  const std::vector<TextRun>& GetRuns() const;
58 
59  //----------------------------------------------------------------------------
60  /// @brief Returns the paint color this text frame was recorded with.
61  ///
62  /// Non-bitmap/COLR fonts always use a black text color here, but
63  /// COLR fonts can potentially use the paint color in the glyph
64  /// atlas, so this color must be considered as part of the cache
65  /// key.
66  bool HasColor() const;
67 
68  //----------------------------------------------------------------------------
69  /// @brief The type of atlas this run should be place in.
71 
72  /// @brief Verifies that all glyphs in this text frame have computed bounds
73  /// information.
74  bool IsFrameComplete() const;
75 
76  /// @brief Retrieve the frame bounds for the glyph at [index].
77  ///
78  /// This method is only valid if [IsFrameComplete] returns true.
79  const FrameBounds& GetFrameBounds(size_t index) const;
80 
81  /// @brief Store text frame scale, offset, and properties for hashing in th
82  /// glyph atlas.
84  Point offset,
85  const Matrix& transform,
86  std::optional<GlyphProperties> properties);
87 
88  // A generation id for the glyph atlas this text run was associated
89  // with. As long as the frame generation matches the atlas generation,
90  // the contents are guaranteed to be populated and do not need to be
91  // processed.
92  std::pair<size_t, intptr_t> GetAtlasGenerationAndID() const;
93 
94  Scalar GetScale() const;
95 
96  TextFrame& operator=(TextFrame&& other) = default;
97 
98  TextFrame(const TextFrame& other) = default;
99 
100  const Matrix& GetTransform() const { return transform_; }
101 
102  Point GetOffset() const;
103 
104  Matrix GetOffsetTransform() const;
105 
106  private:
108  friend class LazyGlyphAtlas;
109 
110  std::optional<GlyphProperties> GetProperties() const;
111 
112  void AppendFrameBounds(const FrameBounds& frame_bounds);
113 
114  void ClearFrameBounds();
115 
116  void SetAtlasGeneration(size_t value, intptr_t atlas_id);
117 
118  std::vector<TextRun> runs_;
119  Rect bounds_;
120  bool has_color_;
121 
122  // Data that is cached when rendering the text frame and is only
123  // valid for the current atlas generation.
124  std::vector<FrameBounds> bound_values_;
125  Scalar scale_ = 0;
126  size_t generation_ = 0;
127  intptr_t atlas_id_ = 0;
128  Point offset_;
129  std::optional<GlyphProperties> properties_;
130  Matrix transform_;
131 };
132 
133 } // namespace impeller
134 
135 #endif // FLUTTER_IMPELLER_TYPOGRAPHER_TEXT_FRAME_H_
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:74
Represents a collection of shaped text runs.
Definition: text_frame.h:22
void SetPerFrameData(Scalar scale, Point offset, const Matrix &transform, std::optional< GlyphProperties > properties)
Store text frame scale, offset, and properties for hashing in th glyph atlas.
Definition: text_frame.cc:91
Rect GetBounds() const
The conservative bounding box for this text frame.
Definition: text_frame.cc:19
bool IsFrameComplete() const
Verifies that all glyphs in this text frame have computed bounds information.
Definition: text_frame.cc:122
Scalar GetScale() const
Definition: text_frame.cc:102
GlyphAtlas::Type GetAtlasType() const
The type of atlas this run should be place in.
Definition: text_frame.cc:31
bool HasColor() const
Returns the paint color this text frame was recorded with.
Definition: text_frame.cc:36
const FrameBounds & GetFrameBounds(size_t index) const
Retrieve the frame bounds for the glyph at [index].
Definition: text_frame.cc:130
const Matrix & GetTransform() const
Definition: text_frame.h:100
std::pair< size_t, intptr_t > GetAtlasGenerationAndID() const
Definition: text_frame.cc:135
static Point ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, const Matrix &transform)
Definition: text_frame.cc:69
TextFrame(const TextFrame &other)=default
size_t GetRunCount() const
The number of runs in this text frame.
Definition: text_frame.cc:23
Point GetOffset() const
Definition: text_frame.cc:106
Matrix GetOffsetTransform() const
Definition: text_frame.cc:87
TextFrame & operator=(TextFrame &&other)=default
static Scalar RoundScaledFontSize(Scalar scale)
Definition: text_frame.cc:41
const std::vector< TextRun > & GetRuns() const
Returns a reference to all the text runs in this frame.
Definition: text_frame.cc:27
int32_t value
float Scalar
Definition: scalar.h:18
AxisAlignment
Determines the axis along which there is subpixel positioning.
Definition: font.h:20
const Scalar scale
SeparatedVector2 offset
A 4x4 matrix using column-major storage.
Definition: matrix.h:37