Debian Codegraph @

modules/skparagraph/src/Run.h

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