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 "SkEndian.h" 10#include "SkFontDescriptor.h" 11#include "SkFontMgr.h" 12#include "SkLazyPtr.h" 13#include "SkOTTable_OS_2.h" 14#include "SkStream.h" 15#include "SkTypeface.h" 16 17//#define TRACE_LIFECYCLE 18 19#ifdef TRACE_LIFECYCLE 20 static int32_t gTypefaceCounter; 21#endif 22 23SkTypeface::SkTypeface(Style style, SkFontID fontID, bool isFixedPitch) 24 : fUniqueID(fontID), fStyle(style), fIsFixedPitch(isFixedPitch) { 25#ifdef TRACE_LIFECYCLE 26 SkDebugf("SkTypeface: create %p fontID %d total %d\n", 27 this, fontID, ++gTypefaceCounter); 28#endif 29} 30 31SkTypeface::~SkTypeface() { 32#ifdef TRACE_LIFECYCLE 33 SkDebugf("SkTypeface: destroy %p fontID %d total %d\n", 34 this, fUniqueID, --gTypefaceCounter); 35#endif 36} 37 38/////////////////////////////////////////////////////////////////////////////// 39 40class SkEmptyTypeface : public SkTypeface { 41public: 42 static SkEmptyTypeface* Create() { 43 return SkNEW(SkEmptyTypeface); 44 } 45protected: 46 SkEmptyTypeface() : SkTypeface(SkTypeface::kNormal, 0, true) { } 47 48 virtual SkStream* onOpenStream(int* ttcIndex) const SK_OVERRIDE { return NULL; } 49 virtual SkScalerContext* onCreateScalerContext(const SkDescriptor*) const SK_OVERRIDE { 50 return NULL; 51 } 52 virtual void onFilterRec(SkScalerContextRec*) const SK_OVERRIDE { } 53 virtual SkAdvancedTypefaceMetrics* onGetAdvancedTypefaceMetrics( 54 SkAdvancedTypefaceMetrics::PerGlyphInfo, 55 const uint32_t*, uint32_t) const SK_OVERRIDE { return NULL; } 56 virtual void onGetFontDescriptor(SkFontDescriptor*, bool*) const SK_OVERRIDE { } 57 virtual int onCharsToGlyphs(const void* chars, Encoding encoding, 58 uint16_t glyphs[], int glyphCount) const SK_OVERRIDE { 59 if (glyphs && glyphCount > 0) { 60 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0])); 61 } 62 return 0; 63 } 64 virtual int onCountGlyphs() const SK_OVERRIDE { return 0; }; 65 virtual int onGetUPEM() const SK_OVERRIDE { return 0; }; 66 class EmptyLocalizedStrings : public SkTypeface::LocalizedStrings { 67 public: 68 virtual bool next(SkTypeface::LocalizedString*) SK_OVERRIDE { return false; } 69 }; 70 virtual void onGetFamilyName(SkString* familyName) const SK_OVERRIDE { 71 familyName->reset(); 72 } 73 virtual SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE { 74 return SkNEW(EmptyLocalizedStrings); 75 }; 76 virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE { return 0; } 77 virtual size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const SK_OVERRIDE { 78 return 0; 79 } 80}; 81 82SK_DECLARE_STATIC_MUTEX(gCreateDefaultMutex); 83SkTypeface* SkTypeface::CreateDefault(int style) { 84 // If backed by fontconfig, it's not safe to call SkFontHost::CreateTypeface concurrently. 85 // To be safe, we serialize here with a mutex so only one call to 86 // CreateTypeface is happening at any given time. 87 // TODO(bungeman, mtklein): This is sad. Make our fontconfig code safe? 88 SkAutoMutexAcquire lock(&gCreateDefaultMutex); 89 90 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); 91 SkTypeface* t = fm->legacyCreateTypeface(NULL, style);; 92 return t ? t : SkEmptyTypeface::Create(); 93} 94 95void SkTypeface::DeleteDefault(SkTypeface* t) { 96 // The SkTypeface returned by SkFontHost::CreateTypeface may _itself_ be a 97 // cleverly-shared singleton. This is less than ideal. This means we 98 // cannot just assert our ownership and SkDELETE(t) like we'd want to. 99 SkSafeUnref(t); 100} 101 102SkTypeface* SkTypeface::GetDefaultTypeface(Style style) { 103 SK_DECLARE_STATIC_LAZY_PTR_ARRAY(SkTypeface, defaults, 4, CreateDefault, DeleteDefault); 104 105 SkASSERT((int)style < 4); 106 return defaults[style]; 107} 108 109SkTypeface* SkTypeface::RefDefault(Style style) { 110 return SkRef(GetDefaultTypeface(style)); 111} 112 113uint32_t SkTypeface::UniqueID(const SkTypeface* face) { 114 if (NULL == face) { 115 face = GetDefaultTypeface(); 116 } 117 return face->uniqueID(); 118} 119 120bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) { 121 return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb); 122} 123 124/////////////////////////////////////////////////////////////////////////////// 125 126SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) { 127 if (NULL == name) { 128 return RefDefault(style); 129 } 130 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); 131 return fm->legacyCreateTypeface(name, style); 132} 133 134SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) { 135 if (!family) { 136 return SkTypeface::RefDefault(s); 137 } 138 139 if (family->style() == s) { 140 family->ref(); 141 return const_cast<SkTypeface*>(family); 142 } 143 144 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); 145 bool bold = s & SkTypeface::kBold; 146 bool italic = s & SkTypeface::kItalic; 147 SkFontStyle newStyle = SkFontStyle(bold ? SkFontStyle::kBold_Weight 148 : SkFontStyle::kNormal_Weight, 149 SkFontStyle::kNormal_Width, 150 italic ? SkFontStyle::kItalic_Slant 151 : SkFontStyle::kUpright_Slant); 152 return fm->matchFaceStyle(family, newStyle); 153} 154 155SkTypeface* SkTypeface::CreateFromStream(SkStream* stream, int index) { 156 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); 157 return fm->createFromStream(stream, index); 158} 159 160SkTypeface* SkTypeface::CreateFromFile(const char path[], int index) { 161 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); 162 return fm->createFromFile(path, index); 163} 164 165/////////////////////////////////////////////////////////////////////////////// 166 167void SkTypeface::serialize(SkWStream* wstream) const { 168 bool isLocal = false; 169 SkFontDescriptor desc(this->style()); 170 this->onGetFontDescriptor(&desc, &isLocal); 171 172 if (isLocal && NULL == desc.getFontData()) { 173 int ttcIndex; 174 desc.setFontData(this->onOpenStream(&ttcIndex)); 175 desc.setFontIndex(ttcIndex); 176 } 177 178 desc.serialize(wstream); 179} 180 181SkTypeface* SkTypeface::Deserialize(SkStream* stream) { 182 SkFontDescriptor desc(stream); 183 SkStream* data = desc.getFontData(); 184 if (data) { 185 SkTypeface* typeface = SkTypeface::CreateFromStream(data, desc.getFontIndex()); 186 if (typeface) { 187 return typeface; 188 } 189 } 190 return SkTypeface::CreateFromName(desc.getFamilyName(), desc.getStyle()); 191} 192 193/////////////////////////////////////////////////////////////////////////////// 194 195int SkTypeface::countTables() const { 196 return this->onGetTableTags(NULL); 197} 198 199int SkTypeface::getTableTags(SkFontTableTag tags[]) const { 200 return this->onGetTableTags(tags); 201} 202 203size_t SkTypeface::getTableSize(SkFontTableTag tag) const { 204 return this->onGetTableData(tag, 0, ~0U, NULL); 205} 206 207size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length, 208 void* data) const { 209 return this->onGetTableData(tag, offset, length, data); 210} 211 212SkStream* SkTypeface::openStream(int* ttcIndex) const { 213 int ttcIndexStorage; 214 if (NULL == ttcIndex) { 215 // So our subclasses don't need to check for null param 216 ttcIndex = &ttcIndexStorage; 217 } 218 return this->onOpenStream(ttcIndex); 219} 220 221int SkTypeface::charsToGlyphs(const void* chars, Encoding encoding, 222 uint16_t glyphs[], int glyphCount) const { 223 if (glyphCount <= 0) { 224 return 0; 225 } 226 if (NULL == chars || (unsigned)encoding > kUTF32_Encoding) { 227 if (glyphs) { 228 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0])); 229 } 230 return 0; 231 } 232 return this->onCharsToGlyphs(chars, encoding, glyphs, glyphCount); 233} 234 235int SkTypeface::countGlyphs() const { 236 return this->onCountGlyphs(); 237} 238 239int SkTypeface::getUnitsPerEm() const { 240 // should we try to cache this in the base-class? 241 return this->onGetUPEM(); 242} 243 244bool SkTypeface::getKerningPairAdjustments(const uint16_t glyphs[], int count, 245 int32_t adjustments[]) const { 246 SkASSERT(count >= 0); 247 // check for the only legal way to pass a NULL.. everything is 0 248 // in which case they just want to know if this face can possibly support 249 // kerning (true) or never (false). 250 if (NULL == glyphs || NULL == adjustments) { 251 SkASSERT(NULL == glyphs); 252 SkASSERT(0 == count); 253 SkASSERT(NULL == adjustments); 254 } 255 return this->onGetKerningPairAdjustments(glyphs, count, adjustments); 256} 257 258SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const { 259 return this->onCreateFamilyNameIterator(); 260} 261 262void SkTypeface::getFamilyName(SkString* name) const { 263 SkASSERT(name); 264 this->onGetFamilyName(name); 265} 266 267SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics( 268 SkAdvancedTypefaceMetrics::PerGlyphInfo info, 269 const uint32_t* glyphIDs, 270 uint32_t glyphIDsCount) const { 271 SkAdvancedTypefaceMetrics* result = 272 this->onGetAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount); 273 if (result && result->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { 274 struct SkOTTableOS2 os2table; 275 if (this->getTableData(SkTEndian_SwapBE32(SkOTTableOS2::TAG), 0, 276 sizeof(os2table), &os2table) > 0) { 277 if (os2table.version.v2.fsType.field.Bitmap || 278 (os2table.version.v2.fsType.field.Restricted && 279 !(os2table.version.v2.fsType.field.PreviewPrint || 280 os2table.version.v2.fsType.field.Editable))) { 281 result->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>( 282 result->fFlags, 283 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag); 284 } 285 if (os2table.version.v2.fsType.field.NoSubsetting) { 286 result->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>( 287 result->fFlags, 288 SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag); 289 } 290 } 291 } 292 return result; 293} 294 295/////////////////////////////////////////////////////////////////////////////// 296 297bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count, 298 int32_t adjustments[]) const { 299 return false; 300} 301