Flutter Impeller
impeller::TextFrame Class Reference

Represents a collection of shaped text runs. More...

#include <text_frame.h>

Public Member Functions

 TextFrame ()
 
 TextFrame (std::vector< TextRun > &runs, Rect bounds, bool has_color)
 
 ~TextFrame ()
 
void CollectUniqueFontGlyphPairs (FontGlyphMap &glyph_map, Scalar scale, Point offset, const GlyphProperties &properties) const
 
Rect GetBounds () const
 The conservative bounding box for this text frame. More...
 
size_t GetRunCount () const
 The number of runs in this text frame. More...
 
const std::vector< TextRun > & GetRuns () const
 Returns a reference to all the text runs in this frame. More...
 
bool HasColor () const
 Returns the paint color this text frame was recorded with. More...
 
GlyphAtlas::Type GetAtlasType () const
 The type of atlas this run should be emplaced in. More...
 
TextFrameoperator= (TextFrame &&other)=default
 
 TextFrame (const TextFrame &other)=default
 

Static Public Member Functions

static Point ComputeSubpixelPosition (const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, Point offset, Scalar scale)
 
static Scalar RoundScaledFontSize (Scalar scale, Scalar point_size)
 

Detailed Description

Represents a collection of shaped text runs.

        This object is typically the entrypoint in the Impeller type
        rendering subsystem.

Definition at line 19 of file text_frame.h.

Constructor & Destructor Documentation

◆ TextFrame() [1/3]

impeller::TextFrame::TextFrame ( )
default

◆ TextFrame() [2/3]

impeller::TextFrame::TextFrame ( std::vector< TextRun > &  runs,
Rect  bounds,
bool  has_color 
)

Definition at line 13 of file text_frame.cc.

14  : runs_(std::move(runs)), bounds_(bounds), has_color_(has_color) {}

◆ ~TextFrame()

impeller::TextFrame::~TextFrame ( )
default

◆ TextFrame() [3/3]

impeller::TextFrame::TextFrame ( const TextFrame other)
default

Member Function Documentation

◆ CollectUniqueFontGlyphPairs()

void impeller::TextFrame::CollectUniqueFontGlyphPairs ( FontGlyphMap glyph_map,
Scalar  scale,
Point  offset,
const GlyphProperties properties 
) const

Definition at line 87 of file text_frame.cc.

91  {
92  std::optional<GlyphProperties> lookup =
93  (properties.stroke || HasColor())
94  ? std::optional<GlyphProperties>(properties)
95  : std::nullopt;
96  for (const TextRun& run : GetRuns()) {
97  const Font& font = run.GetFont();
98  auto rounded_scale =
99  RoundScaledFontSize(scale, font.GetMetrics().point_size);
100  auto& set = glyph_map[ScaledFont{font, rounded_scale}];
101  for (const TextRun::GlyphPosition& glyph_position :
102  run.GetGlyphPositions()) {
103  Point subpixel = ComputeSubpixelPosition(
104  glyph_position, font.GetAxisAlignment(), offset, scale);
105  set.emplace(glyph_position.glyph, subpixel, lookup);
106  }
107  }
108 }

References ComputeSubpixelPosition(), impeller::Font::GetAxisAlignment(), impeller::Font::GetMetrics(), GetRuns(), HasColor(), offset, impeller::Font::Metrics::point_size, RoundScaledFontSize(), scale, and impeller::GlyphProperties::stroke.

Referenced by impeller::LazyGlyphAtlas::AddTextFrame(), and impeller::testing::CreateGlyphAtlas().

◆ ComputeSubpixelPosition()

Point impeller::TextFrame::ComputeSubpixelPosition ( const TextRun::GlyphPosition glyph_position,
AxisAlignment  alignment,
Point  offset,
Scalar  scale 
)
static

Definition at line 68 of file text_frame.cc.

72  {
73  Point pos = glyph_position.position + offset;
74  switch (alignment) {
76  return Point(0, 0);
77  case AxisAlignment::kX:
78  return Point(ComputeFractionalPosition(pos.x * scale), 0);
79  case AxisAlignment::kY:
80  return Point(0, ComputeFractionalPosition(pos.y * scale));
82  return Point(ComputeFractionalPosition(pos.x * scale),
84  }
85 }

References impeller::ComputeFractionalPosition(), impeller::kAll, impeller::kNone, impeller::kX, impeller::kY, offset, impeller::TextRun::GlyphPosition::position, scale, impeller::TPoint< T >::x, and impeller::TPoint< T >::y.

Referenced by CollectUniqueFontGlyphPairs(), and impeller::TextContents::Render().

◆ GetAtlasType()

GlyphAtlas::Type impeller::TextFrame::GetAtlasType ( ) const

The type of atlas this run should be emplaced in.

Definition at line 30 of file text_frame.cc.

30  {
31  return has_color_ ? GlyphAtlas::Type::kColorBitmap
33 }

References impeller::GlyphAtlas::kAlphaBitmap, and impeller::GlyphAtlas::kColorBitmap.

Referenced by impeller::LazyGlyphAtlas::AddTextFrame().

◆ GetBounds()

Rect impeller::TextFrame::GetBounds ( ) const

The conservative bounding box for this text frame.

Returns
The bounds rectangle. If there are no glyphs in this text frame and empty Rectangle is returned instead.

Definition at line 18 of file text_frame.cc.

18  {
19  return bounds_;
20 }

◆ GetRunCount()

size_t impeller::TextFrame::GetRunCount ( ) const

The number of runs in this text frame.

Returns
The run count.

Definition at line 22 of file text_frame.cc.

22  {
23  return runs_.size();
24 }

◆ GetRuns()

const std::vector< TextRun > & impeller::TextFrame::GetRuns ( ) const

Returns a reference to all the text runs in this frame.

Returns
The runs in this frame.

Definition at line 26 of file text_frame.cc.

26  {
27  return runs_;
28 }

Referenced by CollectUniqueFontGlyphPairs().

◆ HasColor()

bool impeller::TextFrame::HasColor ( ) const

Returns the paint color this text frame was recorded with.

        Non-bitmap/COLR fonts always use a black text color here, but
        COLR fonts can potentially use the paint color in the glyph
        atlas, so this color must be considered as part of the cache
        key. 

Definition at line 35 of file text_frame.cc.

35  {
36  return has_color_;
37 }

Referenced by CollectUniqueFontGlyphPairs().

◆ operator=()

TextFrame& impeller::TextFrame::operator= ( TextFrame &&  other)
default

◆ RoundScaledFontSize()

Scalar impeller::TextFrame::RoundScaledFontSize ( Scalar  scale,
Scalar  point_size 
)
static

Definition at line 40 of file text_frame.cc.

40  {
41  // An arbitrarily chosen maximum text scale to ensure that regardless of the
42  // CTM, a glyph will fit in the atlas. If we clamp significantly, this may
43  // reduce fidelity but is preferable to the alternative of failing to render.
44  constexpr Scalar kMaximumTextScale = 48;
45  Scalar result = std::round(scale * 100) / 100;
46  return std::clamp(result, 0.0f, kMaximumTextScale);
47 }

References scale.

Referenced by CollectUniqueFontGlyphPairs(), impeller::TextContents::Render(), and impeller::testing::TEST_P().


The documentation for this class was generated from the following files:
impeller::GlyphAtlas::Type::kColorBitmap
@ kColorBitmap
impeller::TextFrame::HasColor
bool HasColor() const
Returns the paint color this text frame was recorded with.
Definition: text_frame.cc:35
impeller::AxisAlignment::kAll
@ kAll
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::GlyphAtlas::Type::kAlphaBitmap
@ kAlphaBitmap
impeller::TextFrame::ComputeSubpixelPosition
static Point ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, Point offset, Scalar scale)
Definition: text_frame.cc:68
impeller::TextFrame::GetRuns
const std::vector< TextRun > & GetRuns() const
Returns a reference to all the text runs in this frame.
Definition: text_frame.cc:26
impeller::TextFrame::RoundScaledFontSize
static Scalar RoundScaledFontSize(Scalar scale, Scalar point_size)
Definition: text_frame.cc:40
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:304
impeller::Point
TPoint< Scalar > Point
Definition: point.h:327
impeller::AxisAlignment::kX
@ kX
scale
const Scalar scale
Definition: stroke_path_geometry.cc:301
impeller::ComputeFractionalPosition
static constexpr Scalar ComputeFractionalPosition(Scalar value)
Definition: text_frame.cc:49
impeller::AxisAlignment::kNone
@ kNone
impeller::AxisAlignment::kY
@ kY