OLD | NEW |
(Empty) | |
| 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/include/cfx_fontcache.h" |
| 8 |
| 9 #include "core/fxge/include/fx_font.h" |
| 10 #include "core/fxge/include/fx_freetype.h" |
| 11 |
| 12 CFX_FontCache::CFX_FontCache() {} |
| 13 |
| 14 CFX_FontCache::~CFX_FontCache() { |
| 15 FreeCache(TRUE); |
| 16 } |
| 17 |
| 18 CFX_FaceCache* CFX_FontCache::GetCachedFace(CFX_Font* pFont) { |
| 19 FXFT_Face internal_face = pFont->GetFace(); |
| 20 const bool bExternal = !internal_face; |
| 21 FXFT_Face face = |
| 22 bExternal ? (FXFT_Face)pFont->GetSubstFont()->m_ExtHandle : internal_face; |
| 23 CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap; |
| 24 auto it = map.find(face); |
| 25 if (it != map.end()) { |
| 26 CFX_CountedFaceCache* counted_face_cache = it->second; |
| 27 counted_face_cache->m_nCount++; |
| 28 return counted_face_cache->m_Obj; |
| 29 } |
| 30 |
| 31 CFX_FaceCache* face_cache = new CFX_FaceCache(bExternal ? nullptr : face); |
| 32 CFX_CountedFaceCache* counted_face_cache = new CFX_CountedFaceCache; |
| 33 counted_face_cache->m_nCount = 2; |
| 34 counted_face_cache->m_Obj = face_cache; |
| 35 map[face] = counted_face_cache; |
| 36 return face_cache; |
| 37 } |
| 38 |
| 39 #ifdef _SKIA_SUPPORT_ |
| 40 CFX_TypeFace* CFX_FontCache::GetDeviceCache(CFX_Font* pFont) { |
| 41 return GetCachedFace(pFont)->GetDeviceCache(pFont); |
| 42 } |
| 43 |
| 44 CFX_TypeFace* CFX_FaceCache::GetDeviceCache(CFX_Font* pFont) { |
| 45 if (!m_pTypeface) { |
| 46 m_pTypeface = |
| 47 SkTypeface::MakeFromStream( |
| 48 new SkMemoryStream(pFont->GetFontData(), pFont->GetSize())) |
| 49 .release(); |
| 50 } |
| 51 return m_pTypeface; |
| 52 } |
| 53 #endif |
| 54 |
| 55 void CFX_FontCache::ReleaseCachedFace(CFX_Font* pFont) { |
| 56 FXFT_Face internal_face = pFont->GetFace(); |
| 57 const bool bExternal = !internal_face; |
| 58 FXFT_Face face = |
| 59 bExternal ? (FXFT_Face)pFont->GetSubstFont()->m_ExtHandle : internal_face; |
| 60 CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap; |
| 61 |
| 62 auto it = map.find(face); |
| 63 if (it == map.end()) |
| 64 return; |
| 65 |
| 66 CFX_CountedFaceCache* counted_face_cache = it->second; |
| 67 if (counted_face_cache->m_nCount > 1) { |
| 68 counted_face_cache->m_nCount--; |
| 69 } |
| 70 } |
| 71 |
| 72 void CFX_FontCache::FreeCache(FX_BOOL bRelease) { |
| 73 for (auto it = m_FTFaceMap.begin(); it != m_FTFaceMap.end();) { |
| 74 auto curr_it = it++; |
| 75 CFX_CountedFaceCache* cache = curr_it->second; |
| 76 if (bRelease || cache->m_nCount < 2) { |
| 77 delete cache->m_Obj; |
| 78 delete cache; |
| 79 m_FTFaceMap.erase(curr_it); |
| 80 } |
| 81 } |
| 82 |
| 83 for (auto it = m_ExtFaceMap.begin(); it != m_ExtFaceMap.end();) { |
| 84 auto curr_it = it++; |
| 85 CFX_CountedFaceCache* cache = curr_it->second; |
| 86 if (bRelease || cache->m_nCount < 2) { |
| 87 delete cache->m_Obj; |
| 88 delete cache; |
| 89 m_ExtFaceMap.erase(curr_it); |
| 90 } |
| 91 } |
| 92 } |
OLD | NEW |