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