In windows printing, convert src bitmap to dest bitmap using CompositeBitmap.
[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                                 int pitch = pBitmap->GetPitch();
387                                 pDst->Create(size_x, size_y, FXDIB_Rgb32);
388                                 FXSYS_memset(pDst->GetBuffer(), -1, pitch*size_y);
389                                 pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0, FXDIB_BLEND_NORMAL, NULL, FALSE, NULL);
390                                 WinDC.StretchDIBits(pDst,0,0,size_x,size_y);
391                                 delete pDst;
392                         }
393                         else
394                                 WinDC.SetDIBits(pBitmap,0,0);
395
396                 }
397         }
398 #else
399         // get clip region
400         RECT rect, cliprect;
401         rect.left = start_x;
402         rect.top = start_y;
403         rect.right = start_x + size_x;
404         rect.bottom = start_y + size_y;
405         GetClipBox(dc, &cliprect);
406         IntersectRect(&rect, &rect, &cliprect);
407         int width = rect.right - rect.left;
408         int height = rect.bottom - rect.top;
409
410 #ifdef DEBUG_TRACE
411         {
412                 char str[128];
413                 sprintf(str, "Rendering DIB %d x %d", width, height);
414                 CPDF_ModuleMgr::Get()->ReportError(999, str);
415         }
416 #endif
417
418         // Create a DIB section
419         LPVOID pBuffer;
420         BITMAPINFOHEADER bmih;
421         FXSYS_memset(&bmih, 0, sizeof bmih);
422         bmih.biSize = sizeof bmih;
423         bmih.biBitCount = 24;
424         bmih.biHeight = -height;
425         bmih.biPlanes = 1;
426         bmih.biWidth = width;
427         pContext->m_hBitmap = CreateDIBSection(dc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, &pBuffer, NULL, 0);
428         if (pContext->m_hBitmap == NULL) {
429 #if defined(DEBUG) || defined(_DEBUG)
430                 char str[128];
431                 sprintf(str, "Error CreateDIBSection: %d x %d, error code = %d", width, height, GetLastError());
432                 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
433 #else
434                 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
435 #endif
436         }
437         FXSYS_memset(pBuffer, 0xff, height*((width*3+3)/4*4));
438
439 #ifdef DEBUG_TRACE
440         {
441                 CPDF_ModuleMgr::Get()->ReportError(999, "DIBSection created");
442         }
443 #endif
444
445         // Create a device with this external buffer
446         pContext->m_pBitmap = FX_NEW CFX_DIBitmap;
447         pContext->m_pBitmap->Create(width, height, FXDIB_Rgb, (FX_LPBYTE)pBuffer);
448         pContext->m_pDevice = FX_NEW CPDF_FxgeDevice;
449         ((CPDF_FxgeDevice*)pContext->m_pDevice)->Attach(pContext->m_pBitmap);
450         
451 #ifdef DEBUG_TRACE
452         CPDF_ModuleMgr::Get()->ReportError(999, "Ready for PDF rendering");
453 #endif
454
455         // output to bitmap device
456         Func_RenderPage(pContext, page, start_x - rect.left, start_y - rect.top, size_x, size_y, rotate, flags);
457
458 #ifdef DEBUG_TRACE
459         CPDF_ModuleMgr::Get()->ReportError(999, "Finished PDF rendering");
460 #endif
461
462         // Now output to real device
463         HDC hMemDC = CreateCompatibleDC(dc);
464         if (hMemDC == NULL) {
465 #if defined(DEBUG) || defined(_DEBUG)
466                 char str[128];
467                 sprintf(str, "Error CreateCompatibleDC. Error code = %d", GetLastError());
468                 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
469 #else
470                 CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
471 #endif
472         }
473
474         HGDIOBJ hOldBitmap = SelectObject(hMemDC, pContext->m_hBitmap);
475
476 #ifdef DEBUG_TRACE
477         CPDF_ModuleMgr::Get()->ReportError(999, "Ready for screen rendering");
478 #endif
479
480         BitBlt(dc, rect.left, rect.top, width, height, hMemDC, 0, 0, SRCCOPY);
481         SelectObject(hMemDC, hOldBitmap);
482         DeleteDC(hMemDC);
483
484 #ifdef DEBUG_TRACE
485         CPDF_ModuleMgr::Get()->ReportError(999, "Finished screen rendering");
486 #endif
487
488 #endif
489         if (bBackgroundAlphaNeeded)
490         {
491                 if (pBitmap)
492                         delete pBitmap;
493                 pBitmap = NULL;
494         }
495         delete pContext;
496         pPage->RemovePrivateData((void*)1);
497 }
498 #endif
499
500 DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap, FPDF_PAGE page, int start_x, int start_y, 
501                                                 int size_x, int size_y, int rotate, int flags)
502 {
503         if (bitmap == NULL || page == NULL) return;
504         CPDF_Page* pPage = (CPDF_Page*)page;
505
506
507         CRenderContext* pContext = FX_NEW CRenderContext;
508         pPage->SetPrivateData((void*)1, pContext, DropContext);
509 #ifdef _SKIA_SUPPORT_
510         pContext->m_pDevice = FX_NEW CFX_SkiaDevice;
511
512         if (flags & FPDF_REVERSE_BYTE_ORDER)
513                 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap,0,TRUE);
514         else
515                 ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
516 #else
517         pContext->m_pDevice = FX_NEW CFX_FxgeDevice;
518
519         if (flags & FPDF_REVERSE_BYTE_ORDER)
520                 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap,0,TRUE);
521         else
522                 ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
523 #endif
524
525         Func_RenderPage(pContext, page, start_x, start_y, size_x, size_y, rotate, flags,TRUE,NULL);
526
527         delete pContext;
528         pPage->RemovePrivateData((void*)1);
529 }
530
531 DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page)
532 {
533         if (!page) return;
534         CPDFSDK_PageView* pPageView = (CPDFSDK_PageView*)(((CPDF_Page*)page))->GetPrivateData((FX_LPVOID)page);
535         if (pPageView && pPageView->IsLocked()) {
536             pPageView->TakeOverPage();
537             return;
538         }
539         delete (CPDF_Page*)page;
540
541 }
542
543 DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document)
544 {
545         if (!document)
546                 return;
547         CPDF_Document* pDoc = (CPDF_Document*)document; 
548         CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
549         if (pParser == NULL) 
550         {
551                 delete pDoc;
552                 return;
553         }
554         delete pParser;
555 //      delete pDoc;
556 }
557
558 DLLEXPORT unsigned long STDCALL FPDF_GetLastError()
559 {
560         return GetLastError();
561 }
562
563 DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,
564                                                 int rotate, int device_x, int device_y, double* page_x, double* page_y)
565 {
566         if (page == NULL || page_x == NULL || page_y == NULL) return;
567         CPDF_Page* pPage = (CPDF_Page*)page;
568
569         CPDF_Matrix page2device;
570         pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate);
571         CPDF_Matrix device2page;
572         device2page.SetReverse(page2device);
573
574         FX_FLOAT page_x_f, page_y_f;
575         device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f, page_y_f);
576
577         *page_x = (page_x_f);
578         *page_y = (page_y_f);
579 }
580
581 DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,
582                                                 int rotate, double page_x, double page_y, int* device_x, int* device_y)
583 {
584         if (page == NULL || device_x == NULL || device_y == NULL) return;
585         CPDF_Page* pPage = (CPDF_Page*)page;
586
587         CPDF_Matrix page2device;
588         pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate);
589
590         FX_FLOAT device_x_f, device_y_f;
591         page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f, device_y_f);
592
593         *device_x = FXSYS_round(device_x_f);
594         *device_y = FXSYS_round(device_y_f);
595 }
596
597 DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width, int height, int alpha)
598 {
599         CFX_DIBitmap* pBitmap = FX_NEW CFX_DIBitmap;
600         pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32);
601         return pBitmap;
602 }
603
604 DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width, int height, int format, void* first_scan, int stride)
605 {
606         FXDIB_Format fx_format;
607         switch (format) {
608                 case FPDFBitmap_Gray:
609                         fx_format = FXDIB_8bppRgb;
610                         break;
611                 case FPDFBitmap_BGR:
612                         fx_format = FXDIB_Rgb;
613                         break;
614                 case FPDFBitmap_BGRx:
615                         fx_format = FXDIB_Rgb32;
616                         break;
617                 case FPDFBitmap_BGRA:
618                         fx_format = FXDIB_Argb;
619                         break;
620                 default:
621                         return NULL;
622         }
623         CFX_DIBitmap* pBitmap = FX_NEW CFX_DIBitmap;
624         pBitmap->Create(width, height, fx_format, (FX_LPBYTE)first_scan, stride);
625         return pBitmap;
626 }
627
628 DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap, int left, int top, int width, int height, FPDF_DWORD color)
629 {
630         if (bitmap == NULL) return;
631 #ifdef _SKIA_SUPPORT_
632         CFX_SkiaDevice device;
633 #else
634         CFX_FxgeDevice device;
635 #endif
636         device.Attach((CFX_DIBitmap*)bitmap);
637         if (!((CFX_DIBitmap*)bitmap)->HasAlpha()) color |= 0xFF000000;
638         FX_RECT rect(left, top, left+width, top+height);
639         device.FillRect(&rect, color);
640 }
641
642 DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap)
643 {
644         if (bitmap == NULL) return NULL;
645         return ((CFX_DIBitmap*)bitmap)->GetBuffer();
646 }
647
648 DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap)
649 {
650         if (bitmap == NULL) return 0;
651         return ((CFX_DIBitmap*)bitmap)->GetWidth();
652 }
653
654 DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap)
655 {
656         if (bitmap == NULL) return 0;
657         return ((CFX_DIBitmap*)bitmap)->GetHeight();
658 }
659
660 DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap)
661 {
662         if (bitmap == NULL) return 0;
663         return ((CFX_DIBitmap*)bitmap)->GetPitch();
664 }
665
666 DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap)
667 {
668         if (bitmap == NULL) return;
669         delete (CFX_DIBitmap*)bitmap;
670 }
671
672 void FPDF_RenderPage_Retail(CRenderContext* pContext, FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,
673                                                 int rotate, int flags,FX_BOOL bNeedToRestore, IFSDK_PAUSE_Adapter * pause )
674 {
675         CPDF_Page* pPage = (CPDF_Page*)page;
676         if (pPage == NULL) return;
677
678         if (!pContext->m_pOptions)
679                 pContext->m_pOptions = new CPDF_RenderOptions;
680 //      CPDF_RenderOptions options;
681         if (flags & FPDF_LCD_TEXT)
682                 pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
683         else
684                 pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
685         if (flags & FPDF_NO_NATIVETEXT)
686                 pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
687         if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
688                 pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
689         if (flags & FPDF_RENDER_FORCEHALFTONE)
690                 pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
691         //Grayscale output
692         if (flags & FPDF_GRAYSCALE)
693         {
694                 pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
695                 pContext->m_pOptions->m_ForeColor = 0;
696                 pContext->m_pOptions->m_BackColor = 0xffffff;
697         }
698         const CPDF_OCContext::UsageType usage = (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
699
700         pContext->m_pOptions->m_AddFlags = flags >> 8;
701
702         pContext->m_pOptions->m_pOCContext = new CPDF_OCContext(pPage->m_pDocument, usage);
703
704
705         CFX_AffineMatrix matrix;
706         pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate); 
707
708         FX_RECT clip;
709         clip.left = start_x;
710         clip.right = start_x + size_x;
711         clip.top = start_y;
712         clip.bottom = start_y + size_y;
713         pContext->m_pDevice->SaveState();
714         pContext->m_pDevice->SetClip_Rect(&clip);
715
716         pContext->m_pContext = FX_NEW CPDF_RenderContext;
717         pContext->m_pContext->Create(pPage);
718         pContext->m_pContext->AppendObjectList(pPage, &matrix);
719
720         if (flags & FPDF_ANNOT) {
721                 pContext->m_pAnnots = FX_NEW CPDF_AnnotList(pPage);
722                 FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
723                 pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext, bPrinting, &matrix, TRUE, NULL);
724         }
725
726         pContext->m_pRenderer = FX_NEW CPDF_ProgressiveRenderer;
727         pContext->m_pRenderer->Start(pContext->m_pContext, pContext->m_pDevice, pContext->m_pOptions, pause);
728         if (bNeedToRestore)
729         {
730           pContext->m_pDevice->RestoreState();
731         }
732         
733 //#endif
734 }
735
736 DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document, int page_index, double* width, double* height)
737 {
738         CPDF_Document* pDoc = (CPDF_Document*)document;
739         if(pDoc == NULL)
740                 return FALSE;
741
742         CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
743         if (pDict == NULL) return FALSE;
744
745         CPDF_Page page;
746         page.Load(pDoc, pDict);
747         *width = page.GetPageWidth();
748         *height = page.GetPageHeight();
749
750         return TRUE;
751 }
752
753 DLLEXPORT FPDF_BOOL STDCALL FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document)
754 {
755         CPDF_Document* pDoc = (CPDF_Document*)document;
756         if (!pDoc) return TRUE;
757         CPDF_ViewerPreferences viewRef(pDoc);
758         return viewRef.PrintScaling();
759 }
760
761 DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document)
762 {
763     CPDF_Document* pDoc = (CPDF_Document*)document;
764     if (!pDoc) return 1;
765     CPDF_ViewerPreferences viewRef(pDoc);
766     return viewRef.NumCopies();
767 }
768
769 DLLEXPORT FPDF_PAGERANGE STDCALL FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document)
770 {
771     CPDF_Document* pDoc = (CPDF_Document*)document;
772     if (!pDoc) return NULL;
773     CPDF_ViewerPreferences viewRef(pDoc);
774     return viewRef.PrintPageRange();
775 }
776
777 DLLEXPORT FPDF_DUPLEXTYPE STDCALL FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document)
778 {
779     CPDF_Document* pDoc = (CPDF_Document*)document;
780     if (!pDoc) return DuplexUndefined;
781     CPDF_ViewerPreferences viewRef(pDoc);
782     CFX_ByteString duplex = viewRef.Duplex();
783     if (FX_BSTRC("Simplex") == duplex)
784         return Simplex;
785     if (FX_BSTRC("DuplexFlipShortEdge") == duplex)
786         return DuplexFlipShortEdge;
787     if (FX_BSTRC("DuplexFlipLongEdge") == duplex)
788         return DuplexFlipLongEdge;
789     return DuplexUndefined;
790 }
791
792 DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,FPDF_BYTESTRING name)
793 {
794         if (document == NULL)
795                 return NULL;
796         if (name == NULL || name[0] == 0) 
797                 return NULL;
798
799         CPDF_Document* pDoc = (CPDF_Document*)document;
800         CPDF_NameTree name_tree(pDoc, FX_BSTRC("Dests"));
801         return name_tree.LookupNamedDest(pDoc, name);
802 }