blob: def8cb2667749ad29f3c7673660cdbc73c805272 [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) {
38 // Break the cluster into parts by glyph position
39 auto delta = maxWidth - (fWords.width() + fClusters.width());
40 fClip.extend(cluster, cluster->roundPos(delta));
41 fTooLongCluster = true;
42 fTooLongWord = true;
43 break;
44 }
45
46 // Walk further to see if there is a too long word, cluster or glyph
47 SkScalar nextWordLength = fClusters.width();
48 for (auto further = cluster; further != endOfClusters; ++further) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -040049 if (further->isSoftBreak() || further->isHardBreak() || further->isWhitespaces()) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040050 break;
51 }
52 nextWordLength += further->width();
53 }
54 if (nextWordLength > maxWidth) {
55 // If the word is too long we can break it right now and hope it's enough
56 fTooLongWord = true;
57 }
58
59 // TODO: this is the place when we use hyphenation
Julia Lavrovaf3ed2732019-09-05 14:35:17 -040060 fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, fTooLongWord ? maxWidth : nextWordLength);
Julia Lavrovaa3552c52019-05-30 16:12:56 -040061 break;
62 }
63
64 fClusters.extend(cluster);
65
66 // Keep adding clusters/words
67 if (fClusters.endOfWord()) {
68 fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, getClustersTrimmedWidth());
69 fWords.extend(fClusters);
70 }
71
72 if ((fHardLineBreak = cluster->isHardBreak())) {
73 // Stop at the hard line break
74 break;
75 }
76 }
77}
78
79void TextWrapper::moveForward() {
80 do {
81 if (fWords.width() > 0) {
82 fEndLine.extend(fWords);
83 } else if (fClusters.width() > 0) {
84 fEndLine.extend(fClusters);
85 fTooLongWord = false;
86 } else if (fClip.width() > 0) {
87 fEndLine.extend(fClip);
88 fTooLongWord = false;
89 fTooLongCluster = false;
90 } else {
91 break;
92 }
93 } while (fTooLongWord || fTooLongCluster);
94}
95
96// Special case for start/end cluster since they can be clipped
Julia Lavrovadb9f6692019-08-01 16:02:17 -040097void TextWrapper::trimEndSpaces(TextAlign align) {
Julia Lavrovaa3552c52019-05-30 16:12:56 -040098 // Remember the breaking position
99 fEndLine.saveBreak();
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400100 // Skip all space cluster at the end
101 //bool left = align == TextAlign::kStart || align == TextAlign::kLeft;
102 bool right = align == TextAlign::kRight || align == TextAlign::kEnd;
Julia Lavrova6e6333f2019-06-17 10:34:10 -0400103 for (auto cluster = fEndLine.endCluster();
104 cluster >= fEndLine.startCluster() && cluster->isWhitespaces();
105 --cluster) {
Julia Lavrova526df262019-08-21 17:49:44 -0400106 if ((cluster->run()->leftToRight()) ||
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400107 (right && !cluster->run()->leftToRight()) ||
108 align == TextAlign::kJustify || align == TextAlign::kCenter) {
109 fEndLine.trim(cluster);
110 continue;
111 } else {
112 break;
113 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400114 }
Julia Lavrova526df262019-08-21 17:49:44 -0400115 if (!right || true) {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400116 fEndLine.trim();
117 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400118}
119
120SkScalar TextWrapper::getClustersTrimmedWidth() {
121 // Move the end of the line to the left
122 SkScalar width = fClusters.width();
123 auto cluster = fClusters.endCluster();
124 for (; cluster > fClusters.startCluster() && cluster->isWhitespaces(); --cluster) {
125 width -= cluster->width();
126 }
127 if (cluster >= fClusters.startCluster()) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400128 if (cluster->isWhitespaces()) {
129 width -= cluster->width();
130 } else {
131 width -= (cluster->width() - cluster->trimmedWidth(cluster->endPos()));
132 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400133 }
134 return width;
135}
136
137// Trim the beginning spaces in case of soft line break
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400138std::tuple<Cluster*, size_t, SkScalar> TextWrapper::trimStartSpaces(Cluster* endOfClusters) {
139
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400140 if (fHardLineBreak) {
141 // End of line is always end of cluster, but need to skip \n
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400142 auto width = fEndLine.width();
143 auto cluster = fEndLine.endCluster() + 1;
144 while (cluster < fEndLine.breakCluster() && cluster->isWhitespaces()) {
145 width += cluster->width();
146 ++cluster;
147 }
Julia Lavrovaaf89d392019-08-09 15:19:26 -0400148 return std::make_tuple(fEndLine.breakCluster() + 1, 0, width);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400149 }
150
Julia Lavrovac2228562019-08-08 16:51:27 -0400151 auto width = fEndLine.withWithGhostSpaces(); //fEndLine.width();
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400152 auto cluster = fEndLine.breakCluster() + 1;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400153 while (cluster < endOfClusters && cluster->isWhitespaces()) {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400154 width += cluster->width();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400155 ++cluster;
156 }
Julia Lavrovaaf89d392019-08-09 15:19:26 -0400157 return std::make_tuple(cluster, 0, width);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400158}
159
160void TextWrapper::breakTextIntoLines(ParagraphImpl* parent,
161 SkScalar maxWidth,
162 const AddLineToParagraph& addLine) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400163 fHeight = 0;
164 fMinIntrinsicWidth = 0;
165 fMaxIntrinsicWidth = 0;
166
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400167 auto span = parent->clusters();
Julia Lavrova526df262019-08-21 17:49:44 -0400168 if (span.size() == 0) {
169 return;
170 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400171 auto maxLines = parent->paragraphStyle().getMaxLines();
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400172 auto& ellipsisStr = parent->paragraphStyle().getEllipsis();
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400173 auto align = parent->paragraphStyle().effective_align();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400174
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400175 SkScalar softLineMaxIntrinsicWidth = 0;
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400176 fEndLine = TextStretch(span.begin(), span.begin(), parent->strutForceHeight());
177 auto end = span.end() - 1;
178 auto start = span.begin();
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400179 InternalLineMetrics maxRunMetrics;
180 auto needEllipsis = false;
181 auto endlessLine = maxLines == std::numeric_limits<size_t>::max();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400182 while (fEndLine.endCluster() != end) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400183
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400184 reset();
185
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400186 fEndLine.metrics().clean();
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400187 lookAhead(maxWidth, end);
188 moveForward();
189
190 // Do not trim end spaces on the naturally last line of the left aligned text
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400191 trimEndSpaces(align);
192
193 // For soft line breaks add to the line all the spaces next to it
194 Cluster* startLine;
195 size_t pos;
196 SkScalar widthWithSpaces;
197 std::tie(startLine, pos, widthWithSpaces) = trimStartSpaces(end);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400198
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400199 needEllipsis =
200 fEndLine.endCluster() < end - 1 &&
201 SkScalarIsFinite(maxWidth) &&
202 !ellipsisStr.isEmpty();
203
204 auto exceededLines = !endlessLine && fLineNumber >= maxLines;
205
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400206 // TODO: perform ellipsis work here
Julia Lavrova916a9042019-08-08 16:51:27 -0400207
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400208 // If the line is empty with the hard line break, let's take the paragraph font (flutter???)
209 if (fHardLineBreak && fEndLine.width() == 0) {
210 auto emptyMetrics = parent->computeEmptyMetrics();
211 fEndLine.setMetrics(emptyMetrics);
212 }
213
Julia Lavrova916a9042019-08-08 16:51:27 -0400214 // Deal with placeholder clusters == runs[@size==1]
215 for (auto cluster = fEndLine.startCluster(); cluster <= fEndLine.endCluster(); ++cluster) {
216 if (cluster->run()->placeholder() != nullptr) {
217 SkASSERT(cluster->run()->size() == 1);
218 // Update the placeholder metrics so we can get the placeholder positions later
219 // and the line metrics (to make sure the placeholder fits)
220 cluster->run()->updateMetrics(&fEndLine.metrics());
221 }
222 }
223
224 // Before we update the line metrics with struts,
225 // let's save it for GetRectsForRange(RectHeightStyle::kMax)
226 maxRunMetrics = fEndLine.metrics();
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400227 maxRunMetrics.fForceStrut = false;
Julia Lavrova916a9042019-08-08 16:51:27 -0400228
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400229 if (parent->strutEnabled()) {
230 // Make sure font metrics are not less than the strut
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400231 parent->strutMetrics().updateLineMetrics(fEndLine.metrics());
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400232 }
Julia Lavrova916a9042019-08-08 16:51:27 -0400233
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400234 // TODO: keep start/end/break info for text and runs but in a better way that below
Julia Lavrova526df262019-08-21 17:49:44 -0400235 TextRange text(fEndLine.startCluster()->textRange().start, fEndLine.endCluster()->textRange().end);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400236 TextRange textWithSpaces(fEndLine.startCluster()->textRange().start, startLine->textRange().start);
237 if (fEndLine.breakCluster()->isHardBreak()) {
238 textWithSpaces.end = fEndLine.breakCluster()->textRange().start;
239 } else if (startLine == end) {
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400240 textWithSpaces.end = parent->text().size();
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400241 }
Julia Lavrovac2228562019-08-08 16:51:27 -0400242 ClusterRange clusters(fEndLine.startCluster() - start, fEndLine.endCluster() - start + 1);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400243 ClusterRange clustersWithGhosts(fEndLine.startCluster() - start, startLine - start);
244 addLine(text, textWithSpaces, clusters, clustersWithGhosts, widthWithSpaces,
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400245 fEndLine.startPos(),
246 fEndLine.endPos(),
247 SkVector::Make(0, fHeight),
248 SkVector::Make(fEndLine.width(), fEndLine.metrics().height()),
249 fEndLine.metrics(),
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400250 needEllipsis && exceededLines && !fHardLineBreak);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400251
Julia Lavrova916a9042019-08-08 16:51:27 -0400252 parent->lines().back().setMaxRunMetrics(maxRunMetrics);
253
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400254 softLineMaxIntrinsicWidth += widthWithSpaces;
255 fMaxIntrinsicWidth = SkMaxScalar(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
256 if (fHardLineBreak) {
257 softLineMaxIntrinsicWidth = 0;
258 }
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400259 // Start a new line
260 fHeight += fEndLine.metrics().height();
Julia Lavrova916a9042019-08-08 16:51:27 -0400261 if (!fHardLineBreak || startLine != end) {
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400262 fEndLine.clean();
263 }
264 fEndLine.startFrom(startLine, pos);
265 parent->fMaxWidthWithTrailingSpaces = SkMaxScalar(parent->fMaxWidthWithTrailingSpaces, widthWithSpaces);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400266
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400267 if (exceededLines || (needEllipsis && endlessLine && !fHardLineBreak)) {
268 fHardLineBreak = false;
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400269 break;
270 }
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400271
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400272 ++fLineNumber;
273 }
274
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400275 // We finished formatting the text but we need to scan the rest for some numbers
276 auto cluster = fEndLine.endCluster();
277 while (cluster != end) {
278 fExceededMaxLines = true;
279 if (cluster->isHardBreak()) {
280 softLineMaxIntrinsicWidth = 0;
281 } else {
282 softLineMaxIntrinsicWidth += cluster->width();
283 fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth);
284 }
285 ++cluster;
286 }
287
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400288 if (fHardLineBreak) {
Julia Lavrova916a9042019-08-08 16:51:27 -0400289
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400290 // Last character is a line break
291 if (parent->strutEnabled()) {
292 // Make sure font metrics are not less than the strut
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400293 parent->strutMetrics().updateLineMetrics(fEndLine.metrics());
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400294 }
Julia Lavrova5207f352019-06-21 12:22:32 -0400295 TextRange empty(fEndLine.breakCluster()->textRange().start, fEndLine.breakCluster()->textRange().start);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400296 TextRange hardBreak(fEndLine.breakCluster()->textRange().end, fEndLine.breakCluster()->textRange().end);
Julia Lavrova526df262019-08-21 17:49:44 -0400297 ClusterRange clusters(fEndLine.breakCluster() - start, fEndLine.endCluster() - start);
Julia Lavrovadb9f6692019-08-01 16:02:17 -0400298 addLine(empty, hardBreak, clusters, clusters,
299 0,
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400300 0,
301 0,
302 SkVector::Make(0, fHeight),
303 SkVector::Make(0, fEndLine.metrics().height()),
304 fEndLine.metrics(),
Julia Lavrovaf3ed2732019-09-05 14:35:17 -0400305 needEllipsis);
306 fHeight += fEndLine.metrics().height();
Julia Lavrova916a9042019-08-08 16:51:27 -0400307 parent->lines().back().setMaxRunMetrics(maxRunMetrics);
Julia Lavrovaa3552c52019-05-30 16:12:56 -0400308 }
309}
310
311} // namespace textlayout
312} // namespace skia