Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
glyph_atlas.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_GLYPH_ATLAS_H_
6#define FLUTTER_IMPELLER_TYPOGRAPHER_GLYPH_ATLAS_H_
7
8#include <functional>
9#include <memory>
10#include <optional>
11
16#include "third_party/abseil-cpp/absl/container/flat_hash_map.h"
17
18namespace impeller {
19
20class FontGlyphAtlas;
21
23 /// The bounds of the glyph within the glyph atlas texture.
25 /// The glyph bounds within the local coordinate system.
27 /// Whether [atlas_bounds] are still a placeholder and have
28 /// not yet been computed.
29 bool is_placeholder = true;
30};
31
32//------------------------------------------------------------------------------
33/// @brief A texture containing the bitmap representation of glyphs in
34/// different fonts along with the ability to query the location of
35/// specific font glyphs within the texture.
36///
38 public:
39 //----------------------------------------------------------------------------
40 /// @brief Describes how the glyphs are represented in the texture.
41 enum class Type {
42 //--------------------------------------------------------------------------
43 /// The glyphs are reprsented at their requested size using only an 8-bit
44 /// color channel.
45 ///
46 /// This might be backed by a grey or red single channel texture, depending
47 /// on the backend capabilities.
49
50 //--------------------------------------------------------------------------
51 /// The glyphs are reprsented at their requested size using N32 premul
52 /// colors.
53 ///
55 };
56
57 //----------------------------------------------------------------------------
58 /// @brief Create an empty glyph atlas.
59 ///
60 /// @param[in] type How the glyphs are represented in the texture.
61 /// @param[in] initial_generation the atlas generation.
62 ///
63 GlyphAtlas(Type type, size_t initial_generation);
64
66
67 bool IsValid() const;
68
69 //----------------------------------------------------------------------------
70 /// @brief Describes how the glyphs are represented in the texture.
71 ///
72 Type GetType() const;
73
74 //----------------------------------------------------------------------------
75 /// @brief Set the texture for the glyph atlas.
76 ///
77 /// @param[in] texture The texture
78 ///
79 void SetTexture(std::shared_ptr<Texture> texture);
80
81 //----------------------------------------------------------------------------
82 /// @brief Get the texture for the glyph atlas.
83 ///
84 /// @return The texture.
85 ///
86 const std::shared_ptr<Texture>& GetTexture() const;
87
88 //----------------------------------------------------------------------------
89 /// @brief Record the location of a specific font-glyph pair within the
90 /// atlas.
91 ///
92 /// @param[in] pair The font-glyph pair
93 /// @param[in] rect The position in the atlas
94 /// @param[in] bounds The bounds of the glyph at scale
95 ///
97 Rect position,
98 Rect bounds);
99
100 //----------------------------------------------------------------------------
101 /// @brief Get the number of unique font-glyph pairs in this atlas.
102 ///
103 /// @return The glyph count.
104 ///
105 size_t GetGlyphCount() const;
106
107 //----------------------------------------------------------------------------
108 /// @brief Iterate of all the glyphs along with their locations in the
109 /// atlas.
110 ///
111 /// @param[in] iterator The iterator. Return `false` from the iterator to
112 /// stop iterating.
113 ///
114 /// @return The number of glyphs iterated over.
115 ///
116 size_t IterateGlyphs(
117 const std::function<bool(const ScaledFont& scaled_font,
118 const SubpixelGlyph& glyph,
119 const Rect& rect)>& iterator) const;
120
121 //----------------------------------------------------------------------------
122 /// @brief Find the location of a specific font-glyph pair in the atlas.
123 ///
124 /// @param[in] pair The font-glyph pair
125 ///
126 /// @return The location of the font-glyph pair in the atlas.
127 /// `std::nullopt` if the pair is not in the atlas.
128 ///
129 std::optional<FrameBounds> FindFontGlyphBounds(
130 const FontGlyphPair& pair) const;
131
132 //----------------------------------------------------------------------------
133 /// @brief Obtain an interface for querying the location of glyphs in the
134 /// atlas for the given font and scale. This provides a more
135 /// efficient way to look up a run of glyphs in the same font.
136 ///
137 /// @param[in] font The font
138 /// @param[in] scale The scale
139 ///
140 /// @return A pointer to a FontGlyphAtlas, which may be an empty atlas
141 /// if the font and scale are not yet available in the atlas.
142 /// The pointer is only valid for the lifetime of the GlyphAtlas.
143 ///
145
146 //----------------------------------------------------------------------------
147 /// @brief Obtain an interface for querying the location of glyphs in the
148 /// atlas for the given font and scale if such a font atlas
149 /// already exists. This provides a more efficient way to look
150 /// up an existing run of glyphs in the same font.
151 ///
152 /// @param[in] font The font
153 /// @param[in] scale The scale
154 ///
155 /// @return A pointer to a FontGlyphAtlas, or nullptr if the font and
156 /// scale are not available in the atlas. The pointer is only
157 /// valid for the lifetime of the GlyphAtlas.
158 ///
159 const FontGlyphAtlas* GetFontGlyphAtlas(const ScaledFont& scaled_font) const;
160
161 //----------------------------------------------------------------------------
162 /// @brief Retrieve the generation id for this glyph atlas.
163 ///
164 /// The generation id is used to match with a TextFrame to
165 /// determine if the frame is guaranteed to already be populated
166 /// in the atlas.
167 size_t GetAtlasGeneration() const;
168
169 //----------------------------------------------------------------------------
170 /// @brief Update the atlas generation.
171 void SetAtlasGeneration(size_t value);
172
173 private:
174 const Type type_;
175 std::shared_ptr<Texture> texture_;
176 size_t generation_ = 0;
177
178 using FontAtlasMap = absl::flat_hash_map<ScaledFont,
180 absl::Hash<ScaledFont>,
182
183 FontAtlasMap font_atlas_map_;
184
185 GlyphAtlas(const GlyphAtlas&) = delete;
186
187 GlyphAtlas& operator=(const GlyphAtlas&) = delete;
188};
189
190//------------------------------------------------------------------------------
191/// @brief A container for caching a glyph atlas across frames.
192///
194 public:
196
197 virtual ~GlyphAtlasContext();
198
199 //----------------------------------------------------------------------------
200 /// @brief Retrieve the current glyph atlas.
201 std::shared_ptr<GlyphAtlas> GetGlyphAtlas() const;
202
203 //----------------------------------------------------------------------------
204 /// @brief Retrieve the size of the current glyph atlas.
205 const ISize& GetAtlasSize() const;
206
207 //----------------------------------------------------------------------------
208 /// @brief Retrieve the previous (if any) rect packer.
209 std::shared_ptr<RectanglePacker> GetRectPacker() const;
210
211 //----------------------------------------------------------------------------
212 /// @brief A y-coordinate shift that must be applied to glyphs appended
213 /// to
214 /// the atlas.
215 ///
216 /// The rectangle packer is only initialized for unfilled regions
217 /// of the atlas. The area the rectangle packer covers is offset
218 /// from the origin by this height adjustment.
219 int64_t GetHeightAdjustment() const;
220
221 //----------------------------------------------------------------------------
222 /// @brief Update the context with a newly constructed glyph atlas.
223 void UpdateGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas,
224 ISize size,
225 int64_t height_adjustment_);
226
227 void UpdateRectPacker(std::shared_ptr<RectanglePacker> rect_packer);
228
229 private:
230 std::shared_ptr<GlyphAtlas> atlas_;
231 ISize atlas_size_;
232 std::shared_ptr<RectanglePacker> rect_packer_;
233 int64_t height_adjustment_;
234
235 GlyphAtlasContext(const GlyphAtlasContext&) = delete;
236
237 GlyphAtlasContext& operator=(const GlyphAtlasContext&) = delete;
238};
239
240//------------------------------------------------------------------------------
241/// @brief An object that can look up glyph locations within the GlyphAtlas
242/// for a particular typeface.
243///
245 public:
246 FontGlyphAtlas() = default;
248
249 //----------------------------------------------------------------------------
250 /// @brief Find the location of a glyph in the atlas.
251 ///
252 /// @param[in] glyph The glyph
253 ///
254 /// @return The location of the glyph in the atlas.
255 /// `std::nullopt` if the glyph is not in the atlas.
256 ///
257 std::optional<FrameBounds> FindGlyphBounds(const SubpixelGlyph& glyph) const;
258
259 //----------------------------------------------------------------------------
260 /// @brief Append the frame bounds of a glyph to this atlas.
261 ///
262 /// This may indicate a placeholder glyph location to be replaced
263 /// at a later time, as indicated by FrameBounds.placeholder.
264 void AppendGlyph(const SubpixelGlyph& glyph, const FrameBounds& frame_bounds);
265
266 private:
267 friend class GlyphAtlas;
268
269 using PositionsMap = absl::flat_hash_map<SubpixelGlyph,
271 absl::Hash<SubpixelGlyph>,
273
274 PositionsMap positions_;
275 FontGlyphAtlas(const FontGlyphAtlas&) = delete;
276};
277
278} // namespace impeller
279
280#endif // FLUTTER_IMPELLER_TYPOGRAPHER_GLYPH_ATLAS_H_
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
void AppendGlyph(const SubpixelGlyph &glyph, const FrameBounds &frame_bounds)
Append the frame bounds of a glyph to this atlas.
std::optional< FrameBounds > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
FontGlyphAtlas(FontGlyphAtlas &&)=default
A container for caching a glyph atlas across frames.
std::shared_ptr< RectanglePacker > GetRectPacker() const
Retrieve the previous (if any) rect packer.
void UpdateRectPacker(std::shared_ptr< RectanglePacker > rect_packer)
std::shared_ptr< GlyphAtlas > GetGlyphAtlas() const
Retrieve the current glyph atlas.
const ISize & GetAtlasSize() const
Retrieve the size of the current glyph atlas.
int64_t GetHeightAdjustment() const
A y-coordinate shift that must be applied to glyphs appended to the atlas.
void UpdateGlyphAtlas(std::shared_ptr< GlyphAtlas > atlas, ISize size, int64_t height_adjustment_)
Update the context with a newly constructed glyph atlas.
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition glyph_atlas.h:37
std::optional< FrameBounds > FindFontGlyphBounds(const FontGlyphPair &pair) const
Find the location of a specific font-glyph pair in the atlas.
bool IsValid() const
void SetTexture(std::shared_ptr< Texture > texture)
Set the texture for the glyph atlas.
void SetAtlasGeneration(size_t value)
Update the atlas generation.
FontGlyphAtlas * GetOrCreateFontGlyphAtlas(const ScaledFont &scaled_font)
Obtain an interface for querying the location of glyphs in the atlas for the given font and scale....
Type
Describes how the glyphs are represented in the texture.
Definition glyph_atlas.h:41
size_t GetAtlasGeneration() const
Retrieve the generation id for this glyph atlas.
Type GetType() const
Describes how the glyphs are represented in the texture.
const std::shared_ptr< Texture > & GetTexture() const
Get the texture for the glyph atlas.
const FontGlyphAtlas * GetFontGlyphAtlas(const ScaledFont &scaled_font) const
Obtain an interface for querying the location of glyphs in the atlas for the given font and scale if ...
size_t IterateGlyphs(const std::function< bool(const ScaledFont &scaled_font, const SubpixelGlyph &glyph, const Rect &rect)> &iterator) const
Iterate of all the glyphs along with their locations in the atlas.
void AddTypefaceGlyphPositionAndBounds(const FontGlyphPair &pair, Rect position, Rect bounds)
Record the location of a specific font-glyph pair within the atlas.
size_t GetGlyphCount() const
Get the number of unique font-glyph pairs in this atlas.
FlTexture * texture
impeller::ShaderType type
A font along with a glyph in that font rendered at a particular scale and subpixel position.
Rect atlas_bounds
The bounds of the glyph within the glyph atlas texture.
Definition glyph_atlas.h:24
Rect glyph_bounds
The glyph bounds within the local coordinate system.
Definition glyph_atlas.h:26
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
A glyph and its subpixel position.