Debian Codegraph @

modules/skparagraph/src/Run.cpp

raw
1 // Copyright 2019 Google LLC.
2 #include "include/core/SkFontMetrics.h"
3 #include "include/core/SkTextBlob.h"
4 #include "include/private/base/SkFloatingPoint.h"
5 #include "include/private/base/SkMalloc.h"
6 #include "include/private/base/SkTo.h"
7 #include "modules/skparagraph/include/DartTypes.h"
8 #include "modules/skparagraph/include/TextStyle.h"
9 #include "modules/skparagraph/src/ParagraphImpl.h"
10 #include "modules/skparagraph/src/Run.h"
11 #include "modules/skshaper/include/SkShaper.h"
12 #include "src/base/SkUTF.h"
13
14 namespace skia {
15 namespace textlayout {
16
17 Run::Run(ParagraphImpl* owner,
18 const SkShaper::RunHandler::RunInfo& info,
19 size_t firstChar,
20 SkScalar heightMultiplier,
21 bool useHalfLeading,
22 SkScalar baselineShift,
23 size_t index,
24 SkScalar offsetX)
25 : fOwner(owner)
26 , fTextRange(firstChar + info.utf8Range.begin(), firstChar + info.utf8Range.end())
27 , fClusterRange(EMPTY_CLUSTERS)
28 , fFont(info.fFont)
29 , fClusterStart(firstChar)
30 , fGlyphData(std::make_shared<GlyphData>())
31 , fGlyphs(fGlyphData->glyphs)
32 , fPositions(fGlyphData->positions)
33 , fOffsets(fGlyphData->offsets)
34 , fClusterIndexes(fGlyphData->clusterIndexes)
35 , fHeightMultiplier(heightMultiplier)
36 , fUseHalfLeading(useHalfLeading)
37 , fBaselineShift(baselineShift)
38 {
39 fBidiLevel = info.fBidiLevel;
40 fScript = info.fScript;
41 fLanguage = info.fLanguage;
42 fAdvance = info.fAdvance;
43 fIndex = index;
44 fUtf8Range = info.utf8Range;
45 fOffset = SkVector::Make(offsetX, 0);
46
47 fGlyphs.push_back_n(info.glyphCount);
48 fPositions.push_back_n(info.glyphCount + 1);
49 fOffsets.push_back_n(info.glyphCount + 1);
50 fClusterIndexes.push_back_n(info.glyphCount + 1);
51 info.fFont.getMetrics(&fFontMetrics);
52
53 this->calculateMetrics();
54
55 // To make edge cases easier:
56 fPositions[info.glyphCount] = fOffset + fAdvance;
57 fOffsets[info.glyphCount] = {0, 0};
58 fClusterIndexes[info.glyphCount] = this->leftToRight() ? info.utf8Range.end() : info.utf8Range.begin();
59 fEllipsis = false;
60 fPlaceholderIndex = std::numeric_limits<size_t>::max();
61 }
62
63 void Run::calculateMetrics() {
64 fCorrectAscent = fFontMetrics.fAscent - fFontMetrics.fLeading * 0.5;
65 fCorrectDescent = fFontMetrics.fDescent + fFontMetrics.fLeading * 0.5;
66 fCorrectLeading = 0;
67 if (SkScalarNearlyZero(fHeightMultiplier)) {
68 return;
69 }
70 const auto runHeight = fHeightMultiplier * fFont.getSize();
71 const auto fontIntrinsicHeight = fCorrectDescent - fCorrectAscent;
72 if (fUseHalfLeading) {
73 const auto extraLeading = (runHeight - fontIntrinsicHeight) / 2;
74 fCorrectAscent -= extraLeading;
75 fCorrectDescent += extraLeading;
76 } else {
77 const auto multiplier = runHeight / fontIntrinsicHeight;
78 fCorrectAscent *= multiplier;
79 fCorrectDescent *= multiplier;
80 }
81 // If we shift the baseline we need to make sure the shifted text fits the line
82 fCorrectAscent += fBaselineShift;
83 fCorrectDescent += fBaselineShift;
84 }
85
86 SkShaper::RunHandler::Buffer Run::newRunBuffer() {
87 return {fGlyphs.data(), fPositions.data(), fOffsets.data(), fClusterIndexes.data(), fOffset};
88 }
89
90 void Run::copyTo(SkTextBlobBuilder& builder, size_t pos, size_t size) const {
91 SkASSERT(pos + size <= this->size());
92 const auto& blobBuffer = builder.allocRunPos(fFont, SkToInt(size));
93 sk_careful_memcpy(blobBuffer.glyphs, fGlyphs.data() + pos, size * sizeof(SkGlyphID));
94
95 for (size_t i = 0; i < size; ++i) {
96 auto point = fPositions[i + pos];
97 if (!fJustificationShifts.empty()) {
98 point.fX += fJustificationShifts[i + pos].fX;
99 }
100 point += fOffsets[i + pos];
101 blobBuffer.points()[i] = point;
102 }
103 }
104
105 // Find a cluster range from text range (within one run)
106 // Cluster range is normalized ([start:end) start < end regardless of TextDirection
107 // Boolean value in triple indicates whether the cluster range was found or not
108 std::tuple<bool, ClusterIndex, ClusterIndex> Run::findLimitingClusters(TextRange text) const {
109 if (text.width() == 0) {
110 // Special Flutter case for "\n" and "...\n"
111 if (text.end > this->fTextRange.start) {
112 ClusterIndex index = fOwner->clusterIndex(text.end - 1);
113 return std::make_tuple(true, index, index);
114 } else {
115 return std::make_tuple(false, 0, 0);
116 }
117 }
118
119 ClusterRange clusterRange;
120 bool found = true;
121 // Deal with the case when either start or end are not align with glyph cluster edge
122 // In such case we shift the text range to the right
123 // (cutting from the left and adding to the right)
124 if (leftToRight()) {
125 // LTR: [start:end)
126 found = clusterRange.start != fClusterRange.end;
127 clusterRange.start = fOwner->clusterIndex(text.start);
128 clusterRange.end = fOwner->clusterIndex(text.end - 1);
129 } else {
130 // RTL: (start:end]
131 clusterRange.start = fOwner->clusterIndex(text.end);
132 clusterRange.end = fOwner->clusterIndex(text.start + 1);
133 found = clusterRange.end != fClusterRange.start;
134 }
135
136 return std::make_tuple(
137 found,
138 clusterRange.start,
139 clusterRange.end);
140 }
141
142 std::tuple<bool, TextIndex, TextIndex> Run::findLimitingGlyphClusters(TextRange text) const {
143 TextIndex start = fOwner->findPreviousGlyphClusterBoundary(text.start);
144 TextIndex end = fOwner->findNextGlyphClusterBoundary(text.end);
145 return std::make_tuple(true, start, end);
146 }
147
148 // Adjust the text to grapheme edges so the first grapheme start is in the text and the last grapheme start is in the text
149 // It actually means that the first grapheme is entirely in the text and the last grapheme does not have to be
150 // 12345 234 2:2 -> 2,5 4:4
151 std::tuple<bool, TextIndex, TextIndex> Run::findLimitingGraphemes(TextRange text) const {
152 TextIndex start = fOwner->findPreviousGraphemeBoundary(text.start);
153 TextIndex end = fOwner->findNextGraphemeBoundary(text.end);
154 return std::make_tuple(true, start, end);
155 }
156
157 void Run::iterateThroughClusters(const ClusterVisitor& visitor) {
158
159 for (size_t index = 0; index < fClusterRange.width(); ++index) {
160 auto correctIndex = leftToRight() ? fClusterRange.start + index : fClusterRange.end - index - 1;
161 auto cluster = &fOwner->cluster(correctIndex);
162 visitor(cluster);
163 }
164 }
165
166 void Run::addSpacesAtTheEnd(SkScalar space, Cluster* cluster) {
167 // Increment the run width
168 fAdvance.fX += space;
169 // Increment the cluster width
170 cluster->space(space);
171 }
172
173 SkScalar Run::addLetterSpacesEvenly(SkScalar space) {
174 if (this->isCursiveScript()) {
175 // Do not apply letter spacing for script languages
176 return 0.0;
177 }
178 SkScalar shift = 0;
179 for (size_t i = 0; i < this->size(); ++i) {
180 fPositions[i].fX += shift;
181 shift += space;
182 }
183 fPositions[this->size()].fX += shift;
184 fAdvance.fX += shift;
185 return shift;
186 }
187
188 SkScalar Run::addLetterSpacesEvenly(SkScalar space, Cluster* cluster) {
189 if (this->isCursiveScript()) {
190 // Do not apply letter spacing for script languages
191 return 0.0;
192 }
193 // Offset all the glyphs in the cluster
194 SkScalar shift = 0;
195 for (size_t i = cluster->startPos(); i < cluster->endPos(); ++i) {
196 fPositions[i].fX += shift;
197 shift += space;
198 }
199 if (this->size() == cluster->endPos()) {
200 // To make calculations easier
201 fPositions[cluster->endPos()].fX += shift;
202 }
203 // Increment the run width
204 fAdvance.fX += shift;
205 // Increment the cluster width
206 cluster->space(shift);
207 cluster->setHalfLetterSpacing(space / 2);
208
209 return shift;
210 }
211
212 bool Run::isCursiveScript() const {
213 switch (this->fScript) {
214 case SkSetFourByteTag('A', 'r', 'a', 'b'): // ARABIC
215 case SkSetFourByteTag('R', 'o', 'h', 'g'): // HANIFI_ROHINGYA
216 case SkSetFourByteTag('M', 'a', 'n', 'd'): // MANDAIC
217 case SkSetFourByteTag('M', 'o', 'n', 'g'): // MONGOLIAN
218 case SkSetFourByteTag('N', 'k', 'o', 'o'): // NKO
219 case SkSetFourByteTag('P', 'h', 'a', 'g'): // PHAGS_PA
220 case SkSetFourByteTag('S', 'y', 'r', 'c'): // SYRIAC
221 return true;
222 default:
223 return false;
224 }
225 }
226
227 void Run::shift(const Cluster* cluster, SkScalar offset) {
228 if (offset == 0) {
229 return;
230 }
231 for (size_t i = cluster->startPos(); i < cluster->endPos(); ++i) {
232 fPositions[i].fX += offset;
233 }
234 if (this->size() == cluster->endPos()) {
235 // To make calculations easier
236 fPositions[cluster->endPos()].fX += offset;
237 }
238 }
239
240 void Run::extend(const Cluster* cluster, SkScalar offset) {
241 // Extend the cluster at the end
242 fPositions[cluster->endPos()].fX += offset;
243 }
244
245 void Run::updateMetrics(InternalLineMetrics* endlineMetrics) {
246
247 SkASSERT(isPlaceholder());
248 auto placeholderStyle = this->placeholderStyle();
249 // Difference between the placeholder baseline and the line bottom
250 SkScalar baselineAdjustment = 0;
251 switch (placeholderStyle->fBaseline) {
252 case TextBaseline::kAlphabetic:
253 break;
254
255 case TextBaseline::kIdeographic:
256 baselineAdjustment = endlineMetrics->deltaBaselines() / 2;
257 break;
258 }
259
260 auto height = placeholderStyle->fHeight;
261 auto offset = placeholderStyle->fBaselineOffset;
262
263 fFontMetrics.fLeading = 0;
264 switch (placeholderStyle->fAlignment) {
265 case PlaceholderAlignment::kBaseline:
266 fFontMetrics.fAscent = baselineAdjustment - offset;
267 fFontMetrics.fDescent = baselineAdjustment + height - offset;
268 break;
269
270 case PlaceholderAlignment::kAboveBaseline:
271 fFontMetrics.fAscent = baselineAdjustment - height;
272 fFontMetrics.fDescent = baselineAdjustment;
273 break;
274
275 case PlaceholderAlignment::kBelowBaseline:
276 fFontMetrics.fAscent = baselineAdjustment;
277 fFontMetrics.fDescent = baselineAdjustment + height;
278 break;
279
280 case PlaceholderAlignment::kTop:
281 fFontMetrics.fDescent = height + fFontMetrics.fAscent;
282 break;
283
284 case PlaceholderAlignment::kBottom:
285 fFontMetrics.fAscent = fFontMetrics.fDescent - height;
286 break;
287
288 case PlaceholderAlignment::kMiddle:
289 auto mid = (-fFontMetrics.fDescent - fFontMetrics.fAscent)/2.0;
290 fFontMetrics.fDescent = height/2.0 - mid;
291 fFontMetrics.fAscent = - height/2.0 - mid;
292 break;
293 }
294
295 this->calculateMetrics();
296
297 // Make sure the placeholder can fit the line
298 endlineMetrics->add(this);
299 }
300
301 SkScalar Cluster::sizeToChar(TextIndex ch) const {
302 if (ch < fTextRange.start || ch >= fTextRange.end) {
303 return 0;
304 }
305 auto shift = ch - fTextRange.start;
306 auto ratio = shift * 1.0 / fTextRange.width();
307
308 return SkDoubleToScalar(fWidth * ratio);
309 }
310
311 SkScalar Cluster::sizeFromChar(TextIndex ch) const {
312 if (ch < fTextRange.start || ch >= fTextRange.end) {
313 return 0;
314 }
315 auto shift = fTextRange.end - ch - 1;
316 auto ratio = shift * 1.0 / fTextRange.width();
317
318 return SkDoubleToScalar(fWidth * ratio);
319 }
320
321 size_t Cluster::roundPos(SkScalar s) const {
322 auto ratio = (s * 1.0) / fWidth;
323 return sk_double_floor2int(ratio * size());
324 }
325
326 SkScalar Cluster::trimmedWidth(size_t pos) const {
327 // Find the width until the pos and return the min between trimmedWidth and the width(pos)
328 // We don't have to take in account cluster shift since it's the same for 0 and for pos
329 auto& run = fOwner->run(fRunIndex);
330 return std::min(run.positionX(pos) - run.positionX(fStart), fWidth);
331 }
332
333 SkScalar Run::positionX(size_t pos) const {
334 return posX(pos) + (fJustificationShifts.empty() ? 0 : fJustificationShifts[pos].fY);
335 }
336
337 PlaceholderStyle* Run::placeholderStyle() const {
338 if (isPlaceholder()) {
339 return &fOwner->placeholders()[fPlaceholderIndex].fStyle;
340 } else {
341 return nullptr;
342 }
343 }
344
345 bool Run::isResolved() const {
346 for (auto& glyph :fGlyphs) {
347 if (glyph == 0) {
348 return false;
349 }
350 }
351 return true;
352 }
353
354 Run* Cluster::runOrNull() const {
355 if (fRunIndex >= fOwner->runs().size()) {
356 return nullptr;
357 }
358 return &fOwner->run(fRunIndex);
359 }
360
361 Run& Cluster::run() const {
362 SkASSERT(fRunIndex < fOwner->runs().size());
363 return fOwner->run(fRunIndex);
364 }
365
366 SkFont Cluster::font() const {
367 SkASSERT(fRunIndex < fOwner->runs().size());
368 return fOwner->run(fRunIndex).font();
369 }
370
371 bool Cluster::isSoftBreak() const {
372 return fOwner->codeUnitHasProperty(fTextRange.end,
373 SkUnicode::CodeUnitFlags::kSoftLineBreakBefore);
374 }
375
376 bool Cluster::isGraphemeBreak() const {
377 return fOwner->codeUnitHasProperty(fTextRange.end, SkUnicode::CodeUnitFlags::kGraphemeStart);
378 }
379 } // namespace textlayout
380 } // namespace skia