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_FPDFDOC_CBA_FONTMAP_H_
8 #define CORE_FPDFDOC_CBA_FONTMAP_H_
9 
10 #include <memory>
11 #include <vector>
12 
13 #include "core/fpdfdoc/ipvt_fontmap.h"
14 #include "core/fxcrt/fx_codepage.h"
15 #include "core/fxcrt/retain_ptr.h"
16 #include "core/fxcrt/unowned_ptr.h"
17 
18 class CPDF_Dictionary;
19 class CPDF_Document;
20 
21 class CBA_FontMap final : public IPVT_FontMap {
22  public:
23   static int32_t GetNativeCharset();
24 
25   CBA_FontMap(CPDF_Document* pDocument, CPDF_Dictionary* pAnnotDict);
26   ~CBA_FontMap() override;
27 
28   // IPVT_FontMap
29   RetainPtr<CPDF_Font> GetPDFFont(int32_t nFontIndex) override;
30   ByteString GetPDFFontAlias(int32_t nFontIndex) override;
31   int32_t GetWordFontIndex(uint16_t word,
32                            int32_t nCharset,
33                            int32_t nFontIndex) override;
34   int32_t CharCodeFromUnicode(int32_t nFontIndex, uint16_t word) override;
35   int32_t CharSetFromUnicode(uint16_t word, int32_t nOldCharset) override;
36 
37   void Reset();
38   void SetAPType(const ByteString& sAPType);
39 
40  private:
41   struct Data {
42     Data();
43     ~Data();
44 
45     RetainPtr<CPDF_Font> pFont;
46     int32_t nCharset;
47     ByteString sFontName;
48   };
49 
50   struct Native {
51     int32_t nCharset;
52     ByteString sFontName;
53   };
54 
55   void Initialize();
56   RetainPtr<CPDF_Font> FindFontSameCharset(ByteString* sFontAlias,
57                                            int32_t nCharset);
58   RetainPtr<CPDF_Font> FindResFontSameCharset(const CPDF_Dictionary* pResDict,
59                                               ByteString* sFontAlias,
60                                               int32_t nCharset);
61   RetainPtr<CPDF_Font> GetAnnotDefaultFont(ByteString* sAlias);
62   void AddFontToAnnotDict(const RetainPtr<CPDF_Font>& pFont,
63                           const ByteString& sAlias);
64 
65   bool KnowWord(int32_t nFontIndex, uint16_t word);
66 
67   void Clear();
68   int32_t GetFontIndex(const ByteString& sFontName,
69                        int32_t nCharset,
70                        bool bFind);
71   int32_t AddFontData(const RetainPtr<CPDF_Font>& pFont,
72                       const ByteString& sFontAlias,
73                       int32_t nCharset);
74 
75   ByteString EncodeFontAlias(const ByteString& sFontName, int32_t nCharset);
76   ByteString EncodeFontAlias(const ByteString& sFontName);
77 
78   int32_t FindFont(const ByteString& sFontName, int32_t nCharset);
79   ByteString GetNativeFontName(int32_t nCharset);
80   ByteString GetCachedNativeFontName(int32_t nCharset);
81   bool IsStandardFont(const ByteString& sFontName);
82   RetainPtr<CPDF_Font> AddFontToDocument(ByteString sFontName,
83                                          uint8_t nCharset);
84   RetainPtr<CPDF_Font> AddStandardFont(ByteString sFontName);
85   RetainPtr<CPDF_Font> AddSystemFont(ByteString sFontName, uint8_t nCharset);
86 
87   std::vector<std::unique_ptr<Data>> m_Data;
88   std::vector<std::unique_ptr<Native>> m_NativeFont;
89   UnownedPtr<CPDF_Document> const m_pDocument;
90   RetainPtr<CPDF_Dictionary> const m_pAnnotDict;
91   RetainPtr<CPDF_Font> m_pDefaultFont;
92   ByteString m_sDefaultFontName;
93   ByteString m_sAPType = "N";
94 };
95 
96 #endif  // CORE_FPDFDOC_CBA_FONTMAP_H_
97