22// Use of this source code is governed by a BSD-style license that can be
33// found in the LICENSE file.
44
5- #include " text_contents.h"
5+ #include " impeller/entity/contents/ text_contents.h"
66
77#include " impeller/entity/contents/content_context.h"
88#include " impeller/entity/entity.h"
1111#include " impeller/renderer/sampler_library.h"
1212#include " impeller/tessellator/tessellator.h"
1313#include " impeller/typographer/glyph_atlas.h"
14+ #include " impeller/typographer/lazy_glyph_atlas.h"
1415
1516namespace impeller {
17+
1618TextContents::TextContents () = default ;
1719
1820TextContents::~TextContents () = default ;
@@ -25,6 +27,23 @@ void TextContents::SetGlyphAtlas(std::shared_ptr<GlyphAtlas> atlas) {
2527 atlas_ = std::move (atlas);
2628}
2729
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+
2847void TextContents::SetColor (Color color) {
2948 color_ = color;
3049}
@@ -36,7 +55,9 @@ bool TextContents::Render(const ContentContext& renderer,
3655 return true ;
3756 }
3857
39- if (!atlas_ || !atlas_->IsValid ()) {
58+ auto atlas = ResolveAtlas (renderer.GetContext ());
59+
60+ if (!atlas || !atlas->IsValid ()) {
4061 VALIDATION_LOG << " Cannot render glyphs without prepared atlas." ;
4162 return false ;
4263 }
@@ -57,15 +78,15 @@ bool TextContents::Render(const ContentContext& renderer,
5778 frame_info.mvp = Matrix::MakeOrthographic (pass.GetRenderTargetSize ()) *
5879 entity.GetTransformation ();
5980 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 )};
6283 frame_info.text_color = ToVector (color_);
6384 VS::BindFrameInfo (cmd, pass.GetTransientsBuffer ().EmplaceUniform (frame_info));
6485
6586 // Common fragment uniforms for all glyphs.
6687 FS::BindGlyphAtlasSampler (
6788 cmd, // command
68- atlas_ ->GetTexture (), // texture
89+ atlas ->GetTexture (), // texture
6990 renderer.GetContext ()->GetSamplerLibrary ()->GetSampler ({}) // sampler
7091 );
7192
@@ -107,7 +128,7 @@ bool TextContents::Render(const ContentContext& renderer,
107128 // Draw each glyph individually. This should probably be batched.
108129 for (const auto & glyph_position : run.GetGlyphPositions ()) {
109130 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);
111132 if (!atlas_glyph_pos.has_value ()) {
112133 VALIDATION_LOG << " Could not find glyph position in the atlas." ;
113134 return false ;
0 commit comments