forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathglyph_atlas.h
More file actions
165 lines (137 loc) · 5.51 KB
/
glyph_atlas.h
File metadata and controls
165 lines (137 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#pragma once
#include <functional>
#include <memory>
#include <optional>
#include <unordered_map>
#include "flutter/fml/macros.h"
#include "impeller/geometry/rect.h"
#include "impeller/renderer/pipeline.h"
#include "impeller/renderer/texture.h"
#include "impeller/typographer/font_glyph_pair.h"
namespace impeller {
//------------------------------------------------------------------------------
/// @brief A texture containing the bitmap representation of glyphs in
/// different fonts along with the ability to query the location of
/// specific font glyphs within the texture.
///
class GlyphAtlas {
public:
//----------------------------------------------------------------------------
/// @brief Describes how the glyphs are represented in the texture.
enum class Type {
//--------------------------------------------------------------------------
/// The glyphs are represented at a fixed size in an 8-bit grayscale texture
/// where the value of each pixel represents a signed-distance field that
/// stores the glyph outlines.
///
kSignedDistanceField,
//--------------------------------------------------------------------------
/// The glyphs are reprsented at their requested size using only an 8-bit
/// alpha channel.
///
kAlphaBitmap,
//--------------------------------------------------------------------------
/// The glyphs are reprsented at their requested size using N32 premul
/// colors.
///
kColorBitmap,
};
//----------------------------------------------------------------------------
/// @brief Create an empty glyph atlas.
///
/// @param[in] type How the glyphs are represented in the texture.
///
explicit GlyphAtlas(Type type);
~GlyphAtlas();
bool IsValid() const;
//----------------------------------------------------------------------------
/// @brief Describes how the glyphs are represented in the texture.
///
Type GetType() const;
//----------------------------------------------------------------------------
/// @brief Set the texture for the glyph atlas.
///
/// @param[in] texture The texture
///
void SetTexture(std::shared_ptr<Texture> texture);
//----------------------------------------------------------------------------
/// @brief Get the texture for the glyph atlas.
///
/// @return The texture.
///
const std::shared_ptr<Texture>& GetTexture() const;
//----------------------------------------------------------------------------
/// @brief Record the location of a specific font-glyph pair within the
/// atlas.
///
/// @param[in] pair The font-glyph pair
/// @param[in] rect The rectangle
///
void AddTypefaceGlyphPosition(const FontGlyphPair& pair, Rect rect);
//----------------------------------------------------------------------------
/// @brief Get the number of unique font-glyph pairs in this atlas.
///
/// @return The glyph count.
///
size_t GetGlyphCount() const;
//----------------------------------------------------------------------------
/// @brief Iterate of all the glyphs along with their locations in the
/// atlas.
///
/// @param[in] iterator The iterator. Return `false` from the iterator to
/// stop iterating.
///
/// @return The number of glyphs iterated over.
///
size_t IterateGlyphs(
const std::function<bool(const FontGlyphPair& pair, const Rect& rect)>&
iterator) const;
//----------------------------------------------------------------------------
/// @brief Find the location of a specific font-glyph pair in the atlas.
///
/// @param[in] pair The font-glyph pair
///
/// @return The location of the font-glyph pair in the atlas.
/// `std::nullopt` of the pair in not in the atlas.
///
std::optional<Rect> FindFontGlyphPosition(const FontGlyphPair& pair) const;
//----------------------------------------------------------------------------
/// @brief whether this atlas contains all of the same font-glyph pairs
/// as the vector.
///
/// @param[in] new_glyphs The full set of new glyphs
///
/// @return Whether this atlas contains all passed pairs.
///
bool HasSamePairs(const FontGlyphPair::Vector& new_glyphs);
private:
const Type type_;
std::shared_ptr<Texture> texture_;
std::unordered_map<FontGlyphPair,
Rect,
FontGlyphPair::Hash,
FontGlyphPair::Equal>
positions_;
FML_DISALLOW_COPY_AND_ASSIGN(GlyphAtlas);
};
//------------------------------------------------------------------------------
/// @brief A container for caching a glyph atlas across frames.
///
class GlyphAtlasContext {
public:
GlyphAtlasContext();
~GlyphAtlasContext();
//----------------------------------------------------------------------------
/// @brief Retrieve the current glyph atlas.
std::shared_ptr<GlyphAtlas> GetGlyphAtlas() const;
//----------------------------------------------------------------------------
/// @brief Update the context with a newly constructed glyph atlas.
void UpdateGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas);
private:
std::shared_ptr<GlyphAtlas> atlas_;
FML_DISALLOW_COPY_AND_ASSIGN(GlyphAtlasContext);
};
} // namespace impeller