1 // Copyright 2014 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_SRC_FPDFAPI_FPDF_FONT_FONT_INT_H_
8 #define CORE_SRC_FPDFAPI_FPDF_FONT_FONT_INT_H_
9 
10 #include <map>
11 #include <memory>
12 
13 #include "core/include/fpdfapi/fpdf_resource.h"
14 #include "core/include/fxcrt/fx_basic.h"
15 
16 class CPDF_CID2UnicodeMap;
17 class CPDF_CMap;
18 class CPDF_Font;
19 class CPDF_Stream;
20 
21 typedef void* FXFT_Library;
22 
23 short TT2PDF(int m, FXFT_Face face);
24 FX_BOOL FT_UseTTCharmap(FXFT_Face face, int platform_id, int encoding_id);
25 
26 class CPDF_CMapManager {
27  public:
28   CPDF_CMapManager();
29   ~CPDF_CMapManager();
30   void* GetPackage(FX_BOOL bPrompt);
31   CPDF_CMap* GetPredefinedCMap(const CFX_ByteString& name, FX_BOOL bPromptCJK);
32   CPDF_CID2UnicodeMap* GetCID2UnicodeMap(CIDSet charset, FX_BOOL bPromptCJK);
33   void ReloadAll();
34 
35  private:
36   CPDF_CMap* LoadPredefinedCMap(const CFX_ByteString& name, FX_BOOL bPromptCJK);
37   CPDF_CID2UnicodeMap* LoadCID2UnicodeMap(CIDSet charset, FX_BOOL bPromptCJK);
38 
39   FX_BOOL m_bPrompted;
40   std::map<CFX_ByteString, CPDF_CMap*> m_CMaps;
41   CPDF_CID2UnicodeMap* m_CID2UnicodeMaps[6];
42 };
43 
44 class CFX_StockFontArray {
45  public:
46   CFX_StockFontArray();
47   ~CFX_StockFontArray();
48 
49   // Takes ownership of |pFont|.
50   void SetFont(int index, CPDF_Font* pFont);
51   CPDF_Font* GetFont(int index) const;
52 
53  private:
54   std::unique_ptr<CPDF_Font> m_StockFonts[14];
55 };
56 
57 class CPDF_FontGlobals {
58  public:
59   CPDF_FontGlobals();
60   ~CPDF_FontGlobals();
61 
62   void Clear(CPDF_Document* pDoc);
63   CPDF_Font* Find(CPDF_Document* pDoc, int index);
64 
65   // Takes ownership of |pFont|.
66   void Set(CPDF_Document* key, int index, CPDF_Font* pFont);
67 
68   CPDF_CMapManager m_CMapManager;
69   struct {
70     const struct FXCMAP_CMap* m_pMapList;
71     int m_Count;
72   } m_EmbeddedCharsets[CIDSET_NUM_SETS];
73   struct {
74     const FX_WORD* m_pMap;
75     int m_Count;
76   } m_EmbeddedToUnicodes[CIDSET_NUM_SETS];
77 
78  private:
79   std::map<CPDF_Document*, std::unique_ptr<CFX_StockFontArray>> m_StockMap;
80 };
81 
82 struct CMap_CodeRange {
83   int m_CharSize;
84   uint8_t m_Lower[4];
85   uint8_t m_Upper[4];
86 };
87 
88 class CPDF_CMapParser {
89  public:
90   CPDF_CMapParser();
~CPDF_CMapParser()91   ~CPDF_CMapParser() {}
92   FX_BOOL Initialize(CPDF_CMap*);
93   void ParseWord(const CFX_ByteStringC& str);
94   CFX_BinaryBuf m_AddMaps;
95 
96  private:
97   friend class fpdf_font_cid_CMap_GetCode_Test;
98   friend class fpdf_font_cid_CMap_GetCodeRange_Test;
99 
100   static FX_DWORD CMap_GetCode(const CFX_ByteStringC& word);
101   static bool CMap_GetCodeRange(CMap_CodeRange& range,
102                                 const CFX_ByteStringC& first,
103                                 const CFX_ByteStringC& second);
104 
105   CPDF_CMap* m_pCMap;
106   int m_Status;
107   int m_CodeSeq;
108   FX_DWORD m_CodePoints[4];
109   CFX_ArrayTemplate<CMap_CodeRange> m_CodeRanges;
110   CFX_ByteString m_Registry, m_Ordering, m_Supplement;
111   CFX_ByteString m_LastWord;
112 };
113 #define CIDCODING_UNKNOWN 0
114 #define CIDCODING_GB 1
115 #define CIDCODING_BIG5 2
116 #define CIDCODING_JIS 3
117 #define CIDCODING_KOREA 4
118 #define CIDCODING_UCS2 5
119 #define CIDCODING_CID 6
120 #define CIDCODING_UTF16 7
121 class CPDF_CMap {
122  public:
123   CPDF_CMap();
124   FX_BOOL LoadPredefined(CPDF_CMapManager* pMgr,
125                          const FX_CHAR* name,
126                          FX_BOOL bPromptCJK);
127   FX_BOOL LoadEmbedded(const uint8_t* pData, FX_DWORD dwSize);
128   void Release();
IsLoaded()129   FX_BOOL IsLoaded() const { return m_bLoaded; }
IsVertWriting()130   FX_BOOL IsVertWriting() const { return m_bVertical; }
131   FX_WORD CIDFromCharCode(FX_DWORD charcode) const;
132   FX_DWORD CharCodeFromCID(FX_WORD CID) const;
133   int GetCharSize(FX_DWORD charcode) const;
134   FX_DWORD GetNextChar(const FX_CHAR* pString, int nStrLen, int& offset) const;
135   int CountChar(const FX_CHAR* pString, int size) const;
136   int AppendChar(FX_CHAR* str, FX_DWORD charcode) const;
137   typedef enum {
138     OneByte,
139     TwoBytes,
140     MixedTwoBytes,
141     MixedFourBytes
142   } CodingScheme;
143 
144  protected:
145   ~CPDF_CMap();
146   friend class CPDF_CMapParser;
147   friend class CPDF_CMapManager;
148   friend class CPDF_CIDFont;
149 
150  protected:
151   CFX_ByteString m_PredefinedCMap;
152   FX_BOOL m_bVertical;
153   CIDSet m_Charset;
154   int m_Coding;
155   CodingScheme m_CodingScheme;
156   int m_nCodeRanges;
157   uint8_t* m_pLeadingBytes;
158   FX_WORD* m_pMapping;
159   uint8_t* m_pAddMapping;
160   FX_BOOL m_bLoaded;
161   const FXCMAP_CMap* m_pEmbedMap;
162   CPDF_CMap* m_pUseMap;
163 };
164 
165 typedef struct _FileHeader {
166   uint8_t btTag[4];
167   uint8_t btVersion;
168   uint8_t btFormat;
169   uint8_t btReserved1[2];
170   FX_DWORD dwStartIndex;
171   FX_DWORD dwEndIndex;
172   FX_DWORD dwDataSize;
173   FX_DWORD dwDataOffset;
174   FX_DWORD dwRecordSize;
175 } FXMP_FILEHEADER;
176 
177 class CPDF_CID2UnicodeMap {
178  public:
179   CPDF_CID2UnicodeMap();
180   ~CPDF_CID2UnicodeMap();
181   FX_BOOL Initialize();
182   FX_BOOL IsLoaded();
183   void Load(CPDF_CMapManager* pMgr, CIDSet charset, FX_BOOL bPromptCJK);
184   FX_WCHAR UnicodeFromCID(FX_WORD CID);
185 
186  protected:
187   CIDSet m_Charset;
188   const FX_WORD* m_pEmbeddedMap;
189   FX_DWORD m_EmbeddedCount;
190 };
191 
192 class CPDF_ToUnicodeMap {
193  public:
194   void Load(CPDF_Stream* pStream);
195   CFX_WideString Lookup(FX_DWORD charcode);
196   FX_DWORD ReverseLookup(FX_WCHAR unicode);
197 
198  protected:
199   std::map<FX_DWORD, FX_DWORD> m_Map;
200   CPDF_CID2UnicodeMap* m_pBaseMap;
201   CFX_WideTextBuf m_MultiCharBuf;
202 
203  private:
204   friend class fpdf_font_StringToCode_Test;
205   friend class fpdf_font_StringToWideString_Test;
206 
207   static FX_DWORD StringToCode(const CFX_ByteStringC& str);
208   static CFX_WideString StringToWideString(const CFX_ByteStringC& str);
209 };
210 class CPDF_FontCharMap : public CFX_CharMap {
211  public:
212   CPDF_FontCharMap(CPDF_Font* pFont);
213   CPDF_Font* m_pFont;
214 };
215 
216 void FPDFAPI_LoadCID2UnicodeMap(CIDSet charset,
217                                 const FX_WORD*& pMap,
218                                 FX_DWORD& count);
219 
220 #endif  // CORE_SRC_FPDFAPI_FPDF_FONT_FONT_INT_H_
221