Flutter Impeller
text_frame.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 
9 
10 namespace impeller {
11 
12 TextFrame::TextFrame() = default;
13 
14 TextFrame::TextFrame(std::vector<TextRun>& runs, Rect bounds, bool has_color)
15  : runs_(std::move(runs)), bounds_(bounds), has_color_(has_color) {}
16 
17 TextFrame::~TextFrame() = default;
18 
20  return bounds_;
21 }
22 
23 size_t TextFrame::GetRunCount() const {
24  return runs_.size();
25 }
26 
27 const std::vector<TextRun>& TextFrame::GetRuns() const {
28  return runs_;
29 }
30 
32  return has_color_ ? GlyphAtlas::Type::kColorBitmap
34 }
35 
36 bool TextFrame::HasColor() const {
37  return has_color_;
38 }
39 
40 // static
42  // An arbitrarily chosen maximum text scale to ensure that regardless of the
43  // CTM, a glyph will fit in the atlas. If we clamp significantly, this may
44  // reduce fidelity but is preferable to the alternative of failing to render.
45  constexpr Scalar kMaximumTextScale = 48;
46  Scalar result = std::round(scale * 200) / 200;
47  return std::clamp(result, 0.0f, kMaximumTextScale);
48 }
49 
51  value += 0.125;
52  value = (value - floorf(value));
53  if (value < 0.25) {
54  return 0;
55  }
56  if (value < 0.5) {
57  return 0.25;
58  }
59  if (value < 0.75) {
60  return 0.5;
61  }
62  return 0.75;
63 }
64 
65 // Compute subpixel position for glyphs based on X position and provided
66 // max basis length (scale).
67 // This logic is based on the SkPackedGlyphID logic in SkGlyph.h
68 // static
70  const TextRun::GlyphPosition& glyph_position,
71  AxisAlignment alignment,
72  const Matrix& transform) {
73  Point pos = transform * glyph_position.position;
74  switch (alignment) {
76  return Point(0, 0);
77  case AxisAlignment::kX:
78  return Point(ComputeFractionalPosition(pos.x), 0);
79  case AxisAlignment::kY:
80  return Point(0, ComputeFractionalPosition(pos.y));
82  return Point(ComputeFractionalPosition(pos.x),
84  }
85 }
86 
88  return transform_ * Matrix::MakeTranslation(offset_);
89 }
90 
92  Point offset,
93  const Matrix& transform,
94  std::optional<GlyphProperties> properties) {
95  bound_values_.clear();
96  scale_ = scale;
97  offset_ = offset;
98  properties_ = properties;
99  transform_ = transform;
100 }
101 
103  return scale_;
104 }
105 
107  return offset_;
108 }
109 
110 std::optional<GlyphProperties> TextFrame::GetProperties() const {
111  return properties_;
112 }
113 
114 void TextFrame::AppendFrameBounds(const FrameBounds& frame_bounds) {
115  bound_values_.push_back(frame_bounds);
116 }
117 
118 void TextFrame::ClearFrameBounds() {
119  bound_values_.clear();
120 }
121 
123  size_t run_size = 0;
124  for (const auto& x : runs_) {
125  run_size += x.GetGlyphCount();
126  }
127  return bound_values_.size() == run_size;
128 }
129 
130 const FrameBounds& TextFrame::GetFrameBounds(size_t index) const {
131  FML_DCHECK(index < bound_values_.size());
132  return bound_values_[index];
133 }
134 
135 std::pair<size_t, intptr_t> TextFrame::GetAtlasGenerationAndID() const {
136  return std::make_pair(generation_, atlas_id_);
137 }
138 
139 void TextFrame::SetAtlasGeneration(size_t value, intptr_t atlas_id) {
140  generation_ = value;
141  atlas_id_ = atlas_id;
142 }
143 
144 } // namespace impeller
Type
Describes how the glyphs are represented in the texture.
Definition: glyph_atlas.h:74
void SetPerFrameData(Scalar scale, Point offset, const Matrix &transform, std::optional< GlyphProperties > properties)
Store text frame scale, offset, and properties for hashing in th glyph atlas.
Definition: text_frame.cc:91
Rect GetBounds() const
The conservative bounding box for this text frame.
Definition: text_frame.cc:19
bool IsFrameComplete() const
Verifies that all glyphs in this text frame have computed bounds information.
Definition: text_frame.cc:122
Scalar GetScale() const
Definition: text_frame.cc:102
GlyphAtlas::Type GetAtlasType() const
The type of atlas this run should be place in.
Definition: text_frame.cc:31
bool HasColor() const
Returns the paint color this text frame was recorded with.
Definition: text_frame.cc:36
const FrameBounds & GetFrameBounds(size_t index) const
Retrieve the frame bounds for the glyph at [index].
Definition: text_frame.cc:130
std::pair< size_t, intptr_t > GetAtlasGenerationAndID() const
Definition: text_frame.cc:135
static Point ComputeSubpixelPosition(const TextRun::GlyphPosition &glyph_position, AxisAlignment alignment, const Matrix &transform)
Definition: text_frame.cc:69
size_t GetRunCount() const
The number of runs in this text frame.
Definition: text_frame.cc:23
Point GetOffset() const
Definition: text_frame.cc:106
Matrix GetOffsetTransform() const
Definition: text_frame.cc:87
static Scalar RoundScaledFontSize(Scalar scale)
Definition: text_frame.cc:41
const std::vector< TextRun > & GetRuns() const
Returns a reference to all the text runs in this frame.
Definition: text_frame.cc:27
int32_t value
int32_t x
float Scalar
Definition: scalar.h:18
TPoint< Scalar > Point
Definition: point.h:327
AxisAlignment
Determines the axis along which there is subpixel positioning.
Definition: font.h:20
static constexpr Scalar ComputeFractionalPosition(Scalar value)
Definition: text_frame.cc:50
Definition: comparable.h:95
const Scalar scale
SeparatedVector2 offset
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95