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"
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.
39 
40  //--------------------------------------------------------------------------
41  /// The glyphs are reprsented at their requested size using N32 premul
42  /// colors.
43  ///
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  ///
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,
143  font_atlas_map_;
144 
145  GlyphAtlas(const GlyphAtlas&) = delete;
146 
147  GlyphAtlas& operator=(const GlyphAtlas&) = delete;
148 };
149 
150 //------------------------------------------------------------------------------
151 /// @brief A container for caching a glyph atlas across frames.
152 ///
154  public:
156 
157  virtual ~GlyphAtlasContext();
158 
159  //----------------------------------------------------------------------------
160  /// @brief Retrieve the current glyph atlas.
161  std::shared_ptr<GlyphAtlas> GetGlyphAtlas() const;
162 
163  //----------------------------------------------------------------------------
164  /// @brief Retrieve the size of the current glyph atlas.
165  const ISize& GetAtlasSize() const;
166 
167  //----------------------------------------------------------------------------
168  /// @brief Retrieve the previous (if any) rect packer.
169  std::shared_ptr<RectanglePacker> GetRectPacker() const;
170 
171  //----------------------------------------------------------------------------
172  /// @brief A y-coordinate shift that must be applied to glyphs appended
173  /// to
174  /// the atlas.
175  ///
176  /// The rectangle packer is only initialized for unfilled regions
177  /// of the atlas. The area the rectangle packer covers is offset
178  /// from the origin by this height adjustment.
179  int64_t GetHeightAdjustment() const;
180 
181  //----------------------------------------------------------------------------
182  /// @brief Update the context with a newly constructed glyph atlas.
183  void UpdateGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas,
184  ISize size,
185  int64_t height_adjustment_);
186 
187  void UpdateRectPacker(std::shared_ptr<RectanglePacker> rect_packer);
188 
189  private:
190  std::shared_ptr<GlyphAtlas> atlas_;
191  ISize atlas_size_;
192  std::shared_ptr<RectanglePacker> rect_packer_;
193  int64_t height_adjustment_;
194 
195  GlyphAtlasContext(const GlyphAtlasContext&) = delete;
196 
197  GlyphAtlasContext& operator=(const GlyphAtlasContext&) = delete;
198 };
199 
200 //------------------------------------------------------------------------------
201 /// @brief An object that can look up glyph locations within the GlyphAtlas
202 /// for a particular typeface.
203 ///
205  public:
206  FontGlyphAtlas() = default;
207 
208  //----------------------------------------------------------------------------
209  /// @brief Find the location of a glyph in the atlas.
210  ///
211  /// @param[in] glyph The glyph
212  ///
213  /// @return The location of the glyph in the atlas.
214  /// `std::nullopt` if the glyph is not in the atlas.
215  ///
216  std::optional<std::pair<Rect, Rect>> FindGlyphBounds(
217  const SubpixelGlyph& glyph) const;
218 
219  private:
220  friend class GlyphAtlas;
221  std::unordered_map<SubpixelGlyph,
222  std::pair<Rect, Rect>,
225  positions_;
226 
227  FontGlyphAtlas(const FontGlyphAtlas&) = delete;
228 
229  FontGlyphAtlas& operator=(const FontGlyphAtlas&) = delete;
230 };
231 
232 } // namespace impeller
233 
234 #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::ScaledFont::Hash
Definition: font_glyph_pair.h:37
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:204
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:54
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::SubpixelGlyph::Hash
Definition: font_glyph_pair.h:66
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
impeller::ScaledFont::Equal
Definition: font_glyph_pair.h:43
font_glyph_pair.h
type
GLenum type
Definition: blit_command_gles.cc:127
impeller::GlyphAtlasContext
A container for caching a glyph atlas across frames.
Definition: glyph_atlas.h:153
impeller::FontGlyphPair
A font along with a glyph in that font rendered at a particular scale and subpixel position.
Definition: font_glyph_pair.h:114
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:301
impeller::GlyphAtlas::IsValid
bool IsValid() const
Definition: glyph_atlas.cc:50
impeller::SubpixelGlyph::Equal
Definition: font_glyph_pair.h:80
impeller
Definition: allocation.cc:12
impeller::GlyphAtlasContext::GlyphAtlasContext
GlyphAtlasContext(GlyphAtlas::Type type)
Definition: glyph_atlas.cc:12
impeller::TRect
Definition: rect.h:122
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:33
impeller::GlyphAtlasContext::UpdateRectPacker
void UpdateRectPacker(std::shared_ptr< RectanglePacker > rect_packer)
Definition: glyph_atlas.cc:41