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 FPDFSDK_INCLUDE_PDFWINDOW_PWL_FONTMAP_H_
8 #define FPDFSDK_INCLUDE_PDFWINDOW_PWL_FONTMAP_H_
9 
10 #include "fpdfsdk/include/fxedit/fx_edit.h"
11 #include "public/fpdf_sysfontinfo.h"
12 
13 class CPDF_Document;
14 class IFX_SystemHandler;
15 
16 struct CPWL_FontMap_Data {
17   CPDF_Font* pFont;
18   int32_t nCharset;
19   CFX_ByteString sFontName;
20 };
21 
22 struct CPWL_FontMap_Native {
23   int32_t nCharset;
24   CFX_ByteString sFontName;
25 };
26 
27 #ifndef ANSI_CHARSET
28 
29 #define ANSI_CHARSET 0
30 #define DEFAULT_CHARSET 1
31 #define SYMBOL_CHARSET 2
32 #define SHIFTJIS_CHARSET 128
33 #define HANGEUL_CHARSET 129
34 #define HANGUL_CHARSET 129
35 #define GB2312_CHARSET 134
36 #define CHINESEBIG5_CHARSET 136
37 #define OEM_CHARSET 255
38 #define JOHAB_CHARSET 130
39 #define HEBREW_CHARSET 177
40 #define ARABIC_CHARSET 178
41 #define GREEK_CHARSET 161
42 #define TURKISH_CHARSET 162
43 #define VIETNAMESE_CHARSET 163
44 #define THAI_CHARSET 222
45 #define EASTEUROPE_CHARSET 238
46 #define RUSSIAN_CHARSET 204
47 #define BALTIC_CHARSET 186
48 
49 #endif
50 
51 class CPWL_FontMap : public IFX_Edit_FontMap {
52  public:
53   CPWL_FontMap(IFX_SystemHandler* pSystemHandler);
54   ~CPWL_FontMap() override;
55 
56   // IFX_Edit_FontMap
57   CPDF_Font* GetPDFFont(int32_t nFontIndex) override;
58   CFX_ByteString GetPDFFontAlias(int32_t nFontIndex) override;
59   int32_t GetWordFontIndex(FX_WORD word,
60                            int32_t nCharset,
61                            int32_t nFontIndex) override;
62   int32_t CharCodeFromUnicode(int32_t nFontIndex, FX_WORD word) override;
63   int32_t CharSetFromUnicode(FX_WORD word, int32_t nOldCharset) override;
64 
65   void SetSystemHandler(IFX_SystemHandler* pSystemHandler);
66   int32_t GetFontMapCount() const;
67   const CPWL_FontMap_Data* GetFontMapData(int32_t nIndex) const;
68   static int32_t GetNativeCharset();
69   CFX_ByteString GetNativeFontName(int32_t nCharset);
70 
71   static CFX_ByteString GetDefaultFontByCharset(int32_t nCharset);
72 
73   CPDF_Font* AddFontToDocument(CPDF_Document* pDoc,
74                                CFX_ByteString& sFontName,
75                                uint8_t nCharset);
76   static FX_BOOL IsStandardFont(const CFX_ByteString& sFontName);
77   CPDF_Font* AddStandardFont(CPDF_Document* pDoc, CFX_ByteString& sFontName);
78   CPDF_Font* AddSystemFont(CPDF_Document* pDoc,
79                            CFX_ByteString& sFontName,
80                            uint8_t nCharset);
81 
82  protected:
83   virtual void Initialize();
84   virtual CPDF_Document* GetDocument();
85   virtual CPDF_Font* FindFontSameCharset(CFX_ByteString& sFontAlias,
86                                          int32_t nCharset);
87   virtual void AddedFont(CPDF_Font* pFont, const CFX_ByteString& sFontAlias);
88 
89   FX_BOOL KnowWord(int32_t nFontIndex, FX_WORD word);
90 
91   void Empty();
92   int32_t GetFontIndex(const CFX_ByteString& sFontName,
93                        int32_t nCharset,
94                        FX_BOOL bFind);
95   int32_t GetPWLFontIndex(FX_WORD word, int32_t nCharset);
96   int32_t AddFontData(CPDF_Font* pFont,
97                       const CFX_ByteString& sFontAlias,
98                       int32_t nCharset = DEFAULT_CHARSET);
99 
100   CFX_ByteString EncodeFontAlias(const CFX_ByteString& sFontName,
101                                  int32_t nCharset);
102   CFX_ByteString EncodeFontAlias(const CFX_ByteString& sFontName);
103 
104  private:
105   CFX_ByteString GetFontName(int32_t nFontIndex);
106   int32_t FindFont(const CFX_ByteString& sFontName,
107                    int32_t nCharset = DEFAULT_CHARSET);
108 
109   CFX_ByteString GetNativeFont(int32_t nCharset);
110 
111  public:
112   using CharsetFontMap = FPDF_CharsetFontMap;
113   static const CharsetFontMap defaultTTFMap[];
114 
115  protected:
116   CFX_ArrayTemplate<CPWL_FontMap_Data*> m_aData;
117   CFX_ArrayTemplate<CPWL_FontMap_Native*> m_aNativeFont;
118 
119  private:
120   CPDF_Document* m_pPDFDoc;
121   IFX_SystemHandler* m_pSystemHandler;
122 };
123 
124 class CPWL_DocFontMap : public CPWL_FontMap {
125  public:
126   CPWL_DocFontMap(IFX_SystemHandler* pSystemHandler,
127                   CPDF_Document* pAttachedDoc);
128   ~CPWL_DocFontMap() override;
129 
130  private:
131   // CPWL_FontMap:
132   CPDF_Document* GetDocument() override;
133 
134   CPDF_Document* m_pAttachedDoc;
135 };
136 
137 #endif  // FPDFSDK_INCLUDE_PDFWINDOW_PWL_FONTMAP_H_
138