1 // Copyright 2016 PDFium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 6 7 #include "core/fxge/cfx_fontcache.h" 8 9 #include <memory> 10 #include <utility> 11 12 #include "core/fxge/cfx_facecache.h" 13 #include "core/fxge/fx_font.h" 14 #include "core/fxge/fx_freetype.h" 15 #include "third_party/base/ptr_util.h" 16 CountedFaceCache()17CFX_FontCache::CountedFaceCache::CountedFaceCache() {} 18 ~CountedFaceCache()19CFX_FontCache::CountedFaceCache::~CountedFaceCache() {} 20 CFX_FontCache()21CFX_FontCache::CFX_FontCache() {} 22 ~CFX_FontCache()23CFX_FontCache::~CFX_FontCache() { 24 ASSERT(m_ExtFaceMap.empty()); 25 ASSERT(m_FTFaceMap.empty()); 26 } 27 GetCachedFace(const CFX_Font * pFont)28CFX_FaceCache* CFX_FontCache::GetCachedFace(const CFX_Font* pFont) { 29 FXFT_Face face = pFont->GetFace(); 30 const bool bExternal = !face; 31 CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap; 32 auto it = map.find(face); 33 if (it != map.end()) { 34 CountedFaceCache* counted_face_cache = it->second.get(); 35 counted_face_cache->m_nCount++; 36 return counted_face_cache->m_Obj.get(); 37 } 38 39 auto counted_face_cache = pdfium::MakeUnique<CountedFaceCache>(); 40 counted_face_cache->m_nCount = 2; 41 auto new_cache = 42 pdfium::MakeUnique<CFX_FaceCache>(bExternal ? nullptr : face); 43 CFX_FaceCache* face_cache = new_cache.get(); 44 counted_face_cache->m_Obj = std::move(new_cache); 45 map[face] = std::move(counted_face_cache); 46 return face_cache; 47 } 48 49 #ifdef _SKIA_SUPPORT_ GetDeviceCache(const CFX_Font * pFont)50CFX_TypeFace* CFX_FontCache::GetDeviceCache(const CFX_Font* pFont) { 51 return GetCachedFace(pFont)->GetDeviceCache(pFont); 52 } 53 #endif 54 ReleaseCachedFace(const CFX_Font * pFont)55void CFX_FontCache::ReleaseCachedFace(const CFX_Font* pFont) { 56 FXFT_Face face = pFont->GetFace(); 57 const bool bExternal = !face; 58 CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap; 59 60 auto it = map.find(face); 61 if (it == map.end()) 62 return; 63 64 CountedFaceCache* counted_face_cache = it->second.get(); 65 if (counted_face_cache->m_nCount > 2) { 66 counted_face_cache->m_nCount--; 67 } else { 68 map.erase(it); 69 } 70 } 71