Flutter Engine
The 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
10namespace impeller {
11
13 : atlas_(std::make_shared<GlyphAtlas>(GlyphAtlas::Type::kAlphaBitmap)),
14 atlas_size_(ISize(0, 0)) {}
15
17
18std::shared_ptr<GlyphAtlas> GlyphAtlasContext::GetGlyphAtlas() const {
19 return atlas_;
20}
21
23 return atlas_size_;
24}
25
26std::shared_ptr<RectanglePacker> GlyphAtlasContext::GetRectPacker() const {
27 return rect_packer_;
28}
29
30void 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
42
43GlyphAtlas::~GlyphAtlas() = default;
44
45bool GlyphAtlas::IsValid() const {
46 return !!texture_;
47}
48
50 return type_;
51}
52
53const std::shared_ptr<Texture>& GlyphAtlas::GetTexture() const {
54 return texture_;
55}
56
57void 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
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
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
111std::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
int count
An object that can look up glyph locations within the GlyphAtlas for a particular typeface.
std::optional< Rect > FindGlyphBounds(const Glyph &glyph) const
Find the location of a glyph in the atlas.
Describes a typeface along with any modifications to its intrinsic properties.
Definition font.h:22
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.
void UpdateGlyphAtlas(std::shared_ptr< GlyphAtlas > atlas, ISize size)
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:28
GlyphAtlas(Type type)
Create an empty glyph atlas.
void AddTypefaceGlyphPosition(const FontGlyphPair &pair, Rect rect)
Record the location of a specific font-glyph pair within the atlas.
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.
std::optional< Rect > 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.
Type
Describes how the glyphs are represented in the texture.
Definition glyph_atlas.h:32
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.
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....
size_t GetGlyphCount() const
Get the number of unique font-glyph pairs in this atlas.
static bool b
struct MyStruct a[10]
FlTexture * texture
float Scalar
Definition scalar.h:18
Definition ref_ptr.h:256
const Scalar scale
A font along with a glyph in that font rendered at a particular scale.
const ScaledFont & scaled_font
The glyph index in the typeface.
Definition glyph.h:20
A font and a scale. Used as a key that represents a typeface within a glyph atlas.