Kill Func_Renderpage global function pointer, it never changes.
[pdfium.git] / fpdfsdk / src / fpdfview.cpp
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 #include "../../core/include/fxcodec/fx_codec.h"
8 #include "../../core/include/fxcrt/fx_safe_types.h"
9 #include "../../public/fpdf_ext.h"
10 #include "../../public/fpdf_progressive.h"
11 #include "../../public/fpdfview.h"
12 #include "../../third_party/base/nonstd_unique_ptr.h"
13 #include "../../third_party/base/numerics/safe_conversions_impl.h"
14 #include "../include/fsdk_define.h"
15 #include "../include/fsdk_mgr.h"
16 #include "../include/fsdk_rendercontext.h"
17
18 CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
19 {
20         if (pFileAccess)
21                 m_FileAccess = *pFileAccess;
22 }
23
24 FX_BOOL CPDF_CustomAccess::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size)
25 {
26     if (offset < 0) {
27         return FALSE;
28     }
29     FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
30     newPos += offset;
31     if (!newPos.IsValid() || newPos.ValueOrDie() > m_FileAccess.m_FileLen) {
32         return FALSE;
33     }
34     return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,(uint8_t*) buffer, size);
35 }
36
37 //0 bit: FPDF_POLICY_MACHINETIME_ACCESS
38 static FX_DWORD foxit_sandbox_policy = 0xFFFFFFFF;
39
40 void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable)
41 {
42         switch(policy)
43         {
44         case FPDF_POLICY_MACHINETIME_ACCESS:
45                 {
46                         if(enable)
47                                 foxit_sandbox_policy |= 0x01;
48                         else
49                                 foxit_sandbox_policy &= 0xFFFFFFFE;
50                 }
51                 break;
52         default:
53                 break;
54         }
55 }
56
57 FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy)
58 {
59         switch(policy)
60         {
61         case FPDF_POLICY_MACHINETIME_ACCESS:
62                 {
63                         if(foxit_sandbox_policy&0x01)
64                                 return TRUE;
65                         else
66                                 return FALSE;
67                 }
68                 break;
69         default:
70                 break;
71         }
72         return FALSE;
73 }
74
75
76 #ifndef _T
77 #define _T(x) x
78 #endif
79
80 CCodec_ModuleMgr* g_pCodecModule = nullptr;
81
82 #if _FX_OS_ == _FX_LINUX_EMBEDDED_
83 class CFontMapper : public IPDF_FontMapper
84 {
85 public:
86         CFontMapper();
87         virtual ~CFontMapper();
88
89         virtual FT_Face FindSubstFont(
90                                                         CPDF_Document* pDoc,                            // [IN] The PDF document
91                                                         const CFX_ByteString& face_name,        // [IN] Original name
92                                                         FX_BOOL bTrueType,                                      // [IN] TrueType or Type1
93                                                         FX_DWORD flags,                                         // [IN] PDF font flags (see PDF Reference section 5.7.1)
94                                                         int font_weight,                                        // [IN] original font weight. 0 for not specified
95                                                         int CharsetCP,                                          // [IN] code page for charset (see Win32 GetACP())
96                                                         FX_BOOL bVertical,
97                                                         CPDF_SubstFont* pSubstFont                      // [OUT] Subst font data
98                                                 );
99
100         FT_Face m_SysFace;
101 };
102
103 CFontMapper* g_pFontMapper = NULL;
104 #endif          // #if _FX_OS_ == _FX_LINUX_EMBEDDED_
105
106 DLLEXPORT void STDCALL FPDF_InitLibrary()
107 {
108         g_pCodecModule = new CCodec_ModuleMgr();
109
110         CFX_GEModule::Create();
111         CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
112
113         CPDF_ModuleMgr::Create();
114         CPDF_ModuleMgr::Get()->SetCodecModule(g_pCodecModule);
115         CPDF_ModuleMgr::Get()->InitPageModule();
116         CPDF_ModuleMgr::Get()->InitRenderModule();
117         CPDF_ModuleMgr * pModuleMgr = CPDF_ModuleMgr::Get();
118         if ( pModuleMgr )
119         {
120                 pModuleMgr->LoadEmbeddedGB1CMaps();
121                 pModuleMgr->LoadEmbeddedJapan1CMaps();
122                 pModuleMgr->LoadEmbeddedCNS1CMaps();
123                 pModuleMgr->LoadEmbeddedKorea1CMaps();
124         }
125 }
126
127
128 DLLEXPORT void STDCALL FPDF_DestroyLibrary()
129 {
130
131 #if _FX_OS_ == _FX_LINUX_EMBEDDED_
132         delete g_pFontMapper;
133         g_pFontMapper = nullptr;
134 #endif
135         CPDF_ModuleMgr::Destroy();
136         CFX_GEModule::Destroy();
137         delete g_pCodecModule;
138         g_pCodecModule = nullptr;
139 }
140
141 #ifndef _WIN32
142 int g_LastError;
143 void SetLastError(int err)
144 {
145         g_LastError = err;
146 }
147
148 int GetLastError()
149 {
150         return g_LastError;
151 }
152 #endif
153
154 void ProcessParseError(FX_DWORD err_code)
155 {
156         // Translate FPDFAPI error code to FPDFVIEW error code
157         switch (err_code) {
158                 case PDFPARSE_ERROR_FILE:
159                         err_code = FPDF_ERR_FILE;
160                         break;
161                 case PDFPARSE_ERROR_FORMAT:
162                         err_code = FPDF_ERR_FORMAT;
163                         break;
164                 case PDFPARSE_ERROR_PASSWORD:
165                         err_code = FPDF_ERR_PASSWORD;
166                         break;
167                 case PDFPARSE_ERROR_HANDLER:
168                         err_code = FPDF_ERR_SECURITY;
169                         break;
170         }
171         SetLastError(err_code);
172 }
173
174 DLLEXPORT void  STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable)
175 {
176         return FSDK_SetSandBoxPolicy(policy, enable);
177 }
178
179 DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BYTESTRING password)
180 {
181         CPDF_Parser* pParser = new CPDF_Parser;
182         pParser->SetPassword(password);
183
184         FX_DWORD err_code = pParser->StartParse((const FX_CHAR*)file_path);
185         if (err_code) {
186                 delete pParser;
187                 ProcessParseError(err_code);
188                 return NULL;
189         }
190         return pParser->GetDocument();
191 }
192
193 extern void CheckUnSupportError(CPDF_Document * pDoc, FX_DWORD err_code);
194
195 class CMemFile final: public IFX_FileRead
196 {
197 public:
198         CMemFile(uint8_t* pBuf, FX_FILESIZE size):m_pBuf(pBuf),m_size(size) {}
199
200         virtual void                    Release() {delete this;}
201         virtual FX_FILESIZE             GetSize() {return m_size;}
202         virtual FX_BOOL                 ReadBlock(void* buffer, FX_FILESIZE offset, size_t size)
203         {
204             if (offset < 0) {
205                 return FALSE;
206             }
207             FX_SAFE_FILESIZE newPos = pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
208             newPos += offset;
209             if (!newPos.IsValid() || newPos.ValueOrDie() > (FX_DWORD)m_size) {
210                 return FALSE;
211             }
212             FXSYS_memcpy(buffer, m_pBuf+offset, size);
213             return TRUE;
214         }
215 private:
216         uint8_t* m_pBuf;
217         FX_FILESIZE m_size;
218 };
219 DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password)
220 {
221         CPDF_Parser* pParser = new CPDF_Parser;
222         pParser->SetPassword(password);
223         CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
224         FX_DWORD err_code = pParser->StartParse(pMemFile);
225         if (err_code) {
226                 delete pParser;
227                 ProcessParseError(err_code);
228                 return NULL;
229         }
230         CPDF_Document * pDoc = NULL;
231         pDoc = pParser?pParser->GetDocument():NULL;
232         CheckUnSupportError(pDoc, err_code);
233         return pParser->GetDocument();
234 }
235
236 DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess, FPDF_BYTESTRING password)
237 {
238         CPDF_Parser* pParser = new CPDF_Parser;
239         pParser->SetPassword(password);
240         CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
241         FX_DWORD err_code = pParser->StartParse(pFile);
242         if (err_code) {
243                 delete pParser;
244                 ProcessParseError(err_code);
245                 return NULL;
246         }
247         CPDF_Document * pDoc = NULL;
248         pDoc = pParser?pParser->GetDocument():NULL;
249         CheckUnSupportError(pDoc, err_code);
250         return pParser->GetDocument();
251 }
252
253 DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc, int* fileVersion)
254 {
255         if(!doc||!fileVersion) return FALSE;
256         *fileVersion = 0;
257         CPDF_Document* pDoc = (CPDF_Document*)doc;
258         CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
259         if(!pParser)
260                 return FALSE;
261         *fileVersion = pParser->GetFileVersion();
262         return TRUE;
263 }
264
265 // jabdelmalek: changed return type from FX_DWORD to build on Linux (and match header).
266 DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document)
267 {
268         if (document == NULL) return 0;
269         CPDF_Document*pDoc = (CPDF_Document*)document;
270         CPDF_Parser* pParser =  (CPDF_Parser*)pDoc->GetParser();
271         CPDF_Dictionary* pDict = pParser->GetEncryptDict();
272         if (pDict == NULL) return (FX_DWORD)-1;
273
274         return pDict->GetInteger("P");
275 }
276
277 DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document)
278 {
279     if (document == NULL) return -1;
280     CPDF_Document*pDoc = (CPDF_Document*)document;
281     CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
282     CPDF_Dictionary* pDict = pParser->GetEncryptDict();
283     if (pDict == NULL) return -1;
284
285     return pDict->GetInteger("R");
286 }
287
288 DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document)
289 {
290         if (document == NULL) return 0;
291         return ((CPDF_Document*)document)->GetPageCount();
292 }
293
294 DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document, int page_index)
295 {
296         if (document == NULL) return NULL;
297         if (page_index < 0 || page_index >= FPDF_GetPageCount(document)) return NULL;
298
299         CPDF_Document* pDoc = (CPDF_Document*)document;
300         if (pDoc == NULL) return NULL;
301         CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
302         if (pDict == NULL) return NULL;
303         CPDF_Page* pPage = new CPDF_Page;
304         pPage->Load(pDoc, pDict);
305         pPage->ParseContent();
306         return pPage;
307 }
308
309 DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page)
310 {
311         if (!page)
312                 return 0.0;
313         return ((CPDF_Page*)page)->GetPageWidth();
314 }
315
316 DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page)
317 {
318         if (!page) return 0.0;
319         return ((CPDF_Page*)page)->GetPageHeight();
320 }
321
322 void DropContext(void* data)
323 {
324         delete (CRenderContext*)data;
325 }
326
327 #if defined(_DEBUG) || defined(DEBUG)
328 #define DEBUG_TRACE
329 #endif
330
331 #if defined(_WIN32)
332 DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc, FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,
333                                                 int rotate, int flags)
334 {
335         if (page==NULL) return;
336         CPDF_Page* pPage = (CPDF_Page*)page;
337
338         CRenderContext* pContext = new CRenderContext;
339         pPage->SetPrivateData((void*)1, pContext, DropContext);
340
341 #ifndef _WIN32_WCE
342         CFX_DIBitmap* pBitmap = NULL;
343         FX_BOOL bBackgroundAlphaNeeded=FALSE;
344         bBackgroundAlphaNeeded = pPage->BackgroundAlphaNeeded();
345         if (bBackgroundAlphaNeeded)
346         {
347
348                 pBitmap = new CFX_DIBitmap;
349                 pBitmap->Create(size_x, size_y, FXDIB_Argb);
350                 pBitmap->Clear(0x00ffffff);
351 #ifdef _SKIA_SUPPORT_
352                 pContext->m_pDevice = new CFX_SkiaDevice;
353                 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
354 #else
355                 pContext->m_pDevice = new CFX_FxgeDevice;
356                 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
357 #endif
358         }
359         else
360             pContext->m_pDevice = new CFX_WindowsDevice(dc);
361
362         FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
363                            rotate, flags, TRUE, NULL);
364
365         if (bBackgroundAlphaNeeded)
366         {
367                 if (pBitmap)
368                 {
369                         CFX_WindowsDevice WinDC(dc);
370
371                         if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER)
372                         {
373                                 CFX_DIBitmap* pDst = new CFX_DIBitmap;
374                                 int pitch = pBitmap->GetPitch();
375                                 pDst->Create(size_x, size_y, FXDIB_Rgb32);
376                                 FXSYS_memset(pDst->GetBuffer(), -1, pitch*size_y);
377                                 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0, FXDIB_BLEND_NORMAL, NULL, FALSE, NULL);
378                                 WinDC.StretchDIBits(pDst,0,0,size_x,size_y);
379                                 delete pDst;
380                         }
381                         else
382                                 WinDC.SetDIBits(pBitmap,0,0);
383
384                 }
385         }
386 #else
387         // get clip region
388         RECT rect, cliprect;
389         rect.left = start_x;
390         rect.top = start_y;
391         rect.right = start_x + size_x;
392         rect.bottom = start_y + size_y;
393         GetClipBox(dc, &cliprect);
394         IntersectRect(&rect, &rect, &cliprect);
395         int width = rect.right - rect.left;
396         int height = rect.bottom - rect.top;
397
398 #ifdef DEBUG_TRACE
399         {
400                 char str[128];
401                 memset(str, 0, sizeof(str));
402                 FXSYS_snprintf(str, sizeof(str) - 1, "Rendering DIB %d x %d", width, height);
403                 CPDF_ModuleMgr::Get()->ReportError(999, str);
404         }
405 #endif
406
407         // Create a DIB section
408         LPVOID pBuffer;
409         BITMAPINFOHEADER bmih;
410         FXSYS_memset(&bmih, 0, sizeof bmih);
411         bmih.biSize = sizeof bmih;
412         bmih.biBitCount = 24;
413         bmih.biHeight = -height;
414         bmih.biPlanes = 1;
415         bmih.biWidth = width;
416         pContext->m_hBitmap = CreateDIBSection(dc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, &pBuffer, NULL, 0);
417         if (pContext->m_hBitmap == NULL) {
418 #if defined(DEBUG) || defined(_DEBUG)
419                 char str[128];
420                 memset(str, 0, sizeof(str));
421                 FXSYS_snprintf(str, sizeof(str) - 1, "Error CreateDIBSection: %d x %d, error code = %d", width, height, GetLastError());
422                 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
423 #else
424                 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
425 #endif
426         }
427         FXSYS_memset(pBuffer, 0xff, height*((width*3+3)/4*4));
428
429 #ifdef DEBUG_TRACE
430         {
431                 CPDF_ModuleMgr::Get()->ReportError(999, "DIBSection created");
432         }
433 #endif
434
435         // Create a device with this external buffer
436         pContext->m_pBitmap = new CFX_DIBitmap;
437         pContext->m_pBitmap->Create(width, height, FXDIB_Rgb, (uint8_t*)pBuffer);
438         pContext->m_pDevice = new CPDF_FxgeDevice;
439         ((CPDF_FxgeDevice*)pContext->m_pDevice)->Attach(pContext->m_pBitmap);
440
441 #ifdef DEBUG_TRACE
442         CPDF_ModuleMgr::Get()->ReportError(999, "Ready for PDF rendering");
443 #endif
444
445         // output to bitmap device
446         FPDF_RenderPage_Retail(pContext, page, start_x - rect.left,
447                            start_y - rect.top, size_x, size_y, rotate, flags);
448
449 #ifdef DEBUG_TRACE
450         CPDF_ModuleMgr::Get()->ReportError(999, "Finished PDF rendering");
451 #endif
452
453         // Now output to real device
454         HDC hMemDC = CreateCompatibleDC(dc);
455         if (hMemDC == NULL) {
456 #if defined(DEBUG) || defined(_DEBUG)
457                 char str[128];
458                 memset(str, 0, sizeof(str));
459                 FXSYS_snprintf(str, sizeof(str) - 1, "Error CreateCompatibleDC. Error code = %d", GetLastError());
460                 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
461 #else
462                 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
463 #endif
464         }
465
466         HGDIOBJ hOldBitmap = SelectObject(hMemDC, pContext->m_hBitmap);
467
468 #ifdef DEBUG_TRACE
469         CPDF_ModuleMgr::Get()->ReportError(999, "Ready for screen rendering");
470 #endif
471
472         BitBlt(dc, rect.left, rect.top, width, height, hMemDC, 0, 0, SRCCOPY);
473         SelectObject(hMemDC, hOldBitmap);
474         DeleteDC(hMemDC);
475
476 #ifdef DEBUG_TRACE
477         CPDF_ModuleMgr::Get()->ReportError(999, "Finished screen rendering");
478 #endif
479
480 #endif
481     if (bBackgroundAlphaNeeded) {
482         delete pBitmap;
483         pBitmap = NULL;
484     }
485     delete pContext;
486     pPage->RemovePrivateData((void*)1);
487 }
488 #endif
489
490 DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap, FPDF_PAGE page, int start_x, int start_y,
491                                                 int size_x, int size_y, int rotate, int flags)
492 {
493         if (bitmap == NULL || page == NULL) return;
494         CPDF_Page* pPage = (CPDF_Page*)page;
495
496
497         CRenderContext* pContext = new CRenderContext;
498         pPage->SetPrivateData((void*)1, pContext, DropContext);
499 #ifdef _SKIA_SUPPORT_
500         pContext->m_pDevice = new CFX_SkiaDevice;
501
502         if (flags & FPDF_REVERSE_BYTE_ORDER)
503                 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap,0,TRUE);
504         else
505                 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
506 #else
507         pContext->m_pDevice = new CFX_FxgeDevice;
508
509         if (flags & FPDF_REVERSE_BYTE_ORDER)
510                 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap,0,TRUE);
511         else
512                 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
513 #endif
514
515         FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
516                            rotate, flags, TRUE, NULL);
517
518         delete pContext;
519         pPage->RemovePrivateData((void*)1);
520 }
521
522 DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page)
523 {
524         if (!page) return;
525         CPDFSDK_PageView* pPageView = (CPDFSDK_PageView*)(((CPDF_Page*)page))->GetPrivateData((void*)page);
526         if (pPageView && pPageView->IsLocked()) {
527             pPageView->TakeOverPage();
528             return;
529         }
530         delete (CPDF_Page*)page;
531
532 }
533
534 DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document)
535 {
536         if (!document)
537                 return;
538         CPDF_Document* pDoc = (CPDF_Document*)document;
539         CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
540         if (pParser == NULL)
541         {
542                 delete pDoc;
543                 return;
544         }
545         delete pParser;
546 }
547
548 DLLEXPORT unsigned long STDCALL FPDF_GetLastError()
549 {
550         return GetLastError();
551 }
552
553 DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,
554                                                 int rotate, int device_x, int device_y, double* page_x, double* page_y)
555 {
556         if (page == NULL || page_x == NULL || page_y == NULL) return;
557         CPDF_Page* pPage = (CPDF_Page*)page;
558
559         CPDF_Matrix page2device;
560         pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate);
561         CPDF_Matrix device2page;
562         device2page.SetReverse(page2device);
563
564         FX_FLOAT page_x_f, page_y_f;
565         device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f, page_y_f);
566
567         *page_x = (page_x_f);
568         *page_y = (page_y_f);
569 }
570
571 DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,
572                                                 int rotate, double page_x, double page_y, int* device_x, int* device_y)
573 {
574         if (page == NULL || device_x == NULL || device_y == NULL) return;
575         CPDF_Page* pPage = (CPDF_Page*)page;
576
577         CPDF_Matrix page2device;
578         pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate);
579
580         FX_FLOAT device_x_f, device_y_f;
581         page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f, device_y_f);
582
583         *device_x = FXSYS_round(device_x_f);
584         *device_y = FXSYS_round(device_y_f);
585 }
586
587 DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width, int height, int alpha)
588 {
589     nonstd::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
590     if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
591         return NULL;
592     }
593     return pBitmap.release();
594 }
595
596 DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width, int height, int format, void* first_scan, int stride)
597 {
598         FXDIB_Format fx_format;
599         switch (format) {
600                 case FPDFBitmap_Gray:
601                         fx_format = FXDIB_8bppRgb;
602                         break;
603                 case FPDFBitmap_BGR:
604                         fx_format = FXDIB_Rgb;
605                         break;
606                 case FPDFBitmap_BGRx:
607                         fx_format = FXDIB_Rgb32;
608                         break;
609                 case FPDFBitmap_BGRA:
610                         fx_format = FXDIB_Argb;
611                         break;
612                 default:
613                         return NULL;
614         }
615         CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
616         pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
617         return pBitmap;
618 }
619
620 DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap, int left, int top, int width, int height, FPDF_DWORD color)
621 {
622         if (bitmap == NULL) return;
623 #ifdef _SKIA_SUPPORT_
624         CFX_SkiaDevice device;
625 #else
626         CFX_FxgeDevice device;
627 #endif
628         device.Attach((CFX_DIBitmap*)bitmap);
629         if (!((CFX_DIBitmap*)bitmap)->HasAlpha()) color |= 0xFF000000;
630         FX_RECT rect(left, top, left+width, top+height);
631         device.FillRect(&rect, color);
632 }
633
634 DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap)
635 {
636         if (bitmap == NULL) return NULL;
637         return ((CFX_DIBitmap*)bitmap)->GetBuffer();
638 }
639
640 DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap)
641 {
642         if (bitmap == NULL) return 0;
643         return ((CFX_DIBitmap*)bitmap)->GetWidth();
644 }
645
646 DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap)
647 {
648         if (bitmap == NULL) return 0;
649         return ((CFX_DIBitmap*)bitmap)->GetHeight();
650 }
651
652 DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap)
653 {
654         if (bitmap == NULL) return 0;
655         return ((CFX_DIBitmap*)bitmap)->GetPitch();
656 }
657
658 DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap)
659 {
660     delete (CFX_DIBitmap*)bitmap;
661 }
662
663 void FPDF_RenderPage_Retail(CRenderContext* pContext, FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,
664                             int rotate, int flags,FX_BOOL bNeedToRestore, IFSDK_PAUSE_Adapter * pause )
665 {
666         CPDF_Page* pPage = (CPDF_Page*)page;
667         if (pPage == NULL) return;
668
669         if (!pContext->m_pOptions)
670                 pContext->m_pOptions = new CPDF_RenderOptions;
671
672         if (flags & FPDF_LCD_TEXT)
673                 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
674         else
675                 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
676         if (flags & FPDF_NO_NATIVETEXT)
677                 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
678         if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
679                 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
680         if (flags & FPDF_RENDER_FORCEHALFTONE)
681                 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
682         if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
683                 pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
684         if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
685                 pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
686         if (flags & FPDF_RENDER_NO_SMOOTHPATH)
687                 pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
688         //Grayscale output
689         if (flags & FPDF_GRAYSCALE)
690         {
691                 pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
692                 pContext->m_pOptions->m_ForeColor = 0;
693                 pContext->m_pOptions->m_BackColor = 0xffffff;
694         }
695         const CPDF_OCContext::UsageType usage = (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
696         pContext->m_pOptions->m_AddFlags = flags >> 8;
697         pContext->m_pOptions->m_pOCContext = new CPDF_OCContext(pPage->m_pDocument, usage);
698
699         CFX_AffineMatrix matrix;
700         pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
701
702         FX_RECT clip;
703         clip.left = start_x;
704         clip.right = start_x + size_x;
705         clip.top = start_y;
706         clip.bottom = start_y + size_y;
707         pContext->m_pDevice->SaveState();
708         pContext->m_pDevice->SetClip_Rect(&clip);
709
710         pContext->m_pContext = new CPDF_RenderContext;
711         pContext->m_pContext->Create(pPage);
712         pContext->m_pContext->AppendObjectList(pPage, &matrix);
713
714         if (flags & FPDF_ANNOT) {
715                 pContext->m_pAnnots = new CPDF_AnnotList(pPage);
716                 FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
717                 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext, bPrinting, &matrix, TRUE, NULL);
718         }
719
720         pContext->m_pRenderer = new CPDF_ProgressiveRenderer;
721         pContext->m_pRenderer->Start(pContext->m_pContext, pContext->m_pDevice, pContext->m_pOptions, pause);
722         if (bNeedToRestore)
723         {
724                 pContext->m_pDevice->RestoreState();
725         }
726 }
727
728 DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document, int page_index, double* width, double* height)
729 {
730         CPDF_Document* pDoc = (CPDF_Document*)document;
731         if(pDoc == NULL)
732                 return FALSE;
733
734         CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
735         if (pDict == NULL) return FALSE;
736
737         CPDF_Page page;
738         page.Load(pDoc, pDict);
739         *width = page.GetPageWidth();
740         *height = page.GetPageHeight();
741
742         return TRUE;
743 }
744
745 DLLEXPORT FPDF_BOOL STDCALL FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document)
746 {
747         CPDF_Document* pDoc = (CPDF_Document*)document;
748         if (!pDoc) return TRUE;
749         CPDF_ViewerPreferences viewRef(pDoc);
750         return viewRef.PrintScaling();
751 }
752
753 DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document)
754 {
755     CPDF_Document* pDoc = (CPDF_Document*)document;
756     if (!pDoc) return 1;
757     CPDF_ViewerPreferences viewRef(pDoc);
758     return viewRef.NumCopies();
759 }
760
761 DLLEXPORT FPDF_PAGERANGE STDCALL FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document)
762 {
763     CPDF_Document* pDoc = (CPDF_Document*)document;
764     if (!pDoc) return NULL;
765     CPDF_ViewerPreferences viewRef(pDoc);
766     return viewRef.PrintPageRange();
767 }
768
769 DLLEXPORT FPDF_DUPLEXTYPE STDCALL FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document)
770 {
771     CPDF_Document* pDoc = (CPDF_Document*)document;
772     if (!pDoc) return DuplexUndefined;
773     CPDF_ViewerPreferences viewRef(pDoc);
774     CFX_ByteString duplex = viewRef.Duplex();
775     if (FX_BSTRC("Simplex") == duplex)
776         return Simplex;
777     if (FX_BSTRC("DuplexFlipShortEdge") == duplex)
778         return DuplexFlipShortEdge;
779     if (FX_BSTRC("DuplexFlipLongEdge") == duplex)
780         return DuplexFlipLongEdge;
781     return DuplexUndefined;
782 }
783
784 DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document)
785 {
786     if (!document) return 0;
787     CPDF_Document* pDoc = (CPDF_Document*)document;
788
789     CPDF_Dictionary* pRoot = pDoc->GetRoot();
790     if (!pRoot) return 0;
791
792     CPDF_NameTree nameTree(pDoc, FX_BSTRC("Dests"));
793     int count = nameTree.GetCount();
794     CPDF_Dictionary* pDest = pRoot->GetDict(FX_BSTRC("Dests"));
795     if (pDest)
796         count += pDest->GetCount();
797     return count;
798 }
799
800 DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,FPDF_BYTESTRING name)
801 {
802         if (!document)
803                 return NULL;
804         if (!name || name[0] == 0)
805                 return NULL;
806
807         CPDF_Document* pDoc = (CPDF_Document*)document;
808         CPDF_NameTree name_tree(pDoc, FX_BSTRC("Dests"));
809         return name_tree.LookupNamedDest(pDoc, name);
810 }
811
812 DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index, void* buffer, long* buflen)
813 {
814     if (!buffer)
815         *buflen = 0;
816     if (!document || index < 0) return NULL;
817     CPDF_Document* pDoc = (CPDF_Document*)document;
818
819     CPDF_Dictionary* pRoot = pDoc->GetRoot();
820     if (!pRoot) return NULL;
821
822     CPDF_Object* pDestObj = NULL;
823     CFX_ByteString bsName;
824     CPDF_NameTree nameTree(pDoc, FX_BSTRC("Dests"));
825     int count = nameTree.GetCount();
826     if (index >= count) {
827         CPDF_Dictionary* pDest = pRoot->GetDict(FX_BSTRC("Dests"));
828         if (!pDest) return NULL;
829         if (index >= count + pDest->GetCount()) return NULL;
830         index -= count;
831         FX_POSITION pos = pDest->GetStartPos();
832         int i = 0;
833         while (pos) {
834             pDestObj = pDest->GetNextElement(pos, bsName);
835             if (!pDestObj) continue;
836             if (i == index) break;
837             i++;
838         }
839     } else {
840         pDestObj = nameTree.LookupValue(index, bsName);
841     }
842     if (!pDestObj) return NULL;
843     if (pDestObj->GetType() == PDFOBJ_DICTIONARY) {
844         pDestObj = ((CPDF_Dictionary*)pDestObj)->GetArray(FX_BSTRC("D"));
845         if (!pDestObj) return NULL;
846     }
847     if (pDestObj->GetType() != PDFOBJ_ARRAY) return NULL;
848     CFX_WideString wsName = PDF_DecodeText(bsName);
849     CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
850     unsigned int len = utf16Name.GetLength();
851     if (!buffer) {
852         *buflen = len;
853     } else if (*buflen >= len) {
854         memcpy(buffer, utf16Name.c_str(), len);
855         *buflen = len;
856     } else {
857         *buflen = -1;
858     }
859     return (FPDF_DEST)pDestObj;
860 }