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/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 SkScalar addSpacesAtTheEnd(SkScalar space, Cluster* cluster);
124 SkScalar addSpacesEvenly(SkScalar space, Cluster* cluster);
125 void shift(const Cluster* cluster, SkScalar offset);
126
calculateHeight(LineMetricStyle ascentStyle,LineMetricStyle descentStyle)127 SkScalar calculateHeight(LineMetricStyle ascentStyle, LineMetricStyle descentStyle) const {
128 auto ascent = ascentStyle == LineMetricStyle::Typographic ? this->ascent()
129 : this->correctAscent();
130 auto descent = descentStyle == LineMetricStyle::Typographic ? this->descent()
131 : this->correctDescent();
132 return descent - ascent;
133 }
134 SkScalar calculateWidth(size_t start, size_t end, bool clip) const;
135
136 void copyTo(SkTextBlobBuilder& builder, size_t pos, size_t size) const;
137
138 template<typename Visitor>
139 void iterateThroughClustersInTextOrder(Visitor visitor);
140
141 using ClusterVisitor = std::function<void(Cluster* cluster)>;
142 void iterateThroughClusters(const ClusterVisitor& visitor);
143
144 std::tuple<bool, ClusterIndex, ClusterIndex> findLimitingClusters(TextRange text) const;
145 std::tuple<bool, TextIndex, TextIndex> findLimitingGraphemes(TextRange text) const;
glyphs()146 SkSpan<const SkGlyphID> glyphs() const {
147 return SkSpan<const SkGlyphID>(fGlyphs.begin(), fGlyphs.size());
148 }
positions()149 SkSpan<const SkPoint> positions() const {
150 return SkSpan<const SkPoint>(fPositions.begin(), fPositions.size());
151 }
clusterIndexes()152 SkSpan<const uint32_t> clusterIndexes() const {
153 return SkSpan<const uint32_t>(fClusterIndexes.begin(), fClusterIndexes.size());
154 }
shifts()155 SkSpan<const SkScalar> shifts() const { return SkSpan<const SkScalar>(fShifts.begin(), fShifts.size()); }
156
157 void commit();
158
getBounds(size_t pos)159 SkRect getBounds(size_t pos) const { return fBounds[pos]; }
160
resetShifts()161 void resetShifts() {
162 for (auto& r: fShifts) { r = 0; }
163 fSpaced = false;
164 }
165
resetJustificationShifts()166 void resetJustificationShifts() {
167 fJustificationShifts.reset();
168 }
169 private:
170 friend class ParagraphImpl;
171 friend class TextLine;
172 friend class InternalLineMetrics;
173 friend class ParagraphCache;
174 friend class OneLineShaper;
175
176 ParagraphImpl* fOwner;
177 TextRange fTextRange;
178 ClusterRange fClusterRange;
179
180 SkFont fFont;
181 size_t fPlaceholderIndex;
182 size_t fIndex;
183 SkVector fAdvance;
184 SkVector fOffset;
185 TextIndex fClusterStart;
186 SkShaper::RunHandler::Range fUtf8Range;
187 SkSTArray<128, SkGlyphID, true> fGlyphs;
188 SkSTArray<128, SkPoint, true> fPositions;
189 SkSTArray<128, SkPoint, true> fJustificationShifts; // For justification (current and prev shifts)
190 SkSTArray<128, uint32_t, true> fClusterIndexes;
191 SkSTArray<128, SkRect, true> fBounds;
192
193 SkSTArray<128, SkScalar, true> fShifts; // For formatting (letter/word spacing)
194
195 SkFontMetrics fFontMetrics;
196 const SkScalar fHeightMultiplier;
197 const bool fUseHalfLeading;
198 const SkScalar fBaselineShift;
199 SkScalar fCorrectAscent;
200 SkScalar fCorrectDescent;
201 SkScalar fCorrectLeading;
202
203 bool fSpaced;
204 bool fEllipsis;
205 uint8_t fBidiLevel;
206 };
207
208 template<typename Visitor>
iterateThroughClustersInTextOrder(Visitor visitor)209 void Run::iterateThroughClustersInTextOrder(Visitor visitor) {
210 // Can't figure out how to do it with one code for both cases without 100 ifs
211 // Can't go through clusters because there are no cluster table yet
212 if (leftToRight()) {
213 size_t start = 0;
214 size_t cluster = this->clusterIndex(start);
215 for (size_t glyph = 1; glyph <= this->size(); ++glyph) {
216 auto nextCluster = this->clusterIndex(glyph);
217 if (nextCluster <= cluster) {
218 continue;
219 }
220
221 visitor(start,
222 glyph,
223 fClusterStart + cluster,
224 fClusterStart + nextCluster,
225 this->calculateWidth(start, glyph, glyph == size()),
226 this->calculateHeight(LineMetricStyle::CSS, LineMetricStyle::CSS));
227
228 start = glyph;
229 cluster = nextCluster;
230 }
231 } else {
232 size_t glyph = this->size();
233 size_t cluster = this->fUtf8Range.begin();
234 for (int32_t start = this->size() - 1; start >= 0; --start) {
235 size_t nextCluster =
236 start == 0 ? this->fUtf8Range.end() : this->clusterIndex(start - 1);
237 if (nextCluster <= cluster) {
238 continue;
239 }
240
241 visitor(start,
242 glyph,
243 fClusterStart + cluster,
244 fClusterStart + nextCluster,
245 this->calculateWidth(start, glyph, glyph == 0),
246 this->calculateHeight(LineMetricStyle::CSS, LineMetricStyle::CSS));
247
248 glyph = start;
249 cluster = nextCluster;
250 }
251 }
252 }
253
254 class Cluster {
255 public:
256 enum BreakType {
257 None,
258 GraphemeBreak, // calculated for all clusters (UBRK_CHARACTER)
259 SoftLineBreak, // calculated for all clusters (UBRK_LINE & UBRK_CHARACTER)
260 HardLineBreak, // calculated for all clusters (UBRK_LINE)
261 };
262
Cluster()263 Cluster()
264 : fOwner(nullptr)
265 , fRunIndex(EMPTY_RUN)
266 , fTextRange(EMPTY_TEXT)
267 , fGraphemeRange(EMPTY_RANGE)
268 , fStart(0)
269 , fEnd()
270 , fWidth()
271 , fSpacing(0)
272 , fHeight()
273 , fHalfLetterSpacing(0.0) {}
274
275 Cluster(ParagraphImpl* owner,
276 RunIndex runIndex,
277 size_t start,
278 size_t end,
279 SkSpan<const char> text,
280 SkScalar width,
281 SkScalar height);
282
Cluster(TextRange textRange)283 Cluster(TextRange textRange) : fTextRange(textRange), fGraphemeRange(EMPTY_RANGE) { }
284
285 Cluster(const Cluster&) = default;
286 ~Cluster() = default;
287
288 SkScalar sizeToChar(TextIndex ch) const;
289 SkScalar sizeFromChar(TextIndex ch) const;
290
291 size_t roundPos(SkScalar s) const;
292
space(SkScalar shift,SkScalar space)293 void space(SkScalar shift, SkScalar space) {
294 fSpacing += space;
295 fWidth += shift;
296 }
297
isWhitespaceBreak()298 bool isWhitespaceBreak() const { return fIsWhiteSpaceBreak; }
isIntraWordBreak()299 bool isIntraWordBreak() const { return fIsIntraWordBreak; }
isHardBreak()300 bool isHardBreak() const { return fIsHardBreak; }
301
302 bool isSoftBreak() const;
303 bool isGraphemeBreak() const;
canBreakLineAfter()304 bool canBreakLineAfter() const { return isHardBreak() || isSoftBreak(); }
startPos()305 size_t startPos() const { return fStart; }
endPos()306 size_t endPos() const { return fEnd; }
width()307 SkScalar width() const { return fWidth; }
height()308 SkScalar height() const { return fHeight; }
size()309 size_t size() const { return fEnd - fStart; }
310
setHalfLetterSpacing(SkScalar halfLetterSpacing)311 void setHalfLetterSpacing(SkScalar halfLetterSpacing) { fHalfLetterSpacing = halfLetterSpacing; }
getHalfLetterSpacing()312 SkScalar getHalfLetterSpacing() const { return fHalfLetterSpacing; }
313
textRange()314 TextRange textRange() const { return fTextRange; }
315
runIndex()316 RunIndex runIndex() const { return fRunIndex; }
owner()317 ParagraphImpl* owner() const { return fOwner; }
318
319 Run* runOrNull() const;
320 Run& run() const;
321 SkFont font() const;
322
323 SkScalar trimmedWidth(size_t pos) const;
324
contains(TextIndex ch)325 bool contains(TextIndex ch) const { return ch >= fTextRange.start && ch < fTextRange.end; }
326
belongs(TextRange text)327 bool belongs(TextRange text) const {
328 return fTextRange.start >= text.start && fTextRange.end <= text.end;
329 }
330
startsIn(TextRange text)331 bool startsIn(TextRange text) const {
332 return fTextRange.start >= text.start && fTextRange.start < text.end;
333 }
334
335 private:
336
337 friend ParagraphImpl;
338
339 ParagraphImpl* fOwner;
340 RunIndex fRunIndex;
341 TextRange fTextRange;
342 GraphemeRange fGraphemeRange;
343
344 size_t fStart;
345 size_t fEnd;
346 SkScalar fWidth;
347 SkScalar fSpacing;
348 SkScalar fHeight;
349 SkScalar fHalfLetterSpacing;
350
351 bool fIsWhiteSpaceBreak;
352 bool fIsIntraWordBreak;
353 bool fIsHardBreak;
354 };
355
356 class InternalLineMetrics {
357 public:
358
InternalLineMetrics()359 InternalLineMetrics() {
360 clean();
361 fForceStrut = false;
362 }
363
InternalLineMetrics(bool forceStrut)364 InternalLineMetrics(bool forceStrut) {
365 clean();
366 fForceStrut = forceStrut;
367 }
368
InternalLineMetrics(SkScalar a,SkScalar d,SkScalar l)369 InternalLineMetrics(SkScalar a, SkScalar d, SkScalar l) {
370 fAscent = a;
371 fDescent = d;
372 fLeading = l;
373 fRawAscent = a;
374 fRawDescent = d;
375 fRawLeading = l;
376 fForceStrut = false;
377 }
378
InternalLineMetrics(const SkFont & font,bool forceStrut)379 InternalLineMetrics(const SkFont& font, bool forceStrut) {
380 SkFontMetrics metrics;
381 font.getMetrics(&metrics);
382 fAscent = metrics.fAscent;
383 fDescent = metrics.fDescent;
384 fLeading = metrics.fLeading;
385 fRawAscent = metrics.fAscent;
386 fRawDescent = metrics.fDescent;
387 fRawLeading = metrics.fLeading;
388 fForceStrut = forceStrut;
389 }
390
add(Run * run)391 void add(Run* run) {
392 if (fForceStrut) {
393 return;
394 }
395
396 fAscent = std::min(fAscent, run->correctAscent());
397 fDescent = std::max(fDescent, run->correctDescent());
398 fLeading = std::max(fLeading, run->correctLeading());
399
400 fRawAscent = std::min(fRawAscent, run->ascent());
401 fRawDescent = std::max(fRawDescent, run->descent());
402 fRawLeading = std::max(fRawLeading, run->leading());
403 }
404
add(InternalLineMetrics other)405 void add(InternalLineMetrics other) {
406 fAscent = std::min(fAscent, other.fAscent);
407 fDescent = std::max(fDescent, other.fDescent);
408 fLeading = std::max(fLeading, other.fLeading);
409 fRawAscent = std::min(fRawAscent, other.fRawAscent);
410 fRawDescent = std::max(fRawDescent, other.fRawDescent);
411 fRawLeading = std::max(fRawLeading, other.fRawLeading);
412 }
413
clean()414 void clean() {
415 fAscent = SK_ScalarMax;
416 fDescent = SK_ScalarMin;
417 fLeading = 0;
418 fRawAscent = SK_ScalarMax;
419 fRawDescent = SK_ScalarMin;
420 fRawLeading = 0;
421 }
422
delta()423 SkScalar delta() const { return height() - ideographicBaseline(); }
424
updateLineMetrics(InternalLineMetrics & metrics)425 void updateLineMetrics(InternalLineMetrics& metrics) {
426 if (metrics.fForceStrut) {
427 metrics.fAscent = fAscent;
428 metrics.fDescent = fDescent;
429 metrics.fLeading = fLeading;
430 metrics.fRawAscent = fRawAscent;
431 metrics.fRawDescent = fRawDescent;
432 metrics.fRawLeading = fRawLeading;
433 } else {
434 // This is another of those flutter changes. To be removed...
435 metrics.fAscent = std::min(metrics.fAscent, fAscent - fLeading / 2.0f);
436 metrics.fDescent = std::max(metrics.fDescent, fDescent + fLeading / 2.0f);
437 metrics.fRawAscent = std::min(metrics.fRawAscent, fRawAscent - fRawLeading / 2.0f);
438 metrics.fRawDescent = std::max(metrics.fRawDescent, fRawDescent + fRawLeading / 2.0f);
439 }
440 }
441
runTop(const Run * run,LineMetricStyle ascentStyle)442 SkScalar runTop(const Run* run, LineMetricStyle ascentStyle) const {
443 return fLeading / 2 - fAscent +
444 (ascentStyle == LineMetricStyle::Typographic ? run->ascent() : run->correctAscent()) + delta();
445 }
446
height()447 SkScalar height() const {
448 return ::round((double)fDescent - fAscent + fLeading);
449 }
450
update(SkScalar a,SkScalar d,SkScalar l)451 void update(SkScalar a, SkScalar d, SkScalar l) {
452 fAscent = a;
453 fDescent = d;
454 fLeading = l;
455 }
456
alphabeticBaseline()457 SkScalar alphabeticBaseline() const { return fLeading / 2 - fAscent; }
ideographicBaseline()458 SkScalar ideographicBaseline() const { return fDescent - fAscent + fLeading; }
deltaBaselines()459 SkScalar deltaBaselines() const { return fLeading / 2 + fDescent; }
baseline()460 SkScalar baseline() const { return fLeading / 2 - fAscent; }
ascent()461 SkScalar ascent() const { return fAscent; }
descent()462 SkScalar descent() const { return fDescent; }
leading()463 SkScalar leading() const { return fLeading; }
rawAscent()464 SkScalar rawAscent() const { return fRawAscent; }
rawDescent()465 SkScalar rawDescent() const { return fRawDescent; }
setForceStrut(bool value)466 void setForceStrut(bool value) { fForceStrut = value; }
getForceStrut()467 bool getForceStrut() const { return fForceStrut; }
468
469 private:
470
471 friend class TextWrapper;
472 friend class TextLine;
473
474 SkScalar fAscent;
475 SkScalar fDescent;
476 SkScalar fLeading;
477
478 SkScalar fRawAscent;
479 SkScalar fRawDescent;
480 SkScalar fRawLeading;
481
482 bool fForceStrut;
483 };
484 } // namespace textlayout
485 } // namespace skia
486
487 #endif // Run_DEFINED
488