2 * Copyright 2011 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkAdvancedTypefaceMetrics.h"
9 #include "SkFontDescriptor.h"
10 #include "SkFontHost.h"
13 #include "SkTypeface.h"
15 //#define TRACE_LIFECYCLE
17 #ifdef TRACE_LIFECYCLE
18 static int32_t gTypefaceCounter;
21 SkTypeface::SkTypeface(Style style, SkFontID fontID, bool isFixedPitch)
22 : fUniqueID(fontID), fStyle(style), fIsFixedPitch(isFixedPitch) {
23 #ifdef TRACE_LIFECYCLE
24 SkDebugf("SkTypeface: create %p fontID %d total %d\n",
25 this, fontID, ++gTypefaceCounter);
29 SkTypeface::~SkTypeface() {
30 #ifdef TRACE_LIFECYCLE
31 SkDebugf("SkTypeface: destroy %p fontID %d total %d\n",
32 this, fUniqueID, --gTypefaceCounter);
36 ///////////////////////////////////////////////////////////////////////////////
38 class SkEmptyTypeface : public SkTypeface {
40 static SkEmptyTypeface* Create() {
41 return SkNEW(SkEmptyTypeface);
44 SkEmptyTypeface() : SkTypeface(SkTypeface::kNormal, 0, true) { }
46 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { return NULL; }
47 virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK_OVERRIDE {
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 { }
55 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]));
62 virtual int onCountGlyphs() const SK_OVERRIDE { return 0; };
63 virtual int onGetUPEM() const SK_OVERRIDE { return 0; };
64 class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings {
66 virtual bool next(SkTypeface::LocalizedString*) SK_OVERRIDE { return false; }
68 virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE {
69 return SkNEW(EmptyLocalizedStrings);
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 {
77 static SkTypeface* gDefaultTypefaces[] = { NULL, NULL, NULL, NULL };
78 static const size_t FONT_STYLE_COUNT = SK_ARRAY_COUNT(gDefaultTypefaces);
79 static SkOnceFlag gDefaultTypefaceOnce[FONT_STYLE_COUNT] = {
80 SK_ONCE_INIT, SK_ONCE_INIT, SK_ONCE_INIT, SK_ONCE_INIT
82 template <uintmax_t N> struct SkTIsPow2 {
83 static const bool value = (N & (N - 1)) == 0;
85 SK_COMPILE_ASSERT(SkTIsPow2<FONT_STYLE_COUNT>::value, FONT_STYLE_COUNT_not_power_of_2);
87 void SkTypeface::create_default_typeface(Style style) {
88 if (NULL == gDefaultTypefaces[style]) {
89 gDefaultTypefaces[style] = SkFontHost::CreateTypeface(NULL, NULL, style);
91 if (NULL == gDefaultTypefaces[style]) {
92 // FIXME: Use a singleton for SkEmptyTypeface.
93 gDefaultTypefaces[style] = SkEmptyTypeface::Create();
97 SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
98 SkASSERT((size_t)style < FONT_STYLE_COUNT);
100 // mask off any other bits to avoid a crash in SK_RELEASE
101 style = (Style)(style & (FONT_STYLE_COUNT - 1));
103 SkOnce(&gDefaultTypefaceOnce[style], SkTypeface::create_default_typeface, style);
104 return gDefaultTypefaces[style];
107 SkTypeface* SkTypeface::RefDefault(Style style) {
108 return SkRef(GetDefaultTypeface(style));
111 uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
113 face = GetDefaultTypeface();
115 return face->uniqueID();
118 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
119 return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
122 ///////////////////////////////////////////////////////////////////////////////
124 SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) {
126 return RefDefault(style);
128 return SkFontHost::CreateTypeface(NULL, name, style);
131 SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) {
132 if (family && family->style() == s) {
134 return const_cast<SkTypeface*>(family);
136 return SkFontHost::CreateTypeface(family, NULL, s);
139 SkTypeface* SkTypeface::CreateFromStream(SkStream* stream) {
140 return SkFontHost::CreateTypefaceFromStream(stream);
143 SkTypeface* SkTypeface::CreateFromFile(const char path[]) {
144 return SkFontHost::CreateTypefaceFromFile(path);
147 ///////////////////////////////////////////////////////////////////////////////
149 void SkTypeface::serialize(SkWStream* wstream) const {
150 bool isLocal = false;
151 SkFontDescriptor desc(this->style());
152 this->onGetFontDescriptor(&desc, &isLocal);
154 desc.serialize(wstream);
156 int ttcIndex; // TODO: write this to the stream?
157 SkAutoTUnref<SkStream> rstream(this->openStream(&ttcIndex));
159 size_t length = rstream->getLength();
160 wstream->writePackedUInt(length);
161 wstream->writeStream(rstream, length);
163 wstream->writePackedUInt(0);
166 wstream->writePackedUInt(0);
170 SkTypeface* SkTypeface::Deserialize(SkStream* stream) {
171 SkFontDescriptor desc(stream);
172 size_t length = stream->readPackedUInt();
174 void* addr = sk_malloc_flags(length, 0);
176 SkAutoTUnref<SkMemoryStream> localStream(SkNEW(SkMemoryStream));
177 localStream->setMemoryOwned(addr, length);
179 if (stream->read(addr, length) == length) {
180 return SkTypeface::CreateFromStream(localStream.get());
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.
188 // failed to allocate, so just skip and create-from-name
189 stream->skip(length);
193 return SkTypeface::CreateFromName(desc.getFamilyName(), desc.getStyle());
196 ///////////////////////////////////////////////////////////////////////////////
198 int SkTypeface::countTables() const {
199 return this->onGetTableTags(NULL);
202 int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
203 return this->onGetTableTags(tags);
206 size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
207 return this->onGetTableData(tag, 0, ~0U, NULL);
210 size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
212 return this->onGetTableData(tag, offset, length, data);
215 SkStream* SkTypeface::openStream(int* ttcIndex) const {
217 if (NULL == ttcIndex) {
218 // So our subclasses don't need to check for null param
219 ttcIndex = &ttcIndexStorage;
221 return this->onOpenStream(ttcIndex);
224 int SkTypeface::charsToGlyphs(const void* chars, Encoding encoding,
225 uint16_t glyphs[], int glyphCount) const {
226 if (glyphCount <= 0) {
229 if (NULL == chars || (unsigned)encoding > kUTF32_Encoding) {
231 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
235 return this->onCharsToGlyphs(chars, encoding, glyphs, glyphCount);
238 int SkTypeface::countGlyphs() const {
239 return this->onCountGlyphs();
242 int SkTypeface::getUnitsPerEm() const {
243 // should we try to cache this in the base-class?
244 return this->onGetUPEM();
247 bool 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);
258 return this->onGetKerningPairAdjustments(glyphs, count, adjustments);
261 SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const {
262 return this->onCreateFamilyNameIterator();
265 void SkTypeface::getFamilyName(SkString* name) const {
266 bool isLocal = false;
267 SkFontDescriptor desc(this->style());
268 this->onGetFontDescriptor(&desc, &isLocal);
269 name->set(desc.getFamilyName());
272 SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics(
273 SkAdvancedTypefaceMetrics::PerGlyphInfo info,
274 const uint32_t* glyphIDs,
275 uint32_t glyphIDsCount) const {
276 return this->onGetAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
279 ///////////////////////////////////////////////////////////////////////////////
281 bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
282 int32_t adjustments[]) const {