| 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"
11 #include "impeller/geometry/rational.h"
12 #include "impeller/typographer/glyph.h"
13 #include "impeller/typographer/glyph_atlas.h"
14 #include "impeller/typographer/text_run.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:
36 TextFrame();
37
38 TextFrame(std::vector<TextRun>& runs,
39 Rect bounds,
40 bool has_color,
41 const PathCreator& path_creator = {});
42
43 ~TextFrame();
44
45 static SubpixelPosition ComputeSubpixelPosition(
46 const TextRun::GlyphPosition& glyph_position,
47 AxisAlignment alignment,
48 const Matrix& transform);
49
50 static Rational RoundScaledFontSize(Scalar scale);
51 static Rational RoundScaledFontSize(Rational scale);
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.
86 GlyphAtlas::Type GetAtlasType() const;
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.
106 void SetPerFrameData(Rational scale,
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:
128 friend class TypographerContextSkia;
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_
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:74
impeller::LazyGlyphAtlas
Definition: lazy_glyph_atlas.h:16
impeller::Rational
Definition: rational.h:13
impeller::TextFrame
Represents a collection of shaped text runs.
Definition: text_frame.h:34
impeller::TextFrame::RoundScaledFontSize
static Rational RoundScaledFontSize(Scalar scale)
Definition: text_frame.cc:55
impeller::TextFrame::GetBounds
Rect GetBounds() const
The conservative bounding box for this text frame.
Definition: text_frame.cc:27
impeller::TextFrame::TextFrame
TextFrame()
impeller::TextFrame::IsFrameComplete
bool IsFrameComplete() const
Verifies that all glyphs in this text frame have computed bounds information.
Definition: text_frame.cc:156
impeller::TextFrame::GetAtlasType
GlyphAtlas::Type GetAtlasType() const
The type of atlas this run should be place in.
Definition: text_frame.cc:39
impeller::TextFrame::AsSingleGlyph
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
impeller::TextFrame::HasColor
bool HasColor() const
Returns the paint color this text frame was recorded with.
Definition: text_frame.cc:44
impeller::TextFrame::GetScale
Rational GetScale() const
Definition: text_frame.cc:129
impeller::TextFrame::GetFrameBounds
const FrameBounds & GetFrameBounds(size_t index) const
Retrieve the frame bounds for the glyph at [index].
Definition: text_frame.cc:175
impeller::TextFrame::GetTransform
const Matrix & GetTransform() const
Definition: text_frame.h:119
impeller::TextFrame::GetFont
const Font & GetFont() const
Return the font of the first glyph run.
Definition: text_frame.cc:164
impeller::TextFrame::GetAtlasGenerationAndID
std::pair< size_t, intptr_t > GetAtlasGenerationAndID() const
Definition: text_frame.cc:180
impeller::TextFrame::GetRunCount
size_t GetRunCount() const
The number of runs in this text frame.
Definition: text_frame.cc:31
impeller::TextFrame::GetOffset
Point GetOffset() const
Definition: text_frame.cc:133
impeller::TextFrame::SetPerFrameData
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
impeller::TextFrame::GetOffsetTransform
Matrix GetOffsetTransform() const
Definition: text_frame.cc:114
impeller::TextFrame::GetPath
fml::StatusOr< flutter::DlPath > GetPath() const
Definition: text_frame.cc:149
impeller::TextFrame::ComputeSubpixelPosition
static SubpixelPosition ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, const Matrix &transform)
Definition: text_frame.cc:94
impeller::TextFrame::GetRuns
const std::vector< TextRun > & GetRuns() const
Returns a reference to all the text runs in this frame.
Definition: text_frame.cc:35
impeller::TextFrame::~TextFrame
~TextFrame()
impeller::TypographerContextSkia
Definition: typographer_context_skia.h:12
value
int32_t value
Definition: dl_golden_unittests.cc:459
transform
Matrix transform
Definition: gaussian_blur_filter_contents.cc:214
glyph.h
glyph_atlas.h
flutter
Definition: dl_golden_blur_unittests.cc:16
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::AxisAlignment
AxisAlignment
Determines the axis along which there is subpixel positioning.
Definition: font.h:20
impeller::DlPath
flutter::DlPath DlPath
Definition: dl_dispatcher.h:29
impeller::PathCreator
std::function< fml::StatusOr< flutter::DlPath >()> PathCreator
Definition: text_frame.h:24
impeller::SubpixelPosition
SubpixelPosition
Definition: font_glyph_pair.h:61
rational.h
scale
const Scalar scale
Definition: stroke_path_geometry.cc:319
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:322
impeller::FrameBounds
Definition: glyph_atlas.h:55
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::TPoint< Scalar >
impeller::TRect< Scalar >
impeller::TextRun::GlyphPosition
Definition: text_run.h:22
text_run.h