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