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 
10 namespace impeller {
11 
13  : atlas_(std::make_shared<GlyphAtlas>(GlyphAtlas::Type::kAlphaBitmap)),
14  atlas_size_(ISize(0, 0)) {}
15 
17 
18 std::shared_ptr<GlyphAtlas> GlyphAtlasContext::GetGlyphAtlas() const {
19  return atlas_;
20 }
21 
23  return atlas_size_;
24 }
25 
26 std::shared_ptr<RectanglePacker> GlyphAtlasContext::GetRectPacker() const {
27  return rect_packer_;
28 }
29 
30 void GlyphAtlasContext::UpdateGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas,
31  ISize size) {
32  atlas_ = std::move(atlas);
33  atlas_size_ = size;
34 }
35 
37  std::shared_ptr<RectanglePacker> rect_packer) {
38  rect_packer_ = std::move(rect_packer);
39 }
40 
41 GlyphAtlas::GlyphAtlas(Type type) : type_(type) {}
42 
43 GlyphAtlas::~GlyphAtlas() = default;
44 
45 bool GlyphAtlas::IsValid() const {
46  return !!texture_;
47 }
48 
50  return type_;
51 }
52 
53 const std::shared_ptr<Texture>& GlyphAtlas::GetTexture() const {
54  return texture_;
55 }
56 
57 void GlyphAtlas::SetTexture(std::shared_ptr<Texture> texture) {
58  texture_ = std::move(texture);
59 }
60 
62  Rect rect) {
63  font_atlas_map_[pair.scaled_font].positions_[pair.glyph] = rect;
64 }
65 
66 std::optional<Rect> GlyphAtlas::FindFontGlyphBounds(
67  const FontGlyphPair& pair) const {
68  const auto& found = font_atlas_map_.find(pair.scaled_font);
69  if (found == font_atlas_map_.end()) {
70  return std::nullopt;
71  }
72  return found->second.FindGlyphBounds(pair.glyph);
73 }
74 
76  Scalar scale) const {
77  const auto& found = font_atlas_map_.find({font, scale});
78  if (found == font_atlas_map_.end()) {
79  return nullptr;
80  }
81  return &found->second;
82 }
83 
84 size_t GlyphAtlas::GetGlyphCount() const {
85  return std::accumulate(font_atlas_map_.begin(), font_atlas_map_.end(), 0,
86  [](const int a, const auto& b) {
87  return a + b.second.positions_.size();
88  });
89 }
90 
92  const std::function<bool(const ScaledFont& scaled_font,
93  const Glyph& glyph,
94  const Rect& rect)>& iterator) const {
95  if (!iterator) {
96  return 0u;
97  }
98 
99  size_t count = 0u;
100  for (const auto& font_value : font_atlas_map_) {
101  for (const auto& glyph_value : font_value.second.positions_) {
102  count++;
103  if (!iterator(font_value.first, glyph_value.first, glyph_value.second)) {
104  return count;
105  }
106  }
107  }
108  return count;
109 }
110 
111 std::optional<Rect> FontGlyphAtlas::FindGlyphBounds(const Glyph& glyph) const {
112  const auto& found = positions_.find(glyph);
113  if (found == positions_.end()) {
114  return std::nullopt;
115  }
116  return found->second;
117 }
118 
119 } // namespace impeller
impeller::GlyphAtlas::GetGlyphCount
size_t GetGlyphCount() const
Get the number of unique font-glyph pairs in this atlas.
Definition: glyph_atlas.cc:84
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::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
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
Definition: glyph_atlas.h:185
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::FontGlyphPair::glyph
const Glyph & glyph
Definition: font_glyph_pair.h:39
impeller::GlyphAtlasContext::GetAtlasSize
const ISize & GetAtlasSize() const
Retrieve the size of the current glyph atlas.
Definition: glyph_atlas.cc:22
impeller::FontGlyphPair::scaled_font
const ScaledFont & scaled_font
Definition: font_glyph_pair.h:38
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
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::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:28
std
Definition: comparable.h:95
impeller::saturated::b
SI b
Definition: saturated_math.h:87
scale
const Scalar scale
Definition: stroke_path_geometry.cc:297
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
glyph_atlas.h
impeller::GlyphAtlas::IsValid
bool IsValid() const
Definition: glyph_atlas.cc:45
impeller
Definition: aiks_blur_unittests.cc:20
impeller::TRect< Scalar >
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