SkTypeface.cpp revision d71b75757335393d9643a5b7a0f2769b6ba52fb6
1/*
2 * Copyright 2011 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkAdvancedTypefaceMetrics.h"
9#include "SkEndian.h"
10#include "SkFontDescriptor.h"
11#include "SkFontMgr.h"
12#include "SkLazyPtr.h"
13#include "SkOTTable_OS_2.h"
14#include "SkStream.h"
15#include "SkTypeface.h"
16
17//#define TRACE_LIFECYCLE
18
19#ifdef TRACE_LIFECYCLE
20    static int32_t gTypefaceCounter;
21#endif
22
23SkTypeface::SkTypeface(Style style, SkFontID fontID, bool isFixedPitch)
24    : fUniqueID(fontID), fStyle(style), fIsFixedPitch(isFixedPitch) {
25#ifdef TRACE_LIFECYCLE
26    SkDebugf("SkTypeface: create  %p fontID %d total %d\n",
27             this, fontID, ++gTypefaceCounter);
28#endif
29}
30
31SkTypeface::~SkTypeface() {
32#ifdef TRACE_LIFECYCLE
33    SkDebugf("SkTypeface: destroy %p fontID %d total %d\n",
34             this, fUniqueID, --gTypefaceCounter);
35#endif
36}
37
38///////////////////////////////////////////////////////////////////////////////
39
40class SkEmptyTypeface : public SkTypeface {
41public:
42    static SkEmptyTypeface* Create() {
43        return SkNEW(SkEmptyTypeface);
44    }
45protected:
46    SkEmptyTypeface() : SkTypeface(SkTypeface::kNormal, 0, true) { }
47
48    virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { return NULL; }
49    virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK_OVERRIDE {
50        return NULL;
51    }
52    virtual void onFilterRec(SkScalerContextRec*) const SK_OVERRIDE { }
53    virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics(
54                                SkAdvancedTypefaceMetrics::PerGlyphInfo,
55                                const uint32_t*, uint32_t) const SK_OVERRIDE { return NULL; }
56    virtual void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE { }
57    virtual int onCharsToGlyphs(const void* chars, Encoding encoding,
58                                uint16_t glyphs[], int glyphCount) const SK_OVERRIDE {
59        if (glyphs && glyphCount > 0) {
60            sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
61        }
62        return 0;
63    }
64    virtual int onCountGlyphs() const SK_OVERRIDE { return 0; };
65    virtual int onGetUPEM() const SK_OVERRIDE { return 0; };
66    class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings {
67    public:
68        virtual bool next(SkTypeface::LocalizedString*) SK_OVERRIDE { return false; }
69    };
70    virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE {
71        familyName->reset();
72    }
73    virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE {
74        return SkNEW(EmptyLocalizedStrings);
75    };
76    virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE { return 0; }
77    virtual size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const SK_OVERRIDE {
78        return 0;
79    }
80};
81
82SK_DECLARE_STATIC_MUTEX(gCreateDefaultMutex);
83SkTypeface* SkTypeface::CreateDefault(int style) {
84    // If backed by fontconfig, it's not safe to call SkFontHost::CreateTypeface concurrently.
85    // To be safe, we serialize here with a mutex so only one call to
86    // CreateTypeface is happening at any given time.
87    // TODO(bungeman, mtklein): This is sad.  Make our fontconfig code safe?
88    SkAutoMutexAcquire lock(&gCreateDefaultMutex);
89
90    SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
91    SkTypeface* t = fm->legacyCreateTypeface(NULL, style);;
92    return t ? t : SkEmptyTypeface::Create();
93}
94
95void SkTypeface::DeleteDefault(SkTypeface* t) {
96    // The SkTypeface returned by SkFontHost::CreateTypeface may _itself_ be a
97    // cleverly-shared singleton.  This is less than ideal.  This means we
98    // cannot just assert our ownership and SkDELETE(t) like we'd want to.
99    SkSafeUnref(t);
100}
101
102SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
103    SK_DECLARE_STATIC_LAZY_PTR_ARRAY(SkTypeface, defaults, 4, CreateDefault, DeleteDefault);
104
105    SkASSERT((int)style < 4);
106    return defaults[style];
107}
108
109SkTypeface* SkTypeface::RefDefault(Style style) {
110    return SkRef(GetDefaultTypeface(style));
111}
112
113uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
114    if (NULL == face) {
115        face = GetDefaultTypeface();
116    }
117    return face->uniqueID();
118}
119
120bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
121    return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
122}
123
124///////////////////////////////////////////////////////////////////////////////
125
126SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) {
127    if (NULL == name) {
128        return RefDefault(style);
129    }
130    SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
131    return fm->legacyCreateTypeface(name, style);
132}
133
134SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) {
135    if (family && family->style() == s) {
136        family->ref();
137        return const_cast<SkTypeface*>(family);
138    }
139    SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
140    bool bold = s & SkTypeface::kBold;
141    bool italic = s & SkTypeface::kItalic;
142    SkFontStyle newStyle = SkFontStyle(bold ? SkFontStyle::kBold_Weight
143                                            : SkFontStyle::kNormal_Weight,
144                                       SkFontStyle::kNormal_Width,
145                                       italic ? SkFontStyle::kItalic_Slant
146                                                : SkFontStyle::kUpright_Slant);
147    return fm->matchFaceStyle(family, newStyle);
148}
149
150SkTypeface* SkTypeface::CreateFromStream(SkStream* stream, int index) {
151    SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
152    return fm->createFromStream(stream, index);
153}
154
155SkTypeface* SkTypeface::CreateFromFile(const char path[], int index) {
156    SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
157    return fm->createFromFile(path, index);
158}
159
160///////////////////////////////////////////////////////////////////////////////
161
162void SkTypeface::serialize(SkWStream* wstream) const {
163    bool isLocal = false;
164    SkFontDescriptor desc(this->style());
165    this->onGetFontDescriptor(&desc, &isLocal);
166
167    if (isLocal && NULL == desc.getFontData()) {
168        int ttcIndex;
169        desc.setFontData(this->onOpenStream(&ttcIndex));
170        desc.setFontIndex(ttcIndex);
171    }
172
173    desc.serialize(wstream);
174}
175
176SkTypeface* SkTypeface::Deserialize(SkStream* stream) {
177    SkFontDescriptor desc(stream);
178    SkStream* data = desc.getFontData();
179    if (data) {
180        SkTypeface* typeface = SkTypeface::CreateFromStream(data, desc.getFontIndex());
181        if (typeface) {
182            return typeface;
183        }
184    }
185    return SkTypeface::CreateFromName(desc.getFamilyName(), desc.getStyle());
186}
187
188///////////////////////////////////////////////////////////////////////////////
189
190int SkTypeface::countTables() const {
191    return this->onGetTableTags(NULL);
192}
193
194int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
195    return this->onGetTableTags(tags);
196}
197
198size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
199    return this->onGetTableData(tag, 0, ~0U, NULL);
200}
201
202size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
203                                void* data) const {
204    return this->onGetTableData(tag, offset, length, data);
205}
206
207SkStream* SkTypeface::openStream(int* ttcIndex) const {
208    int ttcIndexStorage;
209    if (NULL == ttcIndex) {
210        // So our subclasses don't need to check for null param
211        ttcIndex = &ttcIndexStorage;
212    }
213    return this->onOpenStream(ttcIndex);
214}
215
216int SkTypeface::charsToGlyphs(const void* chars, Encoding encoding,
217                              uint16_t glyphs[], int glyphCount) const {
218    if (glyphCount <= 0) {
219        return 0;
220    }
221    if (NULL == chars || (unsigned)encoding > kUTF32_Encoding) {
222        if (glyphs) {
223            sk_bzero(glyphs, glyphCount * sizeof(glyphs[0]));
224        }
225        return 0;
226    }
227    return this->onCharsToGlyphs(chars, encoding, glyphs, glyphCount);
228}
229
230int SkTypeface::countGlyphs() const {
231    return this->onCountGlyphs();
232}
233
234int SkTypeface::getUnitsPerEm() const {
235    // should we try to cache this in the base-class?
236    return this->onGetUPEM();
237}
238
239bool SkTypeface::getKerningPairAdjustments(const uint16_t glyphs[], int count,
240                                           int32_t adjustments[]) const {
241    SkASSERT(count >= 0);
242    // check for the only legal way to pass a NULL.. everything is 0
243    // in which case they just want to know if this face can possibly support
244    // kerning (true) or never (false).
245    if (NULL == glyphs || NULL == adjustments) {
246        SkASSERT(NULL == glyphs);
247        SkASSERT(0 == count);
248        SkASSERT(NULL == adjustments);
249    }
250    return this->onGetKerningPairAdjustments(glyphs, count, adjustments);
251}
252
253SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const {
254    return this->onCreateFamilyNameIterator();
255}
256
257void SkTypeface::getFamilyName(SkString* name) const {
258    SkASSERT(name);
259    this->onGetFamilyName(name);
260}
261
262SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics(
263                                SkAdvancedTypefaceMetrics::PerGlyphInfo info,
264                                const uint32_t* glyphIDs,
265                                uint32_t glyphIDsCount) const {
266    SkAdvancedTypefaceMetrics* result =
267            this->onGetAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
268    if (result && result->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) {
269        struct SkOTTableOS2 os2table;
270        if (this->getTableData(SkTEndian_SwapBE32(SkOTTableOS2::TAG), 0,
271                               sizeof(os2table), &os2table) > 0) {
272            if (os2table.version.v2.fsType.field.Bitmap ||
273                (os2table.version.v2.fsType.field.Restricted &&
274                 !(os2table.version.v2.fsType.field.PreviewPrint ||
275                   os2table.version.v2.fsType.field.Editable))) {
276                result->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
277                        result->fFlags,
278                        SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag);
279            }
280            if (os2table.version.v2.fsType.field.NoSubsetting) {
281                result->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>(
282                        result->fFlags,
283                        SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag);
284            }
285        }
286    }
287    return result;
288}
289
290///////////////////////////////////////////////////////////////////////////////
291
292bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
293                                             int32_t adjustments[]) const {
294    return false;
295}
296