Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / src / core / SkTypeface.cpp
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 "SkFontDescriptor.h"
10 #include "SkFontHost.h"
11 #include "SkOnce.h"
12 #include "SkStream.h"
13 #include "SkTypeface.h"
14
15 //#define TRACE_LIFECYCLE
16
17 #ifdef TRACE_LIFECYCLE
18     static int32_t gTypefaceCounter;
19 #endif
20
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);
26 #endif
27 }
28
29 SkTypeface::~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
38 class SkEmptyTypeface : public SkTypeface {
39 public:
40     static SkEmptyTypeface* Create() {
41         return SkNEW(SkEmptyTypeface);
42     }
43 protected:
44     SkEmptyTypeface() : SkTypeface(SkTypeface::kNormal, 0, true) { }
45
46     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 { }
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]));
59         }
60         return 0;
61     }
62     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     }
75 };
76
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
81 };
82 template <uintmax_t N> struct SkTIsPow2 {
83     static const bool value = (N & (N - 1)) == 0;
84 };
85 SK_COMPILE_ASSERT(SkTIsPow2<FONT_STYLE_COUNT>::value, FONT_STYLE_COUNT_not_power_of_2);
86
87 void 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     }
95 }
96
97 SkTypeface* SkTypeface::GetDefaultTypeface(Style style) {
98     SkASSERT((size_t)style < FONT_STYLE_COUNT);
99
100     // 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];
105 }
106
107 SkTypeface* SkTypeface::RefDefault(Style style) {
108     return SkRef(GetDefaultTypeface(style));
109 }
110
111 uint32_t SkTypeface::UniqueID(const SkTypeface* face) {
112     if (NULL == face) {
113         face = GetDefaultTypeface();
114     }
115     return face->uniqueID();
116 }
117
118 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) {
119     return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb);
120 }
121
122 ///////////////////////////////////////////////////////////////////////////////
123
124 SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) {
125     if (NULL == name) {
126         return RefDefault(style);
127     }
128     return SkFontHost::CreateTypeface(NULL, name, style);
129 }
130
131 SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) {
132     if (family && family->style() == s) {
133         family->ref();
134         return const_cast<SkTypeface*>(family);
135     }
136     return SkFontHost::CreateTypeface(family, NULL, s);
137 }
138
139 SkTypeface* SkTypeface::CreateFromStream(SkStream* stream) {
140     return SkFontHost::CreateTypefaceFromStream(stream);
141 }
142
143 SkTypeface* SkTypeface::CreateFromFile(const char path[]) {
144     return SkFontHost::CreateTypefaceFromFile(path);
145 }
146
147 ///////////////////////////////////////////////////////////////////////////////
148
149 void 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     }
168 }
169
170 SkTypeface* SkTypeface::Deserialize(SkStream* stream) {
171     SkFontDescriptor desc(stream);
172     size_t length = stream->readPackedUInt();
173     if (length > 0) {
174         void* addr = sk_malloc_flags(length, 0);
175         if (addr) {
176             SkAutoTUnref<SkMemoryStream> localStream(SkNEW(SkMemoryStream));
177             localStream->setMemoryOwned(addr, length);
178
179             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
189             stream->skip(length);
190         }
191     }
192
193     return SkTypeface::CreateFromName(desc.getFamilyName(), desc.getStyle());
194 }
195
196 ///////////////////////////////////////////////////////////////////////////////
197
198 int SkTypeface::countTables() const {
199     return this->onGetTableTags(NULL);
200 }
201
202 int SkTypeface::getTableTags(SkFontTableTag tags[]) const {
203     return this->onGetTableTags(tags);
204 }
205
206 size_t SkTypeface::getTableSize(SkFontTableTag tag) const {
207     return this->onGetTableData(tag, 0, ~0U, NULL);
208 }
209
210 size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length,
211                                 void* data) const {
212     return this->onGetTableData(tag, offset, length, data);
213 }
214
215 SkStream* SkTypeface::openStream(int* ttcIndex) const {
216     int ttcIndexStorage;
217     if (NULL == ttcIndex) {
218         // So our subclasses don't need to check for null param
219         ttcIndex = &ttcIndexStorage;
220     }
221     return this->onOpenStream(ttcIndex);
222 }
223
224 int 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
238 int SkTypeface::countGlyphs() const {
239     return this->onCountGlyphs();
240 }
241
242 int SkTypeface::getUnitsPerEm() const {
243     // should we try to cache this in the base-class?
244     return this->onGetUPEM();
245 }
246
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);
257     }
258     return this->onGetKerningPairAdjustments(glyphs, count, adjustments);
259 }
260
261 SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const {
262     return this->onCreateFamilyNameIterator();
263 }
264
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());
270 }
271
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);
277 }
278
279 ///////////////////////////////////////////////////////////////////////////////
280
281 bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count,
282                                              int32_t adjustments[]) const {
283     return false;
284 }