blob: 4f69c46b722629ca702f0e033f3ed8186f5ff1a8 [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
75void TextWrapper::moveForward() {
76 do {
77 if (fWords.width() > 0) {
78 fEndLine.extend(fWords);
79 } else if (fClusters.width() > 0) {
80 fEndLine.extend(fClusters);
81 fTooLongWord = false;
Julia Lavrova90bfd1c2019-12-04 11:43:32 -050082 fTooLongCluster = false;
Julia Lavrova2e30fde2019-10-09 09:43:02 -040083 } else if (fClip.width() > 0 || (fTooLongWord && fTooLongCluster)) {
Julia Lavrova90bfd1c2019-12-04 11:43:32 -050084 // Flutter: forget the clipped cluster but keep the metrics
85 fEndLine.metrics().add(fClip.metrics());
Julia Lavrovaa3552c52019-05-30 16:12:56 -040086 fTooLongWord = false;
87 fTooLongCluster = false;
88 } else {
89 break;
90 }
91 } while (fTooLongWord || fTooLongCluster);
92}
93
94// Special case for start/end cluster since they can be clipped
Julia Lavrovadb9f6692019-08-01 16:02:17 -040095void TextWrapper::trimEndSpaces(TextAlign align) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040096 // Remember the breaking position
97 fEndLine.saveBreak();
Julia Lavrovadb9f6692019-08-01 16:02:17 -040098 // Skip all space cluster at the end
99 //bool left = align == TextAlign::kStart || align == TextAlign::kLeft;
100 bool right = align == TextAlign::kRight || align == TextAlign::kEnd;
Julia Lavrova6e6333f2019-06-17 10:34:10 -0400101 for (auto cluster = fEndLine.endCluster();
102 cluster >= fEndLine.startCluster() && cluster->isWhitespaces();
103 --cluster) {
Julia Lavrova526df262019-08-21 17:49:44 -0400104 if ((cluster->run()->leftToRight()) ||
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400105 (right && !cluster->run()->leftToRight()) ||
106 align == TextAlign::kJustify || align == TextAlign::kCenter) {
107 fEndLine.trim(cluster);
108 continue;
109 } else {
110 break;
111 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400112 }
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400113 fEndLine.trim();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400114}
115
116SkScalar TextWrapper::getClustersTrimmedWidth() {
117 // Move the end of the line to the left
118 SkScalar width = fClusters.width();
119 auto cluster = fClusters.endCluster();
120 for (; cluster > fClusters.startCluster() && cluster->isWhitespaces(); --cluster) {
121 width -= cluster->width();
122 }
123 if (cluster >= fClusters.startCluster()) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400124 if (cluster->isWhitespaces()) {
125 width -= cluster->width();
126 } else {
127 width -= (cluster->width() - cluster->trimmedWidth(cluster->endPos()));
128 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400129 }
130 return width;
131}
132
133// Trim the beginning spaces in case of soft line break
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400134std::tuple<Cluster*, size_t, SkScalar> TextWrapper::trimStartSpaces(Cluster* endOfClusters) {
135
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400136 if (fHardLineBreak) {
137 // End of line is always end of cluster, but need to skip \n
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400138 auto width = fEndLine.width();
139 auto cluster = fEndLine.endCluster() + 1;
140 while (cluster < fEndLine.breakCluster() && cluster->isWhitespaces()) {
141 width += cluster->width();
142 ++cluster;
143 }
Julia Lavrovaaf89d392019-08-09 15:19:26 -0400144 return std::make_tuple(fEndLine.breakCluster() + 1, 0, width);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400145 }
146
Julia Lavrovac2228562019-08-08 16:51:27 -0400147 auto width = fEndLine.withWithGhostSpaces(); //fEndLine.width();
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400148 auto cluster = fEndLine.breakCluster() + 1;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400149 while (cluster < endOfClusters && cluster->isWhitespaces()) {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400150 width += cluster->width();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400151 ++cluster;
152 }
Julia Lavrovaaf89d392019-08-09 15:19:26 -0400153 return std::make_tuple(cluster, 0, width);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400154}
155
156void TextWrapper::breakTextIntoLines(ParagraphImpl* parent,
157 SkScalar maxWidth,
158 const AddLineToParagraph& addLine) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400159 fHeight = 0;
Julia Lavrova90bfd1c2019-12-04 11:43:32 -0500160 fMinIntrinsicWidth = std::numeric_limits<SkScalar>::min();
161 fMaxIntrinsicWidth = std::numeric_limits<SkScalar>::min();
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400162
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400163 auto span = parent->clusters();
Julia Lavrova526df262019-08-21 17:49:44 -0400164 if (span.size() == 0) {
165 return;
166 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400167 auto maxLines = parent->paragraphStyle().getMaxLines();
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400168 auto& ellipsisStr = parent->paragraphStyle().getEllipsis();
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400169 auto align = parent->paragraphStyle().effective_align();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400170
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400171 SkScalar softLineMaxIntrinsicWidth = 0;
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400172 fEndLine = TextStretch(span.begin(), span.begin(), parent->strutForceHeight());
173 auto end = span.end() - 1;
174 auto start = span.begin();
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400175 InternalLineMetrics maxRunMetrics;
176 auto needEllipsis = false;
177 auto endlessLine = maxLines == std::numeric_limits<size_t>::max();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400178 while (fEndLine.endCluster() != end) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400179
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400180 reset();
181
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400182 fEndLine.metrics().clean();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400183 lookAhead(maxWidth, end);
184 moveForward();
185
186 // Do not trim end spaces on the naturally last line of the left aligned text
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400187 trimEndSpaces(align);
188
189 // For soft line breaks add to the line all the spaces next to it
190 Cluster* startLine;
191 size_t pos;
192 SkScalar widthWithSpaces;
193 std::tie(startLine, pos, widthWithSpaces) = trimStartSpaces(end);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400194
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400195 needEllipsis =
196 fEndLine.endCluster() < end - 1 &&
197 SkScalarIsFinite(maxWidth) &&
198 !ellipsisStr.isEmpty();
199
200 auto exceededLines = !endlessLine && fLineNumber >= maxLines;
201
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400202 // TODO: perform ellipsis work here
Julia Lavrova916a9042019-08-08 16:51:27 -0400203
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400204 // If the line is empty with the hard line break, let's take the paragraph font (flutter???)
205 if (fHardLineBreak && fEndLine.width() == 0) {
Jason Simmons22bb52e2019-12-05 17:56:59 -0800206 fEndLine.setMetrics(parent->getEmptyMetrics());
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400207 }
208
Julia Lavrova916a9042019-08-08 16:51:27 -0400209 // Deal with placeholder clusters == runs[@size==1]
210 for (auto cluster = fEndLine.startCluster(); cluster <= fEndLine.endCluster(); ++cluster) {
211 if (cluster->run()->placeholder() != nullptr) {
212 SkASSERT(cluster->run()->size() == 1);
213 // Update the placeholder metrics so we can get the placeholder positions later
214 // and the line metrics (to make sure the placeholder fits)
215 cluster->run()->updateMetrics(&fEndLine.metrics());
216 }
217 }
218
219 // Before we update the line metrics with struts,
220 // let's save it for GetRectsForRange(RectHeightStyle::kMax)
221 maxRunMetrics = fEndLine.metrics();
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400222 maxRunMetrics.fForceStrut = false;
Julia Lavrova916a9042019-08-08 16:51:27 -0400223
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400224 if (parent->strutEnabled()) {
225 // Make sure font metrics are not less than the strut
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400226 parent->strutMetrics().updateLineMetrics(fEndLine.metrics());
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400227 }
Julia Lavrova916a9042019-08-08 16:51:27 -0400228
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400229 // TODO: keep start/end/break info for text and runs but in a better way that below
Julia Lavrova526df262019-08-21 17:49:44 -0400230 TextRange text(fEndLine.startCluster()->textRange().start, fEndLine.endCluster()->textRange().end);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400231 TextRange textWithSpaces(fEndLine.startCluster()->textRange().start, startLine->textRange().start);
Julia Lavrova2e30fde2019-10-09 09:43:02 -0400232 if (startLine == end) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400233 textWithSpaces.end = parent->text().size();
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400234 }
Julia Lavrovac2228562019-08-08 16:51:27 -0400235 ClusterRange clusters(fEndLine.startCluster() - start, fEndLine.endCluster() - start + 1);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400236 ClusterRange clustersWithGhosts(fEndLine.startCluster() - start, startLine - start);
237 addLine(text, textWithSpaces, clusters, clustersWithGhosts, widthWithSpaces,
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400238 fEndLine.startPos(),
239 fEndLine.endPos(),
240 SkVector::Make(0, fHeight),
241 SkVector::Make(fEndLine.width(), fEndLine.metrics().height()),
242 fEndLine.metrics(),
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400243 needEllipsis && exceededLines && !fHardLineBreak);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400244
Julia Lavrova916a9042019-08-08 16:51:27 -0400245 parent->lines().back().setMaxRunMetrics(maxRunMetrics);
246
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400247 softLineMaxIntrinsicWidth += widthWithSpaces;
248 fMaxIntrinsicWidth = SkMaxScalar(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
249 if (fHardLineBreak) {
250 softLineMaxIntrinsicWidth = 0;
251 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400252 // Start a new line
253 fHeight += fEndLine.metrics().height();
Julia Lavrova916a9042019-08-08 16:51:27 -0400254 if (!fHardLineBreak || startLine != end) {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400255 fEndLine.clean();
256 }
257 fEndLine.startFrom(startLine, pos);
258 parent->fMaxWidthWithTrailingSpaces = SkMaxScalar(parent->fMaxWidthWithTrailingSpaces, widthWithSpaces);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400259
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400260 if (exceededLines || (needEllipsis && endlessLine && !fHardLineBreak)) {
261 fHardLineBreak = false;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400262 break;
263 }
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400264
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400265 ++fLineNumber;
266 }
267
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400268 // We finished formatting the text but we need to scan the rest for some numbers
Julia Lavrova90bfd1c2019-12-04 11:43:32 -0500269 if (fEndLine.breakCluster() != nullptr) {
270 auto lastWordLength = 0.0f;
271 auto cluster = fEndLine.breakCluster();
272 if (cluster != end) {
273 ++cluster;
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400274 }
Julia Lavrova90bfd1c2019-12-04 11:43:32 -0500275 while (cluster != end || cluster->endPos() < end->endPos()) {
276 fExceededMaxLines = true;
277 if (cluster->isHardBreak()) {
278 fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
279 softLineMaxIntrinsicWidth = 0;
280
281 fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
282 lastWordLength = 0;
283 } else if (cluster->isWhitespaces()) {
284 SkASSERT(cluster->isWhitespaces());
285 softLineMaxIntrinsicWidth += cluster->width();
286 fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
287 lastWordLength = 0;
288 } else {
289 softLineMaxIntrinsicWidth += cluster->width();
290 lastWordLength += cluster->width();
291 }
292 ++cluster;
293 }
294 fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength);
295 fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
296 // In case we could not place a single cluster on the line
297 fHeight = SkTMax(fHeight, fEndLine.metrics().height());
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400298 }
299
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400300 if (fHardLineBreak) {
Julia Lavrova916a9042019-08-08 16:51:27 -0400301
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400302 // Last character is a line break
303 if (parent->strutEnabled()) {
304 // Make sure font metrics are not less than the strut
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400305 parent->strutMetrics().updateLineMetrics(fEndLine.metrics());
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400306 }
Julia Lavrova5207f352019-06-21 12:22:32 -0400307 TextRange empty(fEndLine.breakCluster()->textRange().start, fEndLine.breakCluster()->textRange().start);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400308 TextRange hardBreak(fEndLine.breakCluster()->textRange().end, fEndLine.breakCluster()->textRange().end);
Julia Lavrova526df262019-08-21 17:49:44 -0400309 ClusterRange clusters(fEndLine.breakCluster() - start, fEndLine.endCluster() - start);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400310 addLine(empty, hardBreak, clusters, clusters,
311 0,
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400312 0,
313 0,
314 SkVector::Make(0, fHeight),
315 SkVector::Make(0, fEndLine.metrics().height()),
316 fEndLine.metrics(),
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400317 needEllipsis);
318 fHeight += fEndLine.metrics().height();
Julia Lavrova916a9042019-08-08 16:51:27 -0400319 parent->lines().back().setMaxRunMetrics(maxRunMetrics);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400320 }
321}
322
323} // namespace textlayout
324} // namespace skia