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_cmapmanager.h"
8 
9 #include <utility>
10 
11 #include "core/fpdfapi/font/cpdf_cid2unicodemap.h"
12 #include "core/fpdfapi/font/cpdf_cmap.h"
13 
14 namespace {
15 
LoadPredefinedCMap(ByteStringView name)16 RetainPtr<const CPDF_CMap> LoadPredefinedCMap(ByteStringView name) {
17   if (!name.IsEmpty() && name[0] == '/')
18     name = name.Last(name.GetLength() - 1);
19   return pdfium::MakeRetain<CPDF_CMap>(name);
20 }
21 
22 }  // namespace
23 
24 CPDF_CMapManager::CPDF_CMapManager() = default;
25 
26 CPDF_CMapManager::~CPDF_CMapManager() = default;
27 
GetPredefinedCMap(const ByteString & name)28 RetainPtr<const CPDF_CMap> CPDF_CMapManager::GetPredefinedCMap(
29     const ByteString& name) {
30   auto it = m_CMaps.find(name);
31   if (it != m_CMaps.end())
32     return it->second;
33 
34   RetainPtr<const CPDF_CMap> pCMap = LoadPredefinedCMap(name.AsStringView());
35   if (!name.IsEmpty())
36     m_CMaps[name] = pCMap;
37 
38   return pCMap;
39 }
40 
GetCID2UnicodeMap(CIDSet charset)41 CPDF_CID2UnicodeMap* CPDF_CMapManager::GetCID2UnicodeMap(CIDSet charset) {
42   if (!m_CID2UnicodeMaps[charset]) {
43     m_CID2UnicodeMaps[charset] = std::make_unique<CPDF_CID2UnicodeMap>(charset);
44   }
45   return m_CID2UnicodeMaps[charset].get();
46 }
47