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_APPLE_APPLE_INT_H_
8#define CORE_SRC_FXGE_APPLE_APPLE_INT_H_
9
10#if _FXM_PLATFORM_  == _FXM_PLATFORM_APPLE_
11#if _FX_OS_ == _FX_MACOSX_
12#include <Carbon/Carbon.h>
13#endif
14typedef enum eFXIOSFONTCHARSET {
15    eFXFontCharsetDEFAULT       =   0,
16    eFXFontCharsetANSI          =   1,
17    eFXFontCharsetSYMBOL        =   1 << 1,
18    eFXFontCharsetSHIFTJIS      =   1 << 2,
19    eFXFontCharsetHANGEUL       =   1 << 3,
20    eFXFontCharsetGB2312        =   1 << 4,
21    eFXFontCharsetCHINESEBIG5   =   1 << 5,
22    eFXFontCharsetTHAI          =   1 << 6,
23    eFXFontCharsetEASTEUROPE    =   1 << 7,
24    eFXFontCharsetRUSSIAN       =   1 << 8,
25    eFXFontCharsetGREEK         =   1 << 9,
26    eFXFontCharsetTURKISH       =   1 << 10,
27    eFXFontCharsetHEBREW        =   1 << 11,
28    eFXFontCharsetARABIC        =   1 << 12,
29    eFXFontCharsetBALTIC        =   1 << 13,
30} FX_IOSCHARSET;
31FX_IOSCHARSET FX_GetiOSCharset(int charset);
32typedef enum eFXIOSFONTFLAG {
33    eFXFontFlagBold         =  1,
34    eFXFontFlagItalic       =  1 << 1,
35    eFXFontFlagFixedPitch   =  1 << 2,
36    eFXFontFlagSerif        =  1 << 3,
37    eFXFontFlagScript       =  1 << 4,
38} FX_IOSFONTFLAG;
39typedef struct _IOS_FONTDATA {
40    FX_DWORD        nHashCode;
41    const char*       psName;
42    FX_DWORD        charsets;
43    FX_DWORD        styles;
44} IOS_FONTDATA;
45class CQuartz2D
46{
47public:
48    void*	createGraphics(CFX_DIBitmap* bitmap);
49    void	destroyGraphics(void* graphics);
50
51    void*	CreateFont(FX_LPCBYTE pFontData, FX_DWORD dwFontSize);
52    void	DestroyFont(void* pFont);
53    void	setGraphicsTextMatrix(void* graphics, CFX_AffineMatrix* matrix);
54    FX_BOOL	drawGraphicsString(void*                graphics,
55                               void*                font,
56                               FX_FLOAT             fontSize,
57                               FX_WORD*             glyphIndices,
58                               CGPoint*          glyphPositions,
59                               FX_INT32             chars,
60                               FX_ARGB              argb,
61                               CFX_AffineMatrix*    matrix = NULL);
62    void saveGraphicsState(void* graphics);
63    void restoreGraphicsState(void* graphics);
64};
65class CApplePlatform
66{
67public:
68    CApplePlatform()
69    {
70        m_pFontMapper = NULL;
71    }
72    ~CApplePlatform()
73    {
74        if (m_pFontMapper) {
75            delete m_pFontMapper;
76        }
77    }
78    CQuartz2D	_quartz2d;
79    IFX_FontMapper* m_pFontMapper;
80};
81class CFX_QuartzDeviceDriver : public IFX_RenderDeviceDriver
82{
83public:
84    CFX_QuartzDeviceDriver(CGContextRef context, FX_INT32 deviceClass);
85    virtual ~CFX_QuartzDeviceDriver();
86
87    virtual int		GetDeviceCaps(int caps_id);
88    virtual CFX_Matrix	GetCTM() const;
89    virtual CFX_DIBitmap*   GetBackDrop()
90    {
91        return NULL;
92    }
93    virtual void*	GetPlatformSurface()
94    {
95        return NULL;
96    }
97    virtual FX_BOOL IsPSPrintDriver()
98    {
99        return FALSE;
100    }
101    virtual FX_BOOL	StartRendering()
102    {
103        return TRUE;
104    }
105    virtual void	EndRendering() {}
106    virtual void	SaveState();
107    virtual void	RestoreState(FX_BOOL bKeepSaved);
108    virtual FX_BOOL	SetClip_PathFill(const CFX_PathData* pPathData,
109                                     const CFX_AffineMatrix* pObject2Device,
110                                     int fill_mode
111                                    );
112    virtual FX_BOOL	SetClip_PathStroke(const CFX_PathData* pPathData,
113                                       const CFX_AffineMatrix* pObject2Device,
114                                       const CFX_GraphStateData* pGraphState
115                                      );
116    virtual FX_BOOL	DrawPath(const CFX_PathData* pPathData,
117                             const CFX_AffineMatrix* pObject2Device,
118                             const CFX_GraphStateData* pGraphState,
119                             FX_DWORD fill_color,
120                             FX_DWORD stroke_color,
121                             int fill_mode,
122                             int alpha_flag = 0,
123                             void* pIccTransform = NULL,
124                             int blend_type = FXDIB_BLEND_NORMAL
125                            );
126    virtual FX_BOOL	SetPixel(int x, int y, FX_DWORD color,
127                             int alpha_flag = 0, void* pIccTransform = NULL)
128    {
129        return FALSE;
130    }
131    virtual FX_BOOL FillRect(const FX_RECT* pRect, FX_DWORD fill_color,
132                             int alpha_flag = 0, void* pIccTransform = NULL, int blend_type = FXDIB_BLEND_NORMAL);
133    virtual FX_BOOL	DrawCosmeticLine(FX_FLOAT x1, FX_FLOAT y1, FX_FLOAT x2, FX_FLOAT y2, FX_DWORD color,
134                                     int alpha_flag = 0, void* pIccTransform = NULL, int blend_type = FXDIB_BLEND_NORMAL);
135    virtual FX_BOOL GetClipBox(FX_RECT* pRect);
136    virtual FX_BOOL	GetDIBits(CFX_DIBitmap* pBitmap, int left, int top, void* pIccTransform = NULL, FX_BOOL bDEdge = FALSE);
137    virtual FX_BOOL	SetDIBits(const CFX_DIBSource* pBitmap, FX_DWORD color, const FX_RECT* pSrcRect,
138                              int dest_left, int dest_top, int blend_type,
139                              int alpha_flag = 0, void* pIccTransform = NULL);
140    virtual FX_BOOL	StretchDIBits(const CFX_DIBSource* pBitmap, FX_DWORD color, int dest_left, int dest_top,
141                                  int dest_width, int dest_height, const FX_RECT* pClipRect, FX_DWORD flags,
142                                  int alpha_flag = 0, void* pIccTransform = NULL, int blend_type = FXDIB_BLEND_NORMAL);
143    virtual FX_BOOL	StartDIBits(const CFX_DIBSource* pBitmap, int bitmap_alpha, FX_DWORD color,
144                                const CFX_AffineMatrix* pMatrix, FX_DWORD flags, FX_LPVOID& handle,
145                                int alpha_flag = 0, void* pIccTransform = NULL,
146                                int blend_type = FXDIB_BLEND_NORMAL)
147    {
148        return FALSE;
149    }
150    virtual FX_BOOL	ContinueDIBits(FX_LPVOID handle, IFX_Pause* pPause)
151    {
152        return FALSE;
153    }
154    virtual void	CancelDIBits(FX_LPVOID handle) {}
155    virtual FX_BOOL DrawDeviceText(int nChars, const FXTEXT_CHARPOS* pCharPos, CFX_Font* pFont,
156                                   CFX_FontCache* pCache, const CFX_AffineMatrix* pObject2Device, FX_FLOAT font_size, FX_DWORD color,
157                                   int alpha_flag = 0, void* pIccTransform = NULL);
158    virtual void    ClearDriver();
159protected:
160    void setStrokeInfo(const CFX_GraphStateData * graphState, FX_ARGB argb, FX_FLOAT lineWidth);
161    void setFillInfo(FX_ARGB argb);
162    void setPathToContext(const CFX_PathData * pathData);
163    FX_FLOAT getLineWidth(const CFX_GraphStateData * graphState, CGAffineTransform ctm);
164    FX_BOOL CG_DrawGlypRun(int                        nChars,
165                           const FXTEXT_CHARPOS*      pCharPos,
166                           CFX_Font*                  pFont,
167                           CFX_FontCache*             pCache,
168                           const CFX_AffineMatrix*    pGlyphMatrix,
169                           const CFX_AffineMatrix*    pObject2Device,
170                           FX_FLOAT                   font_size,
171                           FX_DWORD                   argb,
172                           int                        alpha_flag,
173                           void*                      pIccTransform);
174    void    CG_SetImageTransform(int dest_left, int dest_top, int dest_width, int dest_height, CGRect* rect = NULL);
175protected:
176    CGContextRef	_context;
177    CGAffineTransform _foxitDevice2User;
178    CGAffineTransform _user2FoxitDevice;
179    FX_INT32        m_saveCount;
180
181    FX_INT32		_width;
182    FX_INT32		_height;
183    FX_INT32		_bitsPerPixel;
184    FX_INT32		_deviceClass;
185    FX_INT32		_renderCaps;
186    FX_INT32	_horzSize;
187    FX_INT32	_vertSize;
188};
189class CFX_FontProvider FX_FINAL : public IFX_FileRead
190{
191public:
192    virtual void			Release() FX_OVERRIDE
193    {
194        delete this;
195    }
196    virtual FX_FILESIZE		GetSize() FX_OVERRIDE
197    {
198        return (FX_FILESIZE)_totalSize;
199    }
200    virtual FX_BOOL			ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) FX_OVERRIDE;
201
202    virtual FX_BOOL			IsEOF() FX_OVERRIDE
203    {
204        return _offSet == _totalSize;
205    }
206    virtual FX_FILESIZE		GetPosition() FX_OVERRIDE
207    {
208        return (FX_FILESIZE)_offSet;
209    }
210    virtual size_t			ReadBlock(void* buffer, size_t size) FX_OVERRIDE;
211public:
212    CFX_FontProvider(CGFontRef cgFont);
213    ~CFX_FontProvider();
214    void            InitTableOffset();
215    unsigned long   Read(unsigned long offset, unsigned char *buffer, unsigned long count);
216protected:
217    uint32_t CalcTableCheckSum(const uint32_t *table, uint32_t numberOfBytesInTable);
218    uint32_t CalcTableDataRefCheckSum(CFDataRef dataRef);
219private:
220    CGFontRef   m_cgFont;
221    UInt32      m_iTableSize;
222    size_t      _offSet;
223    typedef struct FontHeader {
224        int32_t fVersion;
225        uint16_t fNumTables;
226        uint16_t fSearchRange;
227        uint16_t fEntrySelector;
228        uint16_t fRangeShift;
229    } FontHeader;
230    typedef struct TableEntry {
231        uint32_t fTag;
232        uint32_t fCheckSum;
233        uint32_t fOffset;
234        uint32_t fLength;
235    } TableEntry;
236    FontHeader _fontHeader;
237    unsigned char * _tableEntries;
238    size_t * _tableOffsets;
239    int _tableCount;
240    int _totalSize;
241};
242FX_UINT32 FX_GetHashCode( FX_LPCSTR pStr);
243FX_DWORD  FX_IOSGetMatchFamilyNameHashcode(FX_LPCSTR pFontName);
244FX_UINT32 FX_IOSGetFamilyNamesCount();
245FX_LPCSTR FX_IOSGetFamilyName( FX_UINT32 uIndex);
246#endif
247
248#endif  // CORE_SRC_FXGE_APPLE_APPLE_INT_H_
249