epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2011 The Android Open Source Project |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | 325cb9a | 2011-03-30 18:36:29 +0000 | [diff] [blame] | 10 | #include "SkAdvancedTypefaceMetrics.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 11 | #include "SkTypeface.h" |
| 12 | #include "SkFontHost.h" |
| 13 | |
reed@google.com | fcb57b5 | 2012-06-26 17:55:30 +0000 | [diff] [blame] | 14 | SK_DEFINE_INST_COUNT(SkTypeface) |
| 15 | |
reed@google.com | e495905 | 2011-05-02 17:35:04 +0000 | [diff] [blame] | 16 | //#define TRACE_LIFECYCLE |
reed@google.com | 2f3dc9d | 2011-05-02 17:33:45 +0000 | [diff] [blame] | 17 | |
| 18 | #ifdef TRACE_LIFECYCLE |
| 19 | static int32_t gTypefaceCounter; |
| 20 | #endif |
| 21 | |
| 22 | SkTypeface::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 | |
| 30 | SkTypeface::~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 | |
reed@google.com | 538f784 | 2011-12-16 17:56:23 +0000 | [diff] [blame] | 39 | static SkTypeface* get_default_typeface() { |
| 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 = |
reed@google.com | 0389d93 | 2012-05-07 21:06:55 +0000 | [diff] [blame] | 47 | SkFontHost::CreateTypeface(NULL, NULL, SkTypeface::kNormal); |
reed@google.com | 538f784 | 2011-12-16 17:56:23 +0000 | [diff] [blame] | 48 | } |
| 49 | return gDefaultTypeface; |
| 50 | } |
| 51 | |
reed@android.com | ef772dd | 2009-03-03 21:20:49 +0000 | [diff] [blame] | 52 | uint32_t SkTypeface::UniqueID(const SkTypeface* face) { |
reed@google.com | 538f784 | 2011-12-16 17:56:23 +0000 | [diff] [blame] | 53 | if (NULL == face) { |
| 54 | face = get_default_typeface(); |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 55 | } |
reed@google.com | 538f784 | 2011-12-16 17:56:23 +0000 | [diff] [blame] | 56 | return face->uniqueID(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | } |
| 58 | |
reed@android.com | ef772dd | 2009-03-03 21:20:49 +0000 | [diff] [blame] | 59 | bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) { |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 60 | return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /////////////////////////////////////////////////////////////////////////////// |
| 64 | |
reed@android.com | 069b827 | 2009-03-04 15:31:48 +0000 | [diff] [blame] | 65 | SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) { |
reed@google.com | 0389d93 | 2012-05-07 21:06:55 +0000 | [diff] [blame] | 66 | return SkFontHost::CreateTypeface(NULL, name, style); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | } |
| 68 | |
reed@android.com | ef772dd | 2009-03-03 21:20:49 +0000 | [diff] [blame] | 69 | SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) { |
reed@google.com | 0389d93 | 2012-05-07 21:06:55 +0000 | [diff] [blame] | 70 | return SkFontHost::CreateTypeface(family, NULL, s); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | } |
| 72 | |
reed@android.com | ef772dd | 2009-03-03 21:20:49 +0000 | [diff] [blame] | 73 | SkTypeface* SkTypeface::CreateFromStream(SkStream* stream) { |
reed@android.com | b1d9d2e | 2009-03-04 17:37:51 +0000 | [diff] [blame] | 74 | return SkFontHost::CreateTypefaceFromStream(stream); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | } |
| 76 | |
reed@android.com | ef772dd | 2009-03-03 21:20:49 +0000 | [diff] [blame] | 77 | SkTypeface* SkTypeface::CreateFromFile(const char path[]) { |
reed@android.com | 03ca3d1 | 2008-12-22 15:35:46 +0000 | [diff] [blame] | 78 | return SkFontHost::CreateTypefaceFromFile(path); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | /////////////////////////////////////////////////////////////////////////////// |
| 82 | |
| 83 | void SkTypeface::serialize(SkWStream* stream) const { |
| 84 | SkFontHost::Serialize(this, stream); |
| 85 | } |
| 86 | |
| 87 | SkTypeface* SkTypeface::Deserialize(SkStream* stream) { |
reed@android.com | 62533ed | 2009-03-06 15:57:26 +0000 | [diff] [blame] | 88 | return SkFontHost::Deserialize(stream); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | } |
| 90 | |
vandebo@chromium.org | c48b2b3 | 2011-02-02 02:11:22 +0000 | [diff] [blame] | 91 | SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics( |
vandebo@chromium.org | 37ad8fb | 2011-08-18 02:38:50 +0000 | [diff] [blame] | 92 | SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo, |
| 93 | const uint32_t* glyphIDs, |
| 94 | uint32_t glyphIDsCount) const { |
| 95 | return SkFontHost::GetAdvancedTypefaceMetrics(fUniqueID, |
| 96 | perGlyphInfo, |
| 97 | glyphIDs, |
| 98 | glyphIDsCount); |
vandebo@chromium.org | 2a22e10 | 2011-01-25 21:01:34 +0000 | [diff] [blame] | 99 | } |
reed@google.com | f11508d | 2012-04-17 18:01:31 +0000 | [diff] [blame] | 100 | |
| 101 | /////////////////////////////////////////////////////////////////////////////// |
| 102 | |
| 103 | int SkTypeface::countTables() const { |
| 104 | return SkFontHost::CountTables(fUniqueID); |
| 105 | } |
| 106 | |
| 107 | int SkTypeface::getTableTags(SkFontTableTag tags[]) const { |
| 108 | return SkFontHost::GetTableTags(fUniqueID, tags); |
| 109 | } |
| 110 | |
| 111 | size_t SkTypeface::getTableSize(SkFontTableTag tag) const { |
| 112 | return SkFontHost::GetTableSize(fUniqueID, tag); |
| 113 | } |
| 114 | |
| 115 | size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length, |
| 116 | void* data) const { |
| 117 | return SkFontHost::GetTableData(fUniqueID, tag, offset, length, data); |
| 118 | } |
| 119 | |
reed@google.com | 4b2af9c | 2012-07-31 17:24:44 +0000 | [diff] [blame] | 120 | int SkTypeface::getUnitsPerEm() const { |
| 121 | int upem = 0; |
| 122 | |
| 123 | #ifdef SK_BUILD_FOR_ANDROID |
| 124 | upem = SkFontHost::GetUnitsPerEm(fUniqueID); |
| 125 | #else |
| 126 | SkAdvancedTypefaceMetrics* metrics; |
| 127 | metrics = SkFontHost::GetAdvancedTypefaceMetrics(fUniqueID, |
| 128 | SkAdvancedTypefaceMetrics::kNo_PerGlyphInfo, |
| 129 | NULL, 0); |
| 130 | if (metrics) { |
| 131 | upem = metrics->fEmSize; |
| 132 | metrics->unref(); |
| 133 | } |
| 134 | #endif |
| 135 | return upem; |
| 136 | } |
| 137 | |