Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 1 | // Copyright 2019 Google LLC. |
| 2 | #include "modules/skparagraph/src/Run.h" |
| 3 | #include <unicode/brkiter.h> |
| 4 | #include "include/core/SkFontMetrics.h" |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 5 | #include "modules/skparagraph/src/ParagraphImpl.h" |
| 6 | #include <algorithm> |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 7 | #include "src/utils/SkUTF.h" |
| 8 | |
| 9 | namespace { |
| 10 | |
| 11 | SkUnichar utf8_next(const char** ptr, const char* end) { |
| 12 | SkUnichar val = SkUTF::NextUTF8(ptr, end); |
| 13 | return val < 0 ? 0xFFFD : val; |
| 14 | } |
Julia Lavrova | 6207697 | 2020-02-19 15:12:48 -0500 | [diff] [blame] | 15 | |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 16 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 17 | |
| 18 | namespace skia { |
| 19 | namespace textlayout { |
| 20 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 21 | Run::Run(ParagraphImpl* master, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 22 | const SkShaper::RunHandler::RunInfo& info, |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 23 | size_t firstChar, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 24 | SkScalar lineHeight, |
| 25 | size_t index, |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 26 | SkScalar offsetX) |
| 27 | : fMaster(master) |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 28 | , fTextRange(firstChar + info.utf8Range.begin(), firstChar + info.utf8Range.end()) |
| 29 | , fClusterRange(EMPTY_CLUSTERS) |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 30 | , fClusterStart(firstChar) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 31 | fFont = info.fFont; |
| 32 | fHeightMultiplier = lineHeight; |
| 33 | fBidiLevel = info.fBidiLevel; |
| 34 | fAdvance = info.fAdvance; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 35 | fIndex = index; |
| 36 | fUtf8Range = info.utf8Range; |
| 37 | fOffset = SkVector::Make(offsetX, 0); |
| 38 | fGlyphs.push_back_n(info.glyphCount); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 39 | fBounds.push_back_n(info.glyphCount); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 40 | fPositions.push_back_n(info.glyphCount + 1); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 41 | fOffsets.push_back_n(info.glyphCount + 1); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 42 | fClusterIndexes.push_back_n(info.glyphCount + 1); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 43 | fShifts.push_back_n(info.glyphCount + 1, 0.0); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 44 | info.fFont.getMetrics(&fFontMetrics); |
| 45 | fSpaced = false; |
| 46 | // To make edge cases easier: |
| 47 | fPositions[info.glyphCount] = fOffset + fAdvance; |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 48 | fOffsets[info.glyphCount] = { 0, 0}; |
| 49 | fClusterIndexes[info.glyphCount] = this->leftToRight() ? info.utf8Range.end() : info.utf8Range.begin(); |
Julia Lavrova | 526df26 | 2019-08-21 17:49:44 -0400 | [diff] [blame] | 50 | fEllipsis = false; |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 51 | fPlaceholderIndex = std::numeric_limits<size_t>::max(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | SkShaper::RunHandler::Buffer Run::newRunBuffer() { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 55 | return {fGlyphs.data(), fPositions.data(), fOffsets.data(), fClusterIndexes.data(), fOffset}; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 58 | void Run::commit() { |
| 59 | fFont.getBounds(fGlyphs.data(), fGlyphs.size(), fBounds.data(), nullptr); |
| 60 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 61 | SkScalar Run::calculateWidth(size_t start, size_t end, bool clip) const { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 62 | SkASSERT(start <= end); |
| 63 | // clip |= end == size(); // Clip at the end of the run? |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 64 | SkScalar shift = 0; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 65 | if (fSpaced && end > start) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 66 | shift = fShifts[clip ? end - 1 : end] - fShifts[start]; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 67 | } |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 68 | auto correction = 0.0f; |
Julia Lavrova | a0708e8 | 2020-02-28 12:14:58 -0500 | [diff] [blame] | 69 | if (end > start && !fJustificationShifts.empty()) { |
| 70 | correction = fJustificationShifts[end - 1].fX - |
| 71 | fJustificationShifts[start].fY; |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 72 | } |
| 73 | return posX(end) - posX(start) + shift + correction; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 74 | } |
| 75 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 76 | void Run::copyTo(SkTextBlobBuilder& builder, size_t pos, size_t size, SkVector runOffset) const { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 77 | SkASSERT(pos + size <= this->size()); |
| 78 | const auto& blobBuffer = builder.allocRunPos(fFont, SkToInt(size)); |
| 79 | sk_careful_memcpy(blobBuffer.glyphs, fGlyphs.data() + pos, size * sizeof(SkGlyphID)); |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 80 | for (size_t i = 0; i < size; ++i) { |
| 81 | auto point = fPositions[i + pos]; |
| 82 | auto offset = fOffsets[i + pos]; |
| 83 | point.offset(offset.fX, offset.fY); |
| 84 | if (fSpaced) { |
| 85 | point.fX += fShifts[i + pos]; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 86 | } |
Julia Lavrova | a0708e8 | 2020-02-28 12:14:58 -0500 | [diff] [blame] | 87 | if (!fJustificationShifts.empty()) { |
| 88 | point.fX += fJustificationShifts[i + pos].fX; |
| 89 | } |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 90 | blobBuffer.points()[i] = point + runOffset; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
Julia Lavrova | 6207697 | 2020-02-19 15:12:48 -0500 | [diff] [blame] | 94 | std::tuple<bool, ClusterIndex, ClusterIndex> Run::findLimitingClusters(TextRange text, bool extendToClusters) const { |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 95 | |
| 96 | if (text.width() == 0) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 97 | for (auto i = fClusterRange.start; i != fClusterRange.end; ++i) { |
| 98 | auto& cluster = fMaster->cluster(i); |
| 99 | if (cluster.textRange().end >= text.end && cluster.textRange().start <= text.start) { |
| 100 | return std::make_tuple(true, i, i); |
| 101 | } |
| 102 | } |
| 103 | return std::make_tuple(false, 0, 0); |
| 104 | } |
| 105 | Cluster* start = nullptr; |
| 106 | Cluster* end = nullptr; |
Julia Lavrova | 6207697 | 2020-02-19 15:12:48 -0500 | [diff] [blame] | 107 | if (extendToClusters) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 108 | for (auto i = fClusterRange.start; i != fClusterRange.end; ++i) { |
| 109 | auto& cluster = fMaster->cluster(i); |
Julia Lavrova | 6207697 | 2020-02-19 15:12:48 -0500 | [diff] [blame] | 110 | auto clusterRange = cluster.textRange(); |
| 111 | if (clusterRange.end <= text.start) { |
| 112 | continue; |
| 113 | } else if (clusterRange.start >= text.end) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 114 | break; |
| 115 | } |
Julia Lavrova | 6207697 | 2020-02-19 15:12:48 -0500 | [diff] [blame] | 116 | |
| 117 | TextRange s = TextRange(std::max(clusterRange.start, text.start), |
| 118 | std::min(clusterRange.end, text.end)); |
| 119 | if (s.width() > 0) { |
| 120 | if (start == nullptr) { |
| 121 | start = &cluster; |
| 122 | } |
| 123 | end = &cluster; |
| 124 | } |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 125 | } |
| 126 | } else { |
Julia Lavrova | cd2d4e4 | 2020-03-27 15:40:37 -0400 | [diff] [blame^] | 127 | // We need this branch when we draw styles for the part of a cluster |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 128 | for (auto i = fClusterRange.start; i != fClusterRange.end; ++i) { |
| 129 | auto& cluster = fMaster->cluster(i); |
| 130 | if (cluster.textRange().end > text.start && start == nullptr) { |
| 131 | start = &cluster; |
| 132 | } |
| 133 | if (cluster.textRange().start < text.end) { |
| 134 | end = &cluster; |
| 135 | } else { |
| 136 | break; |
| 137 | } |
| 138 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 139 | } |
| 140 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 141 | if (start == nullptr || end == nullptr) { |
| 142 | return std::make_tuple(false, 0, 0); |
| 143 | } |
| 144 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 145 | if (!leftToRight()) { |
| 146 | std::swap(start, end); |
| 147 | } |
| 148 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 149 | size_t startIndex = start - fMaster->clusters().begin(); |
| 150 | size_t endIndex = end - fMaster->clusters().begin(); |
| 151 | return std::make_tuple(startIndex != fClusterRange.end && endIndex != fClusterRange.end, startIndex, endIndex); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void Run::iterateThroughClustersInTextOrder(const ClusterVisitor& visitor) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 155 | // Can't figure out how to do it with one code for both cases without 100 ifs |
| 156 | // Can't go through clusters because there are no cluster table yet |
| 157 | if (leftToRight()) { |
| 158 | size_t start = 0; |
| 159 | size_t cluster = this->clusterIndex(start); |
| 160 | for (size_t glyph = 1; glyph <= this->size(); ++glyph) { |
| 161 | auto nextCluster = this->clusterIndex(glyph); |
Julia Lavrova | 99ede42 | 2020-03-17 13:20:58 -0400 | [diff] [blame] | 162 | if (nextCluster <= cluster) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 163 | continue; |
| 164 | } |
| 165 | |
| 166 | visitor(start, |
| 167 | glyph, |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 168 | fClusterStart + cluster, |
| 169 | fClusterStart + nextCluster, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 170 | this->calculateWidth(start, glyph, glyph == size()), |
| 171 | this->calculateHeight()); |
| 172 | |
| 173 | start = glyph; |
| 174 | cluster = nextCluster; |
| 175 | } |
| 176 | } else { |
| 177 | size_t glyph = this->size(); |
| 178 | size_t cluster = this->fUtf8Range.begin(); |
| 179 | for (int32_t start = this->size() - 1; start >= 0; --start) { |
| 180 | size_t nextCluster = |
| 181 | start == 0 ? this->fUtf8Range.end() : this->clusterIndex(start - 1); |
Julia Lavrova | 99ede42 | 2020-03-17 13:20:58 -0400 | [diff] [blame] | 182 | if (nextCluster <= cluster) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 183 | continue; |
| 184 | } |
| 185 | |
| 186 | visitor(start, |
| 187 | glyph, |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 188 | fClusterStart + cluster, |
| 189 | fClusterStart + nextCluster, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 190 | this->calculateWidth(start, glyph, glyph == 0), |
| 191 | this->calculateHeight()); |
| 192 | |
| 193 | glyph = start; |
| 194 | cluster = nextCluster; |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | SkScalar Run::addSpacesAtTheEnd(SkScalar space, Cluster* cluster) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 200 | if (cluster->endPos() == cluster->startPos()) { |
| 201 | return 0; |
| 202 | } |
| 203 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 204 | fShifts[cluster->endPos() - 1] += space; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 205 | // Increment the run width |
| 206 | fSpaced = true; |
| 207 | fAdvance.fX += space; |
| 208 | // Increment the cluster width |
| 209 | cluster->space(space, space); |
| 210 | |
| 211 | return space; |
| 212 | } |
| 213 | |
| 214 | SkScalar Run::addSpacesEvenly(SkScalar space, Cluster* cluster) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 215 | // Offset all the glyphs in the cluster |
| 216 | SkScalar shift = 0; |
| 217 | for (size_t i = cluster->startPos(); i < cluster->endPos(); ++i) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 218 | fShifts[i] += shift; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 219 | shift += space; |
| 220 | } |
| 221 | if (this->size() == cluster->endPos()) { |
| 222 | // To make calculations easier |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 223 | fShifts[cluster->endPos()] += shift; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 224 | } |
| 225 | // Increment the run width |
| 226 | fSpaced = true; |
| 227 | fAdvance.fX += shift; |
| 228 | // Increment the cluster width |
| 229 | cluster->space(shift, space); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 230 | cluster->setHalfLetterSpacing(space / 2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 231 | |
| 232 | return shift; |
| 233 | } |
| 234 | |
| 235 | void Run::shift(const Cluster* cluster, SkScalar offset) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 236 | if (offset == 0) { |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | fSpaced = true; |
| 241 | for (size_t i = cluster->startPos(); i < cluster->endPos(); ++i) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 242 | fShifts[i] += offset; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 243 | } |
| 244 | if (this->size() == cluster->endPos()) { |
| 245 | // To make calculations easier |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 246 | fShifts[cluster->endPos()] += offset; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 250 | void Run::updateMetrics(InternalLineMetrics* endlineMetrics) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 251 | |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 252 | SkASSERT(isPlaceholder()); |
| 253 | auto placeholderStyle = this->placeholderStyle(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 254 | // Difference between the placeholder baseline and the line bottom |
| 255 | SkScalar baselineAdjustment = 0; |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 256 | switch (placeholderStyle->fBaseline) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 257 | case TextBaseline::kAlphabetic: |
| 258 | break; |
| 259 | |
| 260 | case TextBaseline::kIdeographic: |
| 261 | baselineAdjustment = endlineMetrics->deltaBaselines() / 2; |
| 262 | break; |
| 263 | } |
| 264 | |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 265 | auto height = placeholderStyle->fHeight; |
| 266 | auto offset = placeholderStyle->fBaselineOffset; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 267 | |
| 268 | fFontMetrics.fLeading = 0; |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 269 | switch (placeholderStyle->fAlignment) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 270 | case PlaceholderAlignment::kBaseline: |
| 271 | fFontMetrics.fAscent = baselineAdjustment - offset; |
| 272 | fFontMetrics.fDescent = baselineAdjustment + height - offset; |
| 273 | break; |
| 274 | |
| 275 | case PlaceholderAlignment::kAboveBaseline: |
| 276 | fFontMetrics.fAscent = baselineAdjustment - height; |
| 277 | fFontMetrics.fDescent = baselineAdjustment; |
| 278 | break; |
| 279 | |
| 280 | case PlaceholderAlignment::kBelowBaseline: |
| 281 | fFontMetrics.fAscent = baselineAdjustment; |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 282 | fFontMetrics.fDescent = baselineAdjustment + height; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 283 | break; |
| 284 | |
| 285 | case PlaceholderAlignment::kTop: |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 286 | fFontMetrics.fDescent = height + fFontMetrics.fAscent; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 287 | break; |
| 288 | |
| 289 | case PlaceholderAlignment::kBottom: |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 290 | fFontMetrics.fAscent = fFontMetrics.fDescent - height; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 291 | break; |
| 292 | |
| 293 | case PlaceholderAlignment::kMiddle: |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 294 | auto mid = (-fFontMetrics.fDescent - fFontMetrics.fAscent)/2.0; |
| 295 | fFontMetrics.fDescent = height/2.0 - mid; |
| 296 | fFontMetrics.fAscent = - height/2.0 - mid; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 297 | break; |
| 298 | } |
| 299 | |
| 300 | // Make sure the placeholder can fit the line |
| 301 | endlineMetrics->add(this); |
| 302 | } |
| 303 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 304 | void Cluster::setIsWhiteSpaces() { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 305 | |
| 306 | fWhiteSpaces = false; |
| 307 | |
| 308 | auto span = fMaster->text(fTextRange); |
| 309 | const char* ch = span.begin(); |
| 310 | while (ch < span.end()) { |
| 311 | auto unichar = utf8_next(&ch, span.end()); |
| 312 | if (!u_isWhitespace(unichar)) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 313 | return; |
| 314 | } |
| 315 | } |
| 316 | fWhiteSpaces = true; |
| 317 | } |
| 318 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 319 | SkScalar Cluster::sizeToChar(TextIndex ch) const { |
| 320 | if (ch < fTextRange.start || ch >= fTextRange.end) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 321 | return 0; |
| 322 | } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 323 | auto shift = ch - fTextRange.start; |
| 324 | auto ratio = shift * 1.0 / fTextRange.width(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 325 | |
| 326 | return SkDoubleToScalar(fWidth * ratio); |
| 327 | } |
| 328 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 329 | SkScalar Cluster::sizeFromChar(TextIndex ch) const { |
| 330 | if (ch < fTextRange.start || ch >= fTextRange.end) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 331 | return 0; |
| 332 | } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 333 | auto shift = fTextRange.end - ch - 1; |
| 334 | auto ratio = shift * 1.0 / fTextRange.width(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 335 | |
| 336 | return SkDoubleToScalar(fWidth * ratio); |
| 337 | } |
| 338 | |
| 339 | size_t Cluster::roundPos(SkScalar s) const { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 340 | auto ratio = (s * 1.0) / fWidth; |
| 341 | return sk_double_floor2int(ratio * size()); |
| 342 | } |
| 343 | |
| 344 | SkScalar Cluster::trimmedWidth(size_t pos) const { |
| 345 | // Find the width until the pos and return the min between trimmedWidth and the width(pos) |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 346 | // We don't have to take in account cluster shift since it's the same for 0 and for pos |
| 347 | auto& run = fMaster->run(fRunIndex); |
Julia Lavrova | 212bf07 | 2020-02-18 12:05:55 -0500 | [diff] [blame] | 348 | return std::min(run.positionX(pos) - run.positionX(fStart), fWidth); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | SkScalar Run::positionX(size_t pos) const { |
Julia Lavrova | a0708e8 | 2020-02-28 12:14:58 -0500 | [diff] [blame] | 352 | return posX(pos) + fShifts[pos] + |
| 353 | (fJustificationShifts.empty() ? 0 : fJustificationShifts[pos].fY); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 354 | } |
| 355 | |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 356 | PlaceholderStyle* Run::placeholderStyle() const { |
| 357 | if (isPlaceholder()) { |
| 358 | return &fMaster->placeholders()[fPlaceholderIndex].fStyle; |
| 359 | } else { |
| 360 | return nullptr; |
| 361 | } |
| 362 | } |
| 363 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 364 | Run* Cluster::run() const { |
| 365 | if (fRunIndex >= fMaster->runs().size()) { |
| 366 | return nullptr; |
| 367 | } |
| 368 | return &fMaster->run(fRunIndex); |
| 369 | } |
| 370 | |
| 371 | SkFont Cluster::font() const { |
| 372 | return fMaster->run(fRunIndex).font(); |
| 373 | } |
| 374 | |
| 375 | Cluster::Cluster(ParagraphImpl* master, |
| 376 | RunIndex runIndex, |
| 377 | size_t start, |
| 378 | size_t end, |
| 379 | SkSpan<const char> text, |
| 380 | SkScalar width, |
| 381 | SkScalar height) |
| 382 | : fMaster(master) |
| 383 | , fRunIndex(runIndex) |
| 384 | , fTextRange(text.begin() - fMaster->text().begin(), text.end() - fMaster->text().begin()) |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 385 | , fGraphemeRange(EMPTY_RANGE) |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 386 | , fStart(start) |
| 387 | , fEnd(end) |
| 388 | , fWidth(width) |
| 389 | , fSpacing(0) |
| 390 | , fHeight(height) |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 391 | , fHalfLetterSpacing(0.0) |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 392 | , fWhiteSpaces(false) |
| 393 | , fBreakType(None) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | } // namespace textlayout |
| 397 | } // namespace skia |