• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 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_FXGE_CFX_FOLDERFONTINFO_H_
8 #define CORE_FXGE_CFX_FOLDERFONTINFO_H_
9 
10 #include <map>
11 #include <memory>
12 #include <vector>
13 
14 #include "core/fxcrt/fx_codepage_forward.h"
15 #include "core/fxcrt/unowned_ptr.h"
16 #include "core/fxge/cfx_fontmapper.h"
17 #include "core/fxge/systemfontinfo_iface.h"
18 
19 #define CHARSET_FLAG_ANSI (1 << 0)
20 #define CHARSET_FLAG_SYMBOL (1 << 1)
21 #define CHARSET_FLAG_SHIFTJIS (1 << 2)
22 #define CHARSET_FLAG_BIG5 (1 << 3)
23 #define CHARSET_FLAG_GB (1 << 4)
24 #define CHARSET_FLAG_KOREAN (1 << 5)
25 
26 class CFX_FolderFontInfo : public SystemFontInfoIface {
27  public:
28   CFX_FolderFontInfo();
29   ~CFX_FolderFontInfo() override;
30 
31   void AddPath(const ByteString& path);
32 
33   // SystemFontInfoIface:
34   bool EnumFontList(CFX_FontMapper* pMapper) override;
35   void* MapFont(int weight,
36                 bool bItalic,
37                 FX_Charset charset,
38                 int pitch_family,
39                 const ByteString& face) override;
40   void* GetFont(const ByteString& face) override;
41   size_t GetFontData(void* hFont,
42                      uint32_t table,
43                      pdfium::span<uint8_t> buffer) override;
44   void DeleteFont(void* hFont) override;
45   bool GetFaceName(void* hFont, ByteString* name) override;
46   bool GetFontCharset(void* hFont, FX_Charset* charset) override;
47 
48  protected:
49   friend class CFXFolderFontInfoTest;
50 
51   class FontFaceInfo {
52    public:
53     static constexpr int32_t kSimilarityScoreMax = 68;
54 
55     FontFaceInfo(ByteString filePath,
56                  ByteString faceName,
57                  ByteString fontTables,
58                  uint32_t fontOffset,
59                  uint32_t fileSize);
60 
61     bool IsEligibleForFindFont(uint32_t flag, FX_Charset charset) const;
62     int32_t SimilarityScore(int weight,
63                             bool italic,
64                             int pitch_family,
65                             bool exact_match_bonus) const;
66 
67     const ByteString m_FilePath;
68     const ByteString m_FaceName;
69     const ByteString m_FontTables;
70     const uint32_t m_FontOffset;
71     const uint32_t m_FileSize;
72     uint32_t m_Styles = 0;
73     uint32_t m_Charsets = 0;
74   };
75 
76   void ScanPath(const ByteString& path);
77   void ScanFile(const ByteString& path);
78   void ReportFace(const ByteString& path,
79                   FILE* pFile,
80                   FX_FILESIZE filesize,
81                   uint32_t offset);
82   void* GetSubstFont(const ByteString& face);
83   void* FindFont(int weight,
84                  bool bItalic,
85                  FX_Charset charset,
86                  int pitch_family,
87                  const ByteString& family,
88                  bool bMatchName);
89 
90   std::map<ByteString, std::unique_ptr<FontFaceInfo>> m_FontList;
91   std::vector<ByteString> m_PathList;
92   UnownedPtr<CFX_FontMapper> m_pMapper;
93 };
94 
95 #endif  // CORE_FXGE_CFX_FOLDERFONTINFO_H_
96