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 <vector>
13
14#include "core/fxcrt/cfx_retain_ptr.h"
15#include "core/fxcrt/fx_memory.h"
16#include "xfa/fgas/crt/fgas_utils.h"
17#include "xfa/fgas/font/cfgas_fontmgr.h"
18
19#define FXFONT_SUBST_ITALIC 0x02
20
21class CFGAS_FontMgr;
22class CFX_UnicodeEncoding;
23class CXFA_PDFFontMgr;
24
25class CFGAS_GEFont : public CFX_Retainable {
26 public:
27  template <typename T>
28  friend class CFX_RetainPtr;
29  template <typename T, typename... Args>
30  friend CFX_RetainPtr<T> pdfium::MakeRetain(Args&&... args);
31
32  static CFX_RetainPtr<CFGAS_GEFont> LoadFont(const FX_WCHAR* pszFontFamily,
33                                              uint32_t dwFontStyles,
34                                              uint16_t wCodePage,
35                                              CFGAS_FontMgr* pFontMgr);
36  static CFX_RetainPtr<CFGAS_GEFont> LoadFont(CFX_Font* pExternalFont,
37                                              CFGAS_FontMgr* pFontMgr);
38  static CFX_RetainPtr<CFGAS_GEFont> LoadFont(
39      std::unique_ptr<CFX_Font> pInternalFont,
40      CFGAS_FontMgr* pFontMgr);
41#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
42  static CFX_RetainPtr<CFGAS_GEFont> LoadFont(const uint8_t* pBuffer,
43                                              int32_t iLength,
44                                              CFGAS_FontMgr* pFontMgr);
45  static CFX_RetainPtr<CFGAS_GEFont> LoadFont(
46      const CFX_RetainPtr<IFGAS_Stream>& pFontStream,
47      CFGAS_FontMgr* pFontMgr,
48      bool bSaveStream);
49#endif
50
51  CFX_RetainPtr<CFGAS_GEFont> Derive(uint32_t dwFontStyles,
52                                     uint16_t wCodePage = 0);
53  uint32_t GetFontStyles() const;
54  bool GetCharWidth(FX_WCHAR wUnicode, int32_t& iWidth, bool bCharCode);
55  int32_t GetGlyphIndex(FX_WCHAR wUnicode, bool bCharCode = false);
56  int32_t GetAscent() const;
57  int32_t GetDescent() const;
58  bool GetCharBBox(FX_WCHAR wUnicode, CFX_Rect* bbox, bool bCharCode = false);
59  bool GetBBox(CFX_Rect* bbox);
60  CFX_RetainPtr<CFGAS_GEFont> GetSubstFont(int32_t iGlyphIndex);
61  CFX_Font* GetDevFont() const { return m_pFont; }
62  void SetFontProvider(CXFA_PDFFontMgr* pProvider) { m_pProvider = pProvider; }
63#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
64  void SetLogicalFontStyle(uint32_t dwLogFontStyle) {
65    m_bUseLogFontStyle = true;
66    m_dwLogFontStyle = dwLogFontStyle;
67  }
68#endif
69
70 private:
71  explicit CFGAS_GEFont(CFGAS_FontMgr* pFontMgr);
72  CFGAS_GEFont(const CFX_RetainPtr<CFGAS_GEFont>& src, uint32_t dwFontStyles);
73  ~CFGAS_GEFont() override;
74
75#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_
76  bool LoadFontInternal(const FX_WCHAR* pszFontFamily,
77                        uint32_t dwFontStyles,
78                        uint16_t wCodePage);
79  bool LoadFontInternal(const uint8_t* pBuffer, int32_t length);
80  bool LoadFontInternal(const CFX_RetainPtr<IFGAS_Stream>& pFontStream,
81                        bool bSaveStream);
82#endif
83  bool LoadFontInternal(CFX_Font* pExternalFont);
84  bool LoadFontInternal(std::unique_ptr<CFX_Font> pInternalFont);
85  bool InitFont();
86  bool GetCharBBoxInternal(FX_WCHAR wUnicode,
87                           CFX_Rect* bbox,
88                           bool bRecursive,
89                           bool bCharCode = false);
90  bool GetCharWidthInternal(FX_WCHAR wUnicode,
91                            int32_t& iWidth,
92                            bool bRecursive,
93                            bool bCharCode);
94  int32_t GetGlyphIndex(FX_WCHAR wUnicode,
95                        bool bRecursive,
96                        CFX_RetainPtr<CFGAS_GEFont>* ppFont,
97                        bool bCharCode = false);
98  CFX_WideString GetFamilyName() const;
99
100#if _FXM_PLATFORM_ != _FXM_PLATFORM_WINDOWS_
101  bool m_bUseLogFontStyle;
102  uint32_t m_dwLogFontStyle;
103#endif
104  CFX_Font* m_pFont;
105  CFX_RetainPtr<CFGAS_GEFont> m_pSrcFont;  // Only set by ctor, so no cycles.
106  CFGAS_FontMgr* const m_pFontMgr;
107  bool m_bExternalFont;
108  CFX_RetainPtr<IFGAS_Stream> m_pStream;
109  CFX_RetainPtr<IFX_SeekableReadStream> m_pFileRead;
110  std::unique_ptr<CFX_UnicodeEncoding> m_pFontEncoding;
111  std::unique_ptr<CFX_DiscreteArrayTemplate<uint16_t>> m_pCharWidthMap;
112  std::map<FX_WCHAR, CFX_Rect> m_BBoxMap;
113  CXFA_PDFFontMgr* m_pProvider;  // not owned.
114  std::vector<CFX_RetainPtr<CFGAS_GEFont>> m_SubstFonts;
115  std::map<FX_WCHAR, CFX_RetainPtr<CFGAS_GEFont>> m_FontMapper;
116};
117
118#endif  // XFA_FGAS_FONT_CFGAS_GEFONT_H_
119