Flutter Engine
The Flutter Engine
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#include <unordered_map>
12
17
18namespace impeller {
19
20class FontGlyphAtlas;
21
22//------------------------------------------------------------------------------
23/// @brief A texture containing the bitmap representation of glyphs in
24/// different fonts along with the ability to query the location of
25/// specific font glyphs within the texture.
26///
28 public:
29 //----------------------------------------------------------------------------
30 /// @brief Describes how the glyphs are represented in the texture.
31 enum class Type {
32 //--------------------------------------------------------------------------
33 /// The glyphs are reprsented at their requested size using only an 8-bit
34 /// color channel.
35 ///
36 /// This might be backed by a grey or red single channel texture, depending
37 /// on the backend capabilities.
38 kAlphaBitmap,
39
40 //--------------------------------------------------------------------------
41 /// The glyphs are reprsented at their requested size using N32 premul
42 /// colors.
43 ///
44 kColorBitmap,
45 };
46
47 //----------------------------------------------------------------------------
48 /// @brief Create an empty glyph atlas.
49 ///
50 /// @param[in] type How the glyphs are represented in the texture.
51 ///
52 explicit GlyphAtlas(Type type);
53
55
56 bool IsValid() const;
57
58 //----------------------------------------------------------------------------
59 /// @brief Describes how the glyphs are represented in the texture.
60 ///
61 Type GetType() const;
62
63 //----------------------------------------------------------------------------
64 /// @brief Set the texture for the glyph atlas.
65 ///
66 /// @param[in] texture The texture
67 ///
68 void SetTexture(std::shared_ptr<Texture> texture);
69
70 //----------------------------------------------------------------------------
71 /// @brief Get the texture for the glyph atlas.
72 ///
73 /// @return The texture.
74 ///
75 const std::shared_ptr<Texture>& GetTexture() const;
76
77 //----------------------------------------------------------------------------
78 /// @brief Record the location of a specific font-glyph pair within the
79 /// atlas.
80 ///
81 /// @param[in] pair The font-glyph pair
82 /// @param[in] rect The position in the atlas
83 /// @param[in] bounds The bounds of the glyph at scale
84 ///
86 Rect position,
87 Rect bounds);
88
89 //----------------------------------------------------------------------------
90 /// @brief Get the number of unique font-glyph pairs in this atlas.
91 ///
92 /// @return The glyph count.
93 ///
94 size_t GetGlyphCount() const;
95
96 //----------------------------------------------------------------------------
97 /// @brief Iterate of all the glyphs along with their locations in the
98 /// atlas.
99 ///
100 /// @param[in] iterator The iterator. Return `false` from the iterator to
101 /// stop iterating.
102 ///
103 /// @return The number of glyphs iterated over.
104 ///
105 size_t IterateGlyphs(
106 const std::function<bool(const ScaledFont& scaled_font,
107 const SubpixelGlyph& glyph,
108 const Rect& rect)>& iterator) const;
109
110 //----------------------------------------------------------------------------
111 /// @brief Find the location of a specific font-glyph pair in the atlas.
112 ///
113 /// @param[in] pair The font-glyph pair
114 ///
115 /// @return The location of the font-glyph pair in the atlas.
116 /// `std::nullopt` if the pair is not in the atlas.
117 ///
118 std::optional<std::pair<Rect, Rect>> FindFontGlyphBounds(
119 const FontGlyphPair& pair) const;
120
121 //----------------------------------------------------------------------------
122 /// @brief Obtain an interface for querying the location of glyphs in the
123 /// atlas for the given font and scale. This provides a more
124 /// efficient way to look up a run of glyphs in the same font.
125 ///
126 /// @param[in] font The font
127 /// @param[in] scale The scale
128 ///
129 /// @return A pointer to a FontGlyphAtlas, or nullptr if the font and
130 /// scale are not available in the atlas. The pointer is only
131 /// valid for the lifetime of the GlyphAtlas.
132 ///
134
135 private:
136 const Type type_;
137 std::shared_ptr<Texture> texture_;
138
139 std::unordered_map<ScaledFont, FontGlyphAtlas> font_atlas_map_;
140
141 GlyphAtlas(const GlyphAtlas&) = delete;
142
143 GlyphAtlas& operator=(const GlyphAtlas&) = delete;
144};
145
146//------------------------------------------------------------------------------
147/// @brief A container for caching a glyph atlas across frames.
148///
150 public:
152
153 virtual ~GlyphAtlasContext();
154
155 //----------------------------------------------------------------------------
156 /// @brief Retrieve the current glyph atlas.
157 std::shared_ptr<GlyphAtlas> GetGlyphAtlas() const;
158
159 //----------------------------------------------------------------------------
160 /// @brief Retrieve the size of the current glyph atlas.
161 const ISize& GetAtlasSize() const;
162
163 //----------------------------------------------------------------------------
164 /// @brief Retrieve the previous (if any) rect packer.
165 std::shared_ptr<RectanglePacker> GetRectPacker() const;
166
167 //----------------------------------------------------------------------------
168 /// @brief A y-coordinate shift that must be applied to glyphs appended
169 /// to
170 /// the atlas.
171 ///
172 /// The rectangle packer is only initialized for unfilled regions
173 /// of the atlas. The area the rectangle packer covers is offset
174 /// from the origin by this height adjustment.
175 int64_t GetHeightAdjustment() const;
176
177 //----------------------------------------------------------------------------
178 /// @brief Update the context with a newly constructed glyph atlas.
179 void UpdateGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas,
180 ISize size,
181 int64_t height_adjustment_);
182
183 void UpdateRectPacker(std::shared_ptr<RectanglePacker> rect_packer);
184
185 private:
186 std::shared_ptr<GlyphAtlas> atlas_;
187 ISize atlas_size_;
188 std::shared_ptr<RectanglePacker> rect_packer_;
189 int64_t height_adjustment_;
190
191 GlyphAtlasContext(const GlyphAtlasContext&) = delete;
192
193 GlyphAtlasContext& operator=(const GlyphAtlasContext&) = delete;
194};
195
196//------------------------------------------------------------------------------
197/// @brief An object that can look up glyph locations within the GlyphAtlas
198/// for a particular typeface.
199///
201 public:
202 FontGlyphAtlas() = default;
203
204 //----------------------------------------------------------------------------
205 /// @brief Find the location of a glyph in the atlas.
206 ///
207 /// @param[in] glyph The glyph
208 ///
209 /// @return The location of the glyph in the atlas.
210 /// `std::nullopt` if the glyph is not in the atlas.
211 ///
212 std::optional<std::pair<Rect, Rect>> FindGlyphBounds(
213 const SubpixelGlyph& glyph) const;
214
215 private:
216 friend class GlyphAtlas;
217 std::unordered_map<SubpixelGlyph, std::pair<Rect, Rect>> positions_;
218
219 FontGlyphAtlas(const FontGlyphAtlas&) = delete;
220
221 FontGlyphAtlas& operator=(const FontGlyphAtlas&) = delete;
222};
223
224} // namespace impeller
225
226#endif // FLUTTER_IMPELLER_TYPOGRAPHER_GLYPH_ATLAS_H_
GLenum type
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
Definition: glyph_atlas.h:200
std::optional< std::pair< Rect, Rect > > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
Definition: glyph_atlas.cc:119
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
A container for caching a glyph atlas across frames.
Definition: glyph_atlas.h:149
GlyphAtlasContext(GlyphAtlas::Type type)
Definition: glyph_atlas.cc:12
std::shared_ptr< RectanglePacker > GetRectPacker() const
Retrieve the previous (if any) rect packer.
Definition: glyph_atlas.cc:29
void UpdateRectPacker(std::shared_ptr< RectanglePacker > rect_packer)
Definition: glyph_atlas.cc:41
std::shared_ptr< GlyphAtlas > GetGlyphAtlas() const
Retrieve the current glyph atlas.
Definition: glyph_atlas.cc:17
const ISize & GetAtlasSize() const
Retrieve the size of the current glyph atlas.
Definition: glyph_atlas.cc:21
int64_t GetHeightAdjustment() const
A y-coordinate shift that must be applied to glyphs appended to the atlas.
Definition: glyph_atlas.cc:25
void UpdateGlyphAtlas(std::shared_ptr< GlyphAtlas > atlas, ISize size, int64_t height_adjustment_)
Update the context with a newly constructed glyph atlas.
Definition: glyph_atlas.cc:33
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition: glyph_atlas.h:27
GlyphAtlas(Type type)
Create an empty glyph atlas.
Definition: glyph_atlas.cc:46
bool IsValid() const
Definition: glyph_atlas.cc:50
std::optional< std::pair< Rect, Rect > > FindFontGlyphBounds(const FontGlyphPair &pair) const
Find the location of a specific font-glyph pair in the atlas.
Definition: glyph_atlas.cc:73
void SetTexture(std::shared_ptr< Texture > texture)
Set the texture for the glyph atlas.
Definition: glyph_atlas.cc:62
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:31
Type GetType() const
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.cc:54
const std::shared_ptr< Texture > & GetTexture() const
Get the texture for the glyph atlas.
Definition: glyph_atlas.cc:58
const FontGlyphAtlas * GetFontGlyphAtlas(const Font &font, Scalar scale) const
Obtain an interface for querying the location of glyphs in the atlas for the given font and scale....
Definition: glyph_atlas.cc:82
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.
Definition: glyph_atlas.cc:98
void AddTypefaceGlyphPositionAndBounds(const FontGlyphPair &pair, Rect position, Rect bounds)
Record the location of a specific font-glyph pair within the atlas.
Definition: glyph_atlas.cc:66
size_t GetGlyphCount() const
Get the number of unique font-glyph pairs in this atlas.
Definition: glyph_atlas.cc:91
Dart_NativeFunction function
Definition: fuchsia.cc:51
FlTexture * texture
sk_sp< const SkImage > atlas
Definition: SkRecords.h:331
Optional< SkRect > bounds
Definition: SkRecords.h:189
sk_sp< SkBlender > blender SkRect rect
Definition: SkRecords.h:350
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
font
Font Metadata and Metrics.
float Scalar
Definition: scalar.h:18
const Scalar scale
A font along with a glyph in that font rendered at a particular scale and subpixel position.
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
A glyph and its subpixel position.