Flutter Impeller
glyph_atlas.cc
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 
6 
7 #include <numeric>
8 #include <utility>
9 
11 
12 namespace impeller {
13 
15  : atlas_(std::make_shared<GlyphAtlas>(type, /*initial_generation=*/0)),
16  atlas_size_(ISize(0, 0)) {}
17 
19 
20 std::shared_ptr<GlyphAtlas> GlyphAtlasContext::GetGlyphAtlas() const {
21  return atlas_;
22 }
23 
25  return atlas_size_;
26 }
27 
29  return height_adjustment_;
30 }
31 
32 std::shared_ptr<RectanglePacker> GlyphAtlasContext::GetRectPacker() const {
33  return rect_packer_;
34 }
35 
36 void GlyphAtlasContext::UpdateGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas,
37  ISize size,
38  int64_t height_adjustment) {
39  atlas_ = std::move(atlas);
40  atlas_size_ = size;
41  height_adjustment_ = height_adjustment;
42 }
43 
45  std::shared_ptr<RectanglePacker> rect_packer) {
46  rect_packer_ = std::move(rect_packer);
47 }
48 
49 GlyphAtlas::GlyphAtlas(Type type, size_t initial_generation)
50  : type_(type), generation_(initial_generation) {}
51 
52 GlyphAtlas::~GlyphAtlas() = default;
53 
54 bool GlyphAtlas::IsValid() const {
55  return !!texture_;
56 }
57 
59  return type_;
60 }
61 
62 const std::shared_ptr<Texture>& GlyphAtlas::GetTexture() const {
63  return texture_;
64 }
65 
66 void GlyphAtlas::SetTexture(std::shared_ptr<Texture> texture) {
67  texture_ = std::move(texture);
68 }
69 
71  return generation_;
72 }
73 
74 void GlyphAtlas::SetAtlasGeneration(size_t generation) {
75  generation_ = generation;
76 }
77 
79  Rect position,
80  Rect bounds) {
81  FontAtlasMap::iterator it = font_atlas_map_.find(pair.scaled_font);
82  FML_DCHECK(it != font_atlas_map_.end());
83  it->second.positions_[pair.glyph] =
84  FrameBounds{position, bounds, /*is_placeholder=*/false};
85 }
86 
87 std::optional<FrameBounds> GlyphAtlas::FindFontGlyphBounds(
88  const FontGlyphPair& pair) const {
89  const auto& found = font_atlas_map_.find(pair.scaled_font);
90  if (found == font_atlas_map_.end()) {
91  return std::nullopt;
92  }
93  return found->second.FindGlyphBounds(pair.glyph);
94 }
95 
97  const ScaledFont& scaled_font) {
98  auto [iter, inserted] =
99  font_atlas_map_.try_emplace(scaled_font, FontGlyphAtlas());
100  return &iter->second;
101 }
102 
104  return std::accumulate(font_atlas_map_.begin(), font_atlas_map_.end(), 0,
105  [](const int a, const auto& b) {
106  return a + b.second.positions_.size();
107  });
108 }
109 
111  const std::function<bool(const ScaledFont& scaled_font,
112  const SubpixelGlyph& glyph,
113  const Rect& rect)>& iterator) const {
114  if (!iterator) {
115  return 0u;
116  }
117 
118  size_t count = 0u;
119  for (const auto& font_value : font_atlas_map_) {
120  for (const auto& glyph_value : font_value.second.positions_) {
121  count++;
122  if (!iterator(font_value.first, glyph_value.first,
123  glyph_value.second.atlas_bounds)) {
124  return count;
125  }
126  }
127  }
128  return count;
129 }
130 
131 std::optional<FrameBounds> FontGlyphAtlas::FindGlyphBounds(
132  const SubpixelGlyph& glyph) const {
133  const auto& found = positions_.find(glyph);
134  if (found == positions_.end()) {
135  return std::nullopt;
136  }
137  return found->second;
138 }
139 
141  const FrameBounds& frame_bounds) {
142  positions_[glyph] = frame_bounds;
143 }
144 
145 } // namespace impeller
GLenum type
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
Definition: glyph_atlas.h:269
void AppendGlyph(const SubpixelGlyph &glyph, const FrameBounds &frame_bounds)
Append the frame bounds of a glyph to this atlas.
Definition: glyph_atlas.cc:140
std::optional< FrameBounds > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
Definition: glyph_atlas.cc:131
GlyphAtlasContext(GlyphAtlas::Type type)
Definition: glyph_atlas.cc:14
std::shared_ptr< RectanglePacker > GetRectPacker() const
Retrieve the previous (if any) rect packer.
Definition: glyph_atlas.cc:32
void UpdateRectPacker(std::shared_ptr< RectanglePacker > rect_packer)
Definition: glyph_atlas.cc:44
std::shared_ptr< GlyphAtlas > GetGlyphAtlas() const
Retrieve the current glyph atlas.
Definition: glyph_atlas.cc:20
const ISize & GetAtlasSize() const
Retrieve the size of the current glyph atlas.
Definition: glyph_atlas.cc:24
int64_t GetHeightAdjustment() const
A y-coordinate shift that must be applied to glyphs appended to the atlas.
Definition: glyph_atlas.cc:28
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:36
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition: glyph_atlas.h:70
std::optional< FrameBounds > FindFontGlyphBounds(const FontGlyphPair &pair) const
Find the location of a specific font-glyph pair in the atlas.
Definition: glyph_atlas.cc:87
bool IsValid() const
Definition: glyph_atlas.cc:54
void SetTexture(std::shared_ptr< Texture > texture)
Set the texture for the glyph atlas.
Definition: glyph_atlas.cc:66
void SetAtlasGeneration(size_t value)
Update the atlas generation.
Definition: glyph_atlas.cc:74
FontGlyphAtlas * GetOrCreateFontGlyphAtlas(const ScaledFont &scaled_font)
Obtain an interface for querying the location of glyphs in the atlas for the given font and scale....
Definition: glyph_atlas.cc:96
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:74
size_t GetAtlasGeneration() const
Retrieve the generation id for this glyph atlas.
Definition: glyph_atlas.cc:70
Type GetType() const
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.cc:58
const std::shared_ptr< Texture > & GetTexture() const
Get the texture for the glyph atlas.
Definition: glyph_atlas.cc:62
GlyphAtlas(Type type, size_t initial_generation)
Create an empty glyph atlas.
Definition: glyph_atlas.cc:49
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:110
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:78
size_t GetGlyphCount() const
Get the number of unique font-glyph pairs in this atlas.
Definition: glyph_atlas.cc:103
Definition: comparable.h:95
A font along with a glyph in that font rendered at a particular scale and subpixel position.
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
A glyph and its subpixel position.