1 // Copyright 2016 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 #ifndef CORE_FPDFAPI_FONT_CPDF_CIDFONT_H_
8 #define CORE_FPDFAPI_FONT_CPDF_CIDFONT_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fpdfapi/font/cpdf_font.h"
14 #include "core/fxcrt/fx_string.h"
15 #include "core/fxcrt/fx_system.h"
16 #include "core/fxcrt/retain_ptr.h"
17 #include "core/fxcrt/unowned_ptr.h"
18 
19 enum CIDSet : uint8_t {
20   CIDSET_UNKNOWN,
21   CIDSET_GB1,
22   CIDSET_CNS1,
23   CIDSET_JAPAN1,
24   CIDSET_KOREA1,
25   CIDSET_UNICODE,
26   CIDSET_NUM_SETS
27 };
28 
29 class CFX_CTTGSUBTable;
30 class CPDF_Array;
31 class CPDF_CID2UnicodeMap;
32 class CPDF_CMap;
33 class CPDF_StreamAcc;
34 
35 class CPDF_CIDFont final : public CPDF_Font {
36  public:
37   template <typename T, typename... Args>
38   friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
39 
40   ~CPDF_CIDFont() override;
41 
42   static float CIDTransformToFloat(uint8_t ch);
43 
44   // CPDF_Font:
45   bool IsCIDFont() const override;
46   const CPDF_CIDFont* AsCIDFont() const override;
47   CPDF_CIDFont* AsCIDFont() override;
48   int GlyphFromCharCode(uint32_t charcode, bool* pVertGlyph) override;
49   uint32_t GetCharWidthF(uint32_t charcode) override;
50   FX_RECT GetCharBBox(uint32_t charcode) override;
51   uint32_t GetNextChar(ByteStringView pString, size_t* pOffset) const override;
52   size_t CountChar(ByteStringView pString) const override;
53   int AppendChar(char* str, uint32_t charcode) const override;
54   bool IsVertWriting() const override;
55   bool IsUnicodeCompatible() const override;
56   bool Load() override;
57   WideString UnicodeFromCharCode(uint32_t charcode) const override;
58   uint32_t CharCodeFromUnicode(wchar_t Unicode) const override;
59 
60   uint16_t CIDFromCharCode(uint32_t charcode) const;
61   const uint8_t* GetCIDTransform(uint16_t CID) const;
62   short GetVertWidth(uint16_t CID) const;
63   void GetVertOrigin(uint16_t CID, short& vx, short& vy) const;
64   int GetCharSize(uint32_t charcode) const;
65 
66  private:
67   CPDF_CIDFont(CPDF_Document* pDocument, CPDF_Dictionary* pFontDict);
68 
69   void LoadGB2312();
70   int GetGlyphIndex(uint32_t unicodeb, bool* pVertGlyph);
71   int GetVerticalGlyph(int index, bool* pVertGlyph);
72   void LoadMetricsArray(const CPDF_Array* pArray,
73                         std::vector<uint32_t>* result,
74                         int nElements);
75   void LoadSubstFont();
76   wchar_t GetUnicodeFromCharCode(uint32_t charcode) const;
77 
78   RetainPtr<const CPDF_CMap> m_pCMap;
79   UnownedPtr<const CPDF_CID2UnicodeMap> m_pCID2UnicodeMap;
80   RetainPtr<CPDF_StreamAcc> m_pStreamAcc;
81   std::unique_ptr<CFX_CTTGSUBTable> m_pTTGSUBTable;
82   bool m_bType1 = false;
83   bool m_bCIDIsGID = false;
84   bool m_bAnsiWidthsFixed = false;
85   bool m_bAdobeCourierStd = false;
86   CIDSet m_Charset = CIDSET_UNKNOWN;
87   uint16_t m_DefaultWidth = 1000;
88   short m_DefaultVY = 880;
89   short m_DefaultW1 = -1000;
90   std::vector<uint32_t> m_WidthList;
91   std::vector<uint32_t> m_VertMetrics;
92   FX_RECT m_CharBBox[256];
93 };
94 
95 #endif  // CORE_FPDFAPI_FONT_CPDF_CIDFONT_H_
96