epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/core/SkStrike.h" |
Herb Derby | dce19a7 | 2018-04-18 16:02:17 -0400 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkGraphics.h" |
| 11 | #include "include/core/SkPath.h" |
| 12 | #include "include/core/SkTypeface.h" |
| 13 | #include "include/private/SkMutex.h" |
| 14 | #include "include/private/SkOnce.h" |
| 15 | #include "include/private/SkTemplates.h" |
| 16 | #include "src/core/SkMakeUnique.h" |
ssid | 60df542 | 2015-10-01 12:41:35 -0700 | [diff] [blame] | 17 | #include <cctype> |
| 18 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 19 | SkStrike::SkStrike( |
Herb Derby | dce19a7 | 2018-04-18 16:02:17 -0400 | [diff] [blame] | 20 | const SkDescriptor& desc, |
| 21 | std::unique_ptr<SkScalerContext> scaler, |
Mike Reed | cb6f53e | 2018-11-06 12:44:54 -0500 | [diff] [blame] | 22 | const SkFontMetrics& fontMetrics) |
Herb Derby | dce19a7 | 2018-04-18 16:02:17 -0400 | [diff] [blame] | 23 | : fDesc{desc} |
Herb Derby | aeb425d | 2018-03-19 15:39:16 -0400 | [diff] [blame] | 24 | , fScalerContext{std::move(scaler)} |
Herb Derby | 4f169ec | 2018-08-22 17:26:46 -0400 | [diff] [blame] | 25 | , fFontMetrics{fontMetrics} |
| 26 | , fIsSubpixel{fScalerContext->isSubpixel()} |
| 27 | , fAxisAlignment{fScalerContext->computeAxisAlignmentForHText()} |
Herb Derby | dfeb2aa | 2018-02-28 18:47:27 -0500 | [diff] [blame] | 28 | { |
Herb Derby | aeb425d | 2018-03-19 15:39:16 -0400 | [diff] [blame] | 29 | SkASSERT(fScalerContext != nullptr); |
herb | cd7f035 | 2015-09-15 15:15:40 -0700 | [diff] [blame] | 30 | fMemoryUsed = sizeof(*this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | } |
| 32 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | #ifdef SK_DEBUG |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 34 | #define VALIDATE() AutoValidate av(this) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | #else |
| 36 | #define VALIDATE() |
| 37 | #endif |
| 38 | |
Herb Derby | 33a6f7c | 2019-06-07 11:01:36 -0400 | [diff] [blame] | 39 | // -- glyph creation ------------------------------------------------------------------------------- |
| 40 | SkGlyph* SkStrike::makeGlyph(SkPackedGlyphID packedGlyphID) { |
| 41 | fMemoryUsed += sizeof(SkGlyph); |
| 42 | SkGlyph* glyph = fAlloc.make<SkGlyph>(packedGlyphID); |
| 43 | fGlyphMap.set(glyph); |
| 44 | return glyph; |
| 45 | } |
| 46 | |
Herb Derby | 33a6f7c | 2019-06-07 11:01:36 -0400 | [diff] [blame] | 47 | SkGlyph* SkStrike::glyph(SkPackedGlyphID packedGlyphID) { |
Herb Derby | a4eecd8 | 2019-06-25 15:05:30 -0400 | [diff] [blame] | 48 | VALIDATE(); |
Herb Derby | 33a6f7c | 2019-06-07 11:01:36 -0400 | [diff] [blame] | 49 | SkGlyph* glyph = fGlyphMap.findOrNull(packedGlyphID); |
| 50 | if (glyph == nullptr) { |
| 51 | glyph = this->makeGlyph(packedGlyphID); |
| 52 | fScalerContext->getMetrics(glyph); |
| 53 | } |
| 54 | return glyph; |
| 55 | } |
| 56 | |
| 57 | SkGlyph* SkStrike::glyph(SkGlyphID glyphID) { |
| 58 | return this->glyph(SkPackedGlyphID{glyphID}); |
| 59 | } |
| 60 | |
Herb Derby | 511dcfc | 2019-06-24 12:58:41 -0400 | [diff] [blame] | 61 | SkGlyph* SkStrike::glyph(SkGlyphID glyphID, SkPoint position) { |
| 62 | const SkFixed maskX = (!fIsSubpixel || fAxisAlignment == kY_SkAxisAlignment) ? 0 : ~0; |
| 63 | const SkFixed maskY = (!fIsSubpixel || fAxisAlignment == kX_SkAxisAlignment) ? 0 : ~0; |
| 64 | SkFixed subX = SkScalarToFixed(position.x()) & maskX, |
| 65 | subY = SkScalarToFixed(position.y()) & maskY; |
| 66 | return this->glyph(SkPackedGlyphID{glyphID, subX, subY}); |
| 67 | } |
| 68 | |
| 69 | SkGlyph* SkStrike::glyphFromPrototype(const SkGlyphPrototype& p, void* image) { |
Herb Derby | 3721688 | 2019-06-13 17:24:24 -0400 | [diff] [blame] | 70 | SkGlyph* glyph = fGlyphMap.findOrNull(p.id); |
| 71 | if (glyph == nullptr) { |
| 72 | fMemoryUsed += sizeof(SkGlyph); |
| 73 | glyph = fAlloc.make<SkGlyph>(p); |
| 74 | fGlyphMap.set(glyph); |
| 75 | } |
Herb Derby | 511dcfc | 2019-06-24 12:58:41 -0400 | [diff] [blame] | 76 | if (glyph->setImage(&fAlloc, image)) { |
| 77 | fMemoryUsed += glyph->imageSize(); |
| 78 | } |
Herb Derby | 3721688 | 2019-06-13 17:24:24 -0400 | [diff] [blame] | 79 | return glyph; |
| 80 | } |
| 81 | |
Herb Derby | f0e7581 | 2019-06-12 11:14:50 -0400 | [diff] [blame] | 82 | SkGlyph* SkStrike::glyphOrNull(SkPackedGlyphID id) const { |
| 83 | return fGlyphMap.findOrNull(id); |
| 84 | } |
| 85 | |
Herb Derby | f0e7581 | 2019-06-12 11:14:50 -0400 | [diff] [blame] | 86 | const SkPath* SkStrike::preparePath(SkGlyph* glyph) { |
| 87 | if (glyph->setPath(&fAlloc, fScalerContext.get())) { |
| 88 | fMemoryUsed += glyph->path()->approximateBytesUsed(); |
| 89 | } |
| 90 | return glyph->path(); |
| 91 | } |
| 92 | |
| 93 | const SkPath* SkStrike::preparePath(SkGlyph* glyph, const SkPath* path) { |
| 94 | if (glyph->setPath(&fAlloc, path)) { |
| 95 | fMemoryUsed += glyph->path()->approximateBytesUsed(); |
| 96 | } |
| 97 | return glyph->path(); |
| 98 | } |
| 99 | |
Herb Derby | 33a6f7c | 2019-06-07 11:01:36 -0400 | [diff] [blame] | 100 | const SkDescriptor& SkStrike::getDescriptor() const { |
| 101 | return *fDesc.getDesc(); |
| 102 | } |
| 103 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 104 | unsigned SkStrike::getGlyphCount() const { |
ctguil@chromium.org | 0bc7bf5 | 2011-03-04 19:04:57 +0000 | [diff] [blame] | 105 | return fScalerContext->getGlyphCount(); |
| 106 | } |
| 107 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 108 | int SkStrike::countCachedGlyphs() const { |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 109 | return fGlyphMap.count(); |
| 110 | } |
| 111 | |
Herb Derby | d2a1887 | 2019-06-18 17:07:39 -0400 | [diff] [blame] | 112 | SkSpan<const SkGlyph*> SkStrike::metrics( |
| 113 | SkSpan<const SkGlyphID>glyphIDs, const SkGlyph* results[]) { |
Herb Derby | d2a1887 | 2019-06-18 17:07:39 -0400 | [diff] [blame] | 114 | size_t glyphCount = 0; |
| 115 | for (auto glyphID : glyphIDs) { |
| 116 | SkGlyph* glyphPtr = this->glyph(glyphID); |
| 117 | results[glyphCount++] = glyphPtr; |
| 118 | } |
| 119 | |
| 120 | return {results, glyphCount}; |
| 121 | } |
| 122 | |
Herb Derby | d2a1887 | 2019-06-18 17:07:39 -0400 | [diff] [blame] | 123 | SkSpan<SkPoint> SkStrike::getAdvances(SkSpan<const SkGlyphID> glyphIDs, SkPoint advances[]) { |
| 124 | auto cursor = advances; |
| 125 | SkAutoSTArray<50, const SkGlyph*> glyphStorage{SkTo<int>(glyphIDs.size())}; |
| 126 | auto glyphs = this->metrics(glyphIDs, glyphStorage.get()); |
| 127 | for (const SkGlyph* glyph : glyphs) { |
| 128 | *cursor++ = glyph->advanceVector(); |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 129 | } |
Herb Derby | d2a1887 | 2019-06-18 17:07:39 -0400 | [diff] [blame] | 130 | return {advances, glyphIDs.size()}; |
Herb Derby | 41f4f31 | 2018-06-06 17:45:53 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Herb Derby | a4eecd8 | 2019-06-25 15:05:30 -0400 | [diff] [blame] | 133 | |
| 134 | const void* SkStrike::prepareImage(SkGlyph* glyph) { |
Herb Derby | 9b06f21 | 2019-06-21 14:25:47 -0400 | [diff] [blame] | 135 | if (glyph->setImage(&fAlloc, fScalerContext.get())) { |
| 136 | fMemoryUsed += glyph->imageSize(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | } |
Herb Derby | 9b06f21 | 2019-06-21 14:25:47 -0400 | [diff] [blame] | 138 | return glyph->image(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Herb Derby | 7019060 | 2019-06-24 14:53:59 -0400 | [diff] [blame] | 141 | SkGlyph* SkStrike::mergeGlyphAndImage(SkPackedGlyphID toID, const SkGlyph& from) { |
| 142 | SkGlyph* glyph = fGlyphMap.findOrNull(toID); |
| 143 | if (glyph == nullptr) { |
| 144 | glyph = this->makeGlyph(toID); |
| 145 | } |
| 146 | if (glyph->setMetricsAndImage(&fAlloc, from)) { |
Herb Derby | 9b06f21 | 2019-06-21 14:25:47 -0400 | [diff] [blame] | 147 | fMemoryUsed += glyph->imageSize(); |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 148 | } |
Herb Derby | 7019060 | 2019-06-24 14:53:59 -0400 | [diff] [blame] | 149 | return glyph; |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 152 | bool SkStrike::belongsToCache(const SkGlyph* glyph) const { |
Herb Derby | e46ccf8 | 2019-01-09 11:22:00 -0500 | [diff] [blame] | 153 | return glyph && fGlyphMap.findOrNull(glyph->getPackedID()) == glyph; |
Khushal | 8523b6b | 2018-06-12 11:26:17 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 156 | const SkGlyph* SkStrike::getCachedGlyphAnySubPix(SkGlyphID glyphID, |
Khushal | 8523b6b | 2018-06-12 11:26:17 -0700 | [diff] [blame] | 157 | SkPackedGlyphID vetoID) const { |
| 158 | for (SkFixed subY = 0; subY < SK_Fixed1; subY += SK_FixedQuarter) { |
| 159 | for (SkFixed subX = 0; subX < SK_Fixed1; subX += SK_FixedQuarter) { |
| 160 | SkPackedGlyphID packedGlyphID{glyphID, subX, subY}; |
| 161 | if (packedGlyphID == vetoID) continue; |
Herb Derby | e46ccf8 | 2019-01-09 11:22:00 -0500 | [diff] [blame] | 162 | if (SkGlyph* glyphPtr = fGlyphMap.findOrNull(packedGlyphID)) { |
| 163 | return glyphPtr; |
Khushal | 8523b6b | 2018-06-12 11:26:17 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return nullptr; |
| 169 | } |
| 170 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 171 | SkVector SkStrike::rounding() const { |
| 172 | return SkStrikeCommon::PixelRounding(fIsSubpixel, fAxisAlignment); |
Herb Derby | 4f169ec | 2018-08-22 17:26:46 -0400 | [diff] [blame] | 173 | } |
| 174 | |
Herb Derby | e8d2d01 | 2019-03-11 17:22:47 -0400 | [diff] [blame] | 175 | // N.B. This glyphMetrics call culls all the glyphs which will not display based on a non-finite |
| 176 | // position or that there are no mask pixels. |
Herb Derby | 812499d | 2019-04-10 18:09:13 -0400 | [diff] [blame] | 177 | SkSpan<const SkGlyphPos> SkStrike::prepareForDrawing(const SkGlyphID glyphIDs[], |
| 178 | const SkPoint positions[], |
| 179 | size_t n, |
| 180 | int maxDimension, |
Herb Derby | 1c550f7 | 2019-04-23 11:04:29 -0400 | [diff] [blame] | 181 | PreparationDetail detail, |
Herb Derby | 812499d | 2019-04-10 18:09:13 -0400 | [diff] [blame] | 182 | SkGlyphPos result[]) { |
Herb Derby | 13df106 | 2019-04-08 16:29:36 -0400 | [diff] [blame] | 183 | size_t drawableGlyphCount = 0; |
Herb Derby | 13df106 | 2019-04-08 16:29:36 -0400 | [diff] [blame] | 184 | for (size_t i = 0; i < n; i++) { |
Herb Derby | 812499d | 2019-04-10 18:09:13 -0400 | [diff] [blame] | 185 | SkPoint position = positions[i]; |
| 186 | if (SkScalarsAreFinite(position.x(), position.y())) { |
| 187 | // This assumes that the strike has no sub-pixel positioning for glyphs that are |
| 188 | // transformed from source space to device space. |
Herb Derby | a4eecd8 | 2019-06-25 15:05:30 -0400 | [diff] [blame] | 189 | SkGlyph* glyph = this->glyph(glyphIDs[i], position); |
| 190 | if (!glyph->isEmpty()) { |
| 191 | result[drawableGlyphCount++] = {i, glyph, position}; |
| 192 | if (glyph->maxDimension() <= maxDimension) { |
| 193 | // Glyph is an allowable size, good to go. |
Herb Derby | 1c550f7 | 2019-04-23 11:04:29 -0400 | [diff] [blame] | 194 | if (detail == SkStrikeInterface::kImageIfNeeded) { |
Herb Derby | a4eecd8 | 2019-06-25 15:05:30 -0400 | [diff] [blame] | 195 | this->prepareImage(glyph); |
Herb Derby | 1c550f7 | 2019-04-23 11:04:29 -0400 | [diff] [blame] | 196 | } |
Herb Derby | a4eecd8 | 2019-06-25 15:05:30 -0400 | [diff] [blame] | 197 | } else if (!glyph->isColor()) { |
Herb Derby | 812499d | 2019-04-10 18:09:13 -0400 | [diff] [blame] | 198 | // The out of atlas glyph is not color so we can draw it using paths. |
Herb Derby | a4eecd8 | 2019-06-25 15:05:30 -0400 | [diff] [blame] | 199 | this->preparePath(glyph); |
Herb Derby | 812499d | 2019-04-10 18:09:13 -0400 | [diff] [blame] | 200 | } else { |
Herb Derby | 812499d | 2019-04-10 18:09:13 -0400 | [diff] [blame] | 201 | // This will be handled by the fallback strike. |
Herb Derby | a4eecd8 | 2019-06-25 15:05:30 -0400 | [diff] [blame] | 202 | SkASSERT(glyph->maxDimension() > maxDimension && glyph->isColor()); |
Herb Derby | 812499d | 2019-04-10 18:09:13 -0400 | [diff] [blame] | 203 | } |
Herb Derby | e8d2d01 | 2019-03-11 17:22:47 -0400 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
Herb Derby | eded39a | 2019-04-10 16:13:26 -0400 | [diff] [blame] | 208 | return SkSpan<const SkGlyphPos>{result, drawableGlyphCount}; |
Herb Derby | e8d2d01 | 2019-03-11 17:22:47 -0400 | [diff] [blame] | 209 | } |
| 210 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 211 | void SkStrike::findIntercepts(const SkScalar bounds[2], SkScalar scale, SkScalar xPos, |
Herb Derby | 6a07591 | 2019-05-29 17:02:49 -0400 | [diff] [blame] | 212 | SkGlyph* glyph, SkScalar* array, int* count) { |
Herb Derby | 11697dc | 2019-06-06 11:50:42 -0400 | [diff] [blame] | 213 | glyph->ensureIntercepts(bounds, scale, xPos, array, count, &fAlloc); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 216 | void SkStrike::dump() const { |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 217 | const SkTypeface* face = fScalerContext->getTypeface(); |
| 218 | const SkScalerContextRec& rec = fScalerContext->getRec(); |
| 219 | SkMatrix matrix; |
| 220 | rec.getSingleMatrix(&matrix); |
| 221 | matrix.preScale(SkScalarInvert(rec.fTextSize), SkScalarInvert(rec.fTextSize)); |
| 222 | SkString name; |
| 223 | face->getFamilyName(&name); |
| 224 | |
| 225 | SkString msg; |
Ben Wagner | 26308e1 | 2017-08-08 15:23:47 -0400 | [diff] [blame] | 226 | SkFontStyle style = face->fontStyle(); |
Herb Derby | 108e94a | 2018-02-22 14:46:11 -0500 | [diff] [blame] | 227 | msg.printf("cache typeface:%x %25s:(%d,%d,%d)\n %s glyphs:%3d", |
Ben Wagner | 26308e1 | 2017-08-08 15:23:47 -0400 | [diff] [blame] | 228 | face->uniqueID(), name.c_str(), style.weight(), style.width(), style.slant(), |
Herb Derby | 108e94a | 2018-02-22 14:46:11 -0500 | [diff] [blame] | 229 | rec.dump().c_str(), fGlyphMap.count()); |
reed | 40dab98 | 2015-01-28 13:28:53 -0800 | [diff] [blame] | 230 | SkDebugf("%s\n", msg.c_str()); |
| 231 | } |
| 232 | |
Herb Derby | 4925364 | 2019-02-11 14:20:31 -0500 | [diff] [blame] | 233 | void SkStrike::onAboutToExitScope() { } |
| 234 | |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 235 | #ifdef SK_DEBUG |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 236 | void SkStrike::forceValidate() const { |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 237 | size_t memoryUsed = sizeof(*this); |
Herb Derby | e46ccf8 | 2019-01-09 11:22:00 -0500 | [diff] [blame] | 238 | fGlyphMap.foreach ([&memoryUsed](const SkGlyph* glyphPtr) { |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 239 | memoryUsed += sizeof(SkGlyph); |
Herb Derby | 793818b | 2019-06-25 17:10:11 -0400 | [diff] [blame^] | 240 | if (glyphPtr->setImageHasBeenCalled()) { |
Herb Derby | 9b06f21 | 2019-06-21 14:25:47 -0400 | [diff] [blame] | 241 | memoryUsed += glyphPtr->imageSize(); |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 242 | } |
Herb Derby | f0e7581 | 2019-06-12 11:14:50 -0400 | [diff] [blame] | 243 | if (glyphPtr->setPathHasBeenCalled() && glyphPtr->path() != nullptr) { |
| 244 | memoryUsed += glyphPtr->path()->approximateBytesUsed(); |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 245 | } |
| 246 | }); |
| 247 | SkASSERT(fMemoryUsed == memoryUsed); |
| 248 | } |
reed@google.com | baed71f | 2013-09-26 19:28:27 +0000 | [diff] [blame] | 249 | |
Herb Derby | 5fd955e | 2019-01-16 11:23:29 -0500 | [diff] [blame] | 250 | void SkStrike::validate() const { |
djsollen@google.com | 000dea7 | 2012-11-30 16:19:32 +0000 | [diff] [blame] | 251 | #ifdef SK_DEBUG_GLYPH_CACHE |
Khushal | b2e7127 | 2018-05-15 12:59:48 -0700 | [diff] [blame] | 252 | forceValidate(); |
djsollen@google.com | 000dea7 | 2012-11-30 16:19:32 +0000 | [diff] [blame] | 253 | #endif |
reed@android.com | f2b98d6 | 2010-12-20 18:26:13 +0000 | [diff] [blame] | 254 | } |
Herb Derby | e8d2d01 | 2019-03-11 17:22:47 -0400 | [diff] [blame] | 255 | #endif // SK_DEBUG |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 256 | |
reed@google.com | 0983701 | 2012-04-23 15:04:44 +0000 | [diff] [blame] | 257 | |