1 // Copyright 2017 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/fpdfapi/font/cpdf_fontglobals.h"
8 
9 #include <utility>
10 
11 #include "core/fpdfapi/parser/cpdf_document.h"
12 #include "third_party/base/ptr_util.h"
13 #include "third_party/base/stl_util.h"
14 
CPDF_FontGlobals()15 CPDF_FontGlobals::CPDF_FontGlobals() {
16   memset(m_EmbeddedCharsets, 0, sizeof(m_EmbeddedCharsets));
17   memset(m_EmbeddedToUnicodes, 0, sizeof(m_EmbeddedToUnicodes));
18 }
19 
~CPDF_FontGlobals()20 CPDF_FontGlobals::~CPDF_FontGlobals() {}
21 
Find(CPDF_Document * pDoc,uint32_t index)22 CPDF_Font* CPDF_FontGlobals::Find(CPDF_Document* pDoc, uint32_t index) {
23   auto it = m_StockMap.find(pDoc);
24   if (it == m_StockMap.end())
25     return nullptr;
26   return it->second ? it->second->GetFont(index) : nullptr;
27 }
28 
Set(CPDF_Document * pDoc,uint32_t index,std::unique_ptr<CPDF_Font> pFont)29 CPDF_Font* CPDF_FontGlobals::Set(CPDF_Document* pDoc,
30                                  uint32_t index,
31                                  std::unique_ptr<CPDF_Font> pFont) {
32   if (!pdfium::ContainsKey(m_StockMap, pDoc))
33     m_StockMap[pDoc] = pdfium::MakeUnique<CFX_StockFontArray>();
34   return m_StockMap[pDoc]->SetFont(index, std::move(pFont));
35 }
36 
Clear(CPDF_Document * pDoc)37 void CPDF_FontGlobals::Clear(CPDF_Document* pDoc) {
38   m_StockMap.erase(pDoc);
39 }
40