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