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_FXGE_CFX_FONTMGR_H_
8 #define CORE_FXGE_CFX_FONTMGR_H_
9 
10 #include <map>
11 #include <memory>
12 
13 #include "core/fxcrt/fx_memory_wrappers.h"
14 #include "core/fxcrt/fx_string.h"
15 #include "core/fxcrt/observed_ptr.h"
16 #include "core/fxcrt/retain_ptr.h"
17 #include "core/fxge/fx_freetype.h"
18 #include "third_party/base/optional.h"
19 #include "third_party/base/span.h"
20 
21 class CFX_Face;
22 class CFX_FontMapper;
23 class CFX_SubstFont;
24 class SystemFontInfoIface;
25 
26 class CFX_FontMgr {
27  public:
28   class FontDesc final : public Retainable, public Observable {
29    public:
30     template <typename T, typename... Args>
31     friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
32 
33     ~FontDesc() override;
34 
FontData()35     pdfium::span<uint8_t> FontData() const {
36       return {m_pFontData.get(), m_Size};
37     }
38     void SetFace(size_t index, CFX_Face* face);
39     CFX_Face* GetFace(size_t index) const;
40 
41    private:
42     FontDesc(std::unique_ptr<uint8_t, FxFreeDeleter> pData, size_t size);
43 
44     const size_t m_Size;
45     std::unique_ptr<uint8_t, FxFreeDeleter> const m_pFontData;
46     ObservedPtr<CFX_Face> m_TTCFaces[16];
47   };
48 
49   static Optional<pdfium::span<const uint8_t>> GetBuiltinFont(size_t index);
50 
51   CFX_FontMgr();
52   ~CFX_FontMgr();
53 
54   RetainPtr<FontDesc> GetCachedFontDesc(const ByteString& face_name,
55                                         int weight,
56                                         bool bItalic);
57   RetainPtr<FontDesc> AddCachedFontDesc(
58       const ByteString& face_name,
59       int weight,
60       bool bItalic,
61       std::unique_ptr<uint8_t, FxFreeDeleter> pData,
62       uint32_t size);
63 
64   RetainPtr<FontDesc> GetCachedTTCFontDesc(int ttc_size, uint32_t checksum);
65   RetainPtr<FontDesc> AddCachedTTCFontDesc(
66       int ttc_size,
67       uint32_t checksum,
68       std::unique_ptr<uint8_t, FxFreeDeleter> pData,
69       uint32_t size);
70 
71   RetainPtr<CFX_Face> NewFixedFace(const RetainPtr<FontDesc>& pDesc,
72                                    pdfium::span<const uint8_t> span,
73                                    int face_index);
74   RetainPtr<CFX_Face> FindSubstFont(const ByteString& face_name,
75                                     bool bTrueType,
76                                     uint32_t flags,
77                                     int weight,
78                                     int italic_angle,
79                                     int CharsetCP,
80                                     CFX_SubstFont* pSubstFont);
81 
82   void SetSystemFontInfo(std::unique_ptr<SystemFontInfoIface> pFontInfo);
83 
84   // Always present.
GetBuiltinMapper()85   CFX_FontMapper* GetBuiltinMapper() const { return m_pBuiltinMapper.get(); }
86 
GetFTLibrary()87   FXFT_LibraryRec* GetFTLibrary() const { return m_FTLibrary.get(); }
FTLibrarySupportsHinting()88   bool FTLibrarySupportsHinting() const { return m_FTLibrarySupportsHinting; }
89 
90  private:
91   bool FreeTypeVersionSupportsHinting() const;
92   bool SetLcdFilterMode() const;
93 
94   // Must come before |m_pBuiltinMapper| and |m_FaceMap|.
95   ScopedFXFTLibraryRec const m_FTLibrary;
96   std::unique_ptr<CFX_FontMapper> m_pBuiltinMapper;
97   std::map<ByteString, ObservedPtr<FontDesc>> m_FaceMap;
98   const bool m_FTLibrarySupportsHinting;
99 };
100 
101 #endif  // CORE_FXGE_CFX_FONTMGR_H_
102