| 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
5 #include "impeller/typographer/glyph_atlas.h"
6
7 #include <numeric>
8 #include <utility>
9
10 #include "impeller/typographer/font_glyph_pair.h"
11
12 namespace impeller {
13
14 GlyphAtlasContext::GlyphAtlasContext(GlyphAtlas::Type type)
15 : atlas_(std::make_shared<GlyphAtlas>(type, /*initial_generation=*/0)),
16 atlas_size_(ISize(0, 0)) {}
17
18 GlyphAtlasContext::~GlyphAtlasContext() {}
19
20 std::shared_ptr<GlyphAtlas> GlyphAtlasContext::GetGlyphAtlas() const {
21 return atlas_;
22 }
23
24 const ISize& GlyphAtlasContext::GetAtlasSize() const {
25 return atlas_size_;
26 }
27
28 int64_t GlyphAtlasContext::GetHeightAdjustment() const {
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
44 void GlyphAtlasContext::UpdateRectPacker(
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
58 GlyphAtlas::Type GlyphAtlas::GetType() const {
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
70 size_t GlyphAtlas::GetAtlasGeneration() const {
71 return generation_;
72 }
73
74 void GlyphAtlas::SetAtlasGeneration(size_t generation) {
75 generation_ = generation;
76 }
77
78 void GlyphAtlas::AddTypefaceGlyphPositionAndBounds(const FontGlyphPair& pair,
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
96 FontGlyphAtlas* GlyphAtlas::GetOrCreateFontGlyphAtlas(
97 const ScaledFont& scaled_font) {
98 auto [iter, inserted] =
99 font_atlas_map_.try_emplace(scaled_font, FontGlyphAtlas());
100 return &iter->second;
101 }
102
103 size_t GlyphAtlas::GetGlyphCount() const {
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
110 size_t GlyphAtlas::IterateGlyphs(
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
140 void FontGlyphAtlas::AppendGlyph(const SubpixelGlyph& glyph,
141 const FrameBounds& frame_bounds) {
142 positions_[glyph] = frame_bounds;
143 }
144
145 } // namespace impeller
type
GLenum type
Definition: blit_command_gles.cc:151
impeller::FontGlyphAtlas
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
Definition: glyph_atlas.h:269
impeller::FontGlyphAtlas::AppendGlyph
void AppendGlyph(const SubpixelGlyph &glyph, const FrameBounds &frame_bounds)
Append the frame bounds of a glyph to this atlas.
Definition: glyph_atlas.cc:140
impeller::FontGlyphAtlas::FindGlyphBounds
std::optional< FrameBounds > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
Definition: glyph_atlas.cc:131
impeller::GlyphAtlasContext::GlyphAtlasContext
GlyphAtlasContext(GlyphAtlas::Type type)
Definition: glyph_atlas.cc:14
impeller::GlyphAtlasContext::GetRectPacker
std::shared_ptr< RectanglePacker > GetRectPacker() const
Retrieve the previous (if any) rect packer.
Definition: glyph_atlas.cc:32
impeller::GlyphAtlasContext::UpdateRectPacker
void UpdateRectPacker(std::shared_ptr< RectanglePacker > rect_packer)
Definition: glyph_atlas.cc:44
impeller::GlyphAtlasContext::GetGlyphAtlas
std::shared_ptr< GlyphAtlas > GetGlyphAtlas() const
Retrieve the current glyph atlas.
Definition: glyph_atlas.cc:20
impeller::GlyphAtlasContext::GetAtlasSize
const ISize & GetAtlasSize() const
Retrieve the size of the current glyph atlas.
Definition: glyph_atlas.cc:24
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:28
impeller::GlyphAtlasContext::~GlyphAtlasContext
virtual ~GlyphAtlasContext()
Definition: glyph_atlas.cc:18
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:36
impeller::GlyphAtlas
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition: glyph_atlas.h:70
impeller::GlyphAtlas::FindFontGlyphBounds
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
impeller::GlyphAtlas::IsValid
bool IsValid() const
Definition: glyph_atlas.cc:54
impeller::GlyphAtlas::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Set the texture for the glyph atlas.
Definition: glyph_atlas.cc:66
impeller::GlyphAtlas::SetAtlasGeneration
void SetAtlasGeneration(size_t value)
Update the atlas generation.
Definition: glyph_atlas.cc:74
impeller::GlyphAtlas::GetOrCreateFontGlyphAtlas
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
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:74
impeller::GlyphAtlas::GetAtlasGeneration
size_t GetAtlasGeneration() const
Retrieve the generation id for this glyph atlas.
Definition: glyph_atlas.cc:70
impeller::GlyphAtlas::GetType
Type GetType() const
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.cc:58
impeller::GlyphAtlas::GetTexture
const std::shared_ptr< Texture > & GetTexture() const
Get the texture for the glyph atlas.
Definition: glyph_atlas.cc:62
impeller::GlyphAtlas::~GlyphAtlas
~GlyphAtlas()
impeller::GlyphAtlas::GlyphAtlas
GlyphAtlas(Type type, size_t initial_generation)
Create an empty glyph atlas.
Definition: glyph_atlas.cc:49
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:110
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:78
impeller::GlyphAtlas::GetGlyphCount
size_t GetGlyphCount() const
Get the number of unique font-glyph pairs in this atlas.
Definition: glyph_atlas.cc:103
font_glyph_pair.h
glyph_atlas.h
impeller::saturated::b
SI b
Definition: saturated_math.h:87
impeller
Definition: allocation.cc:12
std
Definition: comparable.h:95
impeller::FontGlyphPair
A font along with a glyph in that font rendered at a particular scale and subpixel position.
Definition: font_glyph_pair.h:138
impeller::FontGlyphPair::glyph
SubpixelGlyph glyph
Definition: font_glyph_pair.h:142
impeller::FontGlyphPair::scaled_font
ScaledFont scaled_font
Definition: font_glyph_pair.h:141
impeller::FrameBounds
Definition: glyph_atlas.h:55
impeller::ScaledFont
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
Definition: font_glyph_pair.h:41
impeller::SubpixelGlyph
A glyph and its subpixel position.
Definition: font_glyph_pair.h:90
impeller::TRect< Scalar >
impeller::TSize
Definition: size.h:25