| Flutter Impeller |

Public Member Functions | List of all members
impeller::TextContents Class Referencefinal

`#include <text_contents.h>`

Inheritance diagram for impeller::TextContents:

## Public Member Functions
| TextContents ()
| ~TextContents ()
void | SetTextFrame (const std::shared_ptr< TextFrame > &frame)
void | SetColor (Color color)
void | SetForceTextColor (bool value)
| Force the text color to apply to the rendered glyphs, even if those glyphs are bitmaps. More...
void | SetTextProperties (Color color, bool stroke, Scalar stroke_width, Cap stroke_cap, Join stroke_join, Scalar stroke_miter)
| Must be set after text frame. More...
Color | GetColor () const
void | SetInheritedOpacity (Scalar opacity) override
| Inherit the provided opacity. More...
void | SetOffset (Vector2 offset)
std::optional< Rect > | GetTextFrameBounds () const
std::optional< Rect > | GetCoverage (const Entity &entity) const override
| Get the area of the render pass that will be affected when this contents is rendered. More...
void | SetScale (Scalar scale)
bool | Render (const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Public Member Functions inherited from impeller::Contents
| Contents ()
virtual | ~Contents ()
void | SetCoverageHint (std::optional< Rect > coverage_hint)
| Hint that specifies the coverage area of this Contents that will actually be used during rendering. This is for optimization purposes only and can not be relied on as a clip. May optionally affect the result of `GetCoverage()`. More...
const std::optional< Rect > & | GetCoverageHint () const
virtual bool | IsOpaque (const Matrix &transform) const
| Whether this Contents only emits opaque source colors from the fragment stage. This value does not account for any entity properties (e.g. the blend mode), clips/visibility culling, or inherited opacity. More...
virtual ClipCoverage | GetClipCoverage (const Entity &entity, const std::optional< Rect > &current_clip_coverage) const
| Given the current pass space bounding rectangle of the clip buffer, return the expected clip coverage after this draw call. This should only be implemented for contents that may write to the clip buffer. More...
virtual std::optional< Snapshot > | RenderToSnapshot (const ContentContext &renderer, const Entity &entity, std::optional< Rect > coverage_limit=std::nullopt, const std::optional< SamplerDescriptor > &sampler_descriptor=std::nullopt, bool msaa_enabled=true, int32_t mip_count=1, const std::string &label="Snapshot") const
| Render this contents to a snapshot, respecting the entity's transform, path, clip depth, and blend mode. The result texture size is always the size of `GetCoverage(entity)`. More...
std::optional< Size > | GetColorSourceSize () const
| Return the color source's intrinsic size, if available. More...
void | SetColorSourceSize (Size size)
virtual std::optional< Color > | AsBackgroundColor (const Entity &entity, ISize target_size) const
| Returns a color if this Contents will flood the given `target_size` with a color. This output color is the "Source" color that will be used for the Entity's blend operation. More...
virtual const FilterContents * | AsFilter () const
| Cast to a filter. Returns `nullptr` if this Contents is not a filter. More...
virtual bool | ApplyColorFilter (const ColorFilterProc &color_filter_proc)
| If possible, applies a color filter to this contents inputs on the CPU. More...

## Additional Inherited Members
Public Types inherited from impeller::Contents
using | ColorFilterProc = std::function< Color(Color)>
using | RenderProc = std::function< bool(const ContentContext &renderer, const Entity &entity, RenderPass &pass)>
using | CoverageProc = std::function< std::optional< Rect >(const Entity &entity)>
Static Public Member Functions inherited from impeller::Contents
static std::shared_ptr< Contents > | MakeAnonymous (RenderProc render_proc, CoverageProc coverage_proc)

## Detailed Description

Definition at line 20 of file text_contents.h.

## Constructor & Destructor Documentation

## ◆ TextContents()

impeller::TextContents::TextContents | ( |  | ) |
| default

## ◆ ~TextContents()

impeller::TextContents::~TextContents | ( |  | ) |
| default

## Member Function Documentation

## ◆ GetColor()

Color impeller::TextContents::GetColor | ( |  | ) | const

Definition at line 36 of file text_contents.cc.

36 {
37 return color_.WithAlpha(color_.alpha * inherited_opacity_);
38 }

References impeller::Color::alpha, and impeller::Color::WithAlpha().

Referenced by Render().

## ◆ GetCoverage()

std::optional< Rect > impeller::TextContents::GetCoverage | ( | const Entity & | entity | ) | const
| overridevirtual

Get the area of the render pass that will be affected when this contents is rendered.

During rendering, coverage coordinates count pixels from the top left corner of the framebuffer.

Returns
  The coverage rectangle. An `std::nullopt` result means that rendering this contents has no effect on the output color.

Implements impeller::Contents.

Definition at line 52 of file text_contents.cc.

52 {
53 return frame_->GetBounds().TransformBounds(entity.GetTransform());
54 }

References impeller::Entity::GetTransform().

## ◆ GetTextFrameBounds()

std::optional<Rect> impeller::TextContents::GetTextFrameBounds | ( |  | ) | const

## ◆ Render()

bool impeller::TextContents::Render | ( | const ContentContext & | renderer,
|  | const Entity & | entity,
|  | RenderPass & | pass
| ) |  | const
| overridevirtual

Implements impeller::Contents.

Definition at line 76 of file text_contents.cc.

78 {
79 auto color = GetColor();
80 if (color.IsTransparent()) {
81 return true;
82 }
83
84 auto type = frame_->GetAtlasType();
85 const std::shared_ptr<GlyphAtlas>& atlas =
86 renderer.GetLazyGlyphAtlas()->CreateOrGetGlyphAtlas(
87 *renderer.GetContext(), renderer.GetTransientsBuffer(), type);
88
89 if (!atlas || !atlas->IsValid()) {
90 VALIDATION_LOG << "Cannot render glyphs without prepared atlas.";
91 return false;
92 }
93
94 // Information shared by all glyph draw calls.
95 pass.SetCommandLabel("TextFrame");
96 auto opts = OptionsFromPassAndEntity(pass, entity);
97 opts.primitive_type = PrimitiveType::kTriangle;
98 pass.SetPipeline(renderer.GetGlyphAtlasPipeline(opts));
99
100 using VS = GlyphAtlasPipeline::VertexShader;
101 using FS = GlyphAtlasPipeline::FragmentShader;
102
103 // Common vertex uniforms for all glyphs.
104 VS::FrameInfo frame_info;
105 frame_info.mvp =
106 Entity::GetShaderTransform(entity.GetShaderClipDepth(), pass, Matrix());
107 ISize atlas_size = atlas->GetTexture()->GetSize();
108 bool is_translation_scale = entity.GetTransform().IsTranslationScaleOnly();
109 Matrix entity_transform = entity.GetTransform();
110 Matrix basis_transform = entity_transform.Basis();
111
112 VS::BindFrameInfo(pass,
113 renderer.GetTransientsBuffer().EmplaceUniform(frame_info));
114
115 FS::FragInfo frag_info;
116 frag_info.use_text_color = force_text_color_ ? 1.0 : 0.0;
117 frag_info.text_color = ToVector(color.Premultiply());
118 frag_info.is_color_glyph = type == GlyphAtlas::Type::kColorBitmap;
119
120 FS::BindFragInfo(pass,
121 renderer.GetTransientsBuffer().EmplaceUniform(frag_info));
122
123 SamplerDescriptor sampler_desc;
124 if (is_translation_scale) {
125 sampler_desc.min_filter = MinMagFilter::kNearest;
126 sampler_desc.mag_filter = MinMagFilter::kNearest;
127 } else {
128 // Currently, we only propagate the scale of the transform to the atlas
129 // renderer, so if the transform has more than just a translation, we turn
130 // on linear sampling to prevent crunchiness caused by the pixel grid not
131 // being perfectly aligned.
132 // The downside is that this slightly over-blurs rotated/skewed text.
133 sampler_desc.min_filter = MinMagFilter::kLinear;
134 sampler_desc.mag_filter = MinMagFilter::kLinear;
135 }
136
137 // No mipmaps for glyph atlas (glyphs are generated at exact scales).
138 sampler_desc.mip_filter = MipFilter::kBase;
139
140 FS::BindGlyphAtlasSampler(
141 pass, // command
142 atlas->GetTexture(), // texture
143 renderer.GetContext()->GetSamplerLibrary()->GetSampler(
144 sampler_desc) // sampler
145 );
146
147 // Common vertex information for all glyphs.
148 // All glyphs are given the same vertex information in the form of a
149 // unit-sized quad. The size of the glyph is specified in per instance data
150 // and the vertex shader uses this to size the glyph correctly. The
151 // interpolated vertex information is also used in the fragment shader to
152 // sample from the glyph atlas.
153
154 constexpr std::array<Point, 6> unit_points = {Point{0, 0}, Point{1, 0},
155 Point{0, 1}, Point{1, 0},
156 Point{0, 1}, Point{1, 1}};
157
158 auto& host_buffer = renderer.GetTransientsBuffer();
159 size_t vertex_count = 0;
160 for (const auto& run : frame_->GetRuns()) {
161 vertex_count += run.GetGlyphPositions().size();
162 }
163 vertex_count *= 6;
164
165 BufferView buffer_view = host_buffer.Emplace(
166 vertex_count * sizeof(VS::PerVertexData), alignof(VS::PerVertexData),
167 [&](uint8_t* contents) {
168 VS::PerVertexData vtx;
169 VS::PerVertexData* vtx_contents =
170 reinterpret_cast<VS::PerVertexData*>(contents);
171 size_t i = 0u;
172 for (const TextRun& run : frame_->GetRuns()) {
173 const Font& font = run.GetFont();
174 Scalar rounded_scale = TextFrame::RoundScaledFontSize(
175 scale_, font.GetMetrics().point_size);
176 const FontGlyphAtlas* font_atlas =
177 atlas->GetFontGlyphAtlas(font, rounded_scale);
178 if (!font_atlas) {
179 VALIDATION_LOG << "Could not find font in the atlas.";
180 continue;
181 }
182
183 // Adjust glyph position based on the subpixel rounding
184 // used by the font.
185 Point subpixel_adjustment(0.5, 0.5);
186 switch (font.GetAxisAlignment()) {
187 case AxisAlignment::kNone:
188 break;
189 case AxisAlignment::kX:
190 subpixel_adjustment.x = 0.125;
191 break;
192 case AxisAlignment::kY:
193 subpixel_adjustment.y = 0.125;
194 break;
195 case AxisAlignment::kAll:
196 subpixel_adjustment.x = 0.125;
197 subpixel_adjustment.y = 0.125;
198 break;
199 }
200
201 Point screen_offset = (entity_transform * Point(0, 0));
202 for (const TextRun::GlyphPosition& glyph_position :
203 run.GetGlyphPositions()) {
204 // Note: uses unrounded scale for more accurate subpixel position.
205 Point subpixel = TextFrame::ComputeSubpixelPosition(
206 glyph_position, font.GetAxisAlignment(), offset_, scale_);
207 std::optional<std::pair<Rect, Rect>> maybe_atlas_glyph_bounds =
208 font_atlas->FindGlyphBounds(SubpixelGlyph{
209 glyph_position.glyph, subpixel,
210 (properties_.stroke || frame_->HasColor())
211 ? std::optional<GlyphProperties>(properties_)
212 : std::nullopt});
213 if (!maybe_atlas_glyph_bounds.has_value()) {
214 VALIDATION_LOG << "Could not find glyph position in the atlas.";
215 continue;
216 }
217 const Rect& atlas_glyph_bounds =
218 maybe_atlas_glyph_bounds.value().first;
219 Rect glyph_bounds = maybe_atlas_glyph_bounds.value().second;
220 Rect scaled_bounds = glyph_bounds.Scale(1.0 / rounded_scale);
221 // For each glyph, we compute two rectangles. One for the vertex
222 // positions and one for the texture coordinates (UVs). The atlas
223 // glyph bounds are used to compute UVs in cases where the
224 // destination and source sizes may differ due to clamping the sizes
225 // of large glyphs.
226 Point uv_origin =
227 (atlas_glyph_bounds.GetLeftTop() - Point(0.5, 0.5)) /
228 atlas_size;
229 Point uv_size =
230 (atlas_glyph_bounds.GetSize() + Point(1, 1)) / atlas_size;
231
232 Point unrounded_glyph_position =
233 basis_transform *
234 (glyph_position.position + scaled_bounds.GetLeftTop());
235
236 Point screen_glyph_position =
237 (screen_offset + unrounded_glyph_position + subpixel_adjustment)
238 .Floor();
239
240 for (const Point& point : unit_points) {
241 Point position;
242 if (is_translation_scale) {
243 position = (screen_glyph_position +
244 (basis_transform * point * scaled_bounds.GetSize()))
245 .Round();
246 } else {
247 position = entity_transform * (glyph_position.position +
248 scaled_bounds.GetLeftTop() +
249 point * scaled_bounds.GetSize());
250 }
251 vtx.uv = uv_origin + (uv_size * point);
252 vtx.position = position;
253 vtx_contents[i++] = vtx;
254 }
255 }
256 }
257 });
258
259 pass.SetVertexBuffer({
260 .vertex_buffer = std::move(buffer_view),
261 .index_buffer = {},
262 .vertex_count = vertex_count,
263 .index_type = IndexType::kNone,
264 });
265
266 return pass.Draw().ok();
267 }

References impeller::Matrix::Basis(), buffer_view, color, impeller::TextFrame::ComputeSubpixelPosition(), impeller::RenderPass::Draw(), impeller::HostBuffer::EmplaceUniform(), impeller::FontGlyphAtlas::FindGlyphBounds(), impeller::Font::GetAxisAlignment(), GetColor(), impeller::ContentContext::GetContext(), impeller::ContentContext::GetGlyphAtlasPipeline(), impeller::ContentContext::GetLazyGlyphAtlas(), impeller::TRect< T >::GetLeftTop(), impeller::Font::GetMetrics(), impeller::Entity::GetShaderClipDepth(), impeller::Entity::GetShaderTransform(), impeller::TRect< T >::GetSize(), impeller::Entity::GetTransform(), impeller::ContentContext::GetTransientsBuffer(), impeller::SubpixelGlyph::glyph, impeller::Matrix::IsTranslationScaleOnly(), impeller::kAll, impeller::kBase, impeller::GlyphAtlas::kColorBitmap, impeller::kLinear, impeller::kNearest, impeller::kNone, impeller::kTriangle, impeller::kX, impeller::kY, impeller::SamplerDescriptor::mag_filter, impeller::SamplerDescriptor::min_filter, impeller::SamplerDescriptor::mip_filter, impeller::OptionsFromPassAndEntity(), impeller::Font::Metrics::point_size, impeller::TPoint< T >::Round(), impeller::TextFrame::RoundScaledFontSize(), impeller::TRect< T >::Scale(), impeller::RenderPass::SetCommandLabel(), impeller::RenderPass::SetPipeline(), impeller::RenderPass::SetVertexBuffer(), impeller::GlyphProperties::stroke, impeller::ToVector(), type, VALIDATION_LOG, vtx, impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

## ◆ SetColor()

void impeller::TextContents::SetColor | ( | Color | color | ) |

Definition at line 32 of file text_contents.cc.

32 {
33 color_ = color;
34 }

References color.

## ◆ SetForceTextColor()

void impeller::TextContents::SetForceTextColor | ( | bool | value | ) |

Force the text color to apply to the rendered glyphs, even if those glyphs are bitmaps.

This is used to ensure that mask blurs work correctly on emoji.

Definition at line 48 of file text_contents.cc.

48 {
49 force_text_color_ = value;
50 }

## ◆ SetInheritedOpacity()

void impeller::TextContents::SetInheritedOpacity | ( | Scalar | opacity | ) |
| overridevirtual

Inherit the provided opacity.

       Use of this method is invalid if CanAcceptOpacity returns false.

Reimplemented from impeller::Contents.

Definition at line 40 of file text_contents.cc.

40 {
41 inherited_opacity_ = opacity;
42 }

## ◆ SetOffset()

void impeller::TextContents::SetOffset | ( | Vector2 | offset | ) |

Definition at line 44 of file text_contents.cc.

44 {
45 offset_ = offset;
46 }

References offset.

## ◆ SetScale()

void impeller::TextContents::SetScale | ( | Scalar | scale | ) |
| inline

Definition at line 57 of file text_contents.h.

57 { scale_ = scale; }

References scale.

## ◆ SetTextFrame()

void impeller::TextContents::SetTextFrame | ( | const std::shared_ptr< TextFrame > & | frame | ) |

Definition at line 28 of file text_contents.cc.

28 {
29 frame_ = frame;
30 }

## ◆ SetTextProperties()

void impeller::TextContents::SetTextProperties | ( | Color | color,
|  | bool | stroke,
|  | Scalar | stroke_width,
|  | Cap | stroke_cap,
|  | Join | stroke_join,
|  | Scalar | stroke_miter
| ) |  |

Must be set after text frame.

Definition at line 56 of file text_contents.cc.

61 {
62 if (frame_->HasColor()) {
63 // Alpha is always applied when rendering, remove it here so
64 // we do not double-apply the alpha.
65 properties_.color = color.WithAlpha(1.0);
66 }
67 if (stroke) {
68 properties_.stroke = true;
69 properties_.stroke_width = stroke_width;
70 properties_.stroke_cap = stroke_cap;
71 properties_.stroke_join = stroke_join;
72 properties_.stroke_miter = stroke_miter;
73 }
74 }

References impeller::GlyphProperties::color, color, stroke, impeller::GlyphProperties::stroke, impeller::GlyphProperties::stroke_cap, impeller::GlyphProperties::stroke_join, impeller::GlyphProperties::stroke_miter, impeller::GlyphProperties::stroke_width, and stroke_width.

The documentation for this class was generated from the following files:

- impeller/entity/contents/text_contents.h
- impeller/entity/contents/text_contents.cc

impeller::ISize
ISize64 ISize
Definition: size.h:140
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:107
impeller::Entity::GetShaderTransform
Matrix GetShaderTransform(const RenderPass &pass) const
Get the vertex shader transform used for drawing this Entity.
Definition: entity.cc:51
impeller::AxisAlignment::kAll
@ kAll
impeller::Scalar
float Scalar
Definition: scalar.h:18
vtx
SolidFillVertexShader::PerVertexData vtx
Definition: stroke_path_geometry.cc:305
impeller::TextFrame::ComputeSubpixelPosition
static Point ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, Point offset, Scalar scale)
Definition: text_frame.cc:68
impeller::TPoint::Round
static constexpr TPoint Round(const TPoint< U > &other)
Definition: point.h:49
impeller::Color::alpha
Scalar alpha
Definition: color.h:142
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:128
impeller::GlyphProperties::stroke_miter
Scalar stroke_miter
Definition: font_glyph_pair.h:25
impeller::TextFrame::RoundScaledFontSize
static Scalar RoundScaledFontSize(Scalar scale, Scalar point_size)
Definition: text_frame.cc:40
stroke_width
const Scalar stroke_width
Definition: stroke_path_geometry.cc:297
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:304
impeller::GlyphProperties::color
Color color
Definition: font_glyph_pair.h:21
impeller::MinMagFilter::kNearest
@ kNearest
Select nearest to the sample point. Most widely supported.
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::PrimitiveType::kTriangle
@ kTriangle
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:34
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
impeller::MinMagFilter::kLinear
@ kLinear
impeller::Color::WithAlpha
constexpr Color WithAlpha(Scalar new_alpha) const
Definition: color.h:277
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:106
type
GLenum type
Definition: blit_command_gles.cc:127
impeller::IndexType::kNone
@ kNone
Does not use the index buffer.
impeller::TRect::Scale
constexpr TRect Scale(Type scale) const
Definition: rect.h:196
impeller::GlyphProperties::stroke_join
Join stroke_join
Definition: font_glyph_pair.h:24
impeller::GlyphProperties::stroke
bool stroke
Definition: font_glyph_pair.h:26
impeller::Rect
TRect< Scalar > Rect
Definition: rect.h:776
impeller::AxisAlignment::kX
@ kX
impeller::GlyphProperties::stroke_width
Scalar stroke_width
Definition: font_glyph_pair.h:22
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
impeller::GlyphProperties::stroke_cap
Cap stroke_cap
Definition: font_glyph_pair.h:23
impeller::MipFilter::kBase
@ kBase
The texture is sampled as if it only had a single mipmap level.
std
Definition: comparable.h:95
impeller::ToVector
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:188
scale
const Scalar scale
Definition: stroke_path_geometry.cc:301
impeller::TextContents::GetColor
Color GetColor() const
Definition: text_contents.cc:36
color
DlColor color
Definition: dl_golden_blur_unittests.cc:24
impeller::AxisAlignment::kNone
@ kNone
stroke
bool stroke
Definition: dl_golden_blur_unittests.cc:22
impeller::AxisAlignment::kY
@ kY