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 "core/fpdfapi/cmaps/CNS1/cmaps_cns1.h"
10 #include "core/fpdfapi/cmaps/GB1/cmaps_gb1.h"
11 #include "core/fpdfapi/cmaps/Japan1/cmaps_japan1.h"
12 #include "core/fpdfapi/cmaps/Korea1/cmaps_korea1.h"
13 #include "core/fpdfapi/font/cfx_stockfontarray.h"
14 #include "core/fpdfapi/parser/cpdf_document.h"
15 #include "third_party/base/stl_util.h"
16 
17 namespace {
18 
19 CPDF_FontGlobals* g_FontGlobals = nullptr;
20 
21 }  // namespace
22 
23 // static
Create()24 void CPDF_FontGlobals::Create() {
25   ASSERT(!g_FontGlobals);
26   g_FontGlobals = new CPDF_FontGlobals();
27 }
28 
29 // static
Destroy()30 void CPDF_FontGlobals::Destroy() {
31   ASSERT(g_FontGlobals);
32   delete g_FontGlobals;
33   g_FontGlobals = nullptr;
34 }
35 
36 // static
GetInstance()37 CPDF_FontGlobals* CPDF_FontGlobals::GetInstance() {
38   ASSERT(g_FontGlobals);
39   return g_FontGlobals;
40 }
41 
CPDF_FontGlobals()42 CPDF_FontGlobals::CPDF_FontGlobals() {
43   memset(m_EmbeddedCharsets, 0, sizeof(m_EmbeddedCharsets));
44   memset(m_EmbeddedToUnicodes, 0, sizeof(m_EmbeddedToUnicodes));
45 }
46 
47 CPDF_FontGlobals::~CPDF_FontGlobals() = default;
48 
LoadEmbeddedMaps()49 void CPDF_FontGlobals::LoadEmbeddedMaps() {
50   LoadEmbeddedGB1CMaps();
51   LoadEmbeddedCNS1CMaps();
52   LoadEmbeddedJapan1CMaps();
53   LoadEmbeddedKorea1CMaps();
54 }
55 
Find(CPDF_Document * pDoc,CFX_FontMapper::StandardFont index)56 RetainPtr<CPDF_Font> CPDF_FontGlobals::Find(
57     CPDF_Document* pDoc,
58     CFX_FontMapper::StandardFont index) {
59   auto it = m_StockMap.find(pDoc);
60   if (it == m_StockMap.end() || !it->second)
61     return nullptr;
62 
63   return it->second->GetFont(index);
64 }
65 
Set(CPDF_Document * pDoc,CFX_FontMapper::StandardFont index,const RetainPtr<CPDF_Font> & pFont)66 void CPDF_FontGlobals::Set(CPDF_Document* pDoc,
67                            CFX_FontMapper::StandardFont index,
68                            const RetainPtr<CPDF_Font>& pFont) {
69   if (!pdfium::Contains(m_StockMap, pDoc))
70     m_StockMap[pDoc] = std::make_unique<CFX_StockFontArray>();
71   m_StockMap[pDoc]->SetFont(index, pFont);
72 }
73 
Clear(CPDF_Document * pDoc)74 void CPDF_FontGlobals::Clear(CPDF_Document* pDoc) {
75   m_StockMap.erase(pDoc);
76 }
77 
LoadEmbeddedGB1CMaps()78 void CPDF_FontGlobals::LoadEmbeddedGB1CMaps() {
79   SetEmbeddedCharset(CIDSET_GB1, pdfium::make_span(g_FXCMAP_GB1_cmaps,
80                                                    g_FXCMAP_GB1_cmaps_size));
81   SetEmbeddedToUnicode(CIDSET_GB1, g_FXCMAP_GB1CID2Unicode_5);
82 }
83 
LoadEmbeddedCNS1CMaps()84 void CPDF_FontGlobals::LoadEmbeddedCNS1CMaps() {
85   SetEmbeddedCharset(CIDSET_CNS1, pdfium::make_span(g_FXCMAP_CNS1_cmaps,
86                                                     g_FXCMAP_CNS1_cmaps_size));
87   SetEmbeddedToUnicode(CIDSET_CNS1, g_FXCMAP_CNS1CID2Unicode_5);
88 }
89 
LoadEmbeddedJapan1CMaps()90 void CPDF_FontGlobals::LoadEmbeddedJapan1CMaps() {
91   SetEmbeddedCharset(
92       CIDSET_JAPAN1,
93       pdfium::make_span(g_FXCMAP_Japan1_cmaps, g_FXCMAP_Japan1_cmaps_size));
94   SetEmbeddedToUnicode(CIDSET_JAPAN1, g_FXCMAP_Japan1CID2Unicode_4);
95 }
96 
LoadEmbeddedKorea1CMaps()97 void CPDF_FontGlobals::LoadEmbeddedKorea1CMaps() {
98   SetEmbeddedCharset(
99       CIDSET_KOREA1,
100       pdfium::make_span(g_FXCMAP_Korea1_cmaps, g_FXCMAP_Korea1_cmaps_size));
101   SetEmbeddedToUnicode(CIDSET_KOREA1, g_FXCMAP_Korea1CID2Unicode_2);
102 }
103