Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 2 | #include "modules/skparagraph/src/ParagraphImpl.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 3 | #include "modules/skparagraph/src/TextWrapper.h" |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 4 | |
| 5 | namespace skia { |
| 6 | namespace textlayout { |
| 7 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 8 | namespace { |
| 9 | SkScalar 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 Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 15 | // Since we allow cluster clipping when they don't fit |
| 16 | // we have to work with stretches - parts of clusters |
| 17 | void TextWrapper::lookAhead(SkScalar maxWidth, Cluster* endOfClusters) { |
Julia Lavrova | 2ea20ea | 2020-01-22 10:56:53 -0500 | [diff] [blame] | 18 | |
| 19 | reset(); |
| 20 | fEndLine.metrics().clean(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 21 | fWords.startFrom(fEndLine.startCluster(), fEndLine.startPos()); |
| 22 | fClusters.startFrom(fEndLine.startCluster(), fEndLine.startPos()); |
| 23 | fClip.startFrom(fEndLine.startCluster(), fEndLine.startPos()); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 24 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 25 | for (auto cluster = fEndLine.endCluster(); cluster < endOfClusters; ++cluster) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 26 | // TODO: Trying to deal with flutter rounding problem. Must be removed... |
| 27 | auto width = fWords.width() + fClusters.width() + cluster->width(); |
| 28 | auto roundedWidth = littleRound(width); |
| 29 | if (cluster->isHardBreak()) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 30 | } else if (roundedWidth > maxWidth) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 31 | if (cluster->isWhitespaces()) { |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 32 | // It's the end of the word |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 33 | fClusters.extend(cluster); |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 34 | fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, getClustersTrimmedWidth()); |
| 35 | fWords.extend(fClusters); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 36 | break; |
| 37 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 38 | |
| 39 | // Walk further to see if there is a too long word, cluster or glyph |
| 40 | SkScalar nextWordLength = fClusters.width(); |
| 41 | for (auto further = cluster; further != endOfClusters; ++further) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 42 | if (further->isSoftBreak() || further->isHardBreak() || further->isWhitespaces()) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 43 | break; |
| 44 | } |
| 45 | nextWordLength += further->width(); |
| 46 | } |
| 47 | if (nextWordLength > maxWidth) { |
| 48 | // If the word is too long we can break it right now and hope it's enough |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 49 | fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, nextWordLength); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 50 | fTooLongWord = true; |
| 51 | } |
Julia Lavrova | 70e9301 | 2020-01-10 14:04:45 -0500 | [diff] [blame] | 52 | |
| 53 | if (cluster->width() > maxWidth) { |
| 54 | fClusters.extend(cluster); |
| 55 | fTooLongCluster = true; |
| 56 | fTooLongWord = true; |
| 57 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 58 | break; |
| 59 | } |
| 60 | |
| 61 | fClusters.extend(cluster); |
| 62 | |
| 63 | // Keep adding clusters/words |
| 64 | if (fClusters.endOfWord()) { |
| 65 | fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, getClustersTrimmedWidth()); |
| 66 | fWords.extend(fClusters); |
| 67 | } |
| 68 | |
| 69 | if ((fHardLineBreak = cluster->isHardBreak())) { |
| 70 | // Stop at the hard line break |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame] | 76 | void TextWrapper::moveForward(bool hasEllipsis) { |
| 77 | |
| 78 | // We normally break lines by words. |
| 79 | // The only way we may go to clusters is if the word is too long or |
| 80 | // it's the first word and it has an ellipsis attached to it. |
| 81 | // If nothing fits we show the clipping. |
| 82 | if (!fWords.empty()) { |
| 83 | fEndLine.extend(fWords); |
| 84 | if (!fTooLongWord || hasEllipsis) { |
| 85 | return; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 86 | } |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame] | 87 | } |
| 88 | if (!fClusters.empty()) { |
| 89 | fEndLine.extend(fClusters); |
| 90 | if (!fTooLongCluster) { |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (!fClip.empty()) { |
| 96 | // Flutter: forget the clipped cluster but keep the metrics |
| 97 | fEndLine.metrics().add(fClip.metrics()); |
| 98 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Special case for start/end cluster since they can be clipped |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 102 | void TextWrapper::trimEndSpaces(TextAlign align) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 103 | // Remember the breaking position |
| 104 | fEndLine.saveBreak(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 105 | // Skip all space cluster at the end |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 106 | for (auto cluster = fEndLine.endCluster(); |
| 107 | cluster >= fEndLine.startCluster() && cluster->isWhitespaces(); |
| 108 | --cluster) { |
Julia Lavrova | d3a32c5 | 2020-02-03 09:43:52 -0500 | [diff] [blame] | 109 | fEndLine.trim(cluster); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 110 | } |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 111 | fEndLine.trim(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | SkScalar TextWrapper::getClustersTrimmedWidth() { |
| 115 | // Move the end of the line to the left |
| 116 | SkScalar width = fClusters.width(); |
| 117 | auto cluster = fClusters.endCluster(); |
| 118 | for (; cluster > fClusters.startCluster() && cluster->isWhitespaces(); --cluster) { |
| 119 | width -= cluster->width(); |
| 120 | } |
| 121 | if (cluster >= fClusters.startCluster()) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 122 | if (cluster->isWhitespaces()) { |
| 123 | width -= cluster->width(); |
| 124 | } else { |
| 125 | width -= (cluster->width() - cluster->trimmedWidth(cluster->endPos())); |
| 126 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 127 | } |
| 128 | return width; |
| 129 | } |
| 130 | |
| 131 | // Trim the beginning spaces in case of soft line break |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 132 | std::tuple<Cluster*, size_t, SkScalar> TextWrapper::trimStartSpaces(Cluster* endOfClusters) { |
| 133 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 134 | if (fHardLineBreak) { |
| 135 | // End of line is always end of cluster, but need to skip \n |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 136 | auto width = fEndLine.width(); |
| 137 | auto cluster = fEndLine.endCluster() + 1; |
| 138 | while (cluster < fEndLine.breakCluster() && cluster->isWhitespaces()) { |
| 139 | width += cluster->width(); |
| 140 | ++cluster; |
| 141 | } |
Julia Lavrova | af89d39 | 2019-08-09 15:19:26 -0400 | [diff] [blame] | 142 | return std::make_tuple(fEndLine.breakCluster() + 1, 0, width); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Julia Lavrova | 53c1447 | 2019-12-18 09:39:55 -0500 | [diff] [blame] | 145 | auto width = fEndLine.withWithGhostSpaces(); |
| 146 | auto cluster = fEndLine.breakCluster(); |
| 147 | if (fEndLine.endCluster() != fEndLine.startCluster() || |
| 148 | fEndLine.endPos() != fEndLine.startPos()) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 149 | ++cluster; |
Julia Lavrova | 53c1447 | 2019-12-18 09:39:55 -0500 | [diff] [blame] | 150 | while (cluster < endOfClusters && cluster->isWhitespaces()) { |
| 151 | width += cluster->width(); |
| 152 | ++cluster; |
| 153 | } |
| 154 | } else { |
| 155 | // Nothing fits the line - no need to check for spaces |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 156 | } |
Julia Lavrova | 53c1447 | 2019-12-18 09:39:55 -0500 | [diff] [blame] | 157 | |
Julia Lavrova | af89d39 | 2019-08-09 15:19:26 -0400 | [diff] [blame] | 158 | return std::make_tuple(cluster, 0, width); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame] | 161 | // TODO: refactor the code for line ending (with/without ellipsis) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 162 | void TextWrapper::breakTextIntoLines(ParagraphImpl* parent, |
| 163 | SkScalar maxWidth, |
| 164 | const AddLineToParagraph& addLine) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 165 | fHeight = 0; |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 166 | fMinIntrinsicWidth = std::numeric_limits<SkScalar>::min(); |
| 167 | fMaxIntrinsicWidth = std::numeric_limits<SkScalar>::min(); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 168 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 169 | auto span = parent->clusters(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 170 | if (span.size() == 0) { |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame] | 171 | return; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 172 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 173 | auto maxLines = parent->paragraphStyle().getMaxLines(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 174 | auto& ellipsisStr = parent->paragraphStyle().getEllipsis(); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 175 | auto align = parent->paragraphStyle().effective_align(); |
Julia Lavrova | 2ea20ea | 2020-01-22 10:56:53 -0500 | [diff] [blame] | 176 | auto unlimitedLines = maxLines == std::numeric_limits<size_t>::max(); |
| 177 | auto endlessLine = !SkScalarIsFinite(maxWidth); |
| 178 | auto hasEllipsis = !ellipsisStr.isEmpty(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 179 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 180 | SkScalar softLineMaxIntrinsicWidth = 0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 181 | fEndLine = TextStretch(span.begin(), span.begin(), parent->strutForceHeight()); |
| 182 | auto end = span.end() - 1; |
| 183 | auto start = span.begin(); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 184 | InternalLineMetrics maxRunMetrics; |
Julia Lavrova | 2ea20ea | 2020-01-22 10:56:53 -0500 | [diff] [blame] | 185 | bool needEllipsis = false; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 186 | while (fEndLine.endCluster() != end) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 187 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 188 | lookAhead(maxWidth, end); |
Julia Lavrova | 2ea20ea | 2020-01-22 10:56:53 -0500 | [diff] [blame] | 189 | |
| 190 | auto lastLine = (hasEllipsis && unlimitedLines) || fLineNumber >= maxLines; |
| 191 | needEllipsis = hasEllipsis && !endlessLine && lastLine; |
| 192 | |
| 193 | moveForward(needEllipsis); |
| 194 | needEllipsis &= fEndLine.endCluster() < end - 1; // Only if we have some text to ellipsize |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 195 | |
| 196 | // Do not trim end spaces on the naturally last line of the left aligned text |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 197 | trimEndSpaces(align); |
| 198 | |
| 199 | // For soft line breaks add to the line all the spaces next to it |
| 200 | Cluster* startLine; |
| 201 | size_t pos; |
| 202 | SkScalar widthWithSpaces; |
| 203 | std::tie(startLine, pos, widthWithSpaces) = trimStartSpaces(end); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 204 | |
Julia Lavrova | 2ea20ea | 2020-01-22 10:56:53 -0500 | [diff] [blame] | 205 | if (needEllipsis && !fHardLineBreak) { |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame] | 206 | // This is what we need to do to preserve a space before the ellipsis |
| 207 | fEndLine.restoreBreak(); |
| 208 | widthWithSpaces = fEndLine.withWithGhostSpaces(); |
| 209 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 210 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 211 | // If the line is empty with the hard line break, let's take the paragraph font (flutter???) |
| 212 | if (fHardLineBreak && fEndLine.width() == 0) { |
Jason Simmons | 22bb52e | 2019-12-05 17:56:59 -0800 | [diff] [blame] | 213 | fEndLine.setMetrics(parent->getEmptyMetrics()); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 216 | // Deal with placeholder clusters == runs[@size==1] |
Julia Lavrova | 2ea20ea | 2020-01-22 10:56:53 -0500 | [diff] [blame] | 217 | Run* lastRun = nullptr; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 218 | for (auto cluster = fEndLine.startCluster(); cluster <= fEndLine.endCluster(); ++cluster) { |
Julia Lavrova | 2ea20ea | 2020-01-22 10:56:53 -0500 | [diff] [blame] | 219 | if (cluster->run() == lastRun) { |
| 220 | continue; |
| 221 | } |
| 222 | lastRun = cluster->run(); |
| 223 | if (lastRun->placeholder() != nullptr) { |
| 224 | SkASSERT(lastRun->size() == 1); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 225 | // Update the placeholder metrics so we can get the placeholder positions later |
| 226 | // and the line metrics (to make sure the placeholder fits) |
Julia Lavrova | 2ea20ea | 2020-01-22 10:56:53 -0500 | [diff] [blame] | 227 | lastRun->updateMetrics(&fEndLine.metrics()); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
| 231 | // Before we update the line metrics with struts, |
| 232 | // let's save it for GetRectsForRange(RectHeightStyle::kMax) |
| 233 | maxRunMetrics = fEndLine.metrics(); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 234 | maxRunMetrics.fForceStrut = false; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 235 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 236 | if (parent->strutEnabled()) { |
| 237 | // Make sure font metrics are not less than the strut |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 238 | parent->strutMetrics().updateLineMetrics(fEndLine.metrics()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 239 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 240 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 241 | // TODO: keep start/end/break info for text and runs but in a better way that below |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 242 | TextRange text(fEndLine.startCluster()->textRange().start, fEndLine.endCluster()->textRange().end); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 243 | TextRange textWithSpaces(fEndLine.startCluster()->textRange().start, startLine->textRange().start); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 244 | if (startLine == end) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 245 | textWithSpaces.end = parent->text().size(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 246 | } |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 247 | ClusterRange clusters(fEndLine.startCluster() - start, fEndLine.endCluster() - start + 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 248 | ClusterRange clustersWithGhosts(fEndLine.startCluster() - start, startLine - start); |
| 249 | addLine(text, textWithSpaces, clusters, clustersWithGhosts, widthWithSpaces, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 250 | fEndLine.startPos(), |
| 251 | fEndLine.endPos(), |
| 252 | SkVector::Make(0, fHeight), |
| 253 | SkVector::Make(fEndLine.width(), fEndLine.metrics().height()), |
| 254 | fEndLine.metrics(), |
Julia Lavrova | 2ea20ea | 2020-01-22 10:56:53 -0500 | [diff] [blame] | 255 | needEllipsis && !fHardLineBreak); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 256 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 257 | softLineMaxIntrinsicWidth += widthWithSpaces; |
| 258 | fMaxIntrinsicWidth = SkMaxScalar(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth); |
| 259 | if (fHardLineBreak) { |
| 260 | softLineMaxIntrinsicWidth = 0; |
| 261 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 262 | // Start a new line |
| 263 | fHeight += fEndLine.metrics().height(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 264 | if (!fHardLineBreak || startLine != end) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 265 | fEndLine.clean(); |
| 266 | } |
| 267 | fEndLine.startFrom(startLine, pos); |
| 268 | parent->fMaxWidthWithTrailingSpaces = SkMaxScalar(parent->fMaxWidthWithTrailingSpaces, widthWithSpaces); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 269 | |
Julia Lavrova | 2ea20ea | 2020-01-22 10:56:53 -0500 | [diff] [blame] | 270 | if (hasEllipsis && unlimitedLines) { |
| 271 | // There is one case when we need an ellipsis on a separate line |
| 272 | // after a line break when width is infinite |
| 273 | if (!fHardLineBreak) { |
| 274 | break; |
| 275 | } |
| 276 | } else if (lastLine) { |
| 277 | // There is nothing more to draw |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 278 | fHardLineBreak = false; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 279 | break; |
| 280 | } |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 281 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 282 | ++fLineNumber; |
| 283 | } |
| 284 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 285 | // We finished formatting the text but we need to scan the rest for some numbers |
Julia Lavrova | 53c1447 | 2019-12-18 09:39:55 -0500 | [diff] [blame] | 286 | if (fEndLine.endCluster() != nullptr) { |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 287 | auto lastWordLength = 0.0f; |
Julia Lavrova | 53c1447 | 2019-12-18 09:39:55 -0500 | [diff] [blame] | 288 | auto cluster = fEndLine.endCluster(); |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 289 | while (cluster != end || cluster->endPos() < end->endPos()) { |
| 290 | fExceededMaxLines = true; |
| 291 | if (cluster->isHardBreak()) { |
| 292 | fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth); |
| 293 | softLineMaxIntrinsicWidth = 0; |
| 294 | |
| 295 | fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength); |
| 296 | lastWordLength = 0; |
| 297 | } else if (cluster->isWhitespaces()) { |
| 298 | SkASSERT(cluster->isWhitespaces()); |
| 299 | softLineMaxIntrinsicWidth += cluster->width(); |
| 300 | fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength); |
| 301 | lastWordLength = 0; |
| 302 | } else { |
| 303 | softLineMaxIntrinsicWidth += cluster->width(); |
| 304 | lastWordLength += cluster->width(); |
| 305 | } |
| 306 | ++cluster; |
| 307 | } |
| 308 | fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, lastWordLength); |
| 309 | fMaxIntrinsicWidth = SkTMax(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth); |
| 310 | // In case we could not place a single cluster on the line |
| 311 | fHeight = SkTMax(fHeight, fEndLine.metrics().height()); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 314 | if (fHardLineBreak) { |
| 315 | // Last character is a line break |
| 316 | if (parent->strutEnabled()) { |
| 317 | // Make sure font metrics are not less than the strut |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 318 | parent->strutMetrics().updateLineMetrics(fEndLine.metrics()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 319 | } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 320 | TextRange empty(fEndLine.breakCluster()->textRange().start, fEndLine.breakCluster()->textRange().start); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 321 | TextRange hardBreak(fEndLine.breakCluster()->textRange().end, fEndLine.breakCluster()->textRange().end); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 322 | ClusterRange clusters(fEndLine.breakCluster() - start, fEndLine.endCluster() - start); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 323 | addLine(empty, hardBreak, clusters, clusters, |
| 324 | 0, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 325 | 0, |
| 326 | 0, |
| 327 | SkVector::Make(0, fHeight), |
| 328 | SkVector::Make(0, fEndLine.metrics().height()), |
| 329 | fEndLine.metrics(), |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 330 | needEllipsis); |
| 331 | fHeight += fEndLine.metrics().height(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 332 | parent->lines().back().setMaxRunMetrics(maxRunMetrics); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
| 336 | } // namespace textlayout |
| 337 | } // namespace skia |