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