2
2
// Use of this source code is governed by a BSD-style license that can be
3
3
// found in the LICENSE file.
4
4
5
- #include " text_contents.h"
5
+ #include " impeller/entity/contents/ text_contents.h"
6
6
7
7
#include " impeller/entity/contents/content_context.h"
8
8
#include " impeller/entity/entity.h"
11
11
#include " impeller/renderer/sampler_library.h"
12
12
#include " impeller/tessellator/tessellator.h"
13
13
#include " impeller/typographer/glyph_atlas.h"
14
+ #include " impeller/typographer/lazy_glyph_atlas.h"
14
15
15
16
namespace impeller {
17
+
16
18
TextContents::TextContents () = default ;
17
19
18
20
TextContents::~TextContents () = default ;
@@ -25,6 +27,23 @@ void TextContents::SetGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas) {
25
27
atlas_ = std::move (atlas);
26
28
}
27
29
30
+ void TextContents::SetGlyphAtlas (std::shared_ptr<LazyGlyphAtlas> atlas) {
31
+ atlas_ = std::move (atlas);
32
+ }
33
+
34
+ std::shared_ptr<GlyphAtlas> TextContents::ResolveAtlas (
35
+ std::shared_ptr<Context> context) const {
36
+ if (auto lazy_atlas = std::get_if<std::shared_ptr<LazyGlyphAtlas>>(&atlas_)) {
37
+ return lazy_atlas->get ()->CreateOrGetGlyphAtlas (context);
38
+ }
39
+
40
+ if (auto atlas = std::get_if<std::shared_ptr<GlyphAtlas>>(&atlas_)) {
41
+ return *atlas;
42
+ }
43
+
44
+ return nullptr ;
45
+ }
46
+
28
47
void TextContents::SetColor (Color color) {
29
48
color_ = color;
30
49
}
@@ -36,7 +55,9 @@ bool TextContents::Render(const ContentContext& renderer,
36
55
return true ;
37
56
}
38
57
39
- if (!atlas_ || !atlas_->IsValid ()) {
58
+ auto atlas = ResolveAtlas (renderer.GetContext ());
59
+
60
+ if (!atlas || !atlas->IsValid ()) {
40
61
VALIDATION_LOG << " Cannot render glyphs without prepared atlas." ;
41
62
return false ;
42
63
}
@@ -57,15 +78,15 @@ bool TextContents::Render(const ContentContext& renderer,
57
78
frame_info.mvp = Matrix::MakeOrthographic (pass.GetRenderTargetSize ()) *
58
79
entity.GetTransformation ();
59
80
frame_info.atlas_size =
60
- Point {static_cast <Scalar>(atlas_ ->GetTexture ()->GetSize ().width ),
61
- static_cast <Scalar>(atlas_ ->GetTexture ()->GetSize ().height )};
81
+ Point {static_cast <Scalar>(atlas ->GetTexture ()->GetSize ().width ),
82
+ static_cast <Scalar>(atlas ->GetTexture ()->GetSize ().height )};
62
83
frame_info.text_color = ToVector (color_);
63
84
VS::BindFrameInfo (cmd, pass.GetTransientsBuffer ().EmplaceUniform (frame_info));
64
85
65
86
// Common fragment uniforms for all glyphs.
66
87
FS::BindGlyphAtlasSampler (
67
88
cmd, // command
68
- atlas_ ->GetTexture (), // texture
89
+ atlas ->GetTexture (), // texture
69
90
renderer.GetContext ()->GetSamplerLibrary ()->GetSampler ({}) // sampler
70
91
);
71
92
@@ -107,7 +128,7 @@ bool TextContents::Render(const ContentContext& renderer,
107
128
// Draw each glyph individually. This should probably be batched.
108
129
for (const auto & glyph_position : run.GetGlyphPositions ()) {
109
130
FontGlyphPair font_glyph_pair{font, glyph_position.glyph };
110
- auto atlas_glyph_pos = atlas_ ->FindFontGlyphPosition (font_glyph_pair);
131
+ auto atlas_glyph_pos = atlas ->FindFontGlyphPosition (font_glyph_pair);
111
132
if (!atlas_glyph_pos.has_value ()) {
112
133
VALIDATION_LOG << " Could not find glyph position in the atlas." ;
113
134
return false ;
0 commit comments