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_FXFA_XFA_FONTMGR_H_
8#define XFA_FXFA_XFA_FONTMGR_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_ext.h"
16#include "core/fxcrt/fx_system.h"
17#include "xfa/fgas/font/cfgas_fontmgr.h"
18#include "xfa/fxfa/fxfa.h"
19
20class CPDF_Font;
21
22struct XFA_FONTINFO {
23  uint32_t dwFontNameHash;
24  const FX_WCHAR* pPsName;
25  const FX_WCHAR* pReplaceFont;
26  uint16_t dwStyles;
27  uint16_t wCodePage;
28};
29
30class CXFA_DefFontMgr {
31 public:
32  CXFA_DefFontMgr();
33  ~CXFA_DefFontMgr();
34
35  CFX_RetainPtr<CFGAS_GEFont> GetFont(CXFA_FFDoc* hDoc,
36                                      const CFX_WideStringC& wsFontFamily,
37                                      uint32_t dwFontStyles,
38                                      uint16_t wCodePage = 0xFFFF);
39  CFX_RetainPtr<CFGAS_GEFont> GetDefaultFont(
40      CXFA_FFDoc* hDoc,
41      const CFX_WideStringC& wsFontFamily,
42      uint32_t dwFontStyles,
43      uint16_t wCodePage = 0xFFFF);
44
45 protected:
46  std::vector<CFX_RetainPtr<CFGAS_GEFont>> m_CacheFonts;
47};
48
49class CXFA_PDFFontMgr {
50 public:
51  explicit CXFA_PDFFontMgr(CXFA_FFDoc* pDoc);
52  ~CXFA_PDFFontMgr();
53
54  CFX_RetainPtr<CFGAS_GEFont> GetFont(const CFX_WideStringC& wsFontFamily,
55                                      uint32_t dwFontStyles,
56                                      CPDF_Font** pPDFFont,
57                                      bool bStrictMatch);
58  bool GetCharWidth(const CFX_RetainPtr<CFGAS_GEFont>& pFont,
59                    FX_WCHAR wUnicode,
60                    bool bCharCode,
61                    int32_t* pWidth);
62  void SetFont(const CFX_RetainPtr<CFGAS_GEFont>& pFont, CPDF_Font* pPDFFont);
63
64 protected:
65  CFX_RetainPtr<CFGAS_GEFont> FindFont(const CFX_ByteString& strFamilyName,
66                                       bool bBold,
67                                       bool bItalic,
68                                       CPDF_Font** pPDFFont,
69                                       bool bStrictMatch);
70  CFX_ByteString PsNameToFontName(const CFX_ByteString& strPsName,
71                                  bool bBold,
72                                  bool bItalic);
73  bool PsNameMatchDRFontName(const CFX_ByteStringC& bsPsName,
74                             bool bBold,
75                             bool bItalic,
76                             const CFX_ByteString& bsDRFontName,
77                             bool bStrictMatch);
78
79  CXFA_FFDoc* const m_pDoc;
80  std::map<CFX_RetainPtr<CFGAS_GEFont>, CPDF_Font*> m_FDE2PDFFont;
81  std::map<CFX_ByteString, CFX_RetainPtr<CFGAS_GEFont>> m_FontMap;
82};
83
84class CXFA_FontMgr {
85 public:
86  CXFA_FontMgr();
87  ~CXFA_FontMgr();
88
89  CFX_RetainPtr<CFGAS_GEFont> GetFont(CXFA_FFDoc* hDoc,
90                                      const CFX_WideStringC& wsFontFamily,
91                                      uint32_t dwFontStyles,
92                                      uint16_t wCodePage = 0xFFFF);
93  void LoadDocFonts(CXFA_FFDoc* hDoc);
94  void ReleaseDocFonts(CXFA_FFDoc* hDoc);
95  void SetDefFontMgr(std::unique_ptr<CXFA_DefFontMgr> pFontMgr);
96
97 protected:
98  std::unique_ptr<CXFA_DefFontMgr> m_pDefFontMgr;
99  std::map<CXFA_FFDoc*, std::unique_ptr<CXFA_PDFFontMgr>> m_PDFFontMgrMap;
100  std::map<CFX_ByteString, CFX_RetainPtr<CFGAS_GEFont>> m_FontMap;
101};
102
103#endif  //  XFA_FXFA_XFA_FONTMGR_H_
104