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