blob: d2ca6f79819de6bb6fe7ebded06f782712595369 [file] [log] [blame]
Julia Lavrovaa3552c52019-05-30 16:12:56 -04001// Copyright 2019 Google LLC.
2#include "modules/skparagraph/src/Run.h"
3#include <unicode/brkiter.h>
4#include "include/core/SkFontMetrics.h"
Julia Lavrova5207f352019-06-21 12:22:32 -04005#include "modules/skparagraph/src/ParagraphImpl.h"
6#include <algorithm>
Julia Lavrovadb9f6692019-08-01 16:02:17 -04007#include "src/utils/SkUTF.h"
8
9namespace {
10
11SkUnichar utf8_next(const char** ptr, const char* end) {
12 SkUnichar val = SkUTF::NextUTF8(ptr, end);
13 return val < 0 ? 0xFFFD : val;
14}
Julia Lavrova62076972020-02-19 15:12:48 -050015
Julia Lavrovadb9f6692019-08-01 16:02:17 -040016}
Julia Lavrovaa3552c52019-05-30 16:12:56 -040017
18namespace skia {
19namespace textlayout {
20
Julia Lavrova5207f352019-06-21 12:22:32 -040021Run::Run(ParagraphImpl* master,
Julia Lavrovaa3552c52019-05-30 16:12:56 -040022 const SkShaper::RunHandler::RunInfo& info,
Julia Lavrova916a9042019-08-08 16:51:27 -040023 size_t firstChar,
Julia Lavrovaa3552c52019-05-30 16:12:56 -040024 SkScalar lineHeight,
25 size_t index,
Julia Lavrova5207f352019-06-21 12:22:32 -040026 SkScalar offsetX)
27 : fMaster(master)
Julia Lavrova916a9042019-08-08 16:51:27 -040028 , fTextRange(firstChar + info.utf8Range.begin(), firstChar + info.utf8Range.end())
29 , fClusterRange(EMPTY_CLUSTERS)
Julia Lavrova2e30fde2019-10-09 09:43:02 -040030 , fClusterStart(firstChar) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040031 fFont = info.fFont;
32 fHeightMultiplier = lineHeight;
33 fBidiLevel = info.fBidiLevel;
34 fAdvance = info.fAdvance;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040035 fIndex = index;
36 fUtf8Range = info.utf8Range;
37 fOffset = SkVector::Make(offsetX, 0);
38 fGlyphs.push_back_n(info.glyphCount);
Julia Lavrova2e30fde2019-10-09 09:43:02 -040039 fBounds.push_back_n(info.glyphCount);
Julia Lavrovaa3552c52019-05-30 16:12:56 -040040 fPositions.push_back_n(info.glyphCount + 1);
Julia Lavrova2e30fde2019-10-09 09:43:02 -040041 fOffsets.push_back_n(info.glyphCount + 1);
Julia Lavrovaa3552c52019-05-30 16:12:56 -040042 fClusterIndexes.push_back_n(info.glyphCount + 1);
Julia Lavrova2e30fde2019-10-09 09:43:02 -040043 fShifts.push_back_n(info.glyphCount + 1, 0.0);
Julia Lavrovaa3552c52019-05-30 16:12:56 -040044 info.fFont.getMetrics(&fFontMetrics);
45 fSpaced = false;
46 // To make edge cases easier:
47 fPositions[info.glyphCount] = fOffset + fAdvance;
Julia Lavrova2e30fde2019-10-09 09:43:02 -040048 fOffsets[info.glyphCount] = { 0, 0};
49 fClusterIndexes[info.glyphCount] = this->leftToRight() ? info.utf8Range.end() : info.utf8Range.begin();
Julia Lavrova526df262019-08-21 17:49:44 -040050 fEllipsis = false;
Julia Lavrovac0360582020-02-05 10:17:53 -050051 fPlaceholderIndex = std::numeric_limits<size_t>::max();
Julia Lavrovaa3552c52019-05-30 16:12:56 -040052}
53
54SkShaper::RunHandler::Buffer Run::newRunBuffer() {
Julia Lavrova2e30fde2019-10-09 09:43:02 -040055 return {fGlyphs.data(), fPositions.data(), fOffsets.data(), fClusterIndexes.data(), fOffset};
Julia Lavrovaa3552c52019-05-30 16:12:56 -040056}
57
Julia Lavrova2e30fde2019-10-09 09:43:02 -040058void Run::commit() {
59 fFont.getBounds(fGlyphs.data(), fGlyphs.size(), fBounds.data(), nullptr);
60}
Julia Lavrovaa3552c52019-05-30 16:12:56 -040061SkScalar Run::calculateWidth(size_t start, size_t end, bool clip) const {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040062 SkASSERT(start <= end);
63 // clip |= end == size(); // Clip at the end of the run?
Julia Lavrova2e30fde2019-10-09 09:43:02 -040064 SkScalar shift = 0;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040065 if (fSpaced && end > start) {
Julia Lavrova2e30fde2019-10-09 09:43:02 -040066 shift = fShifts[clip ? end - 1 : end] - fShifts[start];
Julia Lavrovaa3552c52019-05-30 16:12:56 -040067 }
Julia Lavrova2e30fde2019-10-09 09:43:02 -040068 auto correction = 0.0f;
Julia Lavrovaa0708e82020-02-28 12:14:58 -050069 if (end > start && !fJustificationShifts.empty()) {
70 correction = fJustificationShifts[end - 1].fX -
71 fJustificationShifts[start].fY;
Julia Lavrova2e30fde2019-10-09 09:43:02 -040072 }
73 return posX(end) - posX(start) + shift + correction;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040074}
75
Julia Lavrova2e30fde2019-10-09 09:43:02 -040076void Run::copyTo(SkTextBlobBuilder& builder, size_t pos, size_t size, SkVector runOffset) const {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040077 SkASSERT(pos + size <= this->size());
78 const auto& blobBuffer = builder.allocRunPos(fFont, SkToInt(size));
79 sk_careful_memcpy(blobBuffer.glyphs, fGlyphs.data() + pos, size * sizeof(SkGlyphID));
Julia Lavrova2e30fde2019-10-09 09:43:02 -040080 for (size_t i = 0; i < size; ++i) {
81 auto point = fPositions[i + pos];
82 auto offset = fOffsets[i + pos];
83 point.offset(offset.fX, offset.fY);
84 if (fSpaced) {
85 point.fX += fShifts[i + pos];
Julia Lavrovaa3552c52019-05-30 16:12:56 -040086 }
Julia Lavrovaa0708e82020-02-28 12:14:58 -050087 if (!fJustificationShifts.empty()) {
88 point.fX += fJustificationShifts[i + pos].fX;
89 }
Julia Lavrova2e30fde2019-10-09 09:43:02 -040090 blobBuffer.points()[i] = point + runOffset;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040091 }
92}
93
Julia Lavrova62076972020-02-19 15:12:48 -050094std::tuple<bool, ClusterIndex, ClusterIndex> Run::findLimitingClusters(TextRange text, bool extendToClusters) const {
Julia Lavrova5207f352019-06-21 12:22:32 -040095
96 if (text.width() == 0) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -040097 for (auto i = fClusterRange.start; i != fClusterRange.end; ++i) {
98 auto& cluster = fMaster->cluster(i);
99 if (cluster.textRange().end >= text.end && cluster.textRange().start <= text.start) {
100 return std::make_tuple(true, i, i);
101 }
102 }
103 return std::make_tuple(false, 0, 0);
104 }
105 Cluster* start = nullptr;
106 Cluster* end = nullptr;
Julia Lavrova62076972020-02-19 15:12:48 -0500107 if (extendToClusters) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400108 for (auto i = fClusterRange.start; i != fClusterRange.end; ++i) {
109 auto& cluster = fMaster->cluster(i);
Julia Lavrova62076972020-02-19 15:12:48 -0500110 auto clusterRange = cluster.textRange();
111 if (clusterRange.end <= text.start) {
112 continue;
113 } else if (clusterRange.start >= text.end) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400114 break;
115 }
Julia Lavrova62076972020-02-19 15:12:48 -0500116
117 TextRange s = TextRange(std::max(clusterRange.start, text.start),
118 std::min(clusterRange.end, text.end));
119 if (s.width() > 0) {
120 if (start == nullptr) {
121 start = &cluster;
122 }
123 end = &cluster;
124 }
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400125 }
126 } else {
Julia Lavrovacd2d4e42020-03-27 15:40:37 -0400127 // We need this branch when we draw styles for the part of a cluster
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400128 for (auto i = fClusterRange.start; i != fClusterRange.end; ++i) {
129 auto& cluster = fMaster->cluster(i);
130 if (cluster.textRange().end > text.start && start == nullptr) {
131 start = &cluster;
132 }
133 if (cluster.textRange().start < text.end) {
134 end = &cluster;
135 } else {
136 break;
137 }
138 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400139 }
140
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400141 if (start == nullptr || end == nullptr) {
142 return std::make_tuple(false, 0, 0);
143 }
144
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400145 if (!leftToRight()) {
146 std::swap(start, end);
147 }
148
Julia Lavrova5207f352019-06-21 12:22:32 -0400149 size_t startIndex = start - fMaster->clusters().begin();
150 size_t endIndex = end - fMaster->clusters().begin();
151 return std::make_tuple(startIndex != fClusterRange.end && endIndex != fClusterRange.end, startIndex, endIndex);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400152}
153
Julia Lavrova8335ab62020-04-27 15:49:53 -0400154void Run::iterateThroughClustersInTextOrder(const ClusterTextVisitor& visitor) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400155 // Can't figure out how to do it with one code for both cases without 100 ifs
156 // Can't go through clusters because there are no cluster table yet
157 if (leftToRight()) {
158 size_t start = 0;
159 size_t cluster = this->clusterIndex(start);
160 for (size_t glyph = 1; glyph <= this->size(); ++glyph) {
161 auto nextCluster = this->clusterIndex(glyph);
Julia Lavrova99ede422020-03-17 13:20:58 -0400162 if (nextCluster <= cluster) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400163 continue;
164 }
165
166 visitor(start,
167 glyph,
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400168 fClusterStart + cluster,
169 fClusterStart + nextCluster,
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400170 this->calculateWidth(start, glyph, glyph == size()),
Julia Lavrovadd1de252020-05-08 11:53:19 -0400171 this->calculateHeight(LineMetricStyle::CSS, LineMetricStyle::CSS));
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400172
173 start = glyph;
174 cluster = nextCluster;
175 }
176 } else {
177 size_t glyph = this->size();
178 size_t cluster = this->fUtf8Range.begin();
179 for (int32_t start = this->size() - 1; start >= 0; --start) {
180 size_t nextCluster =
181 start == 0 ? this->fUtf8Range.end() : this->clusterIndex(start - 1);
Julia Lavrova99ede422020-03-17 13:20:58 -0400182 if (nextCluster <= cluster) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400183 continue;
184 }
185
186 visitor(start,
187 glyph,
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400188 fClusterStart + cluster,
189 fClusterStart + nextCluster,
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400190 this->calculateWidth(start, glyph, glyph == 0),
Julia Lavrovadd1de252020-05-08 11:53:19 -0400191 this->calculateHeight(LineMetricStyle::CSS, LineMetricStyle::CSS));
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400192
193 glyph = start;
194 cluster = nextCluster;
195 }
196 }
197}
198
Julia Lavrova8335ab62020-04-27 15:49:53 -0400199void Run::iterateThroughClusters(const ClusterVisitor& visitor) {
200
201 for (size_t index = 0; index < fClusterRange.width(); ++index) {
202 auto correctIndex = leftToRight() ? fClusterRange.start + index : fClusterRange.end - index - 1;
203 auto cluster = &fMaster->cluster(correctIndex);
204 visitor(cluster);
205 }
206}
207
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400208SkScalar Run::addSpacesAtTheEnd(SkScalar space, Cluster* cluster) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400209 if (cluster->endPos() == cluster->startPos()) {
210 return 0;
211 }
212
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400213 fShifts[cluster->endPos() - 1] += space;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400214 // Increment the run width
215 fSpaced = true;
216 fAdvance.fX += space;
217 // Increment the cluster width
218 cluster->space(space, space);
219
220 return space;
221}
222
223SkScalar Run::addSpacesEvenly(SkScalar space, Cluster* cluster) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400224 // Offset all the glyphs in the cluster
225 SkScalar shift = 0;
226 for (size_t i = cluster->startPos(); i < cluster->endPos(); ++i) {
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400227 fShifts[i] += shift;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400228 shift += space;
229 }
230 if (this->size() == cluster->endPos()) {
231 // To make calculations easier
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400232 fShifts[cluster->endPos()] += shift;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400233 }
234 // Increment the run width
235 fSpaced = true;
236 fAdvance.fX += shift;
237 // Increment the cluster width
238 cluster->space(shift, space);
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400239 cluster->setHalfLetterSpacing(space / 2);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400240
241 return shift;
242}
243
244void Run::shift(const Cluster* cluster, SkScalar offset) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400245 if (offset == 0) {
246 return;
247 }
248
249 fSpaced = true;
250 for (size_t i = cluster->startPos(); i < cluster->endPos(); ++i) {
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400251 fShifts[i] += offset;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400252 }
253 if (this->size() == cluster->endPos()) {
254 // To make calculations easier
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400255 fShifts[cluster->endPos()] += offset;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400256 }
257}
258
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400259void Run::updateMetrics(InternalLineMetrics* endlineMetrics) {
Julia Lavrova916a9042019-08-08 16:51:27 -0400260
Julia Lavrovac0360582020-02-05 10:17:53 -0500261 SkASSERT(isPlaceholder());
262 auto placeholderStyle = this->placeholderStyle();
Julia Lavrova916a9042019-08-08 16:51:27 -0400263 // Difference between the placeholder baseline and the line bottom
264 SkScalar baselineAdjustment = 0;
Julia Lavrovac0360582020-02-05 10:17:53 -0500265 switch (placeholderStyle->fBaseline) {
Julia Lavrova916a9042019-08-08 16:51:27 -0400266 case TextBaseline::kAlphabetic:
267 break;
268
269 case TextBaseline::kIdeographic:
270 baselineAdjustment = endlineMetrics->deltaBaselines() / 2;
271 break;
272 }
273
Julia Lavrovac0360582020-02-05 10:17:53 -0500274 auto height = placeholderStyle->fHeight;
275 auto offset = placeholderStyle->fBaselineOffset;
Julia Lavrova916a9042019-08-08 16:51:27 -0400276
277 fFontMetrics.fLeading = 0;
Julia Lavrovac0360582020-02-05 10:17:53 -0500278 switch (placeholderStyle->fAlignment) {
Julia Lavrova916a9042019-08-08 16:51:27 -0400279 case PlaceholderAlignment::kBaseline:
280 fFontMetrics.fAscent = baselineAdjustment - offset;
281 fFontMetrics.fDescent = baselineAdjustment + height - offset;
282 break;
283
284 case PlaceholderAlignment::kAboveBaseline:
285 fFontMetrics.fAscent = baselineAdjustment - height;
286 fFontMetrics.fDescent = baselineAdjustment;
287 break;
288
289 case PlaceholderAlignment::kBelowBaseline:
290 fFontMetrics.fAscent = baselineAdjustment;
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400291 fFontMetrics.fDescent = baselineAdjustment + height;
Julia Lavrova916a9042019-08-08 16:51:27 -0400292 break;
293
294 case PlaceholderAlignment::kTop:
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400295 fFontMetrics.fDescent = height + fFontMetrics.fAscent;
Julia Lavrova916a9042019-08-08 16:51:27 -0400296 break;
297
298 case PlaceholderAlignment::kBottom:
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400299 fFontMetrics.fAscent = fFontMetrics.fDescent - height;
Julia Lavrova916a9042019-08-08 16:51:27 -0400300 break;
301
302 case PlaceholderAlignment::kMiddle:
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400303 auto mid = (-fFontMetrics.fDescent - fFontMetrics.fAscent)/2.0;
304 fFontMetrics.fDescent = height/2.0 - mid;
305 fFontMetrics.fAscent = - height/2.0 - mid;
Julia Lavrova916a9042019-08-08 16:51:27 -0400306 break;
307 }
308
309 // Make sure the placeholder can fit the line
310 endlineMetrics->add(this);
311}
312
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400313void Cluster::setIsWhiteSpaces() {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400314
315 fWhiteSpaces = false;
316
317 auto span = fMaster->text(fTextRange);
318 const char* ch = span.begin();
319 while (ch < span.end()) {
320 auto unichar = utf8_next(&ch, span.end());
321 if (!u_isWhitespace(unichar)) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400322 return;
323 }
324 }
325 fWhiteSpaces = true;
326}
327
Julia Lavrova5207f352019-06-21 12:22:32 -0400328SkScalar Cluster::sizeToChar(TextIndex ch) const {
329 if (ch < fTextRange.start || ch >= fTextRange.end) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400330 return 0;
331 }
Julia Lavrova5207f352019-06-21 12:22:32 -0400332 auto shift = ch - fTextRange.start;
333 auto ratio = shift * 1.0 / fTextRange.width();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400334
335 return SkDoubleToScalar(fWidth * ratio);
336}
337
Julia Lavrova5207f352019-06-21 12:22:32 -0400338SkScalar Cluster::sizeFromChar(TextIndex ch) const {
339 if (ch < fTextRange.start || ch >= fTextRange.end) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400340 return 0;
341 }
Julia Lavrova5207f352019-06-21 12:22:32 -0400342 auto shift = fTextRange.end - ch - 1;
343 auto ratio = shift * 1.0 / fTextRange.width();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400344
345 return SkDoubleToScalar(fWidth * ratio);
346}
347
348size_t Cluster::roundPos(SkScalar s) const {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400349 auto ratio = (s * 1.0) / fWidth;
350 return sk_double_floor2int(ratio * size());
351}
352
353SkScalar Cluster::trimmedWidth(size_t pos) const {
354 // Find the width until the pos and return the min between trimmedWidth and the width(pos)
Julia Lavrova5207f352019-06-21 12:22:32 -0400355 // We don't have to take in account cluster shift since it's the same for 0 and for pos
356 auto& run = fMaster->run(fRunIndex);
Julia Lavrova212bf072020-02-18 12:05:55 -0500357 return std::min(run.positionX(pos) - run.positionX(fStart), fWidth);
Julia Lavrova5207f352019-06-21 12:22:32 -0400358}
359
360SkScalar Run::positionX(size_t pos) const {
Julia Lavrovaa0708e82020-02-28 12:14:58 -0500361 return posX(pos) + fShifts[pos] +
362 (fJustificationShifts.empty() ? 0 : fJustificationShifts[pos].fY);
Julia Lavrova5207f352019-06-21 12:22:32 -0400363}
364
Julia Lavrovac0360582020-02-05 10:17:53 -0500365PlaceholderStyle* Run::placeholderStyle() const {
366 if (isPlaceholder()) {
367 return &fMaster->placeholders()[fPlaceholderIndex].fStyle;
368 } else {
369 return nullptr;
370 }
371}
372
Julia Lavrova5207f352019-06-21 12:22:32 -0400373Run* Cluster::run() const {
374 if (fRunIndex >= fMaster->runs().size()) {
375 return nullptr;
376 }
377 return &fMaster->run(fRunIndex);
378}
379
380SkFont Cluster::font() const {
381 return fMaster->run(fRunIndex).font();
382}
383
384Cluster::Cluster(ParagraphImpl* master,
385 RunIndex runIndex,
386 size_t start,
387 size_t end,
388 SkSpan<const char> text,
389 SkScalar width,
390 SkScalar height)
391 : fMaster(master)
392 , fRunIndex(runIndex)
393 , fTextRange(text.begin() - fMaster->text().begin(), text.end() - fMaster->text().begin())
Julia Lavrovac2228562019-08-08 16:51:27 -0400394 , fGraphemeRange(EMPTY_RANGE)
Julia Lavrova5207f352019-06-21 12:22:32 -0400395 , fStart(start)
396 , fEnd(end)
397 , fWidth(width)
398 , fSpacing(0)
399 , fHeight(height)
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400400 , fHalfLetterSpacing(0.0)
Julia Lavrova5207f352019-06-21 12:22:32 -0400401 , fWhiteSpaces(false)
402 , fBreakType(None) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400403}
404
405} // namespace textlayout
406} // namespace skia