• 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 #ifndef CORE_FPDFAPI_FONT_CPDF_CMAP_H_
8 #define CORE_FPDFAPI_FONT_CPDF_CMAP_H_
9 
10 #include <stdint.h>
11 
12 #include <array>
13 #include <vector>
14 
15 #include "core/fpdfapi/font/cpdf_cidfont.h"
16 #include "core/fxcrt/fixed_size_data_vector.h"
17 #include "core/fxcrt/retain_ptr.h"
18 #include "core/fxcrt/span.h"
19 #include "core/fxcrt/unowned_ptr.h"
20 
21 namespace fxcmap {
22 struct CMap;
23 }
24 
25 enum class CIDCoding : uint8_t {
26   kUNKNOWN = 0,
27   kGB,
28   kBIG5,
29   kJIS,
30   kKOREA,
31   kUCS2,
32   kCID,
33   kUTF16,
34 };
35 
36 class CPDF_CMap final : public Retainable {
37  public:
38   static constexpr size_t kDirectMapTableSize = 65536;
39 
40   enum CodingScheme : uint8_t {
41     OneByte,
42     TwoBytes,
43     MixedTwoBytes,
44     MixedFourBytes
45   };
46 
47   struct CodeRange {
48     size_t m_CharSize;
49     std::array<uint8_t, 4> m_Lower;
50     std::array<uint8_t, 4> m_Upper;
51   };
52 
53   struct CIDRange {
54     uint32_t m_StartCode;
55     uint32_t m_EndCode;
56     uint16_t m_StartCID;
57   };
58 
59   CONSTRUCT_VIA_MAKE_RETAIN;
60 
IsLoaded()61   bool IsLoaded() const { return m_bLoaded; }
IsVertWriting()62   bool IsVertWriting() const { return m_bVertical; }
63 
64   uint16_t CIDFromCharCode(uint32_t charcode) const;
65 
66   int GetCharSize(uint32_t charcode) const;
67   uint32_t GetNextChar(ByteStringView pString, size_t* pOffset) const;
68   size_t CountChar(ByteStringView pString) const;
69   void AppendChar(ByteString* str, uint32_t charcode) const;
70 
SetVertical(bool vert)71   void SetVertical(bool vert) { m_bVertical = vert; }
SetCodingScheme(CodingScheme scheme)72   void SetCodingScheme(CodingScheme scheme) { m_CodingScheme = scheme; }
73   void SetAdditionalMappings(std::vector<CIDRange> mappings);
74   void SetMixedFourByteLeadingRanges(std::vector<CodeRange> ranges);
75 
GetCoding()76   CIDCoding GetCoding() const { return m_Coding; }
GetEmbedMap()77   const fxcmap::CMap* GetEmbedMap() const { return m_pEmbedMap; }
GetCharset()78   CIDSet GetCharset() const { return m_Charset; }
SetCharset(CIDSet set)79   void SetCharset(CIDSet set) { m_Charset = set; }
80 
81   void SetDirectCharcodeToCIDTableRange(uint32_t start_code,
82                                         uint32_t end_code,
83                                         uint16_t start_cid);
IsDirectCharcodeToCIDTableIsEmpty()84   bool IsDirectCharcodeToCIDTableIsEmpty() const {
85     return m_DirectCharcodeToCIDTable.empty();
86   }
87 
88  private:
89   explicit CPDF_CMap(ByteStringView bsPredefinedName);
90   explicit CPDF_CMap(pdfium::span<const uint8_t> spEmbeddedData);
91   ~CPDF_CMap() override;
92 
93   bool m_bLoaded = false;
94   bool m_bVertical = false;
95   CIDSet m_Charset = CIDSET_UNKNOWN;
96   CodingScheme m_CodingScheme = TwoBytes;
97   CIDCoding m_Coding = CIDCoding::kUNKNOWN;
98   std::vector<bool> m_MixedTwoByteLeadingBytes;
99   std::vector<CodeRange> m_MixedFourByteLeadingRanges;
100   FixedSizeDataVector<uint16_t> m_DirectCharcodeToCIDTable;
101   std::vector<CIDRange> m_AdditionalCharcodeToCIDMappings;
102   UnownedPtr<const fxcmap::CMap> m_pEmbedMap;
103 };
104 
105 #endif  // CORE_FPDFAPI_FONT_CPDF_CMAP_H_
106