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