OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 The Android Open Source Project | 2 * Copyright 2011 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkAdvancedTypefaceMetrics.h" | 8 #include "SkAdvancedTypefaceMetrics.h" |
9 #include "SkFontDescriptor.h" | 9 #include "SkFontDescriptor.h" |
10 #include "SkFontHost.h" | 10 #include "SkFontHost.h" |
11 #include "SkLazyPtr.h" | 11 #include "SkOnce.h" |
12 #include "SkStream.h" | 12 #include "SkStream.h" |
13 #include "SkTypeface.h" | 13 #include "SkTypeface.h" |
14 | 14 |
15 //#define TRACE_LIFECYCLE | 15 //#define TRACE_LIFECYCLE |
16 | 16 |
17 #ifdef TRACE_LIFECYCLE | 17 #ifdef TRACE_LIFECYCLE |
18 static int32_t gTypefaceCounter; | 18 static int32_t gTypefaceCounter; |
19 #endif | 19 #endif |
20 | 20 |
21 SkTypeface::SkTypeface(Style style, SkFontID fontID, bool isFixedPitch) | 21 SkTypeface::SkTypeface(Style style, SkFontID fontID, bool isFixedPitch) |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 }; | 67 }; |
68 virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_
OVERRIDE { | 68 virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_
OVERRIDE { |
69 return SkNEW(EmptyLocalizedStrings); | 69 return SkNEW(EmptyLocalizedStrings); |
70 }; | 70 }; |
71 virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE { return
0; } | 71 virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE { return
0; } |
72 virtual size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const S
K_OVERRIDE { | 72 virtual size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const S
K_OVERRIDE { |
73 return 0; | 73 return 0; |
74 } | 74 } |
75 }; | 75 }; |
76 | 76 |
77 SkTypeface* SkTypeface::CreateDefault(int style) { | 77 static SkTypeface* gDefaultTypefaces[] = { NULL, NULL, NULL, NULL }; |
78 SkTypeface* t = SkFontHost::CreateTypeface(NULL, NULL, (Style)style); | 78 static const size_t FONT_STYLE_COUNT = SK_ARRAY_COUNT(gDefaultTypefaces); |
79 return t ? t : SkEmptyTypeface::Create(); | 79 static SkOnceFlag gDefaultTypefaceOnce[FONT_STYLE_COUNT] = { |
80 } | 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); |
81 | 86 |
82 void SkTypeface::DeleteDefault(SkTypeface* t) { | 87 void SkTypeface::create_default_typeface(Style style) { |
83 // The SkTypeface returned by SkFontHost::CreateTypeface may _itself_ be a | 88 if (NULL == gDefaultTypefaces[style]) { |
84 // cleverly-shared singleton. This is less than ideal. This means we | 89 gDefaultTypefaces[style] = SkFontHost::CreateTypeface(NULL, NULL, style)
; |
85 // cannot just assert our ownership and SkDELETE(t) like we'd want to. | 90 } |
86 SkSafeUnref(t); | 91 if (NULL == gDefaultTypefaces[style]) { |
| 92 // FIXME: Use a singleton for SkEmptyTypeface. |
| 93 gDefaultTypefaces[style] = SkEmptyTypeface::Create(); |
| 94 } |
87 } | 95 } |
88 | 96 |
89 SkTypeface* SkTypeface::GetDefaultTypeface(Style style) { | 97 SkTypeface* SkTypeface::GetDefaultTypeface(Style style) { |
90 SK_DECLARE_STATIC_LAZY_PTR_ARRAY(SkTypeface, defaults, 4, CreateDefault, Del
eteDefault); | 98 SkASSERT((size_t)style < FONT_STYLE_COUNT); |
91 | 99 |
92 SkASSERT((int)style < 4); | 100 // mask off any other bits to avoid a crash in SK_RELEASE |
93 return defaults[style]; | 101 style = (Style)(style & (FONT_STYLE_COUNT - 1)); |
| 102 |
| 103 SkOnce(&gDefaultTypefaceOnce[style], SkTypeface::create_default_typeface, st
yle); |
| 104 return gDefaultTypefaces[style]; |
94 } | 105 } |
95 | 106 |
96 SkTypeface* SkTypeface::RefDefault(Style style) { | 107 SkTypeface* SkTypeface::RefDefault(Style style) { |
97 return SkRef(GetDefaultTypeface(style)); | 108 return SkRef(GetDefaultTypeface(style)); |
98 } | 109 } |
99 | 110 |
100 uint32_t SkTypeface::UniqueID(const SkTypeface* face) { | 111 uint32_t SkTypeface::UniqueID(const SkTypeface* face) { |
101 if (NULL == face) { | 112 if (NULL == face) { |
102 face = GetDefaultTypeface(); | 113 face = GetDefaultTypeface(); |
103 } | 114 } |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
264 uint32_t glyphIDsCount) const { | 275 uint32_t glyphIDsCount) const { |
265 return this->onGetAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount); | 276 return this->onGetAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount); |
266 } | 277 } |
267 | 278 |
268 /////////////////////////////////////////////////////////////////////////////// | 279 /////////////////////////////////////////////////////////////////////////////// |
269 | 280 |
270 bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count, | 281 bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count, |
271 int32_t adjustments[]) const { | 282 int32_t adjustments[]) const { |
272 return false; | 283 return false; |
273 } | 284 } |
OLD | NEW |