Flutter Engine
The Flutter Engine
TextLine.h
Go to the documentation of this file.
1// Copyright 2019 Google LLC.
2#ifndef TextLine_DEFINED
3#define TextLine_DEFINED
4
15
16#include <stddef.h>
17#include <functional>
18#include <memory>
19#include <vector>
20
21class SkString;
22
23namespace skia {
24namespace textlayout {
25
26class ParagraphImpl;
27
28class TextLine {
29public:
30
31 struct ClipContext {
32 const Run* run;
33 size_t pos;
34 size_t size;
35 SkScalar fTextShift; // Shifts the text inside the run so it's placed at the right position
39 };
40
42 GlyphCluster = 0x01, // All text producing glyphs pointing to the same ClusterIndex
43 GlyphemeCluster = 0x02, // base glyph + all attached diacritics
44 Grapheme = 0x04, // Text adjusted to graphemes
45 GraphemeGluster = 0x05, // GlyphCluster & Grapheme
46 };
47
48 TextLine() = default;
49 TextLine(const TextLine&) = delete;
50 TextLine& operator=(const TextLine&) = delete;
51 TextLine(TextLine&&) = default;
53 ~TextLine() = default;
54
57 SkVector advance,
58 BlockRange blocks,
59 TextRange textExcludingSpaces,
61 TextRange textIncludingNewlines,
63 ClusterRange clustersWithGhosts,
64 SkScalar widthWithSpaces,
66
67 TextRange trimmedText() const { return fTextExcludingSpaces; }
68 TextRange textWithNewlines() const { return fTextIncludingNewlines; }
69 TextRange text() const { return fText; }
70 ClusterRange clusters() const { return fClusterRange; }
71 ClusterRange clustersWithSpaces() const { return fGhostClusterRange; }
72 Run* ellipsis() const { return fEllipsis.get(); }
73 InternalLineMetrics sizes() const { return fSizes; }
74 bool empty() const { return fTextExcludingSpaces.empty(); }
75
76 SkScalar spacesWidth() const { return fWidthWithSpaces - width(); }
77 SkScalar height() const { return fAdvance.fY; }
78 SkScalar width() const {
79 return fAdvance.fX + (fEllipsis != nullptr ? fEllipsis->fAdvance.fX : 0);
80 }
81 SkScalar widthWithoutEllipsis() const { return fAdvance.fX; }
82 SkVector offset() const;
83
84 SkScalar alphabeticBaseline() const { return fSizes.alphabeticBaseline(); }
86 SkScalar baseline() const { return fSizes.baseline(); }
87
89 const Run* run, SkScalar runOffset, TextRange textRange, SkScalar* width)>;
90 void iterateThroughVisualRuns(bool includingGhostSpaces, const RunVisitor& runVisitor) const;
92 TextRange textRange, const TextStyle& style, const ClipContext& context)>;
94 const Run* run,
95 SkScalar runOffset,
96 TextRange textRange,
97 StyleType styleType,
98 const RunStyleVisitor& visitor) const;
99
100 using ClustersVisitor = std::function<bool(const Cluster* cluster, ClusterIndex index, bool ghost)>;
102 bool includeGhosts,
103 const ClustersVisitor& visitor) const;
104
105 void format(TextAlign align, SkScalar maxWidth);
106 void paint(ParagraphPainter* painter, SkScalar x, SkScalar y);
109
110 void createEllipsis(SkScalar maxWidth, const SkString& ellipsis, bool ltr);
111
112 // For testing internal structures
113 void scanStyles(StyleType style, const RunStyleVisitor& visitor);
114
115 void setMaxRunMetrics(const InternalLineMetrics& metrics) { fMaxRunMetrics = metrics; }
116 InternalLineMetrics getMaxRunMetrics() const { return fMaxRunMetrics; }
117
118 bool isFirstLine() const;
119 bool isLastLine() const;
120 void getRectsForRange(TextRange textRange,
121 RectHeightStyle rectHeightStyle,
122 RectWidthStyle rectWidthStyle,
123 std::vector<TextBox>& boxes) const;
124 void getRectsForPlaceholders(std::vector<TextBox>& boxes);
126
127 ClipContext measureTextInsideOneRun(TextRange textRange,
128 const Run* run,
129 SkScalar runOffsetInLine,
130 SkScalar textOffsetInRunInLine,
131 bool includeGhostSpaces,
132 TextAdjustment textAdjustment) const;
133
134 LineMetrics getMetrics() const;
135
136 SkRect extendHeight(const ClipContext& context) const;
137
138 void shiftVertically(SkScalar shift) { fOffset.fY += shift; }
139
140 void setAscentStyle(LineMetricStyle style) { fAscentStyle = style; }
141 void setDescentStyle(LineMetricStyle style) { fDescentStyle = style; }
142
143 bool endsWithHardLineBreak() const;
144
145private:
146 std::unique_ptr<Run> shapeEllipsis(const SkString& ellipsis, const Cluster* cluster);
147 void justify(SkScalar maxWidth);
148
149 void buildTextBlob(TextRange textRange, const TextStyle& style, const ClipContext& context);
150 void paintBackground(ParagraphPainter* painter,
151 SkScalar x,
152 SkScalar y,
153 TextRange textRange,
154 const TextStyle& style,
155 const ClipContext& context) const;
156 void paintShadow(ParagraphPainter* painter,
157 SkScalar x,
158 SkScalar y,
159 TextRange textRange,
160 const TextStyle& style,
161 const ClipContext& context) const;
162 void paintDecorations(ParagraphPainter* painter,
163 SkScalar x,
164 SkScalar y,
165 TextRange textRange,
166 const TextStyle& style,
167 const ClipContext& context) const;
168
169 void shiftCluster(const Cluster* cluster, SkScalar shift, SkScalar prevShift);
170
171 ParagraphImpl* fOwner;
172 BlockRange fBlockRange;
173 TextRange fTextExcludingSpaces;
174 TextRange fText;
175 TextRange fTextIncludingNewlines;
176 ClusterRange fClusterRange;
177 ClusterRange fGhostClusterRange;
178 // Avoid the malloc/free in the common case of one run per line
180 SkVector fAdvance; // Text size
181 SkVector fOffset; // Text position
182 SkScalar fShift; // Let right
183 SkScalar fWidthWithSpaces;
184 std::unique_ptr<Run> fEllipsis; // In case the line ends with the ellipsis
185 InternalLineMetrics fSizes; // Line metrics as a max of all run metrics and struts
186 InternalLineMetrics fMaxRunMetrics; // No struts - need it for GetRectForRange(max height)
187 bool fHasBackground;
188 bool fHasShadows;
189 bool fHasDecorations;
190
191 LineMetricStyle fAscentStyle;
192 LineMetricStyle fDescentStyle;
193
194 struct TextBlobRecord {
195 void paint(ParagraphPainter* painter, SkScalar x, SkScalar y);
196
197 sk_sp<SkTextBlob> fBlob;
198 SkPoint fOffset = SkPoint::Make(0.0f, 0.0f);
201 bool fClippingNeeded = false;
202 SkRect fClipRect = SkRect::MakeEmpty();
203
204 // Extra fields only used for the (experimental) visitor
205 const Run* fVisitor_Run;
206 size_t fVisitor_Pos;
207 };
208 bool fTextBlobCachePopulated;
209public:
210 std::vector<TextBlobRecord> fTextBlobCache;
211};
212} // namespace textlayout
213} // namespace skia
214
215namespace sknonstd {
216 template <> struct is_bitmask_enum<skia::textlayout::TextLine::TextAdjustment> : std::true_type {};
217} // namespace sknonstd
218
219#endif // TextLine_DEFINED
const SkRect fBounds
SkScalar alphabeticBaseline() const
Definition: Run.h:491
SkScalar ideographicBaseline() const
Definition: Run.h:492
std::variant< SkPaint, PaintID > SkPaintOrID
TextRange textWithNewlines() const
Definition: TextLine.h:68
TextLine(const TextLine &)=delete
SkRect extendHeight(const ClipContext &context) const
Definition: TextLine.cpp:343
void getRectsForPlaceholders(std::vector< TextBox > &boxes)
Definition: TextLine.cpp:1510
ClipContext measureTextInsideOneRun(TextRange textRange, const Run *run, SkScalar runOffsetInLine, SkScalar textOffsetInRunInLine, bool includeGhostSpaces, TextAdjustment textAdjustment) const
Definition: TextLine.cpp:768
TextRange trimmedText() const
Definition: TextLine.h:67
SkVector offset() const
Definition: TextLine.cpp:1109
TextLine(TextLine &&)=default
void scanStyles(StyleType style, const RunStyleVisitor &visitor)
Definition: TextLine.cpp:319
std::function< bool(const Cluster *cluster, ClusterIndex index, bool ghost)> ClustersVisitor
Definition: TextLine.h:100
SkScalar height() const
Definition: TextLine.h:77
bool endsWithHardLineBreak() const
Definition: TextLine.cpp:1168
std::function< bool(const Run *run, SkScalar runOffset, TextRange textRange, SkScalar *width)> RunVisitor
Definition: TextLine.h:89
SkScalar spacesWidth() const
Definition: TextLine.h:76
void setAscentStyle(LineMetricStyle style)
Definition: TextLine.h:140
void shiftVertically(SkScalar shift)
Definition: TextLine.h:138
TextLine & operator=(const TextLine &)=delete
void format(TextAlign align, SkScalar maxWidth)
Definition: TextLine.cpp:298
SkScalar iterateThroughSingleRunByStyles(TextAdjustment textAdjustment, const Run *run, SkScalar runOffset, TextRange textRange, StyleType styleType, const RunStyleVisitor &visitor) const
Definition: TextLine.cpp:944
void visit(SkScalar x, SkScalar y)
void iterateThroughClustersInGlyphsOrder(bool reverse, bool includeGhosts, const ClustersVisitor &visitor) const
Definition: TextLine.cpp:913
SkScalar widthWithoutEllipsis() const
Definition: TextLine.h:81
PositionWithAffinity getGlyphPositionAtCoordinate(SkScalar dx)
Definition: TextLine.cpp:1381
ClusterRange clusters() const
Definition: TextLine.h:70
void setMaxRunMetrics(const InternalLineMetrics &metrics)
Definition: TextLine.h:115
void createEllipsis(SkScalar maxWidth, const SkString &ellipsis, bool ltr)
Definition: TextLine.cpp:580
SkScalar width() const
Definition: TextLine.h:78
void getRectsForRange(TextRange textRange, RectHeightStyle rectHeightStyle, RectWidthStyle rectWidthStyle, std::vector< TextBox > &boxes) const
Definition: TextLine.cpp:1176
TextRange text() const
Definition: TextLine.h:69
std::function< void(TextRange textRange, const TextStyle &style, const ClipContext &context)> RunStyleVisitor
Definition: TextLine.h:92
SkScalar ideographicBaseline() const
Definition: TextLine.h:85
InternalLineMetrics getMaxRunMetrics() const
Definition: TextLine.h:116
std::vector< TextBlobRecord > fTextBlobCache
Definition: TextLine.h:210
SkScalar alphabeticBaseline() const
Definition: TextLine.h:84
void paint(ParagraphPainter *painter, SkScalar x, SkScalar y)
Definition: TextLine.cpp:187
SkScalar baseline() const
Definition: TextLine.h:86
TextLine & operator=(TextLine &&)=default
Run * ellipsis() const
Definition: TextLine.h:72
LineMetrics getMetrics() const
Definition: TextLine.cpp:1113
InternalLineMetrics sizes() const
Definition: TextLine.h:73
void setDescentStyle(LineMetricStyle style)
Definition: TextLine.h:141
ClusterRange clustersWithSpaces() const
Definition: TextLine.h:71
void iterateThroughVisualRuns(bool includingGhostSpaces, const RunVisitor &runVisitor) const
Definition: TextLine.cpp:1053
float SkScalar
Definition: extension.cpp:12
Dart_NativeFunction function
Definition: fuchsia.cc:51
double y
double x
skia_private::AutoTArray< sk_sp< SkImageFilter > > filters TypedMatrix matrix TypedMatrix matrix SkScalar dx
Definition: SkRecords.h:208
Definition: run.py:1
size_t ClusterIndex
Definition: Run.h:35
Definition: DartTypes.h:13
float fX
x-axis value
Definition: SkPoint_impl.h:164
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
float fY
y-axis value
Definition: SkPoint_impl.h:165
static constexpr SkRect MakeEmpty()
Definition: SkRect.h:595