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