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