virtual ~FontConfigTypeface() { const uint32_t id = uniqueID(); if (IsRemoteFont(UniqueIdToFileFaceId(id))) { SkAutoMutexAcquire ac(global_remote_font_map_lock); AllocateGlobalRemoteFontsMapOnce(); std::map<uint32_t, std::pair<uint8_t*, size_t> >::iterator iter = global_remote_fonts->find(id); if (iter != global_remote_fonts->end()) { sk_free(iter->second.first); // remove the font on memory. global_remote_fonts->erase(iter); } } }
// static SkTypeface* SkFontHost::CreateTypefaceFromStream(SkStream* stream) { if (!stream) return NULL; const size_t length = stream->read(0, 0); if (!length) return NULL; if (length >= 1024 * 1024 * 1024) return NULL; // don't accept too large fonts (>= 1GB) for safety. uint8_t* font = (uint8_t*)sk_malloc_throw(length); if (stream->read(font, length) != length) { sk_free(font); return NULL; } SkTypeface::Style style = static_cast<SkTypeface::Style>(0); unsigned id = 0; { SkAutoMutexAcquire ac(global_remote_font_map_lock); AllocateGlobalRemoteFontsMapOnce(); id = FileFaceIdAndStyleToUniqueId( global_next_remote_font_id | kRemoteFontMask, style); if (++global_next_remote_font_id >= kRemoteFontMask) global_next_remote_font_id = 0; if (!global_remote_fonts->insert( std::make_pair(id, std::make_pair(font, length))).second) { sk_free(font); return NULL; } } SkTypeface* typeface = SkNEW_ARGS(FontConfigTypeface, (style, id)); return typeface; }
// static SkStream* SkFontHost::OpenStream(uint32_t id) { const unsigned filefaceid = UniqueIdToFileFaceId(id); if (IsRemoteFont(filefaceid)) { // remote font SkAutoMutexAcquire ac(global_remote_font_map_lock); AllocateGlobalRemoteFontsMapOnce(); std::map<uint32_t, std::pair<uint8_t*, size_t> >::const_iterator iter = global_remote_fonts->find(id); if (iter == global_remote_fonts->end()) return NULL; return SkNEW_ARGS( SkMemoryStream, (iter->second.first, iter->second.second)); } // system font const int fd = GetFcImpl()->Open(filefaceid); if (fd < 0) return NULL; return SkNEW_ARGS(SkFileDescriptorStream, (fd)); }