| Flutter Impeller |

lazy_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/lazy_glyph_atlas.h"
6
7 #include "impeller/base/validation.h"
8 #include "impeller/typographer/typographer_context.h"
9
10 #include <utility>
11
12 namespace impeller {
13
14 LazyGlyphAtlas::LazyGlyphAtlas(
15 std::shared_ptr<TypographerContext> typographer_context)
16 : typographer_context_(std::move(typographer_context)),
17 alpha_context_(typographer_context_
18 ? typographer_context_->CreateGlyphAtlasContext()
19 : nullptr),
20 color_context_(typographer_context_
21 ? typographer_context_->CreateGlyphAtlasContext()
22 : nullptr) {}
23
24 LazyGlyphAtlas::~LazyGlyphAtlas() = default;
25
26 void LazyGlyphAtlas::AddTextFrame(const TextFrame& frame, Scalar scale) {
27 FML_DCHECK(atlas_map_.empty());
28 if (frame.GetAtlasType() == GlyphAtlas::Type::kAlphaBitmap) {
29 frame.CollectUniqueFontGlyphPairs(alpha_glyph_map_, scale);
30 } else {
31 frame.CollectUniqueFontGlyphPairs(color_glyph_map_, scale);
32 }
33 }
34
35 void LazyGlyphAtlas::ResetTextFrames() {
36 alpha_glyph_map_.clear();
37 color_glyph_map_.clear();
38 atlas_map_.clear();
39 }
40
41 std::shared_ptr<GlyphAtlas> LazyGlyphAtlas::CreateOrGetGlyphAtlas(
42 Context& context,
43 GlyphAtlas::Type type) const {
44 {
45 auto atlas_it = atlas_map_.find(type);
46 if (atlas_it != atlas_map_.end()) {
47 return atlas_it->second;
48 }
49 }
50
51 if (!typographer_context_) {
52 VALIDATION_LOG << "Unable to render text because a TypographerContext has "
53 "not been set.";
54 return nullptr;
55 }
56 if (!typographer_context_->IsValid()) {
57 VALIDATION_LOG
58 << "Unable to render text because the TypographerContext is invalid.";
59 return nullptr;
60 }
61
62 auto& glyph_map = type == GlyphAtlas::Type::kAlphaBitmap ? alpha_glyph_map_
63 : color_glyph_map_;
64 auto atlas_context =
65 type == GlyphAtlas::Type::kAlphaBitmap ? alpha_context_ : color_context_;
66 auto atlas = typographer_context_->CreateGlyphAtlas(context, type,
67 atlas_context, glyph_map);
68 if (!atlas || !atlas->IsValid()) {
69 VALIDATION_LOG << "Could not create valid atlas.";
70 return nullptr;
71 }
72 atlas_map_[type] = atlas;
73 return atlas;
74 }
75
76 } // namespace impeller
impeller::LazyGlyphAtlas::~LazyGlyphAtlas
~LazyGlyphAtlas()
lazy_glyph_atlas.h
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::LazyGlyphAtlas::ResetTextFrames
void ResetTextFrames()
Definition: lazy_glyph_atlas.cc:35
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::TextFrame
Represents a collection of shaped text runs.
Definition: text_frame.h:19
impeller::LazyGlyphAtlas::AddTextFrame
void AddTextFrame(const TextFrame &frame, Scalar scale)
Definition: lazy_glyph_atlas.cc:26
impeller::TextFrame::CollectUniqueFontGlyphPairs
void CollectUniqueFontGlyphPairs(FontGlyphMap &glyph_map, Scalar scale) const
Definition: text_frame.cc:70
typographer_context.h
validation.h
impeller::LazyGlyphAtlas::CreateOrGetGlyphAtlas
std::shared_ptr< GlyphAtlas > CreateOrGetGlyphAtlas(Context &context, GlyphAtlas::Type type) const
Definition: lazy_glyph_atlas.cc:41
impeller::LazyGlyphAtlas::LazyGlyphAtlas
LazyGlyphAtlas(std::shared_ptr< TypographerContext > typographer_context)
Definition: lazy_glyph_atlas.cc:14
impeller::GlyphAtlas::Type
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:32
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:47
std
Definition: comparable.h:98
impeller::TextFrame::GetAtlasType
GlyphAtlas::Type GetAtlasType() const
The type of atlas this run should be emplaced in.
Definition: text_frame.cc:28
impeller
Definition: aiks_context.cc:10