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