| 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 namespace impeller {
11
12 GlyphAtlasContext::GlyphAtlasContext(GlyphAtlas::Type type)
13 : atlas_(std::make_shared<GlyphAtlas>(type)), atlas_size_(ISize(0, 0)) {}
14
15 GlyphAtlasContext::~GlyphAtlasContext() {}
16
17 std::shared_ptr<GlyphAtlas> GlyphAtlasContext::GetGlyphAtlas() const {
18 return atlas_;
19 }
20
21 const ISize& GlyphAtlasContext::GetAtlasSize() const {
22 return atlas_size_;
23 }
24
25 int64_t GlyphAtlasContext::GetHeightAdjustment() const {
26 return height_adjustment_;
27 }
28
29 std::shared_ptr<RectanglePacker> GlyphAtlasContext::GetRectPacker() const {
30 return rect_packer_;
31 }
32
33 void GlyphAtlasContext::UpdateGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas,
34 ISize size,
35 int64_t height_adjustment) {
36 atlas_ = std::move(atlas);
37 atlas_size_ = size;
38 height_adjustment_ = height_adjustment;
39 }
40
41 void GlyphAtlasContext::UpdateRectPacker(
42 std::shared_ptr<RectanglePacker> rect_packer) {
43 rect_packer_ = std::move(rect_packer);
44 }
45
46 GlyphAtlas::GlyphAtlas(Type type) : type_(type) {}
47
48 GlyphAtlas::~GlyphAtlas() = default;
49
50 bool GlyphAtlas::IsValid() const {
51 return !!texture_;
52 }
53
54 GlyphAtlas::Type GlyphAtlas::GetType() const {
55 return type_;
56 }
57
58 const std::shared_ptr<Texture>& GlyphAtlas::GetTexture() const {
59 return texture_;
60 }
61
62 void GlyphAtlas::SetTexture(std::shared_ptr<Texture> texture) {
63 texture_ = std::move(texture);
64 }
65
66 void GlyphAtlas::AddTypefaceGlyphPositionAndBounds(const FontGlyphPair& pair,
67 Rect position,
68 Rect bounds) {
69 font_atlas_map_[pair.scaled_font].positions_[pair.glyph] =
70 std::make_pair(position, bounds);
71 }
72
73 std::optional<std::pair<Rect, Rect>> GlyphAtlas::FindFontGlyphBounds(
74 const FontGlyphPair& pair) const {
75 const auto& found = font_atlas_map_.find(pair.scaled_font);
76 if (found == font_atlas_map_.end()) {
77 return std::nullopt;
78 }
79 return found->second.FindGlyphBounds(pair.glyph);
80 }
81
82 const FontGlyphAtlas* GlyphAtlas::GetFontGlyphAtlas(const Font& font,
83 Scalar scale) const {
84 const auto& found = font_atlas_map_.find(ScaledFont{font, scale});
85 if (found == font_atlas_map_.end()) {
86 return nullptr;
87 }
88 return &found->second;
89 }
90
91 size_t GlyphAtlas::GetGlyphCount() const {
92 return std::accumulate(font_atlas_map_.begin(), font_atlas_map_.end(), 0,
93 [](const int a, const auto& b) {
94 return a + b.second.positions_.size();
95 });
96 }
97
98 size_t GlyphAtlas::IterateGlyphs(
99 const std::function<bool(const ScaledFont& scaled_font,
100 const SubpixelGlyph& glyph,
101 const Rect& rect)>& iterator) const {
102 if (!iterator) {
103 return 0u;
104 }
105
106 size_t count = 0u;
107 for (const auto& font_value : font_atlas_map_) {
108 for (const auto& glyph_value : font_value.second.positions_) {
109 count++;
110 if (!iterator(font_value.first, glyph_value.first,
111 glyph_value.second.first)) {
112 return count;
113 }
114 }
115 }
116 return count;
117 }
118
119 std::optional<std::pair<Rect, Rect>> FontGlyphAtlas::FindGlyphBounds(
120 const SubpixelGlyph& glyph) const {
121 const auto& found = positions_.find(glyph);
122 if (found == positions_.end()) {
123 return std::nullopt;
124 }
125 return found->second;
126 }
127
128 } // namespace impeller
impeller::GlyphAtlas::GetGlyphCount
size_t GetGlyphCount() const
Get the number of unique font-glyph pairs in this atlas.
Definition: glyph_atlas.cc:91
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:29
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
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:33
impeller::GlyphAtlasContext::GetGlyphAtlas
std::shared_ptr< GlyphAtlas > GetGlyphAtlas() const
Retrieve the current glyph atlas.
Definition: glyph_atlas.cc:17
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:82
impeller::FontGlyphAtlas
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
Definition: glyph_atlas.h:204
impeller::GlyphAtlas::SetTexture
void SetTexture(std::shared_ptr< Texture > texture)
Set the texture for the glyph atlas.
Definition: glyph_atlas.cc:62
impeller::SubpixelGlyph
A glyph and its subpixel position.
Definition: font_glyph_pair.h:54
impeller::GlyphAtlasContext::~GlyphAtlasContext
virtual ~GlyphAtlasContext()
Definition: glyph_atlas.cc:15
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:25
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:66
impeller::GlyphAtlas::GlyphAtlas
GlyphAtlas(Type type)
Create an empty glyph atlas.
Definition: glyph_atlas.cc:46
impeller::TSize
Definition: size.h:19
impeller::FontGlyphPair::glyph
const SubpixelGlyph & glyph
Definition: font_glyph_pair.h:118
impeller::GlyphAtlasContext::GetAtlasSize
const ISize & GetAtlasSize() const
Retrieve the size of the current glyph atlas.
Definition: glyph_atlas.cc:21
impeller::FontGlyphPair::scaled_font
const ScaledFont & scaled_font
Definition: font_glyph_pair.h:117
impeller::GlyphAtlas::GetType
Type GetType() const
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.cc:54
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:58
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:31
type
GLenum type
Definition: blit_command_gles.cc:127
impeller::FontGlyphPair
A font along with a glyph in that font rendered at a particular scale and subpixel position.
Definition: font_glyph_pair.h:114
impeller::GlyphAtlas
A texture containing the bitmap representation of glyphs in different fonts along with the ability to...
Definition: glyph_atlas.h:27
impeller::FontGlyphAtlas::FindGlyphBounds
std::optional< std::pair< Rect, Rect > > FindGlyphBounds(const SubpixelGlyph &glyph) const
Find the location of a glyph in the atlas.
Definition: glyph_atlas.cc:119
std
Definition: comparable.h:95
impeller::GlyphAtlas::FindFontGlyphBounds
std::optional< std::pair< Rect, Rect > > FindFontGlyphBounds(const FontGlyphPair &pair) const
Find the location of a specific font-glyph pair in the atlas.
Definition: glyph_atlas.cc:73
impeller::saturated::b
SI b
Definition: saturated_math.h:87
scale
const Scalar scale
Definition: stroke_path_geometry.cc:301
glyph_atlas.h
impeller::GlyphAtlas::IsValid
bool IsValid() const
Definition: glyph_atlas.cc:50
impeller
Definition: allocation.cc:12
impeller::GlyphAtlasContext::GlyphAtlasContext
GlyphAtlasContext(GlyphAtlas::Type type)
Definition: glyph_atlas.cc:12
impeller::TRect< Scalar >
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:98
impeller::ScaledFont
A font and a scale. Used as a key that represents a typeface within a glyph atlas.
Definition: font_glyph_pair.h:33
impeller::GlyphAtlasContext::UpdateRectPacker
void UpdateRectPacker(std::shared_ptr< RectanglePacker > rect_packer)
Definition: glyph_atlas.cc:41