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