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 XFA_FGAS_FONT_CFGAS_GEFONT_H_
8 #define XFA_FGAS_FONT_CFGAS_GEFONT_H_
9 
10 #include <map>
11 #include <memory>
12 #include <utility>
13 #include <vector>
14 
15 #include "build/build_config.h"
16 #include "core/fxcrt/fx_coordinates.h"
17 #include "core/fxcrt/fx_system.h"
18 #include "core/fxcrt/maybe_owned.h"
19 #include "core/fxcrt/retain_ptr.h"
20 #include "third_party/base/optional.h"
21 #include "xfa/fgas/font/cfgas_fontmgr.h"
22 
23 class CFX_Font;
24 class CFX_UnicodeEncodingEx;
25 class CPDF_Document;
26 class CPDF_Font;
27 
28 class CFGAS_GEFont final : public Retainable {
29  public:
30   template <typename T, typename... Args>
31   friend RetainPtr<T> pdfium::MakeRetain(Args&&... args);
32 
33   static RetainPtr<CFGAS_GEFont> LoadFont(const wchar_t* pszFontFamily,
34                                           uint32_t dwFontStyles,
35                                           uint16_t wCodePage,
36                                           CFGAS_FontMgr* pFontMgr);
37   static RetainPtr<CFGAS_GEFont> LoadFont(const RetainPtr<CPDF_Font>& pPDFFont,
38                                           CFGAS_FontMgr* pFontMgr);
39   static RetainPtr<CFGAS_GEFont> LoadFont(
40       std::unique_ptr<CFX_Font> pInternalFont,
41       CFGAS_FontMgr* pFontMgr);
42 
43   static RetainPtr<CFGAS_GEFont> LoadStockFont(CPDF_Document* pDoc,
44                                                CFGAS_FontMgr* pMgr,
45                                                const ByteString& font_family);
46 
47   uint32_t GetFontStyles() const;
48   bool GetCharWidth(wchar_t wUnicode, int32_t* pWidth);
49   int32_t GetGlyphIndex(wchar_t wUnicode);
50   int32_t GetAscent() const;
51   int32_t GetDescent() const;
52 
53   bool GetCharBBox(wchar_t wUnicode, FX_RECT* bbox);
54   bool GetBBox(FX_RECT* bbox);
55 
56   RetainPtr<CFGAS_GEFont> GetSubstFont(int32_t iGlyphIndex);
GetDevFont()57   CFX_Font* GetDevFont() const { return m_pFont.Get(); }
58 
SetLogicalFontStyle(uint32_t dwLogFontStyle)59   void SetLogicalFontStyle(uint32_t dwLogFontStyle) {
60     m_dwLogFontStyle = dwLogFontStyle;
61   }
62 
63  private:
64   explicit CFGAS_GEFont(CFGAS_FontMgr* pFontMgr);
65   ~CFGAS_GEFont() override;
66 
67 #if defined(OS_WIN)
68   bool LoadFontInternal(const wchar_t* pszFontFamily,
69                         uint32_t dwFontStyles,
70                         uint16_t wCodePage);
71   bool LoadFontInternal(const uint8_t* pBuffer, int32_t length);
72 #endif
73   bool LoadFontInternal(std::unique_ptr<CFX_Font> pInternalFont);
74   bool LoadFontInternal(const RetainPtr<CPDF_Font>& pPDFFont);
75   bool InitFont();
76   std::pair<int32_t, RetainPtr<CFGAS_GEFont>> GetGlyphIndexAndFont(
77       wchar_t wUnicode,
78       bool bRecursive);
79   WideString GetFamilyName() const;
80 
81   Optional<uint32_t> m_dwLogFontStyle;
82   RetainPtr<CPDF_Font> m_pPDFFont;  // Must come before |m_pFont|.
83   MaybeOwned<CFX_Font> m_pFont;     // Must come before |m_pFontEncoding|.
84   ObservedPtr<CFGAS_FontMgr> const m_pFontMgr;
85   std::unique_ptr<CFX_UnicodeEncodingEx> m_pFontEncoding;
86   std::map<wchar_t, int32_t> m_CharWidthMap;
87   std::map<wchar_t, FX_RECT> m_BBoxMap;
88   std::vector<RetainPtr<CFGAS_GEFont>> m_SubstFonts;
89   std::map<wchar_t, RetainPtr<CFGAS_GEFont>> m_FontMapper;
90 };
91 
92 #endif  // XFA_FGAS_FONT_CFGAS_GEFONT_H_
93