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