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 CORE_SRC_FXGE_ANDROID_FPF_SKIAFONTMGR_H_
8 #define CORE_SRC_FXGE_ANDROID_FPF_SKIAFONTMGR_H_
9 
10 #if _FX_OS_ == _FX_ANDROID_
11 
12 #include "core/include/fxge/fpf.h"
13 
14 #define FPF_SKIAFONTTYPE_Unknown 0
15 #define FPF_SKIAFONTTYPE_Path 1
16 #define FPF_SKIAFONTTYPE_File 2
17 #define FPF_SKIAFONTTYPE_Buffer 3
18 
19 class CFPF_SkiaFontDescriptor {
20  public:
CFPF_SkiaFontDescriptor()21   CFPF_SkiaFontDescriptor()
22       : m_pFamily(NULL),
23         m_dwStyle(0),
24         m_iFaceIndex(0),
25         m_dwCharsets(0),
26         m_iGlyphNum(0) {}
~CFPF_SkiaFontDescriptor()27   virtual ~CFPF_SkiaFontDescriptor() { FX_Free(m_pFamily); }
28 
GetType()29   virtual int32_t GetType() const { return FPF_SKIAFONTTYPE_Unknown; }
30 
SetFamily(const FX_CHAR * pFamily)31   void SetFamily(const FX_CHAR* pFamily) {
32     FX_Free(m_pFamily);
33     int32_t iSize = FXSYS_strlen(pFamily);
34     m_pFamily = FX_Alloc(FX_CHAR, iSize + 1);
35     FXSYS_memcpy(m_pFamily, pFamily, iSize * sizeof(FX_CHAR));
36     m_pFamily[iSize] = 0;
37   }
38   FX_CHAR* m_pFamily;
39   FX_DWORD m_dwStyle;
40   int32_t m_iFaceIndex;
41   FX_DWORD m_dwCharsets;
42   int32_t m_iGlyphNum;
43 };
44 
45 class CFPF_SkiaPathFont : public CFPF_SkiaFontDescriptor {
46  public:
CFPF_SkiaPathFont()47   CFPF_SkiaPathFont() : m_pPath(NULL) {}
~CFPF_SkiaPathFont()48   ~CFPF_SkiaPathFont() override { FX_Free(m_pPath); }
49 
50   // CFPF_SkiaFontDescriptor
GetType()51   int32_t GetType() const override { return FPF_SKIAFONTTYPE_Path; }
52 
SetPath(const FX_CHAR * pPath)53   void SetPath(const FX_CHAR* pPath) {
54     FX_Free(m_pPath);
55     int32_t iSize = FXSYS_strlen(pPath);
56     m_pPath = FX_Alloc(FX_CHAR, iSize + 1);
57     FXSYS_memcpy(m_pPath, pPath, iSize * sizeof(FX_CHAR));
58     m_pPath[iSize] = 0;
59   }
60   FX_CHAR* m_pPath;
61 };
62 
63 class CFPF_SkiaFileFont : public CFPF_SkiaFontDescriptor {
64  public:
CFPF_SkiaFileFont()65   CFPF_SkiaFileFont() : m_pFile(NULL) {}
66 
67   // CFPF_SkiaFontDescriptor
GetType()68   int32_t GetType() const override { return FPF_SKIAFONTTYPE_File; }
69   IFX_FileRead* m_pFile;
70 };
71 
72 class CFPF_SkiaBufferFont : public CFPF_SkiaFontDescriptor {
73  public:
CFPF_SkiaBufferFont()74   CFPF_SkiaBufferFont() : m_pBuffer(NULL), m_szBuffer(0) {}
75 
76   // CFPF_SkiaFontDescriptor
GetType()77   int32_t GetType() const override { return FPF_SKIAFONTTYPE_Buffer; }
78 
79   void* m_pBuffer;
80   size_t m_szBuffer;
81 };
82 
83 class CFPF_SkiaFontMgr : public IFPF_FontMgr {
84  public:
85   CFPF_SkiaFontMgr();
86   ~CFPF_SkiaFontMgr() override;
87 
88   // IFPF_FontMgr
89   void LoadSystemFonts() override;
90   void LoadPrivateFont(IFX_FileRead* pFontFile) override;
91   void LoadPrivateFont(const CFX_ByteStringC& bsFileName) override;
92   void LoadPrivateFont(void* pBuffer, size_t szBuffer) override;
93   IFPF_Font* CreateFont(const CFX_ByteStringC& bsFamilyname,
94                         uint8_t uCharset,
95                         FX_DWORD dwStyle,
96                         FX_DWORD dwMatch = 0) override;
97 
98   FX_BOOL InitFTLibrary();
99   FXFT_Face GetFontFace(IFX_FileRead* pFileRead, int32_t iFaceIndex = 0);
100   FXFT_Face GetFontFace(const CFX_ByteStringC& bsFile, int32_t iFaceIndex = 0);
101   FXFT_Face GetFontFace(const uint8_t* pBuffer,
102                         size_t szBuffer,
103                         int32_t iFaceIndex = 0);
104 
105  protected:
106   void ScanPath(const CFX_ByteStringC& path);
107   void ScanFile(const CFX_ByteStringC& file);
108   void ReportFace(FXFT_Face face, CFPF_SkiaFontDescriptor* pFontDesc);
109   void OutputSystemFonts();
110   FX_BOOL m_bLoaded;
111   CFX_PtrArray m_FontFaces;
112   FXFT_Library m_FTLibrary;
113   CFX_MapPtrToPtr m_FamilyFonts;
114 };
115 
116 #endif
117 
118 #endif  // CORE_SRC_FXGE_ANDROID_FPF_SKIAFONTMGR_H_
119