blob: 972a21a218b727c8ac61a65309a78773f044267b [file] [log] [blame]
Julia Lavrovaa3552c52019-05-30 16:12:56 -04001// Copyright 2019 Google LLC.
Julia Lavrovaa3552c52019-05-30 16:12:56 -04002#include "modules/skparagraph/src/ParagraphImpl.h"
Greg Danielf91aeb22019-06-18 09:58:02 -04003#include "modules/skparagraph/src/TextWrapper.h"
Julia Lavrovaa3552c52019-05-30 16:12:56 -04004
5namespace skia {
6namespace textlayout {
7
Julia Lavrovaf3ed2732019-09-05 14:35:17 -04008namespace {
9SkScalar littleRound(SkScalar a) {
10 // This rounding is done to match Flutter tests. Must be removed..
11 return SkScalarRoundToScalar(a * 100.0)/100.0;
12}
13}
14
Julia Lavrovaa3552c52019-05-30 16:12:56 -040015// Since we allow cluster clipping when they don't fit
16// we have to work with stretches - parts of clusters
17void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters) {
18 fWords.startFrom(fEndLine.startCluster(), fEndLine.startPos());
19 fClusters.startFrom(fEndLine.startCluster(), fEndLine.startPos());
20 fClip.startFrom(fEndLine.startCluster(), fEndLine.startPos());
Julia Lavrovaf3ed2732019-09-05 14:35:17 -040021
Julia Lavrovaa3552c52019-05-30 16:12:56 -040022 for (auto cluster = fEndLine.endCluster(); cluster < endOfClusters; ++cluster) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -040023 // TODO: Trying to deal with flutter rounding problem. Must be removed...
24 auto width = fWords.width() + fClusters.width() + cluster->width();
25 auto roundedWidth = littleRound(width);
26 if (cluster->isHardBreak()) {
27 } else if (maxWidth == 0.0f) {
28 // Do nothing
29 } else if (roundedWidth > maxWidth) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040030 if (cluster->isWhitespaces()) {
Julia Lavrova6e6333f2019-06-17 10:34:10 -040031 // It's the end of the word
Julia Lavrovaf3ed2732019-09-05 14:35:17 -040032 fClusters.extend(cluster);
Julia Lavrova6e6333f2019-06-17 10:34:10 -040033 fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, getClustersTrimmedWidth());
34 fWords.extend(fClusters);
Julia Lavrovaa3552c52019-05-30 16:12:56 -040035 break;
36 }
37 if (cluster->width() > maxWidth) {
Julia Lavrova90bfd1c2019-12-04 11:43:32 -050038 fClusters.extend(cluster);
Julia Lavrovaa3552c52019-05-30 16:12:56 -040039 fTooLongCluster = true;
40 fTooLongWord = true;
41 break;
42 }
43
44 // Walk further to see if there is a too long word, cluster or glyph
45 SkScalar nextWordLength = fClusters.width();
46 for (auto further = cluster; further != endOfClusters; ++further) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -040047 if (further->isSoftBreak() || further->isHardBreak() || further->isWhitespaces()) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040048 break;
49 }
50 nextWordLength += further->width();
51 }
52 if (nextWordLength > maxWidth) {
53 // If the word is too long we can break it right now and hope it's enough
Julia Lavrova90bfd1c2019-12-04 11:43:32 -050054 fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, nextWordLength);
Julia Lavrovaa3552c52019-05-30 16:12:56 -040055 fTooLongWord = true;
56 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -040057 break;
58 }
59
60 fClusters.extend(cluster);
61
62 // Keep adding clusters/words
63 if (fClusters.endOfWord()) {
64 fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, getClustersTrimmedWidth());
65 fWords.extend(fClusters);
66 }
67
68 if ((fHardLineBreak = cluster->isHardBreak())) {
69 // Stop at the hard line break
70 break;
71 }
72 }
73}
74
Julia Lavrovac48687a2020-01-08 16:53:53 -050075void TextWrapper::moveForward(bool hasEllipsis) {
76
77 // We normally break lines by words.
78 // The only way we may go to clusters is if the word is too long or
79 // it's the first word and it has an ellipsis attached to it.
80 // If nothing fits we show the clipping.
81 if (!fWords.empty()) {
82 fEndLine.extend(fWords);
83 if (!fTooLongWord || hasEllipsis) {
84 return;
Julia Lavrovaa3552c52019-05-30 16:12:56 -040085 }
Julia Lavrovac48687a2020-01-08 16:53:53 -050086 }
87 if (!fClusters.empty()) {
88 fEndLine.extend(fClusters);
89 if (!fTooLongCluster) {
90 return;
91 }
92 }
93
94 if (!fClip.empty()) {
95 // Flutter: forget the clipped cluster but keep the metrics
96 fEndLine.metrics().add(fClip.metrics());
97 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -040098}
99
100// Special case for start/end cluster since they can be clipped
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400101void TextWrapper::trimEndSpaces(TextAlign align) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400102 // Remember the breaking position
103 fEndLine.saveBreak();
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400104 // Skip all space cluster at the end
105 //bool left = align == TextAlign::kStart || align == TextAlign::kLeft;
106 bool right = align == TextAlign::kRight || align == TextAlign::kEnd;
Julia Lavrova6e6333f2019-06-17 10:34:10 -0400107 for (auto cluster = fEndLine.endCluster();
108 cluster >= fEndLine.startCluster() && cluster->isWhitespaces();
109 --cluster) {
Julia Lavrova526df262019-08-21 17:49:44 -0400110 if ((cluster->run()->leftToRight()) ||
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400111 (right && !cluster->run()->leftToRight()) ||
112 align == TextAlign::kJustify || align == TextAlign::kCenter) {
113 fEndLine.trim(cluster);
114 continue;
115 } else {
116 break;
117 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400118 }
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400119 fEndLine.trim();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400120}
121
122SkScalar TextWrapper::getClustersTrimmedWidth() {
123 // Move the end of the line to the left
124 SkScalar width = fClusters.width();
125 auto cluster = fClusters.endCluster();
126 for (; cluster > fClusters.startCluster() && cluster->isWhitespaces(); --cluster) {
127 width -= cluster->width();
128 }
129 if (cluster >= fClusters.startCluster()) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400130 if (cluster->isWhitespaces()) {
131 width -= cluster->width();
132 } else {
133 width -= (cluster->width() - cluster->trimmedWidth(cluster->endPos()));
134 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400135 }
136 return width;
137}
138
139// Trim the beginning spaces in case of soft line break
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400140std::tuple<Cluster*, size_t, SkScalar> TextWrapper::trimStartSpaces(Cluster* endOfClusters) {
141
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400142 if (fHardLineBreak) {
143 // End of line is always end of cluster, but need to skip \n
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400144 auto width = fEndLine.width();
145 auto cluster = fEndLine.endCluster() + 1;
146 while (cluster < fEndLine.breakCluster() && cluster->isWhitespaces()) {
147 width += cluster->width();
148 ++cluster;
149 }
Julia Lavrovaaf89d392019-08-09 15:19:26 -0400150 return std::make_tuple(fEndLine.breakCluster() + 1, 0, width);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400151 }
152
Julia Lavrova53c14472019-12-18 09:39:55 -0500153 auto width = fEndLine.withWithGhostSpaces();
154 auto cluster = fEndLine.breakCluster();
155 if (fEndLine.endCluster() != fEndLine.startCluster() ||
156 fEndLine.endPos() != fEndLine.startPos()) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400157 ++cluster;
Julia Lavrova53c14472019-12-18 09:39:55 -0500158 while (cluster < endOfClusters && cluster->isWhitespaces()) {
159 width += cluster->width();
160 ++cluster;
161 }
162 } else {
163 // Nothing fits the line - no need to check for spaces
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400164 }
Julia Lavrova53c14472019-12-18 09:39:55 -0500165
Julia Lavrovaaf89d392019-08-09 15:19:26 -0400166 return std::make_tuple(cluster, 0, width);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400167}
168
Julia Lavrovac48687a2020-01-08 16:53:53 -0500169// TODO: refactor the code for line ending (with/without ellipsis)
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400170void TextWrapper::breakTextIntoLines(ParagraphImpl* parent,
171 SkScalar maxWidth,
172 const AddLineToParagraph& addLine) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400173 fHeight = 0;
Julia Lavrova90bfd1c2019-12-04 11:43:32 -0500174 fMinIntrinsicWidth = std::numeric_limits<SkScalar>::min();
175 fMaxIntrinsicWidth = std::numeric_limits<SkScalar>::min();
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400176
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400177 auto span = parent->clusters();
Julia Lavrova526df262019-08-21 17:49:44 -0400178 if (span.size() == 0) {
Julia Lavrovac48687a2020-01-08 16:53:53 -0500179 return;
Julia Lavrova526df262019-08-21 17:49:44 -0400180 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400181 auto maxLines = parent->paragraphStyle().getMaxLines();
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400182 auto& ellipsisStr = parent->paragraphStyle().getEllipsis();
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400183 auto align = parent->paragraphStyle().effective_align();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400184
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400185 SkScalar softLineMaxIntrinsicWidth = 0;
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400186 fEndLine = TextStretch(span.begin(), span.begin(), parent->strutForceHeight());
187 auto end = span.end() - 1;
188 auto start = span.begin();
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400189 InternalLineMetrics maxRunMetrics;
190 auto needEllipsis = false;
191 auto endlessLine = maxLines == std::numeric_limits<size_t>::max();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400192 while (fEndLine.endCluster() != end) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400193
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400194 reset();
Julia Lavrovac48687a2020-01-08 16:53:53 -0500195 auto exceededLines = !endlessLine && fLineNumber >= maxLines;
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400196 fEndLine.metrics().clean();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400197 lookAhead(maxWidth, end);
Julia Lavrovac48687a2020-01-08 16:53:53 -0500198 moveForward(exceededLines && !ellipsisStr.isEmpty());
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400199
200 // Do not trim end spaces on the naturally last line of the left aligned text
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400201 trimEndSpaces(align);
202
203 // For soft line breaks add to the line all the spaces next to it
204 Cluster* startLine;
205 size_t pos;
206 SkScalar widthWithSpaces;
207 std::tie(startLine, pos, widthWithSpaces) = trimStartSpaces(end);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400208
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400209 needEllipsis =
210 fEndLine.endCluster() < end - 1 &&
211 SkScalarIsFinite(maxWidth) &&
212 !ellipsisStr.isEmpty();
213
Julia Lavrovac48687a2020-01-08 16:53:53 -0500214 if (needEllipsis && exceededLines && !fHardLineBreak) {
215 // This is what we need to do to preserve a space before the ellipsis
216 fEndLine.restoreBreak();
217 widthWithSpaces = fEndLine.withWithGhostSpaces();
218 }
Julia Lavrova916a9042019-08-08 16:51:27 -0400219
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400220 // If the line is empty with the hard line break, let's take the paragraph font (flutter???)
221 if (fHardLineBreak && fEndLine.width() == 0) {
Jason Simmons22bb52e2019-12-05 17:56:59 -0800222 fEndLine.setMetrics(parent->getEmptyMetrics());
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400223 }
224
Julia Lavrova916a9042019-08-08 16:51:27 -0400225 // Deal with placeholder clusters == runs[@size==1]
226 for (auto cluster = fEndLine.startCluster(); cluster <= fEndLine.endCluster(); ++cluster) {
227 if (cluster->run()->placeholder() != nullptr) {
228 SkASSERT(cluster->run()->size() == 1);
229 // Update the placeholder metrics so we can get the placeholder positions later
230 // and the line metrics (to make sure the placeholder fits)
231 cluster->run()->updateMetrics(&fEndLine.metrics());
232 }
233 }
234
235 // Before we update the line metrics with struts,
236 // let's save it for GetRectsForRange(RectHeightStyle::kMax)
237 maxRunMetrics = fEndLine.metrics();
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400238 maxRunMetrics.fForceStrut = false;
Julia Lavrova916a9042019-08-08 16:51:27 -0400239
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400240 if (parent->strutEnabled()) {
241 // Make sure font metrics are not less than the strut
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400242 parent->strutMetrics().updateLineMetrics(fEndLine.metrics());
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400243 }
Julia Lavrova916a9042019-08-08 16:51:27 -0400244
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400245 // TODO: keep start/end/break info for text and runs but in a better way that below
Julia Lavrova526df262019-08-21 17:49:44 -0400246 TextRange text(fEndLine.startCluster()->textRange().start, fEndLine.endCluster()->textRange().end);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400247 TextRange textWithSpaces(fEndLine.startCluster()->textRange().start, startLine->textRange().start);
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400248 if (startLine == end) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400249 textWithSpaces.end = parent->text().size();
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400250 }
Julia Lavrovac2228562019-08-08 16:51:27 -0400251 ClusterRange clusters(fEndLine.startCluster() - start, fEndLine.endCluster() - start + 1);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400252 ClusterRange clustersWithGhosts(fEndLine.startCluster() - start, startLine - start);
253 addLine(text, textWithSpaces, clusters, clustersWithGhosts, widthWithSpaces,
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400254 fEndLine.startPos(),
255 fEndLine.endPos(),
256 SkVector::Make(0, fHeight),
257 SkVector::Make(fEndLine.width(), fEndLine.metrics().height()),
258 fEndLine.metrics(),
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400259 needEllipsis && exceededLines && !fHardLineBreak);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400260
Julia Lavrova916a9042019-08-08 16:51:27 -0400261 parent->lines().back().setMaxRunMetrics(maxRunMetrics);
262
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400263 softLineMaxIntrinsicWidth += widthWithSpaces;
264 fMaxIntrinsicWidth = SkMaxScalar(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
265 if (fHardLineBreak) {
266 softLineMaxIntrinsicWidth = 0;
267 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400268 // Start a new line
269 fHeight += fEndLine.metrics().height();
Julia Lavrova916a9042019-08-08 16:51:27 -0400270 if (!fHardLineBreak || startLine != end) {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400271 fEndLine.clean();
272 }
273 fEndLine.startFrom(startLine, pos);
274 parent->fMaxWidthWithTrailingSpaces = SkMaxScalar(parent->fMaxWidthWithTrailingSpaces, widthWithSpaces);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400275
Julia Lavrovac48687a2020-01-08 16:53:53 -0500276 if (exceededLines) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400277 fHardLineBreak = false;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400278 break;
Julia Lavrovac48687a2020-01-08 16:53:53 -0500279 } else if (endlessLine && needEllipsis) {
280 break;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400281 }
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400282
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400283 ++fLineNumber;
284 }
285
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400286 // We finished formatting the text but we need to scan the rest for some numbers
Julia Lavrova53c14472019-12-18 09:39:55 -0500287 if (fEndLine.endCluster() != nullptr) {
Julia Lavrova90bfd1c2019-12-04 11:43:32 -0500288 auto lastWordLength = 0.0f;
Julia Lavrova53c14472019-12-18 09:39:55 -0500289 auto cluster = fEndLine.endCluster();
Julia Lavrova90bfd1c2019-12-04 11:43:32 -0500290 while (cluster != end || cluster->endPos() < end->endPos()) {
291 fExceededMaxLines = true;
292 if (cluster->isHardBreak()) {
293 fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
294 softLineMaxIntrinsicWidth = 0;
295
296 fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
297 lastWordLength = 0;
298 } else if (cluster->isWhitespaces()) {
299 SkASSERT(cluster->isWhitespaces());
300 softLineMaxIntrinsicWidth += cluster->width();
301 fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
302 lastWordLength = 0;
303 } else {
304 softLineMaxIntrinsicWidth += cluster->width();
305 lastWordLength += cluster->width();
306 }
307 ++cluster;
308 }
309 fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
310 fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
311 // In case we could not place a single cluster on the line
312 fHeight = SkTMax(fHeight, fEndLine.metrics().height());
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400313 }
314
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400315 if (fHardLineBreak) {
316 // Last character is a line break
317 if (parent->strutEnabled()) {
318 // Make sure font metrics are not less than the strut
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400319 parent->strutMetrics().updateLineMetrics(fEndLine.metrics());
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400320 }
Julia Lavrova5207f352019-06-21 12:22:32 -0400321 TextRange empty(fEndLine.breakCluster()->textRange().start, fEndLine.breakCluster()->textRange().start);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400322 TextRange hardBreak(fEndLine.breakCluster()->textRange().end, fEndLine.breakCluster()->textRange().end);
Julia Lavrova526df262019-08-21 17:49:44 -0400323 ClusterRange clusters(fEndLine.breakCluster() - start, fEndLine.endCluster() - start);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400324 addLine(empty, hardBreak, clusters, clusters,
325 0,
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400326 0,
327 0,
328 SkVector::Make(0, fHeight),
329 SkVector::Make(0, fEndLine.metrics().height()),
330 fEndLine.metrics(),
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400331 needEllipsis);
332 fHeight += fEndLine.metrics().height();
Julia Lavrova916a9042019-08-08 16:51:27 -0400333 parent->lines().back().setMaxRunMetrics(maxRunMetrics);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400334 }
335}
336
337} // namespace textlayout
338} // namespace skia