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