blob: 164768f3b08bbfe29f67f226486bca99e1173e8e [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
jvanverth733f5f52014-07-11 19:45:16 -070062GrMaskFormat GrFontScaler::getMaskFormat() {
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;
caryclark@google.com1eeaf0b2011-06-22 13:19:43 +000071 case SkMask::kLCD32_Format:
72 return kA888_GrMaskFormat;
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +000073 case SkMask::kARGB32_Format:
74 return kARGB_GrMaskFormat;
reed@google.com98539c62011-03-15 15:40:16 +000075 default:
mtklein@google.com330313a2013-08-22 15:37:26 +000076 SkDEBUGFAIL("unsupported SkMask::Format");
reed@google.com98539c62011-03-15 15:40:16 +000077 return kA8_GrMaskFormat;
78 }
79}
80
jvanverth733f5f52014-07-11 19:45:16 -070081const GrFontDescKey* GrFontScaler::getKey() {
reed@google.comac10a2d2010-12-22 21:39:39 +000082 if (NULL == fKey) {
jvanverth733f5f52014-07-11 19:45:16 -070083 fKey = SkNEW_ARGS(GrFontDescKey, (fStrike->getDescriptor()));
reed@google.comac10a2d2010-12-22 21:39:39 +000084 }
85 return fKey;
86}
87
jvanverth733f5f52014-07-11 19:45:16 -070088bool GrFontScaler::getPackedGlyphBounds(GrGlyph::PackedID packed, SkIRect* bounds) {
reed@google.comac10a2d2010-12-22 21:39:39 +000089 const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed),
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000090 GrGlyph::UnpackFixedX(packed),
91 GrGlyph::UnpackFixedY(packed));
reed@google.comac10a2d2010-12-22 21:39:39 +000092 bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight);
reed@google.com1fcd51e2011-01-05 15:50:27 +000093
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000094 return true;
95}
96
jvanverth733f5f52014-07-11 19:45:16 -070097bool GrFontScaler::getPackedGlyphDFBounds(GrGlyph::PackedID packed, SkIRect* bounds) {
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000098 const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed),
99 GrGlyph::UnpackFixedX(packed),
100 GrGlyph::UnpackFixedY(packed));
101 bounds->setXYWH(glyph.fLeft, glyph.fTop, glyph.fWidth, glyph.fHeight);
102 bounds->outset(SK_DistanceFieldPad, SK_DistanceFieldPad);
103
104 return true;
reed@google.comac10a2d2010-12-22 21:39:39 +0000105}
106
bsalomon@google.comc8699ef2012-09-10 13:28:00 +0000107namespace {
108// expands each bit in a bitmask to 0 or ~0 of type INT_TYPE. Used to expand a BW glyph mask to
109// A8, RGB565, or RGBA8888.
110template <typename INT_TYPE>
111void expand_bits(INT_TYPE* dst,
112 const uint8_t* src,
113 int width,
114 int height,
115 int dstRowBytes,
116 int srcRowBytes) {
117 for (int i = 0; i < height; ++i) {
118 int rowWritesLeft = width;
119 const uint8_t* s = src;
120 INT_TYPE* d = dst;
121 while (rowWritesLeft > 0) {
122 unsigned mask = *s++;
123 for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) {
124 *d++ = (mask & (1 << i)) ? (INT_TYPE)(~0UL) : 0;
mike@reedtribe.orgc34effe2011-04-06 00:54:45 +0000125 }
126 }
bsalomon@google.comc8699ef2012-09-10 13:28:00 +0000127 dst = reinterpret_cast<INT_TYPE*>(reinterpret_cast<intptr_t>(dst) + dstRowBytes);
128 src += srcRowBytes;
mike@reedtribe.orgc34effe2011-04-06 00:54:45 +0000129 }
130}
bsalomon@google.comc8699ef2012-09-10 13:28:00 +0000131}
mike@reedtribe.orgc34effe2011-04-06 00:54:45 +0000132
jvanverth733f5f52014-07-11 19:45:16 -0700133bool GrFontScaler::getPackedGlyphImage(GrGlyph::PackedID packed,
reed@google.comac10a2d2010-12-22 21:39:39 +0000134 int width, int height,
135 int dstRB, void* dst) {
136 const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed),
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000137 GrGlyph::UnpackFixedX(packed),
138 GrGlyph::UnpackFixedY(packed));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000139 SkASSERT(glyph.fWidth == width);
140 SkASSERT(glyph.fHeight == height);
reed@google.comac10a2d2010-12-22 21:39:39 +0000141 const void* src = fStrike->findImage(glyph);
142 if (NULL == src) {
143 return false;
144 }
145
146 int srcRB = glyph.rowBytes();
bsalomon@google.comc8699ef2012-09-10 13:28:00 +0000147 // The windows font host sometimes has BW glyphs in a non-BW strike. So it is important here to
148 // check the glyph's format, not the strike's format, and to be able to convert to any of the
149 // GrMaskFormats.
150 if (SkMask::kBW_Format == glyph.fMaskFormat) {
151 // expand bits to our mask type
mike@reedtribe.orgc34effe2011-04-06 00:54:45 +0000152 const uint8_t* bits = reinterpret_cast<const uint8_t*>(src);
bsalomon@google.comc8699ef2012-09-10 13:28:00 +0000153 switch (this->getMaskFormat()) {
154 case kA8_GrMaskFormat:{
155 uint8_t* bytes = reinterpret_cast<uint8_t*>(dst);
156 expand_bits(bytes, bits, width, height, dstRB, srcRB);
157 break;
158 }
159 case kA565_GrMaskFormat: {
160 uint16_t* rgb565 = reinterpret_cast<uint16_t*>(dst);
161 expand_bits(rgb565, bits, width, height, dstRB, srcRB);
162 break;
163 }
164 case kA888_GrMaskFormat: {
165 uint32_t* rgba8888 = reinterpret_cast<uint32_t*>(dst);
166 expand_bits(rgba8888, bits, width, height, dstRB, srcRB);
167 break;
168 }
commit-bot@chromium.orgf8cb1842013-12-03 19:45:22 +0000169 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000170 SkFAIL("Invalid GrMaskFormat");
mike@reedtribe.orgc34effe2011-04-06 00:54:45 +0000171 }
172 } else if (srcRB == dstRB) {
reed@google.comac10a2d2010-12-22 21:39:39 +0000173 memcpy(dst, src, dstRB * height);
174 } else {
reed@google.com98539c62011-03-15 15:40:16 +0000175 const int bbp = GrMaskFormatBytesPerPixel(this->getMaskFormat());
reed@google.comac10a2d2010-12-22 21:39:39 +0000176 for (int y = 0; y < height; y++) {
reed@google.com98539c62011-03-15 15:40:16 +0000177 memcpy(dst, src, width * bbp);
reed@google.comac10a2d2010-12-22 21:39:39 +0000178 src = (const char*)src + srcRB;
179 dst = (char*)dst + dstRB;
180 }
181 }
182 return true;
183}
184
jvanverth733f5f52014-07-11 19:45:16 -0700185bool GrFontScaler::getPackedGlyphDFImage(GrGlyph::PackedID packed,
commit-bot@chromium.org762cd802014-04-14 22:05:07 +0000186 int width, int height,
187 void* dst) {
188 const SkGlyph& glyph = fStrike->getGlyphIDMetrics(GrGlyph::UnpackID(packed),
189 GrGlyph::UnpackFixedX(packed),
190 GrGlyph::UnpackFixedY(packed));
191 SkASSERT(glyph.fWidth + 2*SK_DistanceFieldPad == width);
192 SkASSERT(glyph.fHeight + 2*SK_DistanceFieldPad == height);
193 const void* src = fStrike->findDistanceField(glyph);
194 if (NULL == src) {
195 return false;
196 }
197
198 memcpy(dst, src, width * height);
199
200 return true;
201}
202
reed@google.com07f3ee12011-05-16 17:21:57 +0000203// we should just return const SkPath* (NULL means false)
jvanverth733f5f52014-07-11 19:45:16 -0700204bool GrFontScaler::getGlyphPath(uint16_t glyphID, SkPath* path) {
reed@google.com1fcd51e2011-01-05 15:50:27 +0000205
reed@google.comac10a2d2010-12-22 21:39:39 +0000206 const SkGlyph& glyph = fStrike->getGlyphIDMetrics(glyphID);
207 const SkPath* skPath = fStrike->findPath(glyph);
208 if (skPath) {
reed@google.com07f3ee12011-05-16 17:21:57 +0000209 *path = *skPath;
reed@google.comac10a2d2010-12-22 21:39:39 +0000210 return true;
211 }
212 return false;
213}