• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The PDFium Authors
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/cmaps/CNS1/cmaps_cns1.h"
12 #include "core/fpdfapi/cmaps/GB1/cmaps_gb1.h"
13 #include "core/fpdfapi/cmaps/Japan1/cmaps_japan1.h"
14 #include "core/fpdfapi/cmaps/Korea1/cmaps_korea1.h"
15 #include "core/fpdfapi/font/cfx_stockfontarray.h"
16 #include "core/fpdfapi/font/cpdf_cid2unicodemap.h"
17 #include "core/fpdfapi/font/cpdf_cmap.h"
18 #include "core/fpdfapi/parser/cpdf_document.h"
19 #include "third_party/base/check.h"
20 #include "third_party/base/containers/contains.h"
21 
22 namespace {
23 
24 CPDF_FontGlobals* g_FontGlobals = nullptr;
25 
LoadPredefinedCMap(ByteStringView name)26 RetainPtr<const CPDF_CMap> LoadPredefinedCMap(ByteStringView name) {
27   if (!name.IsEmpty() && name[0] == '/')
28     name = name.Last(name.GetLength() - 1);
29   return pdfium::MakeRetain<CPDF_CMap>(name);
30 }
31 
32 }  // namespace
33 
34 // static
Create()35 void CPDF_FontGlobals::Create() {
36   DCHECK(!g_FontGlobals);
37   g_FontGlobals = new CPDF_FontGlobals();
38 }
39 
40 // static
Destroy()41 void CPDF_FontGlobals::Destroy() {
42   DCHECK(g_FontGlobals);
43   delete g_FontGlobals;
44   g_FontGlobals = nullptr;
45 }
46 
47 // static
GetInstance()48 CPDF_FontGlobals* CPDF_FontGlobals::GetInstance() {
49   DCHECK(g_FontGlobals);
50   return g_FontGlobals;
51 }
52 
CPDF_FontGlobals()53 CPDF_FontGlobals::CPDF_FontGlobals() {
54   memset(m_EmbeddedCharsets, 0, sizeof(m_EmbeddedCharsets));
55   memset(m_EmbeddedToUnicodes, 0, sizeof(m_EmbeddedToUnicodes));
56 }
57 
58 CPDF_FontGlobals::~CPDF_FontGlobals() = default;
59 
LoadEmbeddedMaps()60 void CPDF_FontGlobals::LoadEmbeddedMaps() {
61   LoadEmbeddedGB1CMaps();
62   LoadEmbeddedCNS1CMaps();
63   LoadEmbeddedJapan1CMaps();
64   LoadEmbeddedKorea1CMaps();
65 }
66 
Find(CPDF_Document * pDoc,CFX_FontMapper::StandardFont index)67 RetainPtr<CPDF_Font> CPDF_FontGlobals::Find(
68     CPDF_Document* pDoc,
69     CFX_FontMapper::StandardFont index) {
70   auto it = m_StockMap.find(pDoc);
71   if (it == m_StockMap.end() || !it->second)
72     return nullptr;
73 
74   return it->second->GetFont(index);
75 }
76 
Set(CPDF_Document * pDoc,CFX_FontMapper::StandardFont index,RetainPtr<CPDF_Font> pFont)77 void CPDF_FontGlobals::Set(CPDF_Document* pDoc,
78                            CFX_FontMapper::StandardFont index,
79                            RetainPtr<CPDF_Font> pFont) {
80   UnownedPtr<CPDF_Document> pKey(pDoc);
81   if (!pdfium::Contains(m_StockMap, pKey))
82     m_StockMap[pKey] = std::make_unique<CFX_StockFontArray>();
83   m_StockMap[pKey]->SetFont(index, std::move(pFont));
84 }
85 
Clear(CPDF_Document * pDoc)86 void CPDF_FontGlobals::Clear(CPDF_Document* pDoc) {
87   // Avoid constructing smart-pointer key as erase() doesn't invoke
88   // transparent lookup in the same way find() does.
89   auto it = m_StockMap.find(pDoc);
90   if (it != m_StockMap.end())
91     m_StockMap.erase(it);
92 }
93 
LoadEmbeddedGB1CMaps()94 void CPDF_FontGlobals::LoadEmbeddedGB1CMaps() {
95   SetEmbeddedCharset(CIDSET_GB1, pdfium::make_span(fxcmap::kGB1_cmaps,
96                                                    fxcmap::kGB1_cmaps_size));
97   SetEmbeddedToUnicode(CIDSET_GB1, fxcmap::kGB1CID2Unicode_5);
98 }
99 
LoadEmbeddedCNS1CMaps()100 void CPDF_FontGlobals::LoadEmbeddedCNS1CMaps() {
101   SetEmbeddedCharset(CIDSET_CNS1, pdfium::make_span(fxcmap::kCNS1_cmaps,
102                                                     fxcmap::kCNS1_cmaps_size));
103   SetEmbeddedToUnicode(CIDSET_CNS1, fxcmap::kCNS1CID2Unicode_5);
104 }
105 
LoadEmbeddedJapan1CMaps()106 void CPDF_FontGlobals::LoadEmbeddedJapan1CMaps() {
107   SetEmbeddedCharset(
108       CIDSET_JAPAN1,
109       pdfium::make_span(fxcmap::kJapan1_cmaps, fxcmap::kJapan1_cmaps_size));
110   SetEmbeddedToUnicode(CIDSET_JAPAN1, fxcmap::kJapan1CID2Unicode_4);
111 }
112 
LoadEmbeddedKorea1CMaps()113 void CPDF_FontGlobals::LoadEmbeddedKorea1CMaps() {
114   SetEmbeddedCharset(
115       CIDSET_KOREA1,
116       pdfium::make_span(fxcmap::kKorea1_cmaps, fxcmap::kKorea1_cmaps_size));
117   SetEmbeddedToUnicode(CIDSET_KOREA1, fxcmap::kKorea1CID2Unicode_2);
118 }
119 
GetPredefinedCMap(const ByteString & name)120 RetainPtr<const CPDF_CMap> CPDF_FontGlobals::GetPredefinedCMap(
121     const ByteString& name) {
122   auto it = m_CMaps.find(name);
123   if (it != m_CMaps.end())
124     return it->second;
125 
126   RetainPtr<const CPDF_CMap> pCMap = LoadPredefinedCMap(name.AsStringView());
127   if (!name.IsEmpty())
128     m_CMaps[name] = pCMap;
129 
130   return pCMap;
131 }
132 
GetCID2UnicodeMap(CIDSet charset)133 CPDF_CID2UnicodeMap* CPDF_FontGlobals::GetCID2UnicodeMap(CIDSet charset) {
134   if (!m_CID2UnicodeMaps[charset]) {
135     m_CID2UnicodeMaps[charset] = std::make_unique<CPDF_CID2UnicodeMap>(charset);
136   }
137   return m_CID2UnicodeMaps[charset].get();
138 }
139