blob: cd3953ba98bdfda0b923ca88faafd136be0eb7a9 [file] [log] [blame]
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2011 The Android Open Source Project
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
vandebo@chromium.org2a22e102011-01-25 21:01:34 +00006 */
7
vandebo@chromium.org325cb9a2011-03-30 18:36:29 +00008#include "SkAdvancedTypefaceMetrics.h"
reed@google.com5526ede2013-03-25 13:03:37 +00009#include "SkFontDescriptor.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkFontHost.h"
commit-bot@chromium.orgf6351a02014-05-30 21:00:52 +000011#include "SkOnce.h"
reed@google.com5526ede2013-03-25 13:03:37 +000012#include "SkStream.h"
13#include "SkTypeface.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
reed@google.come4959052011-05-02 17:35:04 +000015//#define TRACE_LIFECYCLE
reed@google.com2f3dc9d2011-05-02 17:33:45 +000016
17#ifdef TRACE_LIFECYCLE
18 static int32_t gTypefaceCounter;
19#endif
20
bungeman@google.comfe747652013-03-25 19:36:11 +000021SkTypeface::SkTypeface(Style style, SkFontID fontID, bool isFixedPitch)
22 : fUniqueID(fontID), fStyle(style), fIsFixedPitch(isFixedPitch) {
reed@google.com2f3dc9d2011-05-02 17:33:45 +000023#ifdef TRACE_LIFECYCLE
24 SkDebugf("SkTypeface: create %p fontID %d total %d\n",
25 this, fontID, ++gTypefaceCounter);
26#endif
27}
28
29SkTypeface::~SkTypeface() {
30#ifdef TRACE_LIFECYCLE
31 SkDebugf("SkTypeface: destroy %p fontID %d total %d\n",
32 this, fUniqueID, --gTypefaceCounter);
33#endif
34}
35
36///////////////////////////////////////////////////////////////////////////////
37
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000038class SkEmptyTypeface : public SkTypeface {
39public:
commit-bot@chromium.orgc4df6552014-04-07 19:34:16 +000040 static SkEmptyTypeface* Create() {
41 return SkNEW(SkEmptyTypeface);
42 }
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000043protected:
commit-bot@chromium.orgc4df6552014-04-07 19:34:16 +000044 SkEmptyTypeface() : SkTypeface(SkTypeface::kNormal, 0, true) { }
45
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000046 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { return NULL; }
47 virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK_OVERRIDE {
48 return NULL;
49 }
50 virtual void onFilterRec(SkScalerContextRec*) const SK_OVERRIDE { }
51 virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
52 SkAdvancedTypefaceMetrics::PerGlyphInfo,
53 const uint32_t*, uint32_t) const SK_OVERRIDE { return NULL; }
54 virtual void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE { }
bungeman@google.com3c996f82013-10-24 21:39:35 +000055 virtual int onCharsToGlyphs(const void* chars, Encoding encoding,
56 uint16_t glyphs[], int glyphCount) const SK_OVERRIDE {
57 if (glyphs && glyphCount > 0) {
58 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
59 }
60 return 0;
61 }
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000062 virtual int onCountGlyphs() const SK_OVERRIDE { return 0; };
63 virtual int onGetUPEM() const SK_OVERRIDE { return 0; };
64 class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings {
65 public:
66 virtual bool next(SkTypeface::LocalizedString*) SK_OVERRIDE { return false; }
67 };
68 virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE {
69 return SkNEW(EmptyLocalizedStrings);
70 };
71 virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE { return 0; }
72 virtual size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const SK_OVERRIDE {
73 return 0;
74 }
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000075};
76
commit-bot@chromium.orgf6351a02014-05-30 21:00:52 +000077static SkTypeface* gDefaultTypefaces[] = { NULL, NULL, NULL, NULL };
78static const size_t FONT_STYLE_COUNT = SK_ARRAY_COUNT(gDefaultTypefaces);
79static SkOnceFlag gDefaultTypefaceOnce[FONT_STYLE_COUNT] = {
80 SK_ONCE_INIT, SK_ONCE_INIT, SK_ONCE_INIT, SK_ONCE_INIT
81};
82template <uintmax_t N> struct SkTIsPow2 {
83 static const bool value = (N & (N - 1)) == 0;
84};
85SK_COMPILE_ASSERT(SkTIsPow2<FONT_STYLE_COUNT>::value, FONT_STYLE_COUNT_not_power_of_2);
86
87void SkTypeface::create_default_typeface(Style style) {
88 if (NULL == gDefaultTypefaces[style]) {
89 gDefaultTypefaces[style] = SkFontHost::CreateTypeface(NULL, NULL, style);
90 }
91 if (NULL == gDefaultTypefaces[style]) {
92 // FIXME: Use a singleton for SkEmptyTypeface.
93 gDefaultTypefaces[style] = SkEmptyTypeface::Create();
94 }
bungeman@google.com18b75e52014-01-09 17:13:32 +000095}
bungeman@google.com5c1d88d2013-08-21 17:20:57 +000096
bungeman@google.com18b75e52014-01-09 17:13:32 +000097SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
commit-bot@chromium.orgf6351a02014-05-30 21:00:52 +000098 SkASSERT((size_t)style < FONT_STYLE_COUNT);
bungeman@google.com18b75e52014-01-09 17:13:32 +000099
commit-bot@chromium.orgf6351a02014-05-30 21:00:52 +0000100 // mask off any other bits to avoid a crash in SK_RELEASE
101 style = (Style)(style & (FONT_STYLE_COUNT - 1));
102
103 SkOnce(&gDefaultTypefaceOnce[style], SkTypeface::create_default_typeface, style);
104 return gDefaultTypefaces[style];
reed@google.com538f7842011-12-16 17:56:23 +0000105}
106
djsollen@google.com4fa748d2013-06-05 14:20:25 +0000107SkTypeface* SkTypeface::RefDefault(Style style) {
108 return SkRef(GetDefaultTypeface(style));
reed@google.comfed86bd2013-03-14 15:04:57 +0000109}
110
reed@android.comef772dd2009-03-03 21:20:49 +0000111uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
reed@google.com538f7842011-12-16 17:56:23 +0000112 if (NULL == face) {
bungeman@google.comcb1bbb32012-10-12 18:48:35 +0000113 face = GetDefaultTypeface();
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000114 }
reed@google.com538f7842011-12-16 17:56:23 +0000115 return face->uniqueID();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116}
117
reed@android.comef772dd2009-03-03 21:20:49 +0000118bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000119 return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120}
121
122///////////////////////////////////////////////////////////////////////////////
123
reed@android.com069b8272009-03-04 15:31:48 +0000124SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) {
djsollen@google.com4fa748d2013-06-05 14:20:25 +0000125 if (NULL == name) {
126 return RefDefault(style);
127 }
reed@google.com0389d932012-05-07 21:06:55 +0000128 return SkFontHost::CreateTypeface(NULL, name, style);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129}
130
reed@android.comef772dd2009-03-03 21:20:49 +0000131SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) {
reed@google.com49690442013-03-04 14:39:47 +0000132 if (family && family->style() == s) {
133 family->ref();
134 return const_cast<SkTypeface*>(family);
135 }
reed@google.com0389d932012-05-07 21:06:55 +0000136 return SkFontHost::CreateTypeface(family, NULL, s);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137}
138
reed@android.comef772dd2009-03-03 21:20:49 +0000139SkTypeface* SkTypeface::CreateFromStream(SkStream* stream) {
reed@android.comb1d9d2e2009-03-04 17:37:51 +0000140 return SkFontHost::CreateTypefaceFromStream(stream);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141}
142
reed@android.comef772dd2009-03-03 21:20:49 +0000143SkTypeface* SkTypeface::CreateFromFile(const char path[]) {
reed@android.com03ca3d12008-12-22 15:35:46 +0000144 return SkFontHost::CreateTypefaceFromFile(path);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145}
146
147///////////////////////////////////////////////////////////////////////////////
148
reed@google.com5526ede2013-03-25 13:03:37 +0000149void SkTypeface::serialize(SkWStream* wstream) const {
150 bool isLocal = false;
151 SkFontDescriptor desc(this->style());
152 this->onGetFontDescriptor(&desc, &isLocal);
153
154 desc.serialize(wstream);
155 if (isLocal) {
156 int ttcIndex; // TODO: write this to the stream?
157 SkAutoTUnref<SkStream> rstream(this->openStream(&ttcIndex));
158 if (rstream.get()) {
159 size_t length = rstream->getLength();
160 wstream->writePackedUInt(length);
161 wstream->writeStream(rstream, length);
162 } else {
163 wstream->writePackedUInt(0);
164 }
165 } else {
166 wstream->writePackedUInt(0);
167 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000168}
169
170SkTypeface* SkTypeface::Deserialize(SkStream* stream) {
reed@google.com5526ede2013-03-25 13:03:37 +0000171 SkFontDescriptor desc(stream);
172 size_t length = stream->readPackedUInt();
173 if (length > 0) {
174 void* addr = sk_malloc_flags(length, 0);
175 if (addr) {
robertphillips@google.com9e9dfa32013-05-10 17:22:48 +0000176 SkAutoTUnref<SkMemoryStream> localStream(SkNEW(SkMemoryStream));
177 localStream->setMemoryOwned(addr, length);
178
bungeman@google.com2786d152013-04-09 19:29:45 +0000179 if (stream->read(addr, length) == length) {
180 return SkTypeface::CreateFromStream(localStream.get());
181 } else {
182 // Failed to read the full font data, so fall through and try to create from name.
183 // If this is because of EOF, all subsequent reads from the stream will be EOF.
184 // If this is because of a stream error, the stream is in an error state,
185 // do not attempt to skip any remaining bytes.
186 }
187 } else {
188 // failed to allocate, so just skip and create-from-name
robertphillips@google.com1eb16b92013-04-09 17:35:29 +0000189 stream->skip(length);
reed@google.com5526ede2013-03-25 13:03:37 +0000190 }
reed@google.com5526ede2013-03-25 13:03:37 +0000191 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000192
reed@google.com5526ede2013-03-25 13:03:37 +0000193 return SkTypeface::CreateFromName(desc.getFamilyName(), desc.getStyle());
vandebo@chromium.org2a22e102011-01-25 21:01:34 +0000194}
reed@google.comf11508d2012-04-17 18:01:31 +0000195
196///////////////////////////////////////////////////////////////////////////////
197
198int SkTypeface::countTables() const {
reed@google.com6c66d2f2013-03-21 20:34:27 +0000199 return this->onGetTableTags(NULL);
reed@google.comf11508d2012-04-17 18:01:31 +0000200}
201
202int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
reed@google.com6c66d2f2013-03-21 20:34:27 +0000203 return this->onGetTableTags(tags);
reed@google.comf11508d2012-04-17 18:01:31 +0000204}
205
206size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
reed@google.com6c66d2f2013-03-21 20:34:27 +0000207 return this->onGetTableData(tag, 0, ~0U, NULL);
reed@google.comf11508d2012-04-17 18:01:31 +0000208}
209
210size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
211 void* data) const {
reed@google.com6c66d2f2013-03-21 20:34:27 +0000212 return this->onGetTableData(tag, offset, length, data);
reed@google.comf11508d2012-04-17 18:01:31 +0000213}
214
reed@google.comfed86bd2013-03-14 15:04:57 +0000215SkStream* SkTypeface::openStream(int* ttcIndex) const {
reed@google.com2cdc6712013-03-21 18:22:00 +0000216 int ttcIndexStorage;
217 if (NULL == ttcIndex) {
218 // So our subclasses don't need to check for null param
219 ttcIndex = &ttcIndexStorage;
reed@google.comfed86bd2013-03-14 15:04:57 +0000220 }
reed@google.com2cdc6712013-03-21 18:22:00 +0000221 return this->onOpenStream(ttcIndex);
reed@google.comfed86bd2013-03-14 15:04:57 +0000222}
223
reed@google.combcb42ae2013-07-02 13:56:39 +0000224int SkTypeface::charsToGlyphs(const void* chars, Encoding encoding,
225 uint16_t glyphs[], int glyphCount) const {
226 if (glyphCount <= 0) {
227 return 0;
228 }
229 if (NULL == chars || (unsigned)encoding > kUTF32_Encoding) {
230 if (glyphs) {
231 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
232 }
233 return 0;
234 }
235 return this->onCharsToGlyphs(chars, encoding, glyphs, glyphCount);
236}
237
238int SkTypeface::countGlyphs() const {
239 return this->onCountGlyphs();
240}
241
reed@google.com4b2af9c2012-07-31 17:24:44 +0000242int SkTypeface::getUnitsPerEm() const {
reed@google.com38c37dd2013-03-21 15:36:26 +0000243 // should we try to cache this in the base-class?
244 return this->onGetUPEM();
reed@google.com4b2af9c2012-07-31 17:24:44 +0000245}
mike@reedtribe.orgdc09f072013-03-03 01:15:48 +0000246
reed@google.com35fe7372013-10-30 15:07:03 +0000247bool SkTypeface::getKerningPairAdjustments(const uint16_t glyphs[], int count,
248 int32_t adjustments[]) const {
249 SkASSERT(count >= 0);
250 // check for the only legal way to pass a NULL.. everything is 0
251 // in which case they just want to know if this face can possibly support
252 // kerning (true) or never (false).
253 if (NULL == glyphs || NULL == adjustments) {
254 SkASSERT(NULL == glyphs);
255 SkASSERT(0 == count);
256 SkASSERT(NULL == adjustments);
257 }
258 return this->onGetKerningPairAdjustments(glyphs, count, adjustments);
259}
260
bungeman@google.com839702b2013-08-07 17:09:22 +0000261SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const {
262 return this->onCreateFamilyNameIterator();
bungeman@google.coma9802692013-08-07 02:45:25 +0000263}
264
reed@google.com08df48d2013-07-29 18:54:08 +0000265void SkTypeface::getFamilyName(SkString* name) const {
266 bool isLocal = false;
267 SkFontDescriptor desc(this->style());
268 this->onGetFontDescriptor(&desc, &isLocal);
269 name->set(desc.getFamilyName());
270}
271
reed@google.com5526ede2013-03-25 13:03:37 +0000272SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics(
273 SkAdvancedTypefaceMetrics::PerGlyphInfo info,
274 const uint32_t* glyphIDs,
275 uint32_t glyphIDsCount) const {
vandebo@chromium.org5f209e62013-12-10 17:22:41 +0000276 return this->onGetAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
reed@google.com5526ede2013-03-25 13:03:37 +0000277}
mike@reedtribe.orgdc09f072013-03-03 01:15:48 +0000278
reed@google.com35fe7372013-10-30 15:07:03 +0000279///////////////////////////////////////////////////////////////////////////////
280
281bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
282 int32_t adjustments[]) const {
283 return false;
284}