| Flutter Impeller |

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
13 #include "impeller/core/texture.h"
14 #include "impeller/geometry/rect.h"
15 #include "impeller/typographer/font_glyph_pair.h"
16 #include "impeller/typographer/rectangle_packer.h"
17
18 namespace impeller {
19
20 class 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 ///
27 class GlyphAtlas {
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
54 ~GlyphAtlas();
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 ///
85 void AddTypefaceGlyphPositionAndBounds(const FontGlyphPair& pair,
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 ///
133 const FontGlyphAtlas* GetFontGlyphAtlas(const Font& font, Scalar scale) const;
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 ///
149 class GlyphAtlasContext {
150 public:
151 explicit GlyphAtlasContext(GlyphAtlas::Type type);
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 ///
200 class FontGlyphAtlas {
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_
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::GlyphAtlas::GetGlyphCount
size_t GetGlyphCount() const
Get the number of unique font-glyph pairs in this atlas.
Definition: glyph_atlas.cc:91
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::GlyphAtlasContext::GetRectPacker
std::shared_ptr< RectanglePacker > GetRectPacker() const
Retrieve the previous (if any) rect packer.
Definition: glyph_atlas.cc:29
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::GlyphAtlasContext::UpdateGlyphAtlas
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
impeller::GlyphAtlasContext::GetGlyphAtlas
std::shared_ptr< GlyphAtlas > GetGlyphAtlas() const
Retrieve the current glyph atlas.
Definition: glyph_atlas.cc:17
impeller::GlyphAtlas::GetFontGlyphAtlas
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
impeller::FontGlyphAtlas::FontGlyphAtlas
FontGlyphAtlas()=default
impeller::FontGlyphAtlas
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
Definition: glyph_atlas.h:200
impeller::GlyphAtlas::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Set the texture for the glyph atlas.
Definition: glyph_atlas.cc:62
impeller::SubpixelGlyph
A glyph and its subpixel position.
Definition: font_glyph_pair.h:40
impeller::GlyphAtlasContext::~GlyphAtlasContext
virtual ~GlyphAtlasContext()
Definition: glyph_atlas.cc:15
impeller::GlyphAtlasContext::GetHeightAdjustment
int64_t GetHeightAdjustment() const
A y-coordinate shift that must be applied to glyphs appended to the atlas.
Definition: glyph_atlas.cc:25
impeller::GlyphAtlas::AddTypefaceGlyphPositionAndBounds
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
impeller::GlyphAtlas::GlyphAtlas
GlyphAtlas(Type type)
Create an empty glyph atlas.
Definition: glyph_atlas.cc:46
impeller::TSize
Definition: size.h:19
impeller::GlyphAtlasContext::GetAtlasSize
const ISize & GetAtlasSize() const
Retrieve the size of the current glyph atlas.
Definition: glyph_atlas.cc:21
rectangle_packer.h
impeller::GlyphAtlas::GetType
Type GetType() const
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.cc:54
impeller::GlyphAtlas::~GlyphAtlas
~GlyphAtlas()
impeller::GlyphAtlas::GetTexture
const std::shared_ptr< Texture > & GetTexture() const
Get the texture for the glyph atlas.
Definition: glyph_atlas.cc:58
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:31
font_glyph_pair.h
type
GLenum type
Definition: blit_command_gles.cc:126
impeller::GlyphAtlasContext
A container for caching a glyph atlas across frames.
Definition: glyph_atlas.h:149
impeller::FontGlyphPair
A font along with a glyph in that font rendered at a particular scale and subpixel position.
Definition: font_glyph_pair.h:60
impeller::GlyphAtlas
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition: glyph_atlas.h:27
impeller::FontGlyphAtlas::FindGlyphBounds
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
rect.h
impeller::GlyphAtlas::FindFontGlyphBounds
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
texture.h
scale
const Scalar scale
Definition: stroke_path_geometry.cc:308
impeller::GlyphAtlas::IsValid
bool IsValid() const
Definition: glyph_atlas.cc:50
impeller
Definition: aiks_blend_unittests.cc:18
impeller::GlyphAtlasContext::GlyphAtlasContext
GlyphAtlasContext(GlyphAtlas::Type type)
Definition: glyph_atlas.cc:12
impeller::TRect< Scalar >
impeller::GlyphAtlas::IterateGlyphs
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
impeller::ScaledFont
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
Definition: font_glyph_pair.h:32
impeller::GlyphAtlasContext::UpdateRectPacker
void UpdateRectPacker(std::shared_ptr< RectanglePacker > rect_packer)
Definition: glyph_atlas.cc:41