SkTypeface.cpp revision 38c37ddbaf3b29cdacbc25d4aa2acca1869d276f
1
2/*
3 * Copyright 2011 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10#include "SkAdvancedTypefaceMetrics.h"
11#include "SkTypeface.h"
12#include "SkFontHost.h"
13
14SK_DEFINE_INST_COUNT(SkTypeface)
15
16//#define TRACE_LIFECYCLE
17
18#ifdef TRACE_LIFECYCLE
19    static int32_t gTypefaceCounter;
20#endif
21
22SkTypeface::SkTypeface(Style style, SkFontID fontID, bool isFixedWidth)
23    : fUniqueID(fontID), fStyle(style), fIsFixedWidth(isFixedWidth) {
24#ifdef TRACE_LIFECYCLE
25    SkDebugf("SkTypeface: create  %p fontID %d total %d\n",
26             this, fontID, ++gTypefaceCounter);
27#endif
28}
29
30SkTypeface::~SkTypeface() {
31#ifdef TRACE_LIFECYCLE
32    SkDebugf("SkTypeface: destroy %p fontID %d total %d\n",
33             this, fUniqueID, --gTypefaceCounter);
34#endif
35}
36
37///////////////////////////////////////////////////////////////////////////////
38
39SkTypeface* SkTypeface::GetDefaultTypeface() {
40    // we keep a reference to this guy for all time, since if we return its
41    // fontID, the font cache may later on ask to resolve that back into a
42    // typeface object.
43    static SkTypeface* gDefaultTypeface;
44
45    if (NULL == gDefaultTypeface) {
46        gDefaultTypeface =
47        SkFontHost::CreateTypeface(NULL, NULL, SkTypeface::kNormal);
48    }
49    return gDefaultTypeface;
50}
51
52SkTypeface* SkTypeface::RefDefault() {
53    return SkRef(GetDefaultTypeface());
54}
55
56uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
57    if (NULL == face) {
58        face = GetDefaultTypeface();
59    }
60    return face->uniqueID();
61}
62
63bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
64    return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
65}
66
67///////////////////////////////////////////////////////////////////////////////
68
69SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) {
70    return SkFontHost::CreateTypeface(NULL, name, style);
71}
72
73SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) {
74    if (family && family->style() == s) {
75        family->ref();
76        return const_cast<SkTypeface*>(family);
77    }
78    return SkFontHost::CreateTypeface(family, NULL, s);
79}
80
81SkTypeface* SkTypeface::CreateFromStream(SkStream* stream) {
82    return SkFontHost::CreateTypefaceFromStream(stream);
83}
84
85SkTypeface* SkTypeface::CreateFromFile(const char path[]) {
86    return SkFontHost::CreateTypefaceFromFile(path);
87}
88
89///////////////////////////////////////////////////////////////////////////////
90
91void SkTypeface::serialize(SkWStream* stream) const {
92    SkFontHost::Serialize(this, stream);
93}
94
95SkTypeface* SkTypeface::Deserialize(SkStream* stream) {
96    return SkFontHost::Deserialize(stream);
97}
98
99SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics(
100                                SkAdvancedTypefaceMetrics::PerGlyphInfo info,
101                                const uint32_t* glyphIDs,
102                                uint32_t glyphIDsCount) const {
103    return this->onGetAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount);
104}
105
106///////////////////////////////////////////////////////////////////////////////
107
108int SkTypeface::countTables() const {
109    return SkFontHost::CountTables(fUniqueID);
110}
111
112int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
113    return SkFontHost::GetTableTags(fUniqueID, tags);
114}
115
116size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
117    return SkFontHost::GetTableSize(fUniqueID, tag);
118}
119
120size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
121                                void* data) const {
122    return SkFontHost::GetTableData(fUniqueID, tag, offset, length, data);
123}
124
125SkStream* SkTypeface::openStream(int* ttcIndex) const {
126    if (ttcIndex) {
127        int32_t ndx = 0;
128        (void)SkFontHost::GetFileName(fUniqueID, NULL, 0, &ndx);
129        *ttcIndex = (int)ndx;
130    }
131    return SkFontHost::OpenStream(fUniqueID);
132}
133
134int SkTypeface::getUnitsPerEm() const {
135    // should we try to cache this in the base-class?
136    return this->onGetUPEM();
137}
138
139///////////////////////////////////////////////////////////////////////////////
140///////////////////////////////////////////////////////////////////////////////
141
142#include "SkFontDescriptor.h"
143
144int SkTypeface::onGetUPEM() const {
145    int upem = 0;
146
147    SkAdvancedTypefaceMetrics* metrics;
148    metrics = this->getAdvancedTypefaceMetrics(
149                             SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo,
150                             NULL, 0);
151    if (metrics) {
152        upem = metrics->fEmSize;
153        metrics->unref();
154    }
155    return upem;
156}
157
158int SkTypeface::onGetTableTags(SkFontTableTag tags[]) const { return 0; }
159size_t SkTypeface::onGetTableData(SkFontTableTag, size_t offset,
160                                  size_t length, void* data) const { return 0; }
161void SkTypeface::onGetFontDescriptor(SkFontDescriptor* desc) const {
162    desc->setStyle(this->style());
163}
164
165