blob: ed1970e262a65308490bb248babc9595adc53758 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@google.comac10a2d2010-12-22 21:39:39 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * 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.comac10a2d2010-12-22 21:39:39 +00007 */
8
9
bsalomon@google.coma3201942012-06-21 19:58:20 +000010#include "GrTemplates.h"
jvanverth733f5f52014-07-11 19:45:16 -070011#include "GrFontScaler.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000012#include "SkDescriptor.h"
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000013#include "SkDistanceFieldGen.h"
reed@google.comac10a2d2010-12-22 21:39:39 +000014#include "SkGlyphCache.h"
15
reed@google.comac10a2d2010-12-22 21:39:39 +000016///////////////////////////////////////////////////////////////////////////////
17
jvanverth733f5f52014-07-11 19:45:16 -070018GrFontDescKey::GrFontDescKey(const SkDescriptor& desc) : fHash(desc.getChecksum()) {
reed@google.comac10a2d2010-12-22 21:39:39 +000019 size_t size = desc.getLength();
20 if (size <= sizeof(fStorage)) {
reed@google.com1fcd51e2011-01-05 15:50:27 +000021 fDesc = GrTCast<SkDescriptor*>(fStorage);
reed@google.comac10a2d2010-12-22 21:39:39 +000022 } else {
23 fDesc = SkDescriptor::Alloc(size);
24 }
25 memcpy(fDesc, &desc, size);
26}
27
jvanverth733f5f52014-07-11 19:45:16 -070028GrFontDescKey::~GrFontDescKey() {
reed@google.com1fcd51e2011-01-05 15:50:27 +000029 if (fDesc != GrTCast<SkDescriptor*>(fStorage)) {
reed@google.comac10a2d2010-12-22 21:39:39 +000030 SkDescriptor::Free(fDesc);
31 }
32}
33
jvanverth733f5f52014-07-11 19:45:16 -070034bool GrFontDescKey::lt(const GrFontDescKey& rh) const {
35 const SkDescriptor* srcDesc = (&rh)->fDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +000036 size_t lenLH = fDesc->getLength();
37 size_t lenRH = srcDesc->getLength();
robertphillips@google.comadacc702013-10-14 21:53:24 +000038 int cmp = memcmp(fDesc, srcDesc, SkTMin<size_t>(lenLH, lenRH));
reed@google.comac10a2d2010-12-22 21:39:39 +000039 if (0 == cmp) {
40 return lenLH < lenRH;
41 } else {
42 return cmp < 0;
43 }
44}
45
jvanverth733f5f52014-07-11 19:45:16 -070046bool GrFontDescKey::eq(const GrFontDescKey& rh) const {
47 const SkDescriptor* srcDesc = (&rh)->fDesc;
reed@google.comac10a2d2010-12-22 21:39:39 +000048 return fDesc->equals(*srcDesc);
49}
50
51///////////////////////////////////////////////////////////////////////////////
52
jvanverth733f5f52014-07-11 19:45:16 -070053GrFontScaler::GrFontScaler(SkGlyphCache* strike) {
reed@google.comac10a2d2010-12-22 21:39:39 +000054 fStrike = strike;
55 fKey = NULL;
56}
57
jvanverth733f5f52014-07-11 19:45:16 -070058GrFontScaler::~GrFontScaler() {
commit-bot@chromium.orga4de8c22013-09-09 13:38:37 +000059 SkSafeUnref(fKey);
reed@google.comac10a2d2010-12-22 21:39:39 +000060}
61
jvanverth294c3262014-10-10 11:36:12 -070062GrMaskFormat GrFontScaler::getMaskFormat() const {
reed@google.com98539c62011-03-15 15:40:16 +000063 SkMask::Format format = fStrike->getMaskFormat();
64 switch (format) {
mike@reedtribe.orgc34effe2011-04-06 00:54:45 +000065 case SkMask::kBW_Format:
66 // fall through to kA8 -- we store BW glyphs in our 8-bit cache
reed@google.com98539c62011-03-15 15:40:16 +000067 case SkMask::kA8_Format:
68 return kA8_GrMaskFormat;
69 case SkMask::kLCD16_Format:
70 return kA565_GrMaskFormat;
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +000071 case SkMask::kARGB32_Format:
72 return kARGB_GrMaskFormat;
reed@google.com98539c62011-03-15 15:40:16 +000073 default:
mtklein@google.com330313a2013-08-22 15:37:26 +000074 SkDEBUGFAIL("unsupported SkMask::Format");
reed@google.com98539c62011-03-15 15:40:16 +000075 return kA8_GrMaskFormat;
76 }
77}
78
jvanverth733f5f52014-07-11 19:45:16 -070079const GrFontDescKey* GrFontScaler::getKey() {
reed@google.comac10a2d2010-12-22 21:39:39 +000080 if (NULL == fKey) {
jvanverth733f5f52014-07-11 19:45:16 -070081 fKey = SkNEW_ARGS(GrFontDescKey, (fStrike->getDescriptor()));
reed@google.comac10a2d2010-12-22 21:39:39 +000082 }
83 return fKey;
84}
85
jvanverth294c3262014-10-10 11:36:12 -070086GrMaskFormat GrFontScaler::getPackedGlyphMaskFormat(GrGlyph::PackedID packed) const {
87 const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed),
88 GrGlyph::UnpackFixedX(packed),
89 GrGlyph::UnpackFixedY(packed));
90 SkMask::Format format = static_cast<SkMask::Format>(glyph.fMaskFormat);
91 switch (format) {
92 case SkMask::kBW_Format:
93 // fall through to kA8 -- we store BW glyphs in our 8-bit cache
94 case SkMask::kA8_Format:
95 return kA8_GrMaskFormat;
96 case SkMask::kLCD16_Format:
97 return kA565_GrMaskFormat;
jvanverth294c3262014-10-10 11:36:12 -070098 case SkMask::kARGB32_Format:
99 return kARGB_GrMaskFormat;
100 default:
101 SkDEBUGFAIL("unsupported SkMask::Format");
102 return kA8_GrMaskFormat;
103 }
104}
105
jvanverth733f5f52014-07-11 19:45:16 -0700106bool GrFontScaler::getPackedGlyphBounds(GrGlyph::PackedID packed, SkIRect* bounds) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000107 const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed),
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000108 GrGlyph::UnpackFixedX(packed),
109 GrGlyph::UnpackFixedY(packed));
reed@google.comac10a2d2010-12-22 21:39:39 +0000110 bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight);
reed@google.com1fcd51e2011-01-05 15:50:27 +0000111
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000112 return true;
113}
114
jvanverth733f5f52014-07-11 19:45:16 -0700115bool GrFontScaler::getPackedGlyphDFBounds(GrGlyph::PackedID packed, SkIRect* bounds) {
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000116 const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed),
117 GrGlyph::UnpackFixedX(packed),
118 GrGlyph::UnpackFixedY(packed));
119 bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight);
120 bounds->outset(SK_DistanceFieldPad, SK_DistanceFieldPad);
121
122 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000123}
124
bsalomon@google.comc8699ef2012-09-10 13:28:00 +0000125namespace {
126// expands each bit in a bitmask to 0 or ~0 of type INT_TYPE. Used to expand a BW glyph mask to
127// A8, RGB565, or RGBA8888.
128template <typename INT_TYPE>
129void expand_bits(INT_TYPE* dst,
130 const uint8_t* src,
131 int width,
132 int height,
133 int dstRowBytes,
134 int srcRowBytes) {
135 for (int i = 0; i < height; ++i) {
136 int rowWritesLeft = width;
137 const uint8_t* s = src;
138 INT_TYPE* d = dst;
139 while (rowWritesLeft > 0) {
140 unsigned mask = *s++;
141 for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) {
142 *d++ = (mask & (1 << i)) ? (INT_TYPE)(~0UL) : 0;
mike@reedtribe.orgc34effe2011-04-06 00:54:45 +0000143 }
144 }
bsalomon@google.comc8699ef2012-09-10 13:28:00 +0000145 dst = reinterpret_cast<INT_TYPE*>(reinterpret_cast<intptr_t>(dst) + dstRowBytes);
146 src += srcRowBytes;
mike@reedtribe.orgc34effe2011-04-06 00:54:45 +0000147 }
148}
bsalomon@google.comc8699ef2012-09-10 13:28:00 +0000149}
mike@reedtribe.orgc34effe2011-04-06 00:54:45 +0000150
jvanverth733f5f52014-07-11 19:45:16 -0700151bool GrFontScaler::getPackedGlyphImage(GrGlyph::PackedID packed,
reed@google.comac10a2d2010-12-22 21:39:39 +0000152 int width, int height,
153 int dstRB, void* dst) {
154 const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed),
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000155 GrGlyph::UnpackFixedX(packed),
156 GrGlyph::UnpackFixedY(packed));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000157 SkASSERT(glyph.fWidth == width);
158 SkASSERT(glyph.fHeight == height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000159 const void* src = fStrike->findImage(glyph);
160 if (NULL == src) {
161 return false;
162 }
163
164 int srcRB = glyph.rowBytes();
bsalomon@google.comc8699ef2012-09-10 13:28:00 +0000165 // The windows font host sometimes has BW glyphs in a non-BW strike. So it is important here to
166 // check the glyph's format, not the strike's format, and to be able to convert to any of the
167 // GrMaskFormats.
168 if (SkMask::kBW_Format == glyph.fMaskFormat) {
169 // expand bits to our mask type
mike@reedtribe.orgc34effe2011-04-06 00:54:45 +0000170 const uint8_t* bits = reinterpret_cast<const uint8_t*>(src);
bsalomon@google.comc8699ef2012-09-10 13:28:00 +0000171 switch (this->getMaskFormat()) {
172 case kA8_GrMaskFormat:{
173 uint8_t* bytes = reinterpret_cast<uint8_t*>(dst);
174 expand_bits(bytes, bits, width, height, dstRB, srcRB);
175 break;
176 }
177 case kA565_GrMaskFormat: {
178 uint16_t* rgb565 = reinterpret_cast<uint16_t*>(dst);
179 expand_bits(rgb565, bits, width, height, dstRB, srcRB);
180 break;
181 }
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000182 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000183 SkFAIL("Invalid GrMaskFormat");
mike@reedtribe.orgc34effe2011-04-06 00:54:45 +0000184 }
185 } else if (srcRB == dstRB) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000186 memcpy(dst, src, dstRB * height);
187 } else {
reed@google.com98539c62011-03-15 15:40:16 +0000188 const int bbp = GrMaskFormatBytesPerPixel(this->getMaskFormat());
reed@google.comac10a2d2010-12-22 21:39:39 +0000189 for (int y = 0; y < height; y++) {
reed@google.com98539c62011-03-15 15:40:16 +0000190 memcpy(dst, src, width * bbp);
reed@google.comac10a2d2010-12-22 21:39:39 +0000191 src = (const char*)src + srcRB;
192 dst = (char*)dst + dstRB;
193 }
194 }
195 return true;
196}
197
jvanverth733f5f52014-07-11 19:45:16 -0700198bool GrFontScaler::getPackedGlyphDFImage(GrGlyph::PackedID packed,
jvanvertha27b82d2015-01-07 10:12:16 -0800199 int width, int height,
200 void* dst) {
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000201 const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed),
202 GrGlyph::UnpackFixedX(packed),
203 GrGlyph::UnpackFixedY(packed));
204 SkASSERT(glyph.fWidth + 2*SK_DistanceFieldPad == width);
205 SkASSERT(glyph.fHeight + 2*SK_DistanceFieldPad == height);
jvanvertha27b82d2015-01-07 10:12:16 -0800206 const void* image = fStrike->findImage(glyph);
207 if (NULL == image) {
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000208 return false;
209 }
jvanvertha27b82d2015-01-07 10:12:16 -0800210 // now generate the distance field
211 SkASSERT(dst);
212 SkMask::Format maskFormat = static_cast<SkMask::Format>(glyph.fMaskFormat);
213 if (SkMask::kA8_Format == maskFormat) {
214 // make the distance field from the image
215 SkGenerateDistanceFieldFromA8Image((unsigned char*)dst,
216 (unsigned char*)image,
217 glyph.fWidth, glyph.fHeight,
218 glyph.rowBytes());
219 } else if (SkMask::kBW_Format == maskFormat) {
220 // make the distance field from the image
221 SkGenerateDistanceFieldFromBWImage((unsigned char*)dst,
222 (unsigned char*)image,
223 glyph.fWidth, glyph.fHeight,
224 glyph.rowBytes());
225 } else {
226 return false;
227 }
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000228
229 return true;
230}
231
reed@google.com07f3ee12011-05-16 17:21:57 +0000232// we should just return const SkPath* (NULL means false)
jvanverth733f5f52014-07-11 19:45:16 -0700233bool GrFontScaler::getGlyphPath(uint16_t glyphID, SkPath* path) {
reed@google.com1fcd51e2011-01-05 15:50:27 +0000234
reed@google.comac10a2d2010-12-22 21:39:39 +0000235 const SkGlyph& glyph = fStrike->getGlyphIDMetrics(glyphID);
236 const SkPath* skPath = fStrike->findPath(glyph);
237 if (skPath) {
reed@google.com07f3ee12011-05-16 17:21:57 +0000238 *path = *skPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000239 return true;
240 }
241 return false;
242}