1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "SkAdvancedTypefaceMetrics.h" 18 #include "SkTypeface.h" 19 #include "SkFontHost.h" 20 21 //#define TRACE_LIFECYCLE 22 23 #ifdef TRACE_LIFECYCLE 24 static int32_t gTypefaceCounter; 25 #endif 26 27 SkTypeface::SkTypeface(Style style, SkFontID fontID, bool isFixedWidth) 28 : fUniqueID(fontID), fStyle(style), fIsFixedWidth(isFixedWidth) { 29 #ifdef TRACE_LIFECYCLE 30 SkDebugf("SkTypeface: create %p fontID %d total %d\n", 31 this, fontID, ++gTypefaceCounter); 32 #endif 33 } 34 35 SkTypeface::~SkTypeface() { 36 #ifdef TRACE_LIFECYCLE 37 SkDebugf("SkTypeface: destroy %p fontID %d total %d\n", 38 this, fUniqueID, --gTypefaceCounter); 39 #endif 40 } 41 42 /////////////////////////////////////////////////////////////////////////////// 43 44 uint32_t SkTypeface::UniqueID(const SkTypeface* face) { 45 if (face) { 46 return face->uniqueID(); 47 } 48 49 // We cache the default fontID, assuming it will not change during a boot 50 // The initial value of 0 is fine, since a typeface's uniqueID should not 51 // be zero. 52 static uint32_t gDefaultFontID; 53 54 if (0 == gDefaultFontID) { 55 SkTypeface* defaultFace = 56 SkFontHost::CreateTypeface(NULL, NULL, NULL, 0, 57 SkTypeface::kNormal); 58 SkASSERT(defaultFace); 59 gDefaultFontID = defaultFace->uniqueID(); 60 defaultFace->unref(); 61 } 62 return gDefaultFontID; 63 } 64 65 bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) { 66 return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb); 67 } 68 69 /////////////////////////////////////////////////////////////////////////////// 70 71 SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) { 72 return SkFontHost::CreateTypeface(NULL, name, NULL, 0, style); 73 } 74 75 SkTypeface* SkTypeface::CreateForChars(const void* data, size_t bytelength, 76 Style s) { 77 return SkFontHost::CreateTypeface(NULL, NULL, data, bytelength, s); 78 } 79 80 SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) { 81 return SkFontHost::CreateTypeface(family, NULL, NULL, 0, s); 82 } 83 84 SkTypeface* SkTypeface::CreateFromStream(SkStream* stream) { 85 return SkFontHost::CreateTypefaceFromStream(stream); 86 } 87 88 SkTypeface* SkTypeface::CreateFromFile(const char path[]) { 89 return SkFontHost::CreateTypefaceFromFile(path); 90 } 91 92 /////////////////////////////////////////////////////////////////////////////// 93 94 void SkTypeface::serialize(SkWStream* stream) const { 95 SkFontHost::Serialize(this, stream); 96 } 97 98 SkTypeface* SkTypeface::Deserialize(SkStream* stream) { 99 return SkFontHost::Deserialize(stream); 100 } 101 102 SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics( 103 SkAdvancedTypefaceMetrics::PerGlyphInfo perGlyphInfo) const { 104 return SkFontHost::GetAdvancedTypefaceMetrics(fUniqueID, perGlyphInfo); 105 } 106