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::CountedFaceCache::CountedFaceCache() {} | |
14 | |
15 CFX_FontCache::CountedFaceCache::~CountedFaceCache() {} | |
16 | |
17 CFX_FontCache::CFX_FontCache() {} | 13 CFX_FontCache::CFX_FontCache() {} |
18 | 14 |
19 CFX_FontCache::~CFX_FontCache() { | 15 CFX_FontCache::~CFX_FontCache() { |
20 ASSERT(m_ExtFaceMap.empty()); | 16 FreeCache(TRUE); |
21 ASSERT(m_FTFaceMap.empty()); | |
22 } | 17 } |
23 | 18 |
24 CFX_FaceCache* CFX_FontCache::GetCachedFace(const CFX_Font* pFont) { | 19 CFX_FaceCache* CFX_FontCache::GetCachedFace(CFX_Font* pFont) { |
25 FXFT_Face face = pFont->GetFace(); | 20 FXFT_Face face = pFont->GetFace(); |
26 const bool bExternal = !face; | 21 const bool bExternal = !face; |
27 CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap; | 22 CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap; |
28 auto it = map.find(face); | 23 auto it = map.find(face); |
29 if (it != map.end()) { | 24 if (it != map.end()) { |
30 CountedFaceCache* counted_face_cache = it->second.get(); | 25 CFX_CountedFaceCache* counted_face_cache = it->second; |
31 counted_face_cache->m_nCount++; | 26 counted_face_cache->m_nCount++; |
32 return counted_face_cache->m_Obj.get(); | 27 return counted_face_cache->m_Obj; |
33 } | 28 } |
34 | 29 |
35 std::unique_ptr<CountedFaceCache> counted_face_cache(new CountedFaceCache); | 30 CFX_FaceCache* face_cache = new CFX_FaceCache(bExternal ? nullptr : face); |
| 31 CFX_CountedFaceCache* counted_face_cache = new CFX_CountedFaceCache; |
36 counted_face_cache->m_nCount = 2; | 32 counted_face_cache->m_nCount = 2; |
37 CFX_FaceCache* face_cache = new CFX_FaceCache(bExternal ? nullptr : face); | 33 counted_face_cache->m_Obj = face_cache; |
38 counted_face_cache->m_Obj.reset(face_cache); | 34 map[face] = counted_face_cache; |
39 map[face] = std::move(counted_face_cache); | |
40 return face_cache; | 35 return face_cache; |
41 } | 36 } |
42 | 37 |
43 #ifdef _SKIA_SUPPORT_ | 38 #ifdef _SKIA_SUPPORT_ |
44 CFX_TypeFace* CFX_FontCache::GetDeviceCache(const CFX_Font* pFont) { | 39 CFX_TypeFace* CFX_FontCache::GetDeviceCache(CFX_Font* pFont) { |
45 return GetCachedFace(pFont)->GetDeviceCache(pFont); | 40 return GetCachedFace(pFont)->GetDeviceCache(pFont); |
46 } | 41 } |
47 #endif | 42 #endif |
48 | 43 |
49 void CFX_FontCache::ReleaseCachedFace(const CFX_Font* pFont) { | 44 void CFX_FontCache::ReleaseCachedFace(CFX_Font* pFont) { |
50 FXFT_Face face = pFont->GetFace(); | 45 FXFT_Face face = pFont->GetFace(); |
51 const bool bExternal = !face; | 46 const bool bExternal = !face; |
52 CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap; | 47 CFX_FTCacheMap& map = bExternal ? m_ExtFaceMap : m_FTFaceMap; |
53 | 48 |
54 auto it = map.find(face); | 49 auto it = map.find(face); |
55 if (it == map.end()) | 50 if (it == map.end()) |
56 return; | 51 return; |
57 | 52 |
58 CountedFaceCache* counted_face_cache = it->second.get(); | 53 CFX_CountedFaceCache* counted_face_cache = it->second; |
59 if (counted_face_cache->m_nCount > 2) { | 54 if (counted_face_cache->m_nCount > 1) { |
60 counted_face_cache->m_nCount--; | 55 counted_face_cache->m_nCount--; |
61 } else { | |
62 map.erase(it); | |
63 } | 56 } |
64 } | 57 } |
| 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 |