Flutter Engine
 
Loading...
Searching...
No Matches
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
12namespace impeller {
13
15 : atlas_(std::make_shared<GlyphAtlas>(type, /*initial_generation=*/0)),
16 atlas_size_(ISize(0, 0)) {}
17
19
20std::shared_ptr<GlyphAtlas> GlyphAtlasContext::GetGlyphAtlas() const {
21 return atlas_;
22}
23
25 return atlas_size_;
26}
27
29 return height_adjustment_;
30}
31
32std::shared_ptr<RectanglePacker> GlyphAtlasContext::GetRectPacker() const {
33 return rect_packer_;
34}
35
36void 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
49GlyphAtlas::GlyphAtlas(Type type, size_t initial_generation)
50 : type_(type), generation_(initial_generation) {}
51
52GlyphAtlas::~GlyphAtlas() = default;
53
54bool GlyphAtlas::IsValid() const {
55 return !!texture_;
56}
57
59 return type_;
60}
61
62const std::shared_ptr<Texture>& GlyphAtlas::GetTexture() const {
63 return texture_;
64}
65
66void GlyphAtlas::SetTexture(std::shared_ptr<Texture> texture) {
67 texture_ = std::move(texture);
68}
69
71 return generation_;
72}
73
74void 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
87std::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
131std::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.
void AppendGlyph(const SubpixelGlyph &glyph, const FrameBounds &frame_bounds)
Append the frame bounds of a glyph to this atlas.
std::optional< FrameBounds > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
GlyphAtlasContext(GlyphAtlas::Type type)
std::shared_ptr< RectanglePacker > GetRectPacker() const
Retrieve the previous (if any) rect packer.
void UpdateRectPacker(std::shared_ptr< RectanglePacker > rect_packer)
std::shared_ptr< GlyphAtlas > GetGlyphAtlas() const
Retrieve the current glyph atlas.
const ISize & GetAtlasSize() const
Retrieve the size of the current glyph atlas.
int64_t GetHeightAdjustment() const
A y-coordinate shift that must be applied to glyphs appended to the atlas.
void UpdateGlyphAtlas(std::shared_ptr< GlyphAtlas > atlas, ISize size, int64_t height_adjustment_)
Update the context with a newly constructed glyph atlas.
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition glyph_atlas.h:37
std::optional< FrameBounds > FindFontGlyphBounds(const FontGlyphPair &pair) const
Find the location of a specific font-glyph pair in the atlas.
bool IsValid() const
void SetTexture(std::shared_ptr< Texture > texture)
Set the texture for the glyph atlas.
void SetAtlasGeneration(size_t value)
Update the atlas generation.
FontGlyphAtlas * GetOrCreateFontGlyphAtlas(const ScaledFont &scaled_font)
Obtain an interface for querying the location of glyphs in the atlas for the given font and scale....
Type
Describes how the glyphs are represented in the texture.
Definition glyph_atlas.h:41
size_t GetAtlasGeneration() const
Retrieve the generation id for this glyph atlas.
Type GetType() const
Describes how the glyphs are represented in the texture.
const std::shared_ptr< Texture > & GetTexture() const
Get the texture for the glyph atlas.
GlyphAtlas(Type type, size_t initial_generation)
Create an empty glyph atlas.
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.
void AddTypefaceGlyphPositionAndBounds(const FontGlyphPair &pair, Rect position, Rect bounds)
Record the location of a specific font-glyph pair within the atlas.
size_t GetGlyphCount() const
Get the number of unique font-glyph pairs in this atlas.
#define FML_DCHECK(condition)
Definition logging.h:122
FlTexture * texture
Definition ref_ptr.h:261
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.