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