| 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 "flutter/fml/macros.h"
14 #include "impeller/core/texture.h"
15 #include "impeller/geometry/rect.h"
16 #include "impeller/renderer/pipeline.h"
17 #include "impeller/typographer/font_glyph_pair.h"
18 #include "impeller/typographer/rectangle_packer.h"
19
20 namespace impeller {
21
22 class FontGlyphAtlas;
23
24 //------------------------------------------------------------------------------
25 /// @brief A texture containing the bitmap representation of glyphs in
26 /// different fonts along with the ability to query the location of
27 /// specific font glyphs within the texture.
28 ///
29 class GlyphAtlas {
30 public:
31 //----------------------------------------------------------------------------
32 /// @brief Describes how the glyphs are represented in the texture.
33 enum class Type {
34 //--------------------------------------------------------------------------
35 /// The glyphs are reprsented at their requested size using only an 8-bit
36 /// alpha channel.
37 ///
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 rectangle
83 ///
84 void AddTypefaceGlyphPosition(const FontGlyphPair& pair, Rect rect);
85
86 //----------------------------------------------------------------------------
87 /// @brief Get the number of unique font-glyph pairs in this atlas.
88 ///
89 /// @return The glyph count.
90 ///
91 size_t GetGlyphCount() const;
92
93 //----------------------------------------------------------------------------
94 /// @brief Iterate of all the glyphs along with their locations in the
95 /// atlas.
96 ///
97 /// @param[in] iterator The iterator. Return `false` from the iterator to
98 /// stop iterating.
99 ///
100 /// @return The number of glyphs iterated over.
101 ///
102 size_t IterateGlyphs(
103 const std::function<bool(const ScaledFont& scaled_font,
104 const Glyph& glyph,
105 const Rect& rect)>& iterator) const;
106
107 //----------------------------------------------------------------------------
108 /// @brief Find the location of a specific font-glyph pair in the atlas.
109 ///
110 /// @param[in] pair The font-glyph pair
111 ///
112 /// @return The location of the font-glyph pair in the atlas.
113 /// `std::nullopt` if the pair is not in the atlas.
114 ///
115 std::optional<Rect> FindFontGlyphBounds(const FontGlyphPair& pair) const;
116
117 //----------------------------------------------------------------------------
118 /// @brief Obtain an interface for querying the location of glyphs in the
119 /// atlas for the given font and scale. This provides a more
120 /// efficient way to look up a run of glyphs in the same font.
121 ///
122 /// @param[in] font The font
123 /// @param[in] scale The scale
124 ///
125 /// @return A pointer to a FontGlyphAtlas, or nullptr if the font and
126 /// scale are not available in the atlas. The pointer is only
127 /// valid for the lifetime of the GlyphAtlas.
128 ///
129 const FontGlyphAtlas* GetFontGlyphAtlas(const Font& font, Scalar scale) const;
130
131 private:
132 const Type type_;
133 std::shared_ptr<Texture> texture_;
134
135 std::unordered_map<ScaledFont, FontGlyphAtlas> font_atlas_map_;
136
137 GlyphAtlas(const GlyphAtlas&) = delete;
138
139 GlyphAtlas& operator=(const GlyphAtlas&) = delete;
140 };
141
142 //------------------------------------------------------------------------------
143 /// @brief A container for caching a glyph atlas across frames.
144 ///
145 class GlyphAtlasContext {
146 public:
147 virtual ~GlyphAtlasContext();
148
149 //----------------------------------------------------------------------------
150 /// @brief Retrieve the current glyph atlas.
151 std::shared_ptr<GlyphAtlas> GetGlyphAtlas() const;
152
153 //----------------------------------------------------------------------------
154 /// @brief Retrieve the size of the current glyph atlas.
155 const ISize& GetAtlasSize() const;
156
157 //----------------------------------------------------------------------------
158 /// @brief Retrieve the previous (if any) rect packer.
159 std::shared_ptr<RectanglePacker> GetRectPacker() const;
160
161 //----------------------------------------------------------------------------
162 /// @brief Update the context with a newly constructed glyph atlas.
163 void UpdateGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas, ISize size);
164
165 void UpdateRectPacker(std::shared_ptr<RectanglePacker> rect_packer);
166
167 protected:
168 GlyphAtlasContext();
169
170 private:
171 std::shared_ptr<GlyphAtlas> atlas_;
172 ISize atlas_size_;
173 std::shared_ptr<RectanglePacker> rect_packer_;
174
175 GlyphAtlasContext(const GlyphAtlasContext&) = delete;
176
177 GlyphAtlasContext& operator=(const GlyphAtlasContext&) = delete;
178 };
179
180 //------------------------------------------------------------------------------
181 /// @brief An object that can look up glyph locations within the GlyphAtlas
182 /// for a particular typeface.
183 ///
184 class FontGlyphAtlas {
185 public:
186 FontGlyphAtlas() = default;
187
188 //----------------------------------------------------------------------------
189 /// @brief Find the location of a glyph in the atlas.
190 ///
191 /// @param[in] glyph The glyph
192 ///
193 /// @return The location of the glyph in the atlas.
194 /// `std::nullopt` if the glyph is not in the atlas.
195 ///
196 std::optional<Rect> FindGlyphBounds(const Glyph& glyph) const;
197
198 private:
199 friend class GlyphAtlas;
200 std::unordered_map<Glyph, Rect> positions_;
201
202 FontGlyphAtlas(const FontGlyphAtlas&) = delete;
203
204 FontGlyphAtlas& operator=(const FontGlyphAtlas&) = delete;
205 };
206
207 } // namespace impeller
208
209 #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:84
pipeline.h
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:26
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:22
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::GlyphAtlas::FindFontGlyphBounds
std::optional< Rect > FindFontGlyphBounds(const FontGlyphPair &pair) const
Find the location of a specific font-glyph pair in the atlas.
Definition: glyph_atlas.cc:66
impeller::GlyphAtlasContext::GetGlyphAtlas
std::shared_ptr< GlyphAtlas > GetGlyphAtlas() const
Retrieve the current glyph atlas.
Definition: glyph_atlas.cc:18
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:75
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:184
impeller::GlyphAtlas::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Set the texture for the glyph atlas.
Definition: glyph_atlas.cc:57
impeller::GlyphAtlasContext::GlyphAtlasContext
GlyphAtlasContext()
Definition: glyph_atlas.cc:12
impeller::GlyphAtlasContext::~GlyphAtlasContext
virtual ~GlyphAtlasContext()
Definition: glyph_atlas.cc:16
impeller::GlyphAtlas::IterateGlyphs
size_t IterateGlyphs(const std::function< bool(const ScaledFont &scaled_font, const Glyph &glyph, const Rect &rect)> &iterator) const
Iterate of all the glyphs along with their locations in the atlas.
Definition: glyph_atlas.cc:91
impeller::GlyphAtlas::GlyphAtlas
GlyphAtlas(Type type)
Create an empty glyph atlas.
Definition: glyph_atlas.cc:41
impeller::Glyph
The glyph index in the typeface.
Definition: glyph.h:20
impeller::TSize< int64_t >
impeller::GlyphAtlasContext::GetAtlasSize
const ISize & GetAtlasSize() const
Retrieve the size of the current glyph atlas.
Definition: glyph_atlas.cc:22
rectangle_packer.h
impeller::GlyphAtlas::GetType
Type GetType() const
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.cc:49
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:53
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:33
font_glyph_pair.h
impeller::GlyphAtlas::AddTypefaceGlyphPosition
void AddTypefaceGlyphPosition(const FontGlyphPair &pair, Rect rect)
Record the location of a specific font-glyph pair within the atlas.
Definition: glyph_atlas.cc:61
impeller::GlyphAtlasContext
A container for caching a glyph atlas across frames.
Definition: glyph_atlas.h:145
impeller::FontGlyphPair
A font along with a glyph in that font rendered at a particular scale.
Definition: font_glyph_pair.h:35
impeller::GlyphAtlas
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition: glyph_atlas.h:29
rect.h
texture.h
impeller::FontGlyphAtlas::FindGlyphBounds
std::optional< Rect > FindGlyphBounds(const Glyph &glyph) const
Find the location of a glyph in the atlas.
Definition: glyph_atlas.cc:111
impeller::GlyphAtlas::IsValid
bool IsValid() const
Definition: glyph_atlas.cc:45
impeller
Definition: aiks_context.cc:10
impeller::TRect
Definition: rect.h:22
impeller::ScaledFont
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
Definition: font_glyph_pair.h:24
impeller::GlyphAtlasContext::UpdateGlyphAtlas
void UpdateGlyphAtlas(std::shared_ptr< GlyphAtlas > atlas, ISize size)
Update the context with a newly constructed glyph atlas.
Definition: glyph_atlas.cc:30
impeller::GlyphAtlasContext::UpdateRectPacker
void UpdateRectPacker(std::shared_ptr< RectanglePacker > rect_packer)
Definition: glyph_atlas.cc:36