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