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>
9 
10 #include "fml/status_or.h"
15 
16 // TODO(https://github.com/flutter/flutter/issues/166593): This is required to
17 // break a cyclical dependency between display list, impeller, and typographer.
18 namespace flutter {
19 class DlPath;
20 }
21 
22 namespace impeller {
23 
24 using PathCreator = std::function<fml::StatusOr<flutter::DlPath>()>;
25 
26 //------------------------------------------------------------------------------
27 /// @brief Represents a collection of shaped text runs.
28 ///
29 /// This object is typically the entrypoint in the Impeller type
30 /// rendering subsystem.
31 ///
32 /// A text frame should not be reused in multiple places within a single frame,
33 /// as internally it is used as a cache for various glyph properties.
34 class TextFrame {
35  public:
37 
38  TextFrame(std::vector<TextRun>& runs,
39  Rect bounds,
40  bool has_color,
41  const PathCreator& path_creator = {});
42 
44 
46  const TextRun::GlyphPosition& glyph_position,
47  AxisAlignment alignment,
48  const Matrix& transform);
49 
52 
53  //----------------------------------------------------------------------------
54  /// @brief The conservative bounding box for this text frame.
55  ///
56  /// @return The bounds rectangle. If there are no glyphs in this text
57  /// frame and empty Rectangle is returned instead.
58  ///
59  Rect GetBounds() const;
60 
61  //----------------------------------------------------------------------------
62  /// @brief The number of runs in this text frame.
63  ///
64  /// @return The run count.
65  ///
66  size_t GetRunCount() const;
67 
68  //----------------------------------------------------------------------------
69  /// @brief Returns a reference to all the text runs in this frame.
70  ///
71  /// @return The runs in this frame.
72  ///
73  const std::vector<TextRun>& GetRuns() const;
74 
75  //----------------------------------------------------------------------------
76  /// @brief Returns the paint color this text frame was recorded with.
77  ///
78  /// Non-bitmap/COLR fonts always use a black text color here, but
79  /// COLR fonts can potentially use the paint color in the glyph
80  /// atlas, so this color must be considered as part of the cache
81  /// key.
82  bool HasColor() const;
83 
84  //----------------------------------------------------------------------------
85  /// @brief The type of atlas this run should be place in.
87 
88  /// @brief Verifies that all glyphs in this text frame have computed bounds
89  /// information.
90  bool IsFrameComplete() const;
91 
92  /// @brief Retrieve the frame bounds for the glyph at [index].
93  ///
94  /// This method is only valid if [IsFrameComplete] returns true.
95  const FrameBounds& GetFrameBounds(size_t index) const;
96 
97  /// @brief If this text frame contains a single glyph (such as for an Icon),
98  /// then return it, otherwise std::nullopt.
99  std::optional<Glyph> AsSingleGlyph() const;
100 
101  /// @brief Return the font of the first glyph run.
102  const Font& GetFont() const;
103 
104  /// @brief Store text frame scale, offset, and properties for hashing in th
105  /// glyph atlas.
107  Point offset,
108  const Matrix& transform,
109  std::optional<GlyphProperties> properties);
110 
111  // A generation id for the glyph atlas this text run was associated
112  // with. As long as the frame generation matches the atlas generation,
113  // the contents are guaranteed to be populated and do not need to be
114  // processed.
115  std::pair<size_t, intptr_t> GetAtlasGenerationAndID() const;
116 
117  Rational GetScale() const;
118 
119  const Matrix& GetTransform() const { return transform_; }
120 
121  fml::StatusOr<flutter::DlPath> GetPath() const;
122 
123  Point GetOffset() const;
124 
125  Matrix GetOffsetTransform() const;
126 
127  private:
129  friend class LazyGlyphAtlas;
130 
131  std::optional<GlyphProperties> GetProperties() const;
132 
133  void AppendFrameBounds(const FrameBounds& frame_bounds);
134 
135  void ClearFrameBounds();
136 
137  void SetAtlasGeneration(size_t value, intptr_t atlas_id);
138 
139  std::vector<TextRun> runs_;
140  Rect bounds_;
141  bool has_color_;
142  const PathCreator path_creator_;
143 
144  // Data that is cached when rendering the text frame and is only
145  // valid for the current atlas generation.
146  std::vector<FrameBounds> bound_values_;
147  Rational scale_ = Rational(0, 1);
148  size_t generation_ = 0;
149  intptr_t atlas_id_ = 0;
150  Point offset_;
151  std::optional<GlyphProperties> properties_;
152  Matrix transform_;
153 };
154 
155 } // namespace impeller
156 
157 #endif // FLUTTER_IMPELLER_TYPOGRAPHER_TEXT_FRAME_H_
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
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:34
static Rational RoundScaledFontSize(Scalar scale)
Definition: text_frame.cc:55
Rect GetBounds() const
The conservative bounding box for this text frame.
Definition: text_frame.cc:27
bool IsFrameComplete() const
Verifies that all glyphs in this text frame have computed bounds information.
Definition: text_frame.cc:156
GlyphAtlas::Type GetAtlasType() const
The type of atlas this run should be place in.
Definition: text_frame.cc:39
std::optional< Glyph > AsSingleGlyph() const
If this text frame contains a single glyph (such as for an Icon), then return it, otherwise std::null...
Definition: text_frame.cc:168
bool HasColor() const
Returns the paint color this text frame was recorded with.
Definition: text_frame.cc:44
Rational GetScale() const
Definition: text_frame.cc:129
const FrameBounds & GetFrameBounds(size_t index) const
Retrieve the frame bounds for the glyph at [index].
Definition: text_frame.cc:175
const Matrix & GetTransform() const
Definition: text_frame.h:119
const Font & GetFont() const
Return the font of the first glyph run.
Definition: text_frame.cc:164
std::pair< size_t, intptr_t > GetAtlasGenerationAndID() const
Definition: text_frame.cc:180
size_t GetRunCount() const
The number of runs in this text frame.
Definition: text_frame.cc:31
Point GetOffset() const
Definition: text_frame.cc:133
void SetPerFrameData(Rational 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:118
Matrix GetOffsetTransform() const
Definition: text_frame.cc:114
fml::StatusOr< flutter::DlPath > GetPath() const
Definition: text_frame.cc:149
static SubpixelPosition ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, const Matrix &transform)
Definition: text_frame.cc:94
const std::vector< TextRun > & GetRuns() const
Returns a reference to all the text runs in this frame.
Definition: text_frame.cc:35
int32_t value
float Scalar
Definition: scalar.h:18
AxisAlignment
Determines the axis along which there is subpixel positioning.
Definition: font.h:20
flutter::DlPath DlPath
Definition: dl_dispatcher.h:29
std::function< fml::StatusOr< flutter::DlPath >()> PathCreator
Definition: text_frame.h:24
const Scalar scale
SeparatedVector2 offset
A 4x4 matrix using column-major storage.
Definition: matrix.h:37