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 "SkFontHost.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 SkTypeface::LocalizedStrings* onCreateFamilyNameIterator() const SK_OVERRIDE { 71 return SkNEW(EmptyLocalizedStrings); 72 }; 73 virtual int onGetTableTags(SkFontTableTag tags[]) const SK_OVERRIDE { return 0; } 74 virtual size_t onGetTableData(SkFontTableTag, size_t, size_t, void*) const SK_OVERRIDE { 75 return 0; 76 } 77}; 78 79SkTypeface* SkTypeface::CreateDefault(int style) { 80 // If backed by fontconfig, it's not safe to call SkFontHost::CreateTypeface concurrently. 81 // To be safe, we serialize here with a mutex so only one call to 82 // CreateTypeface is happening at any given time. 83 // TODO(bungeman, mtklein): This is sad. Make our fontconfig code safe? 84 SK_DECLARE_STATIC_MUTEX(mutex); 85 SkAutoMutexAcquire lock(&mutex); 86 87 SkTypeface* t = SkFontHost::CreateTypeface(NULL, NULL, (Style)style); 88 return t ? t : SkEmptyTypeface::Create(); 89} 90 91void SkTypeface::DeleteDefault(SkTypeface* t) { 92 // The SkTypeface returned by SkFontHost::CreateTypeface may _itself_ be a 93 // cleverly-shared singleton. This is less than ideal. This means we 94 // cannot just assert our ownership and SkDELETE(t) like we'd want to. 95 SkSafeUnref(t); 96} 97 98SkTypeface* SkTypeface::GetDefaultTypeface(Style style) { 99 SK_DECLARE_STATIC_LAZY_PTR_ARRAY(SkTypeface, defaults, 4, CreateDefault, DeleteDefault); 100 101 SkASSERT((int)style < 4); 102 return defaults[style]; 103} 104 105SkTypeface* SkTypeface::RefDefault(Style style) { 106 return SkRef(GetDefaultTypeface(style)); 107} 108 109uint32_t SkTypeface::UniqueID(const SkTypeface* face) { 110 if (NULL == face) { 111 face = GetDefaultTypeface(); 112 } 113 return face->uniqueID(); 114} 115 116bool SkTypeface::Equal(const SkTypeface* facea, const SkTypeface* faceb) { 117 return SkTypeface::UniqueID(facea) == SkTypeface::UniqueID(faceb); 118} 119 120/////////////////////////////////////////////////////////////////////////////// 121 122SkTypeface* SkTypeface::CreateFromName(const char name[], Style style) { 123 if (NULL == name) { 124 return RefDefault(style); 125 } 126 return SkFontHost::CreateTypeface(NULL, name, style); 127} 128 129SkTypeface* SkTypeface::CreateFromTypeface(const SkTypeface* family, Style s) { 130 if (family && family->style() == s) { 131 family->ref(); 132 return const_cast<SkTypeface*>(family); 133 } 134 return SkFontHost::CreateTypeface(family, NULL, s); 135} 136 137SkTypeface* SkTypeface::CreateFromStream(SkStream* stream) { 138 return SkFontHost::CreateTypefaceFromStream(stream); 139} 140 141SkTypeface* SkTypeface::CreateFromFile(const char path[]) { 142 return SkFontHost::CreateTypefaceFromFile(path); 143} 144 145/////////////////////////////////////////////////////////////////////////////// 146 147void SkTypeface::serialize(SkWStream* wstream) const { 148 bool isLocal = false; 149 SkFontDescriptor desc(this->style()); 150 this->onGetFontDescriptor(&desc, &isLocal); 151 152 desc.serialize(wstream); 153 if (isLocal) { 154 int ttcIndex; // TODO: write this to the stream? 155 SkAutoTUnref<SkStream> rstream(this->openStream(&ttcIndex)); 156 if (rstream.get()) { 157 size_t length = rstream->getLength(); 158 wstream->writePackedUInt(length); 159 wstream->writeStream(rstream, length); 160 } else { 161 wstream->writePackedUInt(0); 162 } 163 } else { 164 wstream->writePackedUInt(0); 165 } 166} 167 168SkTypeface* SkTypeface::Deserialize(SkStream* stream) { 169 SkFontDescriptor desc(stream); 170 size_t length = stream->readPackedUInt(); 171 if (length > 0) { 172 void* addr = sk_malloc_flags(length, 0); 173 if (addr) { 174 SkAutoTUnref<SkMemoryStream> localStream(SkNEW(SkMemoryStream)); 175 localStream->setMemoryOwned(addr, length); 176 177 if (stream->read(addr, length) == length) { 178 return SkTypeface::CreateFromStream(localStream.get()); 179 } else { 180 // Failed to read the full font data, so fall through and try to create from name. 181 // If this is because of EOF, all subsequent reads from the stream will be EOF. 182 // If this is because of a stream error, the stream is in an error state, 183 // do not attempt to skip any remaining bytes. 184 } 185 } else { 186 // failed to allocate, so just skip and create-from-name 187 stream->skip(length); 188 } 189 } 190 191 return SkTypeface::CreateFromName(desc.getFamilyName(), desc.getStyle()); 192} 193 194/////////////////////////////////////////////////////////////////////////////// 195 196int SkTypeface::countTables() const { 197 return this->onGetTableTags(NULL); 198} 199 200int SkTypeface::getTableTags(SkFontTableTag tags[]) const { 201 return this->onGetTableTags(tags); 202} 203 204size_t SkTypeface::getTableSize(SkFontTableTag tag) const { 205 return this->onGetTableData(tag, 0, ~0U, NULL); 206} 207 208size_t SkTypeface::getTableData(SkFontTableTag tag, size_t offset, size_t length, 209 void* data) const { 210 return this->onGetTableData(tag, offset, length, data); 211} 212 213SkStream* SkTypeface::openStream(int* ttcIndex) const { 214 int ttcIndexStorage; 215 if (NULL == ttcIndex) { 216 // So our subclasses don't need to check for null param 217 ttcIndex = &ttcIndexStorage; 218 } 219 return this->onOpenStream(ttcIndex); 220} 221 222int SkTypeface::charsToGlyphs(const void* chars, Encoding encoding, 223 uint16_t glyphs[], int glyphCount) const { 224 if (glyphCount <= 0) { 225 return 0; 226 } 227 if (NULL == chars || (unsigned)encoding > kUTF32_Encoding) { 228 if (glyphs) { 229 sk_bzero(glyphs, glyphCount * sizeof(glyphs[0])); 230 } 231 return 0; 232 } 233 return this->onCharsToGlyphs(chars, encoding, glyphs, glyphCount); 234} 235 236int SkTypeface::countGlyphs() const { 237 return this->onCountGlyphs(); 238} 239 240int SkTypeface::getUnitsPerEm() const { 241 // should we try to cache this in the base-class? 242 return this->onGetUPEM(); 243} 244 245bool SkTypeface::getKerningPairAdjustments(const uint16_t glyphs[], int count, 246 int32_t adjustments[]) const { 247 SkASSERT(count >= 0); 248 // check for the only legal way to pass a NULL.. everything is 0 249 // in which case they just want to know if this face can possibly support 250 // kerning (true) or never (false). 251 if (NULL == glyphs || NULL == adjustments) { 252 SkASSERT(NULL == glyphs); 253 SkASSERT(0 == count); 254 SkASSERT(NULL == adjustments); 255 } 256 return this->onGetKerningPairAdjustments(glyphs, count, adjustments); 257} 258 259SkTypeface::LocalizedStrings* SkTypeface::createFamilyNameIterator() const { 260 return this->onCreateFamilyNameIterator(); 261} 262 263void SkTypeface::getFamilyName(SkString* name) const { 264 bool isLocal = false; 265 SkFontDescriptor desc(this->style()); 266 this->onGetFontDescriptor(&desc, &isLocal); 267 name->set(desc.getFamilyName()); 268} 269 270SkAdvancedTypefaceMetrics* SkTypeface::getAdvancedTypefaceMetrics( 271 SkAdvancedTypefaceMetrics::PerGlyphInfo info, 272 const uint32_t* glyphIDs, 273 uint32_t glyphIDsCount) const { 274 SkAdvancedTypefaceMetrics* result = 275 this->onGetAdvancedTypefaceMetrics(info, glyphIDs, glyphIDsCount); 276 if (result && result->fType == SkAdvancedTypefaceMetrics::kTrueType_Font) { 277 struct SkOTTableOS2 os2table; 278 if (this->getTableData(SkTEndian_SwapBE32(SkOTTableOS2::TAG), 0, 279 sizeof(os2table), &os2table) > 0) { 280 if (os2table.version.v2.fsType.field.Bitmap || 281 (os2table.version.v2.fsType.field.Restricted && 282 !(os2table.version.v2.fsType.field.PreviewPrint || 283 os2table.version.v2.fsType.field.Editable))) { 284 result->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>( 285 result->fFlags, 286 SkAdvancedTypefaceMetrics::kNotEmbeddable_FontFlag); 287 } 288 if (os2table.version.v2.fsType.field.NoSubsetting) { 289 result->fFlags = SkTBitOr<SkAdvancedTypefaceMetrics::FontFlags>( 290 result->fFlags, 291 SkAdvancedTypefaceMetrics::kNotSubsettable_FontFlag); 292 } 293 } 294 } 295 return result; 296} 297 298/////////////////////////////////////////////////////////////////////////////// 299 300bool SkTypeface::onGetKerningPairAdjustments(const uint16_t glyphs[], int count, 301 int32_t adjustments[]) const { 302 return false; 303} 304