Home | History | Annotate | Download | only in fxge
      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 
     17 CFX_FontCache::CountedFaceCache::CountedFaceCache() {}
     18 
     19 CFX_FontCache::CountedFaceCache::~CountedFaceCache() {}
     20 
     21 CFX_FontCache::CFX_FontCache() {}
     22 
     23 CFX_FontCache::~CFX_FontCache() {
     24   ASSERT(m_ExtFaceMap.empty());
     25   ASSERT(m_FTFaceMap.empty());
     26 }
     27 
     28 CFX_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_
     50 CFX_TypeFace* CFX_FontCache::GetDeviceCache(const CFX_Font* pFont) {
     51   return GetCachedFace(pFont)->GetDeviceCache(pFont);
     52 }
     53 #endif
     54 
     55 void 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