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