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; |
| 69 | if (end > start) { |
| 70 | correction = fMaster->posShift(fIndex, clip ? end - 1 : end) - |
| 71 | fMaster->posShift(fIndex, start); |
| 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 | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 87 | point.fX += fMaster->posShift(fIndex, i + pos); |
| 88 | blobBuffer.points()[i] = point + runOffset; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Julia Lavrova | 6207697 | 2020-02-19 15:12:48 -0500 | [diff] [blame^] | 92 | std::tuple<bool, ClusterIndex, ClusterIndex> Run::findLimitingClusters(TextRange text, bool extendToClusters) const { |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 93 | |
| 94 | if (text.width() == 0) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 95 | for (auto i = fClusterRange.start; i != fClusterRange.end; ++i) { |
| 96 | auto& cluster = fMaster->cluster(i); |
| 97 | if (cluster.textRange().end >= text.end && cluster.textRange().start <= text.start) { |
| 98 | return std::make_tuple(true, i, i); |
| 99 | } |
| 100 | } |
| 101 | return std::make_tuple(false, 0, 0); |
| 102 | } |
| 103 | Cluster* start = nullptr; |
| 104 | Cluster* end = nullptr; |
Julia Lavrova | 6207697 | 2020-02-19 15:12:48 -0500 | [diff] [blame^] | 105 | if (extendToClusters) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 106 | for (auto i = fClusterRange.start; i != fClusterRange.end; ++i) { |
| 107 | auto& cluster = fMaster->cluster(i); |
Julia Lavrova | 6207697 | 2020-02-19 15:12:48 -0500 | [diff] [blame^] | 108 | auto clusterRange = cluster.textRange(); |
| 109 | if (clusterRange.end <= text.start) { |
| 110 | continue; |
| 111 | } else if (clusterRange.start >= text.end) { |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 112 | break; |
| 113 | } |
Julia Lavrova | 6207697 | 2020-02-19 15:12:48 -0500 | [diff] [blame^] | 114 | |
| 115 | TextRange s = TextRange(std::max(clusterRange.start, text.start), |
| 116 | std::min(clusterRange.end, text.end)); |
| 117 | if (s.width() > 0) { |
| 118 | if (start == nullptr) { |
| 119 | start = &cluster; |
| 120 | } |
| 121 | end = &cluster; |
| 122 | } |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 123 | } |
| 124 | } else { |
Julia Lavrova | 6207697 | 2020-02-19 15:12:48 -0500 | [diff] [blame^] | 125 | // TODO: Do we need to use this branch?.. |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 126 | for (auto i = fClusterRange.start; i != fClusterRange.end; ++i) { |
| 127 | auto& cluster = fMaster->cluster(i); |
| 128 | if (cluster.textRange().end > text.start && start == nullptr) { |
| 129 | start = &cluster; |
| 130 | } |
| 131 | if (cluster.textRange().start < text.end) { |
| 132 | end = &cluster; |
| 133 | } else { |
| 134 | break; |
| 135 | } |
| 136 | } |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 137 | } |
| 138 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 139 | if (start == nullptr || end == nullptr) { |
| 140 | return std::make_tuple(false, 0, 0); |
| 141 | } |
| 142 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 143 | if (!leftToRight()) { |
| 144 | std::swap(start, end); |
| 145 | } |
| 146 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 147 | size_t startIndex = start - fMaster->clusters().begin(); |
| 148 | size_t endIndex = end - fMaster->clusters().begin(); |
| 149 | return std::make_tuple(startIndex != fClusterRange.end && endIndex != fClusterRange.end, startIndex, endIndex); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void Run::iterateThroughClustersInTextOrder(const ClusterVisitor& visitor) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 153 | // Can't figure out how to do it with one code for both cases without 100 ifs |
| 154 | // Can't go through clusters because there are no cluster table yet |
| 155 | if (leftToRight()) { |
| 156 | size_t start = 0; |
| 157 | size_t cluster = this->clusterIndex(start); |
| 158 | for (size_t glyph = 1; glyph <= this->size(); ++glyph) { |
| 159 | auto nextCluster = this->clusterIndex(glyph); |
| 160 | if (nextCluster == cluster) { |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | visitor(start, |
| 165 | glyph, |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 166 | fClusterStart + cluster, |
| 167 | fClusterStart + nextCluster, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 168 | this->calculateWidth(start, glyph, glyph == size()), |
| 169 | this->calculateHeight()); |
| 170 | |
| 171 | start = glyph; |
| 172 | cluster = nextCluster; |
| 173 | } |
| 174 | } else { |
| 175 | size_t glyph = this->size(); |
| 176 | size_t cluster = this->fUtf8Range.begin(); |
| 177 | for (int32_t start = this->size() - 1; start >= 0; --start) { |
| 178 | size_t nextCluster = |
| 179 | start == 0 ? this->fUtf8Range.end() : this->clusterIndex(start - 1); |
| 180 | if (nextCluster == cluster) { |
| 181 | continue; |
| 182 | } |
| 183 | |
| 184 | visitor(start, |
| 185 | glyph, |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 186 | fClusterStart + cluster, |
| 187 | fClusterStart + nextCluster, |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 188 | this->calculateWidth(start, glyph, glyph == 0), |
| 189 | this->calculateHeight()); |
| 190 | |
| 191 | glyph = start; |
| 192 | cluster = nextCluster; |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | SkScalar Run::addSpacesAtTheEnd(SkScalar space, Cluster* cluster) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 198 | if (cluster->endPos() == cluster->startPos()) { |
| 199 | return 0; |
| 200 | } |
| 201 | |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 202 | fShifts[cluster->endPos() - 1] += space; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 203 | // Increment the run width |
| 204 | fSpaced = true; |
| 205 | fAdvance.fX += space; |
| 206 | // Increment the cluster width |
| 207 | cluster->space(space, space); |
| 208 | |
| 209 | return space; |
| 210 | } |
| 211 | |
| 212 | SkScalar Run::addSpacesEvenly(SkScalar space, Cluster* cluster) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 213 | // Offset all the glyphs in the cluster |
| 214 | SkScalar shift = 0; |
| 215 | for (size_t i = cluster->startPos(); i < cluster->endPos(); ++i) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 216 | fShifts[i] += shift; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 217 | shift += space; |
| 218 | } |
| 219 | if (this->size() == cluster->endPos()) { |
| 220 | // To make calculations easier |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 221 | fShifts[cluster->endPos()] += shift; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 222 | } |
| 223 | // Increment the run width |
| 224 | fSpaced = true; |
| 225 | fAdvance.fX += shift; |
| 226 | // Increment the cluster width |
| 227 | cluster->space(shift, space); |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 228 | cluster->setHalfLetterSpacing(space / 2); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 229 | |
| 230 | return shift; |
| 231 | } |
| 232 | |
| 233 | void Run::shift(const Cluster* cluster, SkScalar offset) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 234 | if (offset == 0) { |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | fSpaced = true; |
| 239 | for (size_t i = cluster->startPos(); i < cluster->endPos(); ++i) { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 240 | fShifts[i] += offset; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 241 | } |
| 242 | if (this->size() == cluster->endPos()) { |
| 243 | // To make calculations easier |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 244 | fShifts[cluster->endPos()] += offset; |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 248 | void Run::updateMetrics(InternalLineMetrics* endlineMetrics) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 249 | |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 250 | SkASSERT(isPlaceholder()); |
| 251 | auto placeholderStyle = this->placeholderStyle(); |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 252 | // Difference between the placeholder baseline and the line bottom |
| 253 | SkScalar baselineAdjustment = 0; |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 254 | switch (placeholderStyle->fBaseline) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 255 | case TextBaseline::kAlphabetic: |
| 256 | break; |
| 257 | |
| 258 | case TextBaseline::kIdeographic: |
| 259 | baselineAdjustment = endlineMetrics->deltaBaselines() / 2; |
| 260 | break; |
| 261 | } |
| 262 | |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 263 | auto height = placeholderStyle->fHeight; |
| 264 | auto offset = placeholderStyle->fBaselineOffset; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 265 | |
| 266 | fFontMetrics.fLeading = 0; |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 267 | switch (placeholderStyle->fAlignment) { |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 268 | case PlaceholderAlignment::kBaseline: |
| 269 | fFontMetrics.fAscent = baselineAdjustment - offset; |
| 270 | fFontMetrics.fDescent = baselineAdjustment + height - offset; |
| 271 | break; |
| 272 | |
| 273 | case PlaceholderAlignment::kAboveBaseline: |
| 274 | fFontMetrics.fAscent = baselineAdjustment - height; |
| 275 | fFontMetrics.fDescent = baselineAdjustment; |
| 276 | break; |
| 277 | |
| 278 | case PlaceholderAlignment::kBelowBaseline: |
| 279 | fFontMetrics.fAscent = baselineAdjustment; |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 280 | fFontMetrics.fDescent = baselineAdjustment + height; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 281 | break; |
| 282 | |
| 283 | case PlaceholderAlignment::kTop: |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 284 | fFontMetrics.fDescent = height + fFontMetrics.fAscent; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 285 | break; |
| 286 | |
| 287 | case PlaceholderAlignment::kBottom: |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 288 | fFontMetrics.fAscent = fFontMetrics.fDescent - height; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 289 | break; |
| 290 | |
| 291 | case PlaceholderAlignment::kMiddle: |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 292 | auto mid = (-fFontMetrics.fDescent - fFontMetrics.fAscent)/2.0; |
| 293 | fFontMetrics.fDescent = height/2.0 - mid; |
| 294 | fFontMetrics.fAscent = - height/2.0 - mid; |
Julia Lavrova | 916a904 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 295 | break; |
| 296 | } |
| 297 | |
| 298 | // Make sure the placeholder can fit the line |
| 299 | endlineMetrics->add(this); |
| 300 | } |
| 301 | |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 302 | void Cluster::setIsWhiteSpaces() { |
Julia Lavrova | db9f669 | 2019-08-01 16:02:17 -0400 | [diff] [blame] | 303 | |
| 304 | fWhiteSpaces = false; |
| 305 | |
| 306 | auto span = fMaster->text(fTextRange); |
| 307 | const char* ch = span.begin(); |
| 308 | while (ch < span.end()) { |
| 309 | auto unichar = utf8_next(&ch, span.end()); |
| 310 | if (!u_isWhitespace(unichar)) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 311 | return; |
| 312 | } |
| 313 | } |
| 314 | fWhiteSpaces = true; |
| 315 | } |
| 316 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 317 | SkScalar Cluster::sizeToChar(TextIndex ch) const { |
| 318 | if (ch < fTextRange.start || ch >= fTextRange.end) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 319 | return 0; |
| 320 | } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 321 | auto shift = ch - fTextRange.start; |
| 322 | auto ratio = shift * 1.0 / fTextRange.width(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 323 | |
| 324 | return SkDoubleToScalar(fWidth * ratio); |
| 325 | } |
| 326 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 327 | SkScalar Cluster::sizeFromChar(TextIndex ch) const { |
| 328 | if (ch < fTextRange.start || ch >= fTextRange.end) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 329 | return 0; |
| 330 | } |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 331 | auto shift = fTextRange.end - ch - 1; |
| 332 | auto ratio = shift * 1.0 / fTextRange.width(); |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 333 | |
| 334 | return SkDoubleToScalar(fWidth * ratio); |
| 335 | } |
| 336 | |
| 337 | size_t Cluster::roundPos(SkScalar s) const { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 338 | auto ratio = (s * 1.0) / fWidth; |
| 339 | return sk_double_floor2int(ratio * size()); |
| 340 | } |
| 341 | |
| 342 | SkScalar Cluster::trimmedWidth(size_t pos) const { |
| 343 | // 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] | 344 | // We don't have to take in account cluster shift since it's the same for 0 and for pos |
| 345 | auto& run = fMaster->run(fRunIndex); |
Julia Lavrova | 212bf07 | 2020-02-18 12:05:55 -0500 | [diff] [blame] | 346 | return std::min(run.positionX(pos) - run.positionX(fStart), fWidth); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | SkScalar Run::positionX(size_t pos) const { |
Julia Lavrova | 2e30fde | 2019-10-09 09:43:02 -0400 | [diff] [blame] | 350 | return posX(pos) + fShifts[pos] + fMaster->posShift(fIndex, pos); |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 351 | } |
| 352 | |
Julia Lavrova | c036058 | 2020-02-05 10:17:53 -0500 | [diff] [blame] | 353 | PlaceholderStyle* Run::placeholderStyle() const { |
| 354 | if (isPlaceholder()) { |
| 355 | return &fMaster->placeholders()[fPlaceholderIndex].fStyle; |
| 356 | } else { |
| 357 | return nullptr; |
| 358 | } |
| 359 | } |
| 360 | |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 361 | Run* Cluster::run() const { |
| 362 | if (fRunIndex >= fMaster->runs().size()) { |
| 363 | return nullptr; |
| 364 | } |
| 365 | return &fMaster->run(fRunIndex); |
| 366 | } |
| 367 | |
| 368 | SkFont Cluster::font() const { |
| 369 | return fMaster->run(fRunIndex).font(); |
| 370 | } |
| 371 | |
| 372 | Cluster::Cluster(ParagraphImpl* master, |
| 373 | RunIndex runIndex, |
| 374 | size_t start, |
| 375 | size_t end, |
| 376 | SkSpan<const char> text, |
| 377 | SkScalar width, |
| 378 | SkScalar height) |
| 379 | : fMaster(master) |
| 380 | , fRunIndex(runIndex) |
| 381 | , fTextRange(text.begin() - fMaster->text().begin(), text.end() - fMaster->text().begin()) |
Julia Lavrova | c222856 | 2019-08-08 16:51:27 -0400 | [diff] [blame] | 382 | , fGraphemeRange(EMPTY_RANGE) |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 383 | , fStart(start) |
| 384 | , fEnd(end) |
| 385 | , fWidth(width) |
| 386 | , fSpacing(0) |
| 387 | , fHeight(height) |
Julia Lavrova | f3ed273 | 2019-09-05 14:35:17 -0400 | [diff] [blame] | 388 | , fHalfLetterSpacing(0.0) |
Julia Lavrova | 5207f35 | 2019-06-21 12:22:32 -0400 | [diff] [blame] | 389 | , fWhiteSpaces(false) |
| 390 | , fBreakType(None) { |
Julia Lavrova | a3552c5 | 2019-05-30 16:12:56 -0400 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | } // namespace textlayout |
| 394 | } // namespace skia |