1 // Copyright 2019 Google LLC.
2 #ifndef Run_DEFINED
3 #define Run_DEFINED
4
5 #include "include/core/SkFont.h"
6 #include "include/core/SkFontMetrics.h"
7 #include "include/core/SkPoint.h"
8 #include "include/core/SkRect.h"
9 #include "include/core/SkScalar.h"
10 #include "include/core/SkSpan.h"
11 #include "include/core/SkTypes.h"
12 #include "include/private/base/SkTArray.h"
13 #include "modules/skparagraph/include/DartTypes.h"
14 #include "modules/skparagraph/include/TextStyle.h"
15 #include "modules/skshaper/include/SkShaper.h"
16
17 #include <math.h>
18 #include <algorithm>
19 #include <functional>
20 #include <limits>
21 #include <tuple>
22
23 class SkTextBlobBuilder;
24
25 namespace skia {
26 namespace textlayout {
27
28 class Cluster;
29 class InternalLineMetrics;
30 class ParagraphImpl;
31
32 typedef size_t RunIndex;
33 const size_t EMPTY_RUN = EMPTY_INDEX;
34
35 typedef size_t ClusterIndex;
36 typedef SkRange<size_t> ClusterRange;
37 const size_t EMPTY_CLUSTER = EMPTY_INDEX;
38 const SkRange<size_t> EMPTY_CLUSTERS = EMPTY_RANGE;
39
40 typedef size_t GraphemeIndex;
41 typedef SkRange<GraphemeIndex> GraphemeRange;
42
43 typedef size_t GlyphIndex;
44 typedef SkRange<GlyphIndex> GlyphRange;
45
46 // LTR: [start: end) where start <= end
47 // RTL: [end: start) where start >= end
48 class DirText {
DirText(bool dir,size_t s,size_t e)49 DirText(bool dir, size_t s, size_t e) : start(s), end(e) { }
isLeftToRight()50 bool isLeftToRight() const { return start <= end; }
51 size_t start;
52 size_t end;
53 };
54
55 class Run {
56 public:
57 Run(ParagraphImpl* owner,
58 const SkShaper::RunHandler::RunInfo& info,
59 size_t firstChar,
60 SkScalar heightMultiplier,
61 bool useHalfLeading,
62 SkScalar baselineShift,
63 size_t index,
64 SkScalar shiftX);
65 Run(const Run&) = default;
66 Run& operator=(const Run&) = delete;
67 Run(Run&&) = default;
68 Run& operator=(Run&&) = delete;
69 ~Run() = default;
70
setOwner(ParagraphImpl * owner)71 void setOwner(ParagraphImpl* owner) { fOwner = owner; }
72
73 SkShaper::RunHandler::Buffer newRunBuffer();
74
posX(size_t index)75 SkScalar posX(size_t index) const { return fPositions[index].fX; }
addX(size_t index,SkScalar shift)76 void addX(size_t index, SkScalar shift) { fPositions[index].fX += shift; }
posY(size_t index)77 SkScalar posY(size_t index) const { return fPositions[index].fY; }
size()78 size_t size() const { return fGlyphs.size(); }
setWidth(SkScalar width)79 void setWidth(SkScalar width) { fAdvance.fX = width; }
setHeight(SkScalar height)80 void setHeight(SkScalar height) { fAdvance.fY = height; }
shift(SkScalar shiftX,SkScalar shiftY)81 void shift(SkScalar shiftX, SkScalar shiftY) {
82 fOffset.fX += shiftX;
83 fOffset.fY += shiftY;
84 }
advance()85 SkVector advance() const {
86 return SkVector::Make(fAdvance.fX, fFontMetrics.fDescent - fFontMetrics.fAscent + fFontMetrics.fLeading);
87 }
offset()88 SkVector offset() const { return fOffset; }
ascent()89 SkScalar ascent() const { return fFontMetrics.fAscent + fBaselineShift; }
descent()90 SkScalar descent() const { return fFontMetrics.fDescent + fBaselineShift; }
leading()91 SkScalar leading() const { return fFontMetrics.fLeading; }
correctAscent()92 SkScalar correctAscent() const { return fCorrectAscent + fBaselineShift; }
correctDescent()93 SkScalar correctDescent() const { return fCorrectDescent + fBaselineShift; }
correctLeading()94 SkScalar correctLeading() const { return fCorrectLeading; }
font()95 const SkFont& font() const { return fFont; }
leftToRight()96 bool leftToRight() const { return fBidiLevel % 2 == 0; }
getTextDirection()97 TextDirection getTextDirection() const { return leftToRight() ? TextDirection::kLtr : TextDirection::kRtl; }
index()98 size_t index() const { return fIndex; }
heightMultiplier()99 SkScalar heightMultiplier() const { return fHeightMultiplier; }
useHalfLeading()100 bool useHalfLeading() const { return fUseHalfLeading; }
baselineShift()101 SkScalar baselineShift() const { return fBaselineShift; }
102 PlaceholderStyle* placeholderStyle() const;
isPlaceholder()103 bool isPlaceholder() const { return fPlaceholderIndex != std::numeric_limits<size_t>::max(); }
clusterIndex(size_t pos)104 size_t clusterIndex(size_t pos) const { return fClusterIndexes[pos]; }
globalClusterIndex(size_t pos)105 size_t globalClusterIndex(size_t pos) const { return fClusterStart + fClusterIndexes[pos]; }
106 SkScalar positionX(size_t pos) const;
107
textRange()108 TextRange textRange() const { return fTextRange; }
clusterRange()109 ClusterRange clusterRange() const { return fClusterRange; }
110
owner()111 ParagraphImpl* owner() const { return fOwner; }
112
isEllipsis()113 bool isEllipsis() const { return fEllipsis; }
114
115 void calculateMetrics();
116 void updateMetrics(InternalLineMetrics* endlineMetrics);
117
setClusterRange(size_t from,size_t to)118 void setClusterRange(size_t from, size_t to) { fClusterRange = ClusterRange(from, to); }
clip()119 SkRect clip() const {
120 return SkRect::MakeXYWH(fOffset.fX, fOffset.fY, fAdvance.fX, fAdvance.fY);
121 }
122
123 void addSpacesAtTheEnd(SkScalar space, Cluster* cluster);
124 SkScalar addSpacesEvenly(SkScalar space, Cluster* cluster);
125 SkScalar addSpacesEvenly(SkScalar space);
126 void shift(const Cluster* cluster, SkScalar offset);
127
calculateHeight(LineMetricStyle ascentStyle,LineMetricStyle descentStyle)128 SkScalar calculateHeight(LineMetricStyle ascentStyle, LineMetricStyle descentStyle) const {
129 auto ascent = ascentStyle == LineMetricStyle::Typographic ? this->ascent()
130 : this->correctAscent();
131 auto descent = descentStyle == LineMetricStyle::Typographic ? this->descent()
132 : this->correctDescent();
133 return descent - ascent;
134 }
135 SkScalar calculateWidth(size_t start, size_t end, bool clip) const;
136
137 void copyTo(SkTextBlobBuilder& builder, size_t pos, size_t size) const;
138
139 template<typename Visitor>
140 void iterateThroughClustersInTextOrder(Visitor visitor);
141
142 using ClusterVisitor = std::function<void(Cluster* cluster)>;
143 void iterateThroughClusters(const ClusterVisitor& visitor);
144
145 std::tuple<bool, ClusterIndex, ClusterIndex> findLimitingClusters(TextRange text) const;
146 std::tuple<bool, TextIndex, TextIndex> findLimitingGlyphClusters(TextRange text) const;
147 std::tuple<bool, TextIndex, TextIndex> findLimitingGraphemes(TextRange text) const;
glyphs()148 SkSpan<const SkGlyphID> glyphs() const {
149 return SkSpan<const SkGlyphID>(fGlyphs.begin(), fGlyphs.size());
150 }
positions()151 SkSpan<const SkPoint> positions() const {
152 return SkSpan<const SkPoint>(fPositions.begin(), fPositions.size());
153 }
offsets()154 SkSpan<const SkPoint> offsets() const {
155 return SkSpan<const SkPoint>(fOffsets.begin(), fOffsets.size());
156 }
clusterIndexes()157 SkSpan<const uint32_t> clusterIndexes() const {
158 return SkSpan<const uint32_t>(fClusterIndexes.begin(), fClusterIndexes.size());
159 }
160
commit()161 void commit() { }
162
resetJustificationShifts()163 void resetJustificationShifts() {
164 fJustificationShifts.clear();
165 }
166
167 bool isResolved() const;
168 private:
169 friend class ParagraphImpl;
170 friend class TextLine;
171 friend class InternalLineMetrics;
172 friend class ParagraphCache;
173 friend class OneLineShaper;
174
175 ParagraphImpl* fOwner;
176 TextRange fTextRange;
177 ClusterRange fClusterRange;
178
179 SkFont fFont;
180 size_t fPlaceholderIndex;
181 size_t fIndex;
182 SkVector fAdvance;
183 SkVector fOffset;
184 TextIndex fClusterStart;
185 SkShaper::RunHandler::Range fUtf8Range;
186
187 // These fields are not modified after shaping completes and can safely be
188 // shared among copies of the run that are held by different paragraphs.
189 struct GlyphData {
190 SkSTArray<64, SkGlyphID, true> glyphs;
191 SkSTArray<64, SkPoint, true> positions;
192 SkSTArray<64, SkPoint, true> offsets;
193 SkSTArray<64, uint32_t, true> clusterIndexes;
194 };
195 std::shared_ptr<GlyphData> fGlyphData;
196 SkSTArray<64, SkGlyphID, true>& fGlyphs;
197 SkSTArray<64, SkPoint, true>& fPositions;
198 SkSTArray<64, SkPoint, true>& fOffsets;
199 SkSTArray<64, uint32_t, true>& fClusterIndexes;
200
201 SkSTArray<64, SkPoint, true> fJustificationShifts; // For justification (current and prev shifts)
202
203 SkFontMetrics fFontMetrics;
204 const SkScalar fHeightMultiplier;
205 const bool fUseHalfLeading;
206 const SkScalar fBaselineShift;
207 SkScalar fCorrectAscent;
208 SkScalar fCorrectDescent;
209 SkScalar fCorrectLeading;
210
211 bool fEllipsis;
212 uint8_t fBidiLevel;
213 };
214
215 template<typename Visitor>
iterateThroughClustersInTextOrder(Visitor visitor)216 void Run::iterateThroughClustersInTextOrder(Visitor visitor) {
217 // Can't figure out how to do it with one code for both cases without 100 ifs
218 // Can't go through clusters because there are no cluster table yet
219 if (leftToRight()) {
220 size_t start = 0;
221 size_t cluster = this->clusterIndex(start);
222 for (size_t glyph = 1; glyph <= this->size(); ++glyph) {
223 auto nextCluster = this->clusterIndex(glyph);
224 if (nextCluster <= cluster) {
225 continue;
226 }
227
228 visitor(start,
229 glyph,
230 fClusterStart + cluster,
231 fClusterStart + nextCluster,
232 this->calculateWidth(start, glyph, glyph == size()),
233 this->calculateHeight(LineMetricStyle::CSS, LineMetricStyle::CSS));
234
235 start = glyph;
236 cluster = nextCluster;
237 }
238 } else {
239 size_t glyph = this->size();
240 size_t cluster = this->fUtf8Range.begin();
241 for (int32_t start = this->size() - 1; start >= 0; --start) {
242 size_t nextCluster =
243 start == 0 ? this->fUtf8Range.end() : this->clusterIndex(start - 1);
244 if (nextCluster <= cluster) {
245 continue;
246 }
247
248 visitor(start,
249 glyph,
250 fClusterStart + cluster,
251 fClusterStart + nextCluster,
252 this->calculateWidth(start, glyph, glyph == 0),
253 this->calculateHeight(LineMetricStyle::CSS, LineMetricStyle::CSS));
254
255 glyph = start;
256 cluster = nextCluster;
257 }
258 }
259 }
260
261 class Cluster {
262 public:
263 enum BreakType {
264 None,
265 GraphemeBreak, // calculated for all clusters (UBRK_CHARACTER)
266 SoftLineBreak, // calculated for all clusters (UBRK_LINE & UBRK_CHARACTER)
267 HardLineBreak, // calculated for all clusters (UBRK_LINE)
268 };
269
Cluster()270 Cluster()
271 : fOwner(nullptr)
272 , fRunIndex(EMPTY_RUN)
273 , fTextRange(EMPTY_TEXT)
274 , fGraphemeRange(EMPTY_RANGE)
275 , fStart(0)
276 , fEnd()
277 , fWidth()
278 , fHeight()
279 , fHalfLetterSpacing(0.0) {}
280
281 Cluster(ParagraphImpl* owner,
282 RunIndex runIndex,
283 size_t start,
284 size_t end,
285 SkSpan<const char> text,
286 SkScalar width,
287 SkScalar height);
288
Cluster(TextRange textRange)289 Cluster(TextRange textRange) : fTextRange(textRange), fGraphemeRange(EMPTY_RANGE) { }
290
291 Cluster(const Cluster&) = default;
292 ~Cluster() = default;
293
294 SkScalar sizeToChar(TextIndex ch) const;
295 SkScalar sizeFromChar(TextIndex ch) const;
296
297 size_t roundPos(SkScalar s) const;
298
space(SkScalar shift)299 void space(SkScalar shift) {
300 fWidth += shift;
301 }
302
getOwner()303 ParagraphImpl* getOwner() const { return fOwner; }
setOwner(ParagraphImpl * owner)304 void setOwner(ParagraphImpl* owner) { fOwner = owner; }
305
isWhitespaceBreak()306 bool isWhitespaceBreak() const { return fIsWhiteSpaceBreak; }
isIntraWordBreak()307 bool isIntraWordBreak() const { return fIsIntraWordBreak; }
isHardBreak()308 bool isHardBreak() const { return fIsHardBreak; }
309
310 bool isSoftBreak() const;
311 bool isGraphemeBreak() const;
canBreakLineAfter()312 bool canBreakLineAfter() const { return isHardBreak() || isSoftBreak(); }
startPos()313 size_t startPos() const { return fStart; }
endPos()314 size_t endPos() const { return fEnd; }
width()315 SkScalar width() const { return fWidth; }
height()316 SkScalar height() const { return fHeight; }
size()317 size_t size() const { return fEnd - fStart; }
318
setHalfLetterSpacing(SkScalar halfLetterSpacing)319 void setHalfLetterSpacing(SkScalar halfLetterSpacing) { fHalfLetterSpacing = halfLetterSpacing; }
getHalfLetterSpacing()320 SkScalar getHalfLetterSpacing() const { return fHalfLetterSpacing; }
321
textRange()322 TextRange textRange() const { return fTextRange; }
323
runIndex()324 RunIndex runIndex() const { return fRunIndex; }
owner()325 ParagraphImpl* owner() const { return fOwner; }
326
327 Run* runOrNull() const;
328 Run& run() const;
329 SkFont font() const;
330
331 SkScalar trimmedWidth(size_t pos) const;
332
contains(TextIndex ch)333 bool contains(TextIndex ch) const { return ch >= fTextRange.start && ch < fTextRange.end; }
334
belongs(TextRange text)335 bool belongs(TextRange text) const {
336 return fTextRange.start >= text.start && fTextRange.end <= text.end;
337 }
338
startsIn(TextRange text)339 bool startsIn(TextRange text) const {
340 return fTextRange.start >= text.start && fTextRange.start < text.end;
341 }
342
343 private:
344
345 friend ParagraphImpl;
346
347 ParagraphImpl* fOwner;
348 RunIndex fRunIndex;
349 TextRange fTextRange;
350 GraphemeRange fGraphemeRange;
351
352 size_t fStart;
353 size_t fEnd;
354 SkScalar fWidth;
355 SkScalar fHeight;
356 SkScalar fHalfLetterSpacing;
357
358 bool fIsWhiteSpaceBreak;
359 bool fIsIntraWordBreak;
360 bool fIsHardBreak;
361 };
362
363 class InternalLineMetrics {
364 public:
365
InternalLineMetrics()366 InternalLineMetrics() {
367 clean();
368 fForceStrut = false;
369 }
370
InternalLineMetrics(bool forceStrut)371 InternalLineMetrics(bool forceStrut) {
372 clean();
373 fForceStrut = forceStrut;
374 }
375
InternalLineMetrics(SkScalar a,SkScalar d,SkScalar l)376 InternalLineMetrics(SkScalar a, SkScalar d, SkScalar l) {
377 fAscent = a;
378 fDescent = d;
379 fLeading = l;
380 fRawAscent = a;
381 fRawDescent = d;
382 fRawLeading = l;
383 fForceStrut = false;
384 }
385
InternalLineMetrics(SkScalar a,SkScalar d,SkScalar l,SkScalar ra,SkScalar rd,SkScalar rl)386 InternalLineMetrics(SkScalar a, SkScalar d, SkScalar l, SkScalar ra, SkScalar rd, SkScalar rl) {
387 fAscent = a;
388 fDescent = d;
389 fLeading = l;
390 fRawAscent = ra;
391 fRawDescent = rd;
392 fRawLeading = rl;
393 fForceStrut = false;
394 }
395
InternalLineMetrics(const SkFont & font,bool forceStrut)396 InternalLineMetrics(const SkFont& font, bool forceStrut) {
397 SkFontMetrics metrics;
398 font.getMetrics(&metrics);
399 fAscent = metrics.fAscent;
400 fDescent = metrics.fDescent;
401 fLeading = metrics.fLeading;
402 fRawAscent = metrics.fAscent;
403 fRawDescent = metrics.fDescent;
404 fRawLeading = metrics.fLeading;
405 fForceStrut = forceStrut;
406 }
407
add(Run * run)408 void add(Run* run) {
409 if (fForceStrut) {
410 return;
411 }
412 fAscent = std::min(fAscent, run->correctAscent());
413 fDescent = std::max(fDescent, run->correctDescent());
414 fLeading = std::max(fLeading, run->correctLeading());
415
416 fRawAscent = std::min(fRawAscent, run->ascent());
417 fRawDescent = std::max(fRawDescent, run->descent());
418 fRawLeading = std::max(fRawLeading, run->leading());
419 }
420
add(InternalLineMetrics other)421 void add(InternalLineMetrics other) {
422 fAscent = std::min(fAscent, other.fAscent);
423 fDescent = std::max(fDescent, other.fDescent);
424 fLeading = std::max(fLeading, other.fLeading);
425 fRawAscent = std::min(fRawAscent, other.fRawAscent);
426 fRawDescent = std::max(fRawDescent, other.fRawDescent);
427 fRawLeading = std::max(fRawLeading, other.fRawLeading);
428 }
429
clean()430 void clean() {
431 fAscent = SK_ScalarMax;
432 fDescent = SK_ScalarMin;
433 fLeading = 0;
434 fRawAscent = SK_ScalarMax;
435 fRawDescent = SK_ScalarMin;
436 fRawLeading = 0;
437 }
438
isClean()439 bool isClean() {
440 return (fAscent == SK_ScalarMax &&
441 fDescent == SK_ScalarMin &&
442 fLeading == 0 &&
443 fRawAscent == SK_ScalarMax &&
444 fRawDescent == SK_ScalarMin &&
445 fRawLeading == 0);
446 }
447
delta()448 SkScalar delta() const { return height() - ideographicBaseline(); }
449
updateLineMetrics(InternalLineMetrics & metrics)450 void updateLineMetrics(InternalLineMetrics& metrics) {
451 if (metrics.fForceStrut) {
452 metrics.fAscent = fAscent;
453 metrics.fDescent = fDescent;
454 metrics.fLeading = fLeading;
455 metrics.fRawAscent = fRawAscent;
456 metrics.fRawDescent = fRawDescent;
457 metrics.fRawLeading = fRawLeading;
458 } else {
459 // This is another of those flutter changes. To be removed...
460 metrics.fAscent = std::min(metrics.fAscent, fAscent - fLeading / 2.0f);
461 metrics.fDescent = std::max(metrics.fDescent, fDescent + fLeading / 2.0f);
462 metrics.fRawAscent = std::min(metrics.fRawAscent, fRawAscent - fRawLeading / 2.0f);
463 metrics.fRawDescent = std::max(metrics.fRawDescent, fRawDescent + fRawLeading / 2.0f);
464 }
465 }
466
runTop(const Run * run,LineMetricStyle ascentStyle)467 SkScalar runTop(const Run* run, LineMetricStyle ascentStyle) const {
468 return fLeading / 2 - fAscent +
469 (ascentStyle == LineMetricStyle::Typographic ? run->ascent() : run->correctAscent()) + delta();
470 }
471
height()472 SkScalar height() const {
473 return ::round((double)fDescent - fAscent + fLeading);
474 }
475
update(SkScalar a,SkScalar d,SkScalar l)476 void update(SkScalar a, SkScalar d, SkScalar l) {
477 fAscent = a;
478 fDescent = d;
479 fLeading = l;
480 }
481
updateRawData(SkScalar ra,SkScalar rd)482 void updateRawData(SkScalar ra, SkScalar rd) {
483 fRawAscent = ra;
484 fRawDescent = rd;
485 }
486
alphabeticBaseline()487 SkScalar alphabeticBaseline() const { return fLeading / 2 - fAscent; }
ideographicBaseline()488 SkScalar ideographicBaseline() const { return fDescent - fAscent + fLeading; }
deltaBaselines()489 SkScalar deltaBaselines() const { return fLeading / 2 + fDescent; }
baseline()490 SkScalar baseline() const { return fLeading / 2 - fAscent; }
ascent()491 SkScalar ascent() const { return fAscent; }
descent()492 SkScalar descent() const { return fDescent; }
leading()493 SkScalar leading() const { return fLeading; }
rawAscent()494 SkScalar rawAscent() const { return fRawAscent; }
rawDescent()495 SkScalar rawDescent() const { return fRawDescent; }
setForceStrut(bool value)496 void setForceStrut(bool value) { fForceStrut = value; }
getForceStrut()497 bool getForceStrut() const { return fForceStrut; }
498
499 private:
500
501 friend class ParagraphImpl;
502 friend class TextWrapper;
503 friend class TextLine;
504
505 SkScalar fAscent;
506 SkScalar fDescent;
507 SkScalar fLeading;
508
509 SkScalar fRawAscent;
510 SkScalar fRawDescent;
511 SkScalar fRawLeading;
512
513 bool fForceStrut;
514 };
515 } // namespace textlayout
516 } // namespace skia
517
518 #endif // Run_DEFINED
519