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