epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 10 | #include "GrTemplates.h" |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 11 | #include "GrFontScaler.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 12 | #include "SkDescriptor.h" |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 13 | #include "SkDistanceFieldGen.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 14 | #include "SkGlyphCache.h" |
| 15 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | /////////////////////////////////////////////////////////////////////////////// |
| 17 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 18 | GrFontDescKey::GrFontDescKey(const SkDescriptor& desc) : fHash(desc.getChecksum()) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | size_t size = desc.getLength(); |
| 20 | if (size <= sizeof(fStorage)) { |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 21 | fDesc = GrTCast<SkDescriptor*>(fStorage); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | } else { |
| 23 | fDesc = SkDescriptor::Alloc(size); |
| 24 | } |
| 25 | memcpy(fDesc, &desc, size); |
| 26 | } |
| 27 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 28 | GrFontDescKey::~GrFontDescKey() { |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 29 | if (fDesc != GrTCast<SkDescriptor*>(fStorage)) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 30 | SkDescriptor::Free(fDesc); |
| 31 | } |
| 32 | } |
| 33 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 34 | bool GrFontDescKey::lt(const GrFontDescKey& rh) const { |
| 35 | const SkDescriptor* srcDesc = (&rh)->fDesc; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 36 | size_t lenLH = fDesc->getLength(); |
| 37 | size_t lenRH = srcDesc->getLength(); |
robertphillips@google.com | adacc70 | 2013-10-14 21:53:24 +0000 | [diff] [blame] | 38 | int cmp = memcmp(fDesc, srcDesc, SkTMin<size_t>(lenLH, lenRH)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 39 | if (0 == cmp) { |
| 40 | return lenLH < lenRH; |
| 41 | } else { |
| 42 | return cmp < 0; |
| 43 | } |
| 44 | } |
| 45 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 46 | bool GrFontDescKey::eq(const GrFontDescKey& rh) const { |
| 47 | const SkDescriptor* srcDesc = (&rh)->fDesc; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | return fDesc->equals(*srcDesc); |
| 49 | } |
| 50 | |
| 51 | /////////////////////////////////////////////////////////////////////////////// |
| 52 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 53 | GrFontScaler::GrFontScaler(SkGlyphCache* strike) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | fStrike = strike; |
| 55 | fKey = NULL; |
| 56 | } |
| 57 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 58 | GrFontScaler::~GrFontScaler() { |
commit-bot@chromium.org | a4de8c2 | 2013-09-09 13:38:37 +0000 | [diff] [blame] | 59 | SkSafeUnref(fKey); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 60 | } |
| 61 | |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 62 | GrMaskFormat GrFontScaler::getMaskFormat() const { |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 63 | SkMask::Format format = fStrike->getMaskFormat(); |
| 64 | switch (format) { |
mike@reedtribe.org | c34effe | 2011-04-06 00:54:45 +0000 | [diff] [blame] | 65 | case SkMask::kBW_Format: |
| 66 | // fall through to kA8 -- we store BW glyphs in our 8-bit cache |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 67 | case SkMask::kA8_Format: |
| 68 | return kA8_GrMaskFormat; |
| 69 | case SkMask::kLCD16_Format: |
| 70 | return kA565_GrMaskFormat; |
caryclark@google.com | 1eeaf0b | 2011-06-22 13:19:43 +0000 | [diff] [blame] | 71 | case SkMask::kLCD32_Format: |
| 72 | return kA888_GrMaskFormat; |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 73 | case SkMask::kARGB32_Format: |
| 74 | return kARGB_GrMaskFormat; |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 75 | default: |
mtklein@google.com | 330313a | 2013-08-22 15:37:26 +0000 | [diff] [blame] | 76 | SkDEBUGFAIL("unsupported SkMask::Format"); |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 77 | return kA8_GrMaskFormat; |
| 78 | } |
| 79 | } |
| 80 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 81 | const GrFontDescKey* GrFontScaler::getKey() { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 82 | if (NULL == fKey) { |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 83 | fKey = SkNEW_ARGS(GrFontDescKey, (fStrike->getDescriptor())); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 84 | } |
| 85 | return fKey; |
| 86 | } |
| 87 | |
jvanverth | 294c326 | 2014-10-10 11:36:12 -0700 | [diff] [blame] | 88 | GrMaskFormat GrFontScaler::getPackedGlyphMaskFormat(GrGlyph::PackedID packed) const { |
| 89 | const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed), |
| 90 | GrGlyph::UnpackFixedX(packed), |
| 91 | GrGlyph::UnpackFixedY(packed)); |
| 92 | SkMask::Format format = static_cast<SkMask::Format>(glyph.fMaskFormat); |
| 93 | switch (format) { |
| 94 | case SkMask::kBW_Format: |
| 95 | // fall through to kA8 -- we store BW glyphs in our 8-bit cache |
| 96 | case SkMask::kA8_Format: |
| 97 | return kA8_GrMaskFormat; |
| 98 | case SkMask::kLCD16_Format: |
| 99 | return kA565_GrMaskFormat; |
| 100 | case SkMask::kLCD32_Format: |
| 101 | return kA888_GrMaskFormat; |
| 102 | case SkMask::kARGB32_Format: |
| 103 | return kARGB_GrMaskFormat; |
| 104 | default: |
| 105 | SkDEBUGFAIL("unsupported SkMask::Format"); |
| 106 | return kA8_GrMaskFormat; |
| 107 | } |
| 108 | } |
| 109 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 110 | bool GrFontScaler::getPackedGlyphBounds(GrGlyph::PackedID packed, SkIRect* bounds) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 111 | const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed), |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 112 | GrGlyph::UnpackFixedX(packed), |
| 113 | GrGlyph::UnpackFixedY(packed)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 114 | bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight); |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 115 | |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 116 | return true; |
| 117 | } |
| 118 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 119 | bool GrFontScaler::getPackedGlyphDFBounds(GrGlyph::PackedID packed, SkIRect* bounds) { |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 120 | const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed), |
| 121 | GrGlyph::UnpackFixedX(packed), |
| 122 | GrGlyph::UnpackFixedY(packed)); |
| 123 | bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight); |
| 124 | bounds->outset(SK_DistanceFieldPad, SK_DistanceFieldPad); |
| 125 | |
| 126 | return true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 127 | } |
| 128 | |
bsalomon@google.com | c8699ef | 2012-09-10 13:28:00 +0000 | [diff] [blame] | 129 | namespace { |
| 130 | // expands each bit in a bitmask to 0 or ~0 of type INT_TYPE. Used to expand a BW glyph mask to |
| 131 | // A8, RGB565, or RGBA8888. |
| 132 | template <typename INT_TYPE> |
| 133 | void expand_bits(INT_TYPE* dst, |
| 134 | const uint8_t* src, |
| 135 | int width, |
| 136 | int height, |
| 137 | int dstRowBytes, |
| 138 | int srcRowBytes) { |
| 139 | for (int i = 0; i < height; ++i) { |
| 140 | int rowWritesLeft = width; |
| 141 | const uint8_t* s = src; |
| 142 | INT_TYPE* d = dst; |
| 143 | while (rowWritesLeft > 0) { |
| 144 | unsigned mask = *s++; |
| 145 | for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) { |
| 146 | *d++ = (mask & (1 << i)) ? (INT_TYPE)(~0UL) : 0; |
mike@reedtribe.org | c34effe | 2011-04-06 00:54:45 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
bsalomon@google.com | c8699ef | 2012-09-10 13:28:00 +0000 | [diff] [blame] | 149 | dst = reinterpret_cast<INT_TYPE*>(reinterpret_cast<intptr_t>(dst) + dstRowBytes); |
| 150 | src += srcRowBytes; |
mike@reedtribe.org | c34effe | 2011-04-06 00:54:45 +0000 | [diff] [blame] | 151 | } |
| 152 | } |
bsalomon@google.com | c8699ef | 2012-09-10 13:28:00 +0000 | [diff] [blame] | 153 | } |
mike@reedtribe.org | c34effe | 2011-04-06 00:54:45 +0000 | [diff] [blame] | 154 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 155 | bool GrFontScaler::getPackedGlyphImage(GrGlyph::PackedID packed, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 156 | int width, int height, |
| 157 | int dstRB, void* dst) { |
| 158 | const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed), |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 159 | GrGlyph::UnpackFixedX(packed), |
| 160 | GrGlyph::UnpackFixedY(packed)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 161 | SkASSERT(glyph.fWidth == width); |
| 162 | SkASSERT(glyph.fHeight == height); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 163 | const void* src = fStrike->findImage(glyph); |
| 164 | if (NULL == src) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | int srcRB = glyph.rowBytes(); |
bsalomon@google.com | c8699ef | 2012-09-10 13:28:00 +0000 | [diff] [blame] | 169 | // The windows font host sometimes has BW glyphs in a non-BW strike. So it is important here to |
| 170 | // check the glyph's format, not the strike's format, and to be able to convert to any of the |
| 171 | // GrMaskFormats. |
| 172 | if (SkMask::kBW_Format == glyph.fMaskFormat) { |
| 173 | // expand bits to our mask type |
mike@reedtribe.org | c34effe | 2011-04-06 00:54:45 +0000 | [diff] [blame] | 174 | const uint8_t* bits = reinterpret_cast<const uint8_t*>(src); |
bsalomon@google.com | c8699ef | 2012-09-10 13:28:00 +0000 | [diff] [blame] | 175 | switch (this->getMaskFormat()) { |
| 176 | case kA8_GrMaskFormat:{ |
| 177 | uint8_t* bytes = reinterpret_cast<uint8_t*>(dst); |
| 178 | expand_bits(bytes, bits, width, height, dstRB, srcRB); |
| 179 | break; |
| 180 | } |
| 181 | case kA565_GrMaskFormat: { |
| 182 | uint16_t* rgb565 = reinterpret_cast<uint16_t*>(dst); |
| 183 | expand_bits(rgb565, bits, width, height, dstRB, srcRB); |
| 184 | break; |
| 185 | } |
| 186 | case kA888_GrMaskFormat: { |
| 187 | uint32_t* rgba8888 = reinterpret_cast<uint32_t*>(dst); |
| 188 | expand_bits(rgba8888, bits, width, height, dstRB, srcRB); |
| 189 | break; |
| 190 | } |
commit-bot@chromium.org | f8cb184 | 2013-12-03 19:45:22 +0000 | [diff] [blame] | 191 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 192 | SkFAIL("Invalid GrMaskFormat"); |
mike@reedtribe.org | c34effe | 2011-04-06 00:54:45 +0000 | [diff] [blame] | 193 | } |
| 194 | } else if (srcRB == dstRB) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 195 | memcpy(dst, src, dstRB * height); |
| 196 | } else { |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 197 | const int bbp = GrMaskFormatBytesPerPixel(this->getMaskFormat()); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 198 | for (int y = 0; y < height; y++) { |
reed@google.com | 98539c6 | 2011-03-15 15:40:16 +0000 | [diff] [blame] | 199 | memcpy(dst, src, width * bbp); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 200 | src = (const char*)src + srcRB; |
| 201 | dst = (char*)dst + dstRB; |
| 202 | } |
| 203 | } |
| 204 | return true; |
| 205 | } |
| 206 | |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 207 | bool GrFontScaler::getPackedGlyphDFImage(GrGlyph::PackedID packed, |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 208 | int width, int height, |
| 209 | void* dst) { |
| 210 | const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed), |
| 211 | GrGlyph::UnpackFixedX(packed), |
| 212 | GrGlyph::UnpackFixedY(packed)); |
| 213 | SkASSERT(glyph.fWidth + 2*SK_DistanceFieldPad == width); |
| 214 | SkASSERT(glyph.fHeight + 2*SK_DistanceFieldPad == height); |
| 215 | const void* src = fStrike->findDistanceField(glyph); |
| 216 | if (NULL == src) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | memcpy(dst, src, width * height); |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 225 | // we should just return const SkPath* (NULL means false) |
jvanverth | 733f5f5 | 2014-07-11 19:45:16 -0700 | [diff] [blame] | 226 | bool GrFontScaler::getGlyphPath(uint16_t glyphID, SkPath* path) { |
reed@google.com | 1fcd51e | 2011-01-05 15:50:27 +0000 | [diff] [blame] | 227 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 228 | const SkGlyph& glyph = fStrike->getGlyphIDMetrics(glyphID); |
| 229 | const SkPath* skPath = fStrike->findPath(glyph); |
| 230 | if (skPath) { |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 231 | *path = *skPath; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 232 | return true; |
| 233 | } |
| 234 | return false; |
| 235 | } |