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) { |
| 18 | fWords.startFrom(fEndLine.startCluster(), fEndLine.startPos()); |
| 19 | fClusters.startFrom(fEndLine.startCluster(), fEndLine.startPos()); |
| 20 | fClip.startFrom(fEndLine.startCluster(), fEndLine.startPos()); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 21 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 22 | for (auto cluster = fEndLine.endCluster(); cluster < endOfClusters; ++cluster) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 23 | // 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 Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 30 | if (cluster->isWhitespaces()) { |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 31 | // It's the end of the word |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 32 | fClusters.extend(cluster); |
Julia Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 33 | fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, getClustersTrimmedWidth()); |
| 34 | fWords.extend(fClusters); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 35 | break; |
| 36 | } |
| 37 | if (cluster->width() > maxWidth) { |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 38 | fClusters.extend(cluster); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 39 | 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 Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 47 | if (further->isSoftBreak() || further->isHardBreak() || further->isWhitespaces()) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 48 | 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 Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 54 | fMinIntrinsicWidth = SkTMax(fMinIntrinsicWidth, nextWordLength); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 55 | fTooLongWord = true; |
| 56 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 57 | 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 Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame^] | 75 | void 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 Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 85 | } |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame^] | 86 | } |
| 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 Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Special case for start/end cluster since they can be clipped |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 101 | void TextWrapper::trimEndSpaces(TextAlign align) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 102 | // Remember the breaking position |
| 103 | fEndLine.saveBreak(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 104 | // 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 Lavrova | 6e6333f | 2019-06-17 10:34:10 -0400 | [diff] [blame] | 107 | for (auto cluster = fEndLine.endCluster(); |
| 108 | cluster >= fEndLine.startCluster() && cluster->isWhitespaces(); |
| 109 | --cluster) { |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 110 | if ((cluster->run()->leftToRight()) || |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 111 | (right && !cluster->run()->leftToRight()) || |
| 112 | align == TextAlign::kJustify || align == TextAlign::kCenter) { |
| 113 | fEndLine.trim(cluster); |
| 114 | continue; |
| 115 | } else { |
| 116 | break; |
| 117 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 118 | } |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 119 | fEndLine.trim(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | SkScalar 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 Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 130 | if (cluster->isWhitespaces()) { |
| 131 | width -= cluster->width(); |
| 132 | } else { |
| 133 | width -= (cluster->width() - cluster->trimmedWidth(cluster->endPos())); |
| 134 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 135 | } |
| 136 | return width; |
| 137 | } |
| 138 | |
| 139 | // Trim the beginning spaces in case of soft line break |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 140 | std::tuple<Cluster*, size_t, SkScalar> TextWrapper::trimStartSpaces(Cluster* endOfClusters) { |
| 141 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 142 | if (fHardLineBreak) { |
| 143 | // 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] | 144 | 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 Lavrova | af89d39 | 2019-08-09 15:19:26 -0400 | [diff] [blame] | 150 | return std::make_tuple(fEndLine.breakCluster() + 1, 0, width); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 151 | } |
| 152 | |
Julia Lavrova | 53c1447 | 2019-12-18 09:39:55 -0500 | [diff] [blame] | 153 | auto width = fEndLine.withWithGhostSpaces(); |
| 154 | auto cluster = fEndLine.breakCluster(); |
| 155 | if (fEndLine.endCluster() != fEndLine.startCluster() || |
| 156 | fEndLine.endPos() != fEndLine.startPos()) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 157 | ++cluster; |
Julia Lavrova | 53c1447 | 2019-12-18 09:39:55 -0500 | [diff] [blame] | 158 | 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 Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 164 | } |
Julia Lavrova | 53c1447 | 2019-12-18 09:39:55 -0500 | [diff] [blame] | 165 | |
Julia Lavrova | af89d39 | 2019-08-09 15:19:26 -0400 | [diff] [blame] | 166 | return std::make_tuple(cluster, 0, width); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 167 | } |
| 168 | |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame^] | 169 | // TODO: refactor the code for line ending (with/without ellipsis) |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 170 | void TextWrapper::breakTextIntoLines(ParagraphImpl* parent, |
| 171 | SkScalar maxWidth, |
| 172 | const AddLineToParagraph& addLine) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 173 | fHeight = 0; |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 174 | fMinIntrinsicWidth = std::numeric_limits<SkScalar>::min(); |
| 175 | fMaxIntrinsicWidth = std::numeric_limits<SkScalar>::min(); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 176 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 177 | auto span = parent->clusters(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 178 | if (span.size() == 0) { |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame^] | 179 | return; |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 180 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 181 | auto maxLines = parent->paragraphStyle().getMaxLines(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 182 | auto& ellipsisStr = parent->paragraphStyle().getEllipsis(); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 183 | auto align = parent->paragraphStyle().effective_align(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 184 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 185 | SkScalar softLineMaxIntrinsicWidth = 0; |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 186 | fEndLine = TextStretch(span.begin(), span.begin(), parent->strutForceHeight()); |
| 187 | auto end = span.end() - 1; |
| 188 | auto start = span.begin(); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 189 | InternalLineMetrics maxRunMetrics; |
| 190 | auto needEllipsis = false; |
| 191 | auto endlessLine = maxLines == std::numeric_limits<size_t>::max(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 192 | while (fEndLine.endCluster() != end) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 193 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 194 | reset(); |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame^] | 195 | auto exceededLines = !endlessLine && fLineNumber >= maxLines; |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 196 | fEndLine.metrics().clean(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 197 | lookAhead(maxWidth, end); |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame^] | 198 | moveForward(exceededLines && !ellipsisStr.isEmpty()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 199 | |
| 200 | // 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] | 201 | 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 Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 208 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 209 | needEllipsis = |
| 210 | fEndLine.endCluster() < end - 1 && |
| 211 | SkScalarIsFinite(maxWidth) && |
| 212 | !ellipsisStr.isEmpty(); |
| 213 | |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame^] | 214 | 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 Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 219 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 220 | // If the line is empty with the hard line break, let's take the paragraph font (flutter???) |
| 221 | if (fHardLineBreak && fEndLine.width() == 0) { |
Jason Simmons | 22bb52e | 2019-12-05 17:56:59 -0800 | [diff] [blame] | 222 | fEndLine.setMetrics(parent->getEmptyMetrics()); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 223 | } |
| 224 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 225 | // 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 Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 238 | maxRunMetrics.fForceStrut = false; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 239 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 240 | if (parent->strutEnabled()) { |
| 241 | // Make sure font metrics are not less than the strut |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 242 | parent->strutMetrics().updateLineMetrics(fEndLine.metrics()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 243 | } |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 244 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 245 | // 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] | 246 | TextRange text(fEndLine.startCluster()->textRange().start, fEndLine.endCluster()->textRange().end); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 247 | TextRange textWithSpaces(fEndLine.startCluster()->textRange().start, startLine->textRange().start); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 248 | if (startLine == end) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 249 | textWithSpaces.end = parent->text().size(); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 250 | } |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 251 | ClusterRange clusters(fEndLine.startCluster() - start, fEndLine.endCluster() - start + 1); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 252 | ClusterRange clustersWithGhosts(fEndLine.startCluster() - start, startLine - start); |
| 253 | addLine(text, textWithSpaces, clusters, clustersWithGhosts, widthWithSpaces, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 254 | fEndLine.startPos(), |
| 255 | fEndLine.endPos(), |
| 256 | SkVector::Make(0, fHeight), |
| 257 | SkVector::Make(fEndLine.width(), fEndLine.metrics().height()), |
| 258 | fEndLine.metrics(), |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 259 | needEllipsis && exceededLines && !fHardLineBreak); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 260 | |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 261 | parent->lines().back().setMaxRunMetrics(maxRunMetrics); |
| 262 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 263 | softLineMaxIntrinsicWidth += widthWithSpaces; |
| 264 | fMaxIntrinsicWidth = SkMaxScalar(fMaxIntrinsicWidth, softLineMaxIntrinsicWidth); |
| 265 | if (fHardLineBreak) { |
| 266 | softLineMaxIntrinsicWidth = 0; |
| 267 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 268 | // Start a new line |
| 269 | fHeight += fEndLine.metrics().height(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 270 | if (!fHardLineBreak || startLine != end) { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 271 | fEndLine.clean(); |
| 272 | } |
| 273 | fEndLine.startFrom(startLine, pos); |
| 274 | parent->fMaxWidthWithTrailingSpaces = SkMaxScalar(parent->fMaxWidthWithTrailingSpaces, widthWithSpaces); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 275 | |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame^] | 276 | if (exceededLines) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 277 | fHardLineBreak = false; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 278 | break; |
Julia Lavrova | c48687a | 2020-01-08 16:53:53 -0500 | [diff] [blame^] | 279 | } else if (endlessLine && needEllipsis) { |
| 280 | break; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 281 | } |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 282 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 283 | ++fLineNumber; |
| 284 | } |
| 285 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 286 | // 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] | 287 | if (fEndLine.endCluster() != nullptr) { |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 288 | auto lastWordLength = 0.0f; |
Julia Lavrova | 53c1447 | 2019-12-18 09:39:55 -0500 | [diff] [blame] | 289 | auto cluster = fEndLine.endCluster(); |
Julia Lavrova | 90bfd1c | 2019-12-04 11:43:32 -0500 | [diff] [blame] | 290 | 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 Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 315 | 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 Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 319 | parent->strutMetrics().updateLineMetrics(fEndLine.metrics()); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 320 | } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 321 | TextRange empty(fEndLine.breakCluster()->textRange().start, fEndLine.breakCluster()->textRange().start); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 322 | TextRange hardBreak(fEndLine.breakCluster()->textRange().end, fEndLine.breakCluster()->textRange().end); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 323 | ClusterRange clusters(fEndLine.breakCluster() - start, fEndLine.endCluster() - start); |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 324 | addLine(empty, hardBreak, clusters, clusters, |
| 325 | 0, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 326 | 0, |
| 327 | 0, |
| 328 | SkVector::Make(0, fHeight), |
| 329 | SkVector::Make(0, fEndLine.metrics().height()), |
| 330 | fEndLine.metrics(), |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 331 | needEllipsis); |
| 332 | fHeight += fEndLine.metrics().height(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 333 | parent->lines().back().setMaxRunMetrics(maxRunMetrics); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | } // namespace textlayout |
| 338 | } // namespace skia |