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