Changes to master to more closely match XFA.
[pdfium.git] / fpdfsdk / src / fpdfview.cpp
index 453b006..dfa91d1 100644 (file)
-// Copyright 2014 PDFium Authors. All rights reserved.\r
-// Use of this source code is governed by a BSD-style license that can be\r
-// found in the LICENSE file.\r
\r
-// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com\r
-\r
-#include "../include/fsdk_define.h"\r
-#include "../include/fpdfview.h"\r
-#include "../include/fsdk_rendercontext.h"\r
-#include "../include/fpdf_progressive.h"\r
-#include "../include/fpdf_ext.h"\r
-\r
-\r
-CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)\r
-{\r
-       m_FileAccess = *pFileAccess;\r
-       m_BufferOffset = (FX_DWORD)-1;\r
-}\r
-\r
-FX_BOOL CPDF_CustomAccess::GetByte(FX_DWORD pos, FX_BYTE& ch)\r
-{\r
-       if (pos >= m_FileAccess.m_FileLen) return FALSE;\r
-       if (m_BufferOffset == (FX_DWORD)-1 || pos < m_BufferOffset || pos >= m_BufferOffset + 512) {\r
-               // Need to read from file access\r
-               m_BufferOffset = pos;\r
-               int size = 512;\r
-               if (pos + 512 > m_FileAccess.m_FileLen)\r
-                       size = m_FileAccess.m_FileLen - pos;\r
-               if (!m_FileAccess.m_GetBlock(m_FileAccess.m_Param, m_BufferOffset, m_Buffer, size))\r
-                       return FALSE;\r
-       }\r
-       ch = m_Buffer[pos - m_BufferOffset];\r
-       return TRUE;\r
-}\r
-\r
-FX_BOOL CPDF_CustomAccess::GetBlock(FX_DWORD pos, FX_LPBYTE pBuf, FX_DWORD size)\r
-{\r
-       if (pos + size > m_FileAccess.m_FileLen) return FALSE;\r
-       return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, pos, pBuf, size);\r
-}\r
-\r
-FX_BOOL CPDF_CustomAccess::ReadBlock(void* buffer, FX_FILESIZE offset, size_t size)\r
-{\r
-       //      m_FileAccess = *pFileAccess;\r
-       //      m_BufferOffset = (FX_DWORD)-1;\r
-       if (offset + size > m_FileAccess.m_FileLen) return FALSE;\r
-       return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset,(FX_LPBYTE) buffer, size);\r
-\r
-       //      return FALSE;\r
-}\r
-\r
-//0 bit: FPDF_POLICY_MACHINETIME_ACCESS\r
-static FX_DWORD foxit_sandbox_policy = 0xFFFFFFFF;\r
-\r
-void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable)\r
-{\r
-       switch(policy)\r
-       {\r
-       case FPDF_POLICY_MACHINETIME_ACCESS:\r
-               {\r
-                       if(enable)\r
-                               foxit_sandbox_policy |= 0x01;\r
-                       else\r
-                               foxit_sandbox_policy &= 0xFFFFFFFE;\r
-               }\r
-               break;\r
-       default:\r
-               break;\r
-       }\r
-}\r
-\r
-FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy)\r
-{\r
-       switch(policy)\r
-       {\r
-       case FPDF_POLICY_MACHINETIME_ACCESS:\r
-               {\r
-                       if(foxit_sandbox_policy&0x01)\r
-                               return TRUE;\r
-                       else\r
-                               return FALSE;\r
-               }\r
-               break;\r
-       default:\r
-               break;\r
-       }\r
-       return FALSE;\r
-}\r
-\r
-\r
-#ifndef _T\r
-#define _T(x) x\r
-#endif\r
-\r
-#ifdef API5\r
-       CPDF_ModuleMgr* g_pModuleMgr = NULL;\r
-#else\r
-       CCodec_ModuleMgr*       g_pCodecModule = NULL;\r
-#ifdef _FXSDK_OPENSOURCE_\r
-       FXMEM_FoxitMgr* g_pFoxitMgr = NULL;\r
-#endif\r
-#endif\r
-\r
-//extern CPDFSDK_FormFillApp* g_pFormFillApp;\r
-\r
-#if _FX_OS_ == _FX_LINUX_EMBEDDED_\r
-class CFontMapper : public IPDF_FontMapper\r
-{\r
-public:\r
-       CFontMapper();\r
-       virtual ~CFontMapper();\r
-\r
-       virtual FT_Face FindSubstFont(\r
-                                                       CPDF_Document* pDoc,                            // [IN] The PDF document\r
-                                                       const CFX_ByteString& face_name,        // [IN] Original name\r
-                                                       FX_BOOL bTrueType,                                      // [IN] TrueType or Type1\r
-                                                       FX_DWORD flags,                                         // [IN] PDF font flags (see PDF Reference section 5.7.1)\r
-                                                       int font_weight,                                        // [IN] original font weight. 0 for not specified\r
-                                                       int CharsetCP,                                          // [IN] code page for charset (see Win32 GetACP())\r
-                                                       FX_BOOL bVertical,\r
-                                                       CPDF_SubstFont* pSubstFont                      // [OUT] Subst font data\r
-                                               );\r
-\r
-       FT_Face m_SysFace;\r
-};\r
-\r
-CFontMapper* g_pFontMapper = NULL;\r
-#endif         // #if _FX_OS_ == _FX_LINUX_EMBEDDED_\r
-\r
-DLLEXPORT void STDCALL FPDF_InitLibrary(FX_LPVOID hInstance)\r
-{\r
-#ifdef API5\r
-       CPDF_ModuleMgr::Create();\r
-       g_pModuleMgr = CPDF_ModuleMgr::Get();\r
-        #if _FX_OS_ == _FX_WIN32_MOBILE_ || _FX_OS_ == _FX_LINUX_EMBEDDED_\r
-               g_pModuleMgr->InitEmbedded();\r
-        #ifdef _GB1_CMAPS_\r
-               g_pModuleMgr->LoadEmbeddedGB1CMaps();\r
-        #endif\r
-        #ifdef _GB1_CMAPS_4_\r
-               g_pModuleMgr->LoadEmbeddedGB1CMaps_4();\r
-        #endif\r
-        #ifdef _CNS1_CMAPS_\r
-               g_pModuleMgr->LoadEmbeddedCNS1CMaps();\r
-        #endif\r
-        #ifdef _JAPAN1_CMAPS_\r
-               g_pModuleMgr->LoadEmbeddedJapan1CMaps();\r
-        #endif\r
-        #ifdef _JAPAN1_CMAPS_6_\r
-               g_pModuleMgr->LoadEmbeddedJapan1CMaps_6();\r
-        #endif\r
-        #ifdef _KOREA1_CMAPS_\r
-               g_pModuleMgr->LoadEmbeddedKorea1CMaps();\r
-        #endif\r
-        #ifdef _JPX_DECODER_\r
-               g_pModuleMgr->InitJpxModule();\r
-               g_pModuleMgr->InitJbig2Module();\r
-        //     g_pModuleMgr->InitIccModule();\r
-        #endif\r
-        #else\r
-               g_pModuleMgr->InitDesktop();\r
-        #endif\r
-#else\r
-#ifdef _FXSDK_OPENSOURCE_\r
-       g_pFoxitMgr = FXMEM_CreateMemoryMgr(1024 * 1024 * 32, TRUE);\r
-#endif\r
-       g_pCodecModule = CCodec_ModuleMgr::Create();\r
-       \r
-       CFX_GEModule::Create();\r
-       CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);\r
-       \r
-       CPDF_ModuleMgr::Create();\r
-       CPDF_ModuleMgr::Get()->SetCodecModule(g_pCodecModule);\r
-       CPDF_ModuleMgr::Get()->InitPageModule();\r
-       CPDF_ModuleMgr::Get()->InitRenderModule();\r
-#ifdef FOXIT_CHROME_BUILD\r
-       CPDF_ModuleMgr * pModuleMgr = CPDF_ModuleMgr::Get();\r
-       if ( pModuleMgr )\r
-       {\r
-               pModuleMgr->LoadEmbeddedGB1CMaps();\r
-               pModuleMgr->LoadEmbeddedJapan1CMaps();\r
-               pModuleMgr->LoadEmbeddedCNS1CMaps();\r
-               pModuleMgr->LoadEmbeddedKorea1CMaps();\r
-       }\r
-#endif \r
-#endif\r
-\r
-#ifdef _WIN32\r
-       // Get module path\r
-       TCHAR app_path[MAX_PATH];\r
-       ::GetModuleFileName((HINSTANCE)hInstance, app_path, MAX_PATH);\r
-       size_t len = _tcslen(app_path);\r
-       for (size_t i = len; i >= 0; i --)\r
-               if (app_path[i] == '\\') {\r
-                       app_path[i] = 0;\r
-                       break;\r
-               }\r
-               \r
-#ifdef _UNICODE\r
-               #ifndef _FXSDK_OPENSOURCE_\r
-               CPDF_ModuleMgr::Get()->SetModulePath(NULL, CFX_ByteString::FromUnicode(app_path));\r
-               #endif\r
-#else\r
-#ifndef _FXSDK_OPENSOURCE_\r
-               CPDF_ModuleMgr::Get()->SetModulePath(NULL, app_path);\r
-#endif\r
-#endif\r
-#endif\r
-}\r
-\r
-\r
-DLLEXPORT void STDCALL FPDF_DestroyLibrary()\r
-{\r
-\r
-#if _FX_OS_ == _FX_LINUX_EMBEDDED_\r
-       if (g_pFontMapper) delete g_pFontMapper;\r
-#endif\r
-#ifdef API5\r
-       g_pModuleMgr->Destroy();\r
-#else\r
-       CPDF_ModuleMgr::Destroy();\r
-       CFX_GEModule::Destroy();\r
-       g_pCodecModule->Destroy();\r
-#endif\r
-#ifndef _FXSDK_OPENSOURCE_\r
-       FXMEM_CollectAll(FXMEM_GetDefaultMgr());\r
-#else\r
-       FXMEM_DestroyFoxitMgr(g_pFoxitMgr);\r
-#endif\r
-}\r
-\r
-#ifndef _WIN32\r
-int g_LastError;\r
-void SetLastError(int err)\r
-{\r
-       g_LastError = err;\r
-}\r
-\r
-int GetLastError()\r
-{\r
-       return g_LastError;\r
-}\r
-#endif\r
-\r
-void ProcessParseError(FX_DWORD err_code)\r
-{\r
-       // Translate FPDFAPI error code to FPDFVIEW error code\r
-       switch (err_code) {\r
-               case PDFPARSE_ERROR_FILE:\r
-                       err_code = FPDF_ERR_FILE;\r
-                       break;\r
-               case PDFPARSE_ERROR_FORMAT:\r
-                       err_code = FPDF_ERR_FORMAT;\r
-                       break;\r
-               case PDFPARSE_ERROR_PASSWORD:\r
-                       err_code = FPDF_ERR_PASSWORD;\r
-                       break;\r
-               case PDFPARSE_ERROR_HANDLER:\r
-                       err_code = FPDF_ERR_SECURITY;\r
-                       break;\r
-       }\r
-       SetLastError(err_code);\r
-}\r
-\r
-DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable)\r
-{\r
-       return FSDK_SetSandBoxPolicy(policy, enable);\r
-}\r
-\r
-DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BYTESTRING password)\r
-{\r
-       CPDF_Parser* pParser = FX_NEW CPDF_Parser;\r
-       pParser->SetPassword(password);\r
-       try {\r
-               FX_DWORD err_code = pParser->StartParse((FX_LPCSTR)file_path);\r
-               if (err_code) {\r
-                       delete pParser;\r
-                       ProcessParseError(err_code);\r
-                       return NULL;\r
-               }\r
-       }\r
-       catch (...) {\r
-               delete pParser;\r
-               SetLastError(FPDF_ERR_UNKNOWN);\r
-               return NULL;\r
-       }\r
-       return pParser->GetDocument();\r
-}\r
-\r
-extern void CheckUnSupportError(CPDF_Document * pDoc, FX_DWORD err_code);\r
-\r
-class CMemFile: public IFX_FileRead, public CFX_Object\r
-{\r
-public:\r
-       CMemFile(FX_BYTE* pBuf, FX_FILESIZE size):m_pBuf(pBuf),m_size(size) {}\r
-\r
-       virtual void                    Release() {delete this;}\r
-       virtual FX_FILESIZE             GetSize() {return m_size;}\r
-       virtual FX_BOOL                 ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) \r
-       {\r
-               if(offset+size > (FX_DWORD)m_size) return FALSE;\r
-               FXSYS_memcpy(buffer, m_pBuf+offset, size);\r
-               return TRUE;\r
-       }\r
-private:\r
-       FX_BYTE* m_pBuf;\r
-       FX_FILESIZE m_size;\r
-};\r
-DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password)\r
-{\r
-       CPDF_Parser* pParser = FX_NEW CPDF_Parser;\r
-       pParser->SetPassword(password);\r
-       try {\r
-               CMemFile* pMemFile = FX_NEW CMemFile((FX_BYTE*)data_buf, size);\r
-               FX_DWORD err_code = pParser->StartParse(pMemFile);\r
-               if (err_code) {\r
-                       delete pParser;\r
-                       ProcessParseError(err_code);\r
-                       return NULL;\r
-               }\r
-               CPDF_Document * pDoc = NULL;\r
-               pDoc = pParser?pParser->GetDocument():NULL;\r
-               CheckUnSupportError(pDoc, err_code);\r
-       }\r
-       catch (...) {\r
-               delete pParser;\r
-               SetLastError(FPDF_ERR_UNKNOWN);\r
-               return NULL;\r
-       }\r
-       return pParser->GetDocument();\r
-}\r
-\r
-DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess, FPDF_BYTESTRING password)\r
-{\r
-       CPDF_Parser* pParser = FX_NEW CPDF_Parser;\r
-       pParser->SetPassword(password);\r
-       CPDF_CustomAccess* pFile = FX_NEW CPDF_CustomAccess(pFileAccess);\r
-       try {\r
-               FX_DWORD err_code = pParser->StartParse(pFile);\r
-               if (err_code) {\r
-                       delete pParser;\r
-                       ProcessParseError(err_code);\r
-                       return NULL;\r
-               }\r
-               CPDF_Document * pDoc = NULL;\r
-               pDoc = pParser?pParser->GetDocument():NULL;\r
-               CheckUnSupportError(pDoc, err_code);\r
-       }\r
-       catch (...) {\r
-               delete pParser;\r
-               SetLastError(FPDF_ERR_UNKNOWN);\r
-               return NULL;\r
-       }\r
-       return pParser->GetDocument();\r
-}\r
-\r
-DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc, int* fileVersion)\r
-{\r
-       if(!doc||!fileVersion) return FALSE;\r
-       *fileVersion = 0;\r
-       CPDF_Document* pDoc = (CPDF_Document*)doc;\r
-       CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();\r
-       if(!pParser)\r
-               return FALSE;\r
-       *fileVersion = pParser->GetFileVersion();\r
-       return TRUE;\r
-}\r
-\r
-// jabdelmalek: changed return type from FX_DWORD to build on Linux (and match header).\r
-DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document)\r
-{\r
-       if (document == NULL) return 0;\r
-       CPDF_Document*pDoc = (CPDF_Document*)document;\r
-       CPDF_Parser* pParser =  (CPDF_Parser*)pDoc->GetParser();\r
-       CPDF_Dictionary* pDict = pParser->GetEncryptDict();\r
-       if (pDict == NULL) return (FX_DWORD)-1;\r
-\r
-       return pDict->GetInteger("P");\r
-}\r
-\r
-DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document)\r
-{\r
-       if (document == NULL) return 0;\r
-       return ((CPDF_Document*)document)->GetPageCount();\r
-}\r
-\r
-DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document, int page_index)\r
-{\r
-       if (document == NULL) return NULL;\r
-       if (page_index < 0 || page_index >= FPDF_GetPageCount(document)) return NULL;\r
-//     CPDF_Parser* pParser = (CPDF_Parser*)document;\r
-       CPDF_Document* pDoc = (CPDF_Document*)document;\r
-       if (pDoc == NULL) return NULL;\r
-       CPDF_Dictionary* pDict = pDoc->GetPage(page_index);\r
-       if (pDict == NULL) return NULL;\r
-       CPDF_Page* pPage = FX_NEW CPDF_Page;\r
-       pPage->Load(pDoc, pDict);\r
-       try {\r
-               pPage->ParseContent();\r
-       }\r
-       catch (...) {\r
-               delete pPage;\r
-               return NULL;\r
-       }\r
-       \r
-//     CheckUnSupportError(pDoc, 0);\r
-\r
-       return pPage;\r
-}\r
-\r
-DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page)\r
-{\r
-       if (!page)\r
-               return 0.0;\r
-       return ((CPDF_Page*)page)->GetPageWidth();\r
-}\r
-\r
-DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page)\r
-{\r
-       if (!page) return 0.0;\r
-       return ((CPDF_Page*)page)->GetPageHeight();\r
-}\r
-\r
-void DropContext(void* data)\r
-{\r
-       delete (CRenderContext*)data;\r
-}\r
-\r
-void FPDF_RenderPage_Retail(CRenderContext* pContext, FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,\r
-                                               int rotate, int flags,FX_BOOL bNeedToRestore, IFSDK_PAUSE_Adapter * pause  );\r
-void (*Func_RenderPage)(CRenderContext*, FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,\r
-                                               int rotate, int flags,FX_BOOL bNeedToRestore, IFSDK_PAUSE_Adapter * pause  ) = FPDF_RenderPage_Retail;\r
-\r
-#if defined(_DEBUG) || defined(DEBUG)\r
-#define DEBUG_TRACE\r
-#endif\r
-\r
-#if defined(_WIN32)\r
-DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc, FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,\r
-                                               int rotate, int flags)\r
-{\r
-       if (page==NULL) return;\r
-       CPDF_Page* pPage = (CPDF_Page*)page;\r
-\r
-       CRenderContext* pContext = FX_NEW CRenderContext;\r
-       pPage->SetPrivateData((void*)1, pContext, DropContext);\r
-\r
-#ifndef _WIN32_WCE\r
-       CFX_DIBitmap* pBitmap = NULL;\r
-       FX_BOOL bBackgroundAlphaNeeded=FALSE;\r
-       bBackgroundAlphaNeeded = pPage->BackgroundAlphaNeeded();\r
-       if (bBackgroundAlphaNeeded)\r
-       {\r
-               \r
-               pBitmap = FX_NEW CFX_DIBitmap;\r
-               pBitmap->Create(size_x, size_y, FXDIB_Argb);\r
-               pBitmap->Clear(0x00ffffff);\r
-#ifdef _SKIA_SUPPORT_\r
-               pContext->m_pDevice = FX_NEW CFX_SkiaDevice;\r
-               ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);\r
-#else\r
-               pContext->m_pDevice = FX_NEW CFX_FxgeDevice;\r
-               ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);\r
-#endif\r
-       }\r
-       else\r
-       pContext->m_pDevice = FX_NEW CFX_WindowsDevice(dc);\r
-       if (flags & FPDF_NO_CATCH)\r
-               Func_RenderPage(pContext, page, start_x, start_y, size_x, size_y, rotate, flags,TRUE,NULL);\r
-       else {\r
-               try {\r
-                       Func_RenderPage(pContext, page, start_x, start_y, size_x, size_y, rotate, flags,TRUE,NULL);\r
-               } catch (...) {\r
-               }\r
-       }\r
-       if (bBackgroundAlphaNeeded) \r
-       {\r
-               if (pBitmap)\r
-               {\r
-                       CFX_WindowsDevice WinDC(dc);\r
-                       \r
-                       if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER)\r
-                       {\r
-                               CFX_DIBitmap* pDst = FX_NEW CFX_DIBitmap;\r
-                               pDst->Create(pBitmap->GetWidth(), pBitmap->GetHeight(),FXDIB_Rgb32);\r
-                               FXSYS_memcpy(pDst->GetBuffer(), pBitmap->GetBuffer(), pBitmap->GetPitch()*pBitmap->GetHeight());\r
-//                             WinDC.SetDIBits(pDst,0,0);\r
-                               WinDC.StretchDIBits(pDst,0,0,size_x*2,size_y*2);\r
-                               delete pDst;\r
-                       }\r
-                       else\r
-                               WinDC.SetDIBits(pBitmap,0,0);\r
-\r
-               }\r
-       }\r
-#else\r
-       // get clip region\r
-       RECT rect, cliprect;\r
-       rect.left = start_x;\r
-       rect.top = start_y;\r
-       rect.right = start_x + size_x;\r
-       rect.bottom = start_y + size_y;\r
-       GetClipBox(dc, &cliprect);\r
-       IntersectRect(&rect, &rect, &cliprect);\r
-       int width = rect.right - rect.left;\r
-       int height = rect.bottom - rect.top;\r
-\r
-#ifdef DEBUG_TRACE\r
-       {\r
-               char str[128];\r
-               sprintf(str, "Rendering DIB %d x %d", width, height);\r
-               CPDF_ModuleMgr::Get()->ReportError(999, str);\r
-       }\r
-#endif\r
-\r
-       // Create a DIB section\r
-       LPVOID pBuffer;\r
-       BITMAPINFOHEADER bmih;\r
-       FXSYS_memset(&bmih, 0, sizeof bmih);\r
-       bmih.biSize = sizeof bmih;\r
-       bmih.biBitCount = 24;\r
-       bmih.biHeight = -height;\r
-       bmih.biPlanes = 1;\r
-       bmih.biWidth = width;\r
-       pContext->m_hBitmap = CreateDIBSection(dc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS, &pBuffer, NULL, 0);\r
-       if (pContext->m_hBitmap == NULL) {\r
-#if defined(DEBUG) || defined(_DEBUG)\r
-               char str[128];\r
-               sprintf(str, "Error CreateDIBSection: %d x %d, error code = %d", width, height, GetLastError());\r
-               CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);\r
-#else\r
-               CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);\r
-#endif\r
-       }\r
-       FXSYS_memset(pBuffer, 0xff, height*((width*3+3)/4*4));\r
-\r
-#ifdef DEBUG_TRACE\r
-       {\r
-               CPDF_ModuleMgr::Get()->ReportError(999, "DIBSection created");\r
-       }\r
-#endif\r
-\r
-       // Create a device with this external buffer\r
-       pContext->m_pBitmap = FX_NEW CFX_DIBitmap;\r
-       pContext->m_pBitmap->Create(width, height, FXDIB_Rgb, (FX_LPBYTE)pBuffer);\r
-       pContext->m_pDevice = FX_NEW CPDF_FxgeDevice;\r
-       ((CPDF_FxgeDevice*)pContext->m_pDevice)->Attach(pContext->m_pBitmap);\r
-       \r
-#ifdef DEBUG_TRACE\r
-       CPDF_ModuleMgr::Get()->ReportError(999, "Ready for PDF rendering");\r
-#endif\r
-\r
-       // output to bitmap device\r
-       if (flags & FPDF_NO_CATCH)\r
-               Func_RenderPage(pContext, page, start_x - rect.left, start_y - rect.top, size_x, size_y, rotate, flags);\r
-       else {\r
-               try {\r
-                       Func_RenderPage(pContext, page, start_x - rect.left, start_y - rect.top, size_x, size_y, rotate, flags);\r
-               } catch (...) {\r
-               }\r
-       }\r
-\r
-#ifdef DEBUG_TRACE\r
-       CPDF_ModuleMgr::Get()->ReportError(999, "Finished PDF rendering");\r
-#endif\r
-\r
-       // Now output to real device\r
-       HDC hMemDC = CreateCompatibleDC(dc);\r
-       if (hMemDC == NULL) {\r
-#if defined(DEBUG) || defined(_DEBUG)\r
-               char str[128];\r
-               sprintf(str, "Error CreateCompatibleDC. Error code = %d", GetLastError());\r
-               CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);\r
-#else\r
-               CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);\r
-#endif\r
-       }\r
-\r
-       HGDIOBJ hOldBitmap = SelectObject(hMemDC, pContext->m_hBitmap);\r
-\r
-#ifdef DEBUG_TRACE\r
-       CPDF_ModuleMgr::Get()->ReportError(999, "Ready for screen rendering");\r
-#endif\r
-\r
-       BitBlt(dc, rect.left, rect.top, width, height, hMemDC, 0, 0, SRCCOPY);\r
-       SelectObject(hMemDC, hOldBitmap);\r
-       DeleteDC(hMemDC);\r
-\r
-#ifdef DEBUG_TRACE\r
-       CPDF_ModuleMgr::Get()->ReportError(999, "Finished screen rendering");\r
-#endif\r
-\r
-#endif\r
-       if (bBackgroundAlphaNeeded)\r
-       {\r
-               if (pBitmap)\r
-                       delete pBitmap;\r
-               pBitmap = NULL;\r
-       }\r
-       delete pContext;\r
-       pPage->RemovePrivateData((void*)1);\r
-}\r
-#endif\r
-\r
-DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap, FPDF_PAGE page, int start_x, int start_y, \r
-                                               int size_x, int size_y, int rotate, int flags)\r
-{\r
-       if (bitmap == NULL || page == NULL) return;\r
-       CPDF_Page* pPage = (CPDF_Page*)page;\r
-\r
-\r
-       CRenderContext* pContext = FX_NEW CRenderContext;\r
-       pPage->SetPrivateData((void*)1, pContext, DropContext);\r
-#ifdef _SKIA_SUPPORT_\r
-       pContext->m_pDevice = FX_NEW CFX_SkiaDevice;\r
-\r
-       if (flags & FPDF_REVERSE_BYTE_ORDER)\r
-               ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap,0,TRUE);\r
-       else\r
-               ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);\r
-#else\r
-       pContext->m_pDevice = FX_NEW CFX_FxgeDevice;\r
-\r
-       if (flags & FPDF_REVERSE_BYTE_ORDER)\r
-               ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap,0,TRUE);\r
-       else\r
-               ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);\r
-#endif\r
-       if (flags & FPDF_NO_CATCH)\r
-               Func_RenderPage(pContext, page, start_x, start_y, size_x, size_y, rotate, flags,TRUE,NULL);\r
-       else {\r
-               try {\r
-                       Func_RenderPage(pContext, page, start_x, start_y, size_x, size_y, rotate, flags,TRUE,NULL);\r
-               } catch (...) {\r
-               }\r
-       }\r
-\r
-       delete pContext;\r
-       pPage->RemovePrivateData((void*)1);\r
-}\r
-\r
-DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page)\r
-{\r
-       if (!page) return;\r
-       delete (CPDF_Page*)page;\r
-\r
-}\r
-\r
-DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document)\r
-{\r
-       if (!document)\r
-               return;\r
-       CPDF_Document* pDoc = (CPDF_Document*)document; \r
-       CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();\r
-       if (pParser == NULL) \r
-       {\r
-               delete pDoc;\r
-               return;\r
-       }\r
-       delete pParser;\r
-//     delete pDoc;\r
-}\r
-\r
-DLLEXPORT unsigned long STDCALL FPDF_GetLastError()\r
-{\r
-       return GetLastError();\r
-}\r
-\r
-DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,\r
-                                               int rotate, int device_x, int device_y, double* page_x, double* page_y)\r
-{\r
-       if (page == NULL || page_x == NULL || page_y == NULL) return;\r
-       CPDF_Page* pPage = (CPDF_Page*)page;\r
-\r
-       CPDF_Matrix page2device;\r
-       pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate);\r
-       CPDF_Matrix device2page;\r
-       device2page.SetReverse(page2device);\r
-\r
-       FX_FLOAT page_x_f, page_y_f;\r
-       device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f, page_y_f);\r
-\r
-       *page_x = (page_x_f);\r
-       *page_y = (page_y_f);\r
-}\r
-\r
-DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,\r
-                                               int rotate, double page_x, double page_y, int* device_x, int* device_y)\r
-{\r
-       if (page == NULL || device_x == NULL || device_y == NULL) return;\r
-       CPDF_Page* pPage = (CPDF_Page*)page;\r
-\r
-       CPDF_Matrix page2device;\r
-       pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y, rotate);\r
-\r
-       FX_FLOAT device_x_f, device_y_f;\r
-       page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f, device_y_f);\r
-\r
-       *device_x = FXSYS_round(device_x_f);\r
-       *device_y = FXSYS_round(device_y_f);\r
-}\r
-\r
-DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width, int height, int alpha)\r
-{\r
-       CFX_DIBitmap* pBitmap = FX_NEW CFX_DIBitmap;\r
-       pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32);\r
-       return pBitmap;\r
-}\r
-\r
-DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width, int height, int format, void* first_scan, int stride)\r
-{\r
-       FXDIB_Format fx_format;\r
-       switch (format) {\r
-               case FPDFBitmap_Gray:\r
-                       fx_format = FXDIB_8bppRgb;\r
-                       break;\r
-               case FPDFBitmap_BGR:\r
-                       fx_format = FXDIB_Rgb;\r
-                       break;\r
-               case FPDFBitmap_BGRx:\r
-                       fx_format = FXDIB_Rgb32;\r
-                       break;\r
-               case FPDFBitmap_BGRA:\r
-                       fx_format = FXDIB_Argb;\r
-                       break;\r
-               default:\r
-                       return NULL;\r
-       }\r
-       CFX_DIBitmap* pBitmap = FX_NEW CFX_DIBitmap;\r
-       pBitmap->Create(width, height, fx_format, (FX_LPBYTE)first_scan, stride);\r
-       return pBitmap;\r
-}\r
-\r
-DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap, int left, int top, int width, int height, \r
-                                                                       int red, int green, int blue, int alpha)\r
-{\r
-       if (bitmap == NULL) return;\r
-#ifdef _SKIA_SUPPORT_\r
-       CFX_SkiaDevice device;\r
-#else\r
-       CFX_FxgeDevice device;\r
-#endif\r
-       device.Attach((CFX_DIBitmap*)bitmap);\r
-       if (!((CFX_DIBitmap*)bitmap)->HasAlpha()) alpha = 255;\r
-       FX_RECT rect(left, top, left+width, top+height);\r
-       device.FillRect(&rect, FXARGB_MAKE(alpha, red, green, blue));\r
-}\r
-\r
-DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap)\r
-{\r
-       if (bitmap == NULL) return NULL;\r
-       return ((CFX_DIBitmap*)bitmap)->GetBuffer();\r
-}\r
-\r
-DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap)\r
-{\r
-       if (bitmap == NULL) return 0;\r
-       return ((CFX_DIBitmap*)bitmap)->GetWidth();\r
-}\r
-\r
-DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap)\r
-{\r
-       if (bitmap == NULL) return 0;\r
-       return ((CFX_DIBitmap*)bitmap)->GetHeight();\r
-}\r
-\r
-DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap)\r
-{\r
-       if (bitmap == NULL) return 0;\r
-       return ((CFX_DIBitmap*)bitmap)->GetPitch();\r
-}\r
-\r
-DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap)\r
-{\r
-       if (bitmap == NULL) return;\r
-       delete (CFX_DIBitmap*)bitmap;\r
-}\r
-\r
-void FPDF_RenderPage_Retail(CRenderContext* pContext, FPDF_PAGE page, int start_x, int start_y, int size_x, int size_y,\r
-                                               int rotate, int flags,FX_BOOL bNeedToRestore, IFSDK_PAUSE_Adapter * pause )\r
-{\r
-//#ifdef _LICENSED_BUILD_\r
-       CPDF_Page* pPage = (CPDF_Page*)page;\r
-       if (pPage == NULL) return;\r
-\r
-       if (!pContext->m_pOptions)\r
-               pContext->m_pOptions = new CPDF_RenderOptions;\r
-//     CPDF_RenderOptions options;\r
-       if (flags & FPDF_LCD_TEXT)\r
-               pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;\r
-       else\r
-               pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;\r
-       if (flags & FPDF_NO_NATIVETEXT)\r
-               pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;\r
-       if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)\r
-               pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;\r
-       if (flags & FPDF_RENDER_FORCEHALFTONE)\r
-               pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;\r
-       //Grayscale output\r
-       if (flags & FPDF_GRAYSCALE)\r
-       {\r
-               pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;\r
-               pContext->m_pOptions->m_ForeColor = 0;\r
-               pContext->m_pOptions->m_BackColor = 0xffffff;\r
-       }\r
-       const CPDF_OCContext::UsageType usage = (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;\r
-\r
-       pContext->m_pOptions->m_AddFlags = flags >> 8;\r
-\r
-       pContext->m_pOptions->m_pOCContext = new CPDF_OCContext(pPage->m_pDocument, usage);\r
-\r
-\r
-       CFX_AffineMatrix matrix;\r
-       pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate); \r
-\r
-       FX_RECT clip;\r
-       clip.left = start_x;\r
-       clip.right = start_x + size_x;\r
-       clip.top = start_y;\r
-       clip.bottom = start_y + size_y;\r
-       pContext->m_pDevice->SaveState();\r
-       pContext->m_pDevice->SetClip_Rect(&clip);\r
-\r
-       pContext->m_pContext = FX_NEW CPDF_RenderContext;\r
-       pContext->m_pContext->Create(pPage);\r
-       pContext->m_pContext->AppendObjectList(pPage, &matrix);\r
-\r
-       if (flags & FPDF_ANNOT) {\r
-               pContext->m_pAnnots = FX_NEW CPDF_AnnotList(pPage);\r
-               FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;\r
-               pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext, bPrinting, &matrix, TRUE, NULL);\r
-       }\r
-\r
-       pContext->m_pRenderer = FX_NEW CPDF_ProgressiveRenderer;\r
-       pContext->m_pRenderer->Start(pContext->m_pContext, pContext->m_pDevice, pContext->m_pOptions, pause);\r
-       if (bNeedToRestore)\r
-       {\r
-         pContext->m_pDevice->RestoreState();\r
-       }\r
-       \r
-//#endif\r
-}\r
-\r
-DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document, int page_index, double* width, double* height)\r
-{\r
-       CPDF_Document* pDoc = (CPDF_Document*)document;\r
-       if(pDoc == NULL)\r
-               return FALSE;\r
-\r
-       CPDF_Dictionary* pDict = pDoc->GetPage(page_index);\r
-       if (pDict == NULL) return FALSE;\r
-\r
-       CPDF_Page page;\r
-       page.Load(pDoc, pDict);\r
-       *width = page.GetPageWidth();\r
-       *height = page.GetPageHeight();\r
-\r
-       return TRUE;\r
-}\r
-\r
-DLLEXPORT FPDF_BOOL STDCALL FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document)\r
-{\r
-       CPDF_Document* pDoc = (CPDF_Document*)document;\r
-       if (!pDoc) return TRUE;\r
-       CPDF_ViewerPreferences viewRef(pDoc);\r
-       return viewRef.PrintScaling();\r
-}\r
-\r
-DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,FPDF_BYTESTRING name)\r
-{\r
-       if (document == NULL)\r
-               return NULL;\r
-       if (name == NULL || name[0] == 0) \r
-               return NULL;\r
-\r
-       CPDF_Document* pDoc = (CPDF_Document*)document;\r
-       CPDF_NameTree name_tree(pDoc, FX_BSTRC("Dests"));\r
-       return name_tree.LookupNamedDest(pDoc, name);\r
-}
\ No newline at end of file
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
+
+#include "../../core/include/fxcodec/fx_codec.h"
+#include "../../core/include/fxcrt/fx_safe_types.h"
+#include "../../public/fpdf_ext.h"
+#include "../../public/fpdf_progressive.h"
+#include "../../public/fpdfview.h"
+#include "../../third_party/base/nonstd_unique_ptr.h"
+#include "../../third_party/base/numerics/safe_conversions_impl.h"
+#include "../include/fsdk_define.h"
+#include "../include/fsdk_mgr.h"
+#include "../include/fsdk_rendercontext.h"
+#include "../include/javascript/IJavaScript.h"
+
+CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess) {
+  if (pFileAccess)
+    m_FileAccess = *pFileAccess;
+}
+
+FX_BOOL CPDF_CustomAccess::ReadBlock(void* buffer,
+                                     FX_FILESIZE offset,
+                                     size_t size) {
+  if (offset < 0) {
+    return FALSE;
+  }
+  FX_SAFE_FILESIZE newPos =
+      pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
+  newPos += offset;
+  if (!newPos.IsValid() || newPos.ValueOrDie() > m_FileAccess.m_FileLen) {
+    return FALSE;
+  }
+  return m_FileAccess.m_GetBlock(m_FileAccess.m_Param, offset, (uint8_t*)buffer,
+                                 size);
+}
+
+// 0 bit: FPDF_POLICY_MACHINETIME_ACCESS
+static FX_DWORD foxit_sandbox_policy = 0xFFFFFFFF;
+
+void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable) {
+  switch (policy) {
+    case FPDF_POLICY_MACHINETIME_ACCESS: {
+      if (enable)
+        foxit_sandbox_policy |= 0x01;
+      else
+        foxit_sandbox_policy &= 0xFFFFFFFE;
+    } break;
+    default:
+      break;
+  }
+}
+
+FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy) {
+  switch (policy) {
+    case FPDF_POLICY_MACHINETIME_ACCESS:
+      return (foxit_sandbox_policy & 0x01) ? TRUE : FALSE;
+    default:
+      return FALSE;
+  }
+}
+
+CCodec_ModuleMgr* g_pCodecModule = nullptr;
+
+#if _FX_OS_ == _FX_LINUX_EMBEDDED_
+class CFontMapper : public IPDF_FontMapper {
+ public:
+  CFontMapper();
+  ~CFontMapper() override;
+
+  // IPDF_FontMapper
+  FT_Face FindSubstFont(
+      CPDF_Document* pDoc,              // [IN] The PDF document
+      const CFX_ByteString& face_name,  // [IN] Original name
+      FX_BOOL bTrueType,                // [IN] TrueType or Type1
+      FX_DWORD flags,   // [IN] PDF font flags (see PDF Reference section 5.7.1)
+      int font_weight,  // [IN] original font weight. 0 for not specified
+      int CharsetCP,    // [IN] code page for charset (see Win32 GetACP())
+      FX_BOOL bVertical,
+      CPDF_SubstFont* pSubstFont  // [OUT] Subst font data
+      ) override;
+
+  FT_Face m_SysFace;
+};
+
+CFontMapper* g_pFontMapper = NULL;
+#endif  // #if _FX_OS_ == _FX_LINUX_EMBEDDED_
+
+DLLEXPORT void STDCALL FPDF_InitLibrary() {
+  FPDF_InitLibraryWithConfig(nullptr);
+}
+
+DLLEXPORT void STDCALL FPDF_InitLibraryWithConfig(
+    const FPDF_LIBRARY_CONFIG* cfg) {
+  g_pCodecModule = new CCodec_ModuleMgr();
+
+  CFX_GEModule::Create(cfg ? cfg->m_pUserFontPaths : nullptr);
+  CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
+
+  CPDF_ModuleMgr::Create();
+  CPDF_ModuleMgr::Get()->SetCodecModule(g_pCodecModule);
+  CPDF_ModuleMgr::Get()->InitPageModule();
+  CPDF_ModuleMgr::Get()->InitRenderModule();
+  CPDF_ModuleMgr* pModuleMgr = CPDF_ModuleMgr::Get();
+  if (pModuleMgr) {
+    pModuleMgr->LoadEmbeddedGB1CMaps();
+    pModuleMgr->LoadEmbeddedJapan1CMaps();
+    pModuleMgr->LoadEmbeddedCNS1CMaps();
+    pModuleMgr->LoadEmbeddedKorea1CMaps();
+  }
+  if (cfg && cfg->version >= 2)
+    IJS_Runtime::Initialize(cfg->m_v8EmbedderSlot, cfg->m_pIsolate);
+}
+
+DLLEXPORT void STDCALL FPDF_DestroyLibrary() {
+#if _FX_OS_ == _FX_LINUX_EMBEDDED_
+  delete g_pFontMapper;
+  g_pFontMapper = nullptr;
+#endif
+  CPDF_ModuleMgr::Destroy();
+  CFX_GEModule::Destroy();
+
+  delete g_pCodecModule;
+  g_pCodecModule = nullptr;
+}
+
+#ifndef _WIN32
+int g_LastError;
+void SetLastError(int err) {
+  g_LastError = err;
+}
+
+int GetLastError() {
+  return g_LastError;
+}
+#endif
+
+void ProcessParseError(FX_DWORD err_code) {
+  // Translate FPDFAPI error code to FPDFVIEW error code
+  switch (err_code) {
+    case PDFPARSE_ERROR_FILE:
+      err_code = FPDF_ERR_FILE;
+      break;
+    case PDFPARSE_ERROR_FORMAT:
+      err_code = FPDF_ERR_FORMAT;
+      break;
+    case PDFPARSE_ERROR_PASSWORD:
+      err_code = FPDF_ERR_PASSWORD;
+      break;
+    case PDFPARSE_ERROR_HANDLER:
+      err_code = FPDF_ERR_SECURITY;
+      break;
+  }
+  SetLastError(err_code);
+}
+
+DLLEXPORT void STDCALL FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
+                                             FPDF_BOOL enable) {
+  return FSDK_SetSandBoxPolicy(policy, enable);
+}
+
+DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path,
+                                                  FPDF_BYTESTRING password) {
+  // NOTE: the creation of the file needs to be by the embedder on the
+  // other side of this API.
+  IFX_FileRead* pFileAccess = FX_CreateFileRead((const FX_CHAR*)file_path);
+  if (!pFileAccess) {
+    return nullptr;
+  }
+
+  CPDF_Parser* pParser = new CPDF_Parser;
+  pParser->SetPassword(password);
+
+  FX_DWORD err_code = pParser->StartParse(pFileAccess);
+  if (err_code) {
+    delete pParser;
+    ProcessParseError(err_code);
+    return NULL;
+  }
+  return pParser->GetDocument();
+}
+
+class CMemFile final : public IFX_FileRead {
+ public:
+  CMemFile(uint8_t* pBuf, FX_FILESIZE size) : m_pBuf(pBuf), m_size(size) {}
+
+  virtual void Release() { delete this; }
+  virtual FX_FILESIZE GetSize() { return m_size; }
+  virtual FX_BOOL ReadBlock(void* buffer, FX_FILESIZE offset, size_t size) {
+    if (offset < 0) {
+      return FALSE;
+    }
+    FX_SAFE_FILESIZE newPos =
+        pdfium::base::checked_cast<FX_FILESIZE, size_t>(size);
+    newPos += offset;
+    if (!newPos.IsValid() || newPos.ValueOrDie() > (FX_DWORD)m_size) {
+      return FALSE;
+    }
+    FXSYS_memcpy(buffer, m_pBuf + offset, size);
+    return TRUE;
+  }
+
+ private:
+  ~CMemFile() {}
+
+  uint8_t* m_pBuf;
+  FX_FILESIZE m_size;
+};
+DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf,
+                                                     int size,
+                                                     FPDF_BYTESTRING password) {
+  CPDF_Parser* pParser = new CPDF_Parser;
+  pParser->SetPassword(password);
+  CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
+  FX_DWORD err_code = pParser->StartParse(pMemFile);
+  if (err_code) {
+    delete pParser;
+    ProcessParseError(err_code);
+    return NULL;
+  }
+  CPDF_Document* pDoc = NULL;
+  pDoc = pParser ? pParser->GetDocument() : NULL;
+  CheckUnSupportError(pDoc, err_code);
+  return pParser->GetDocument();
+}
+
+DLLEXPORT FPDF_DOCUMENT STDCALL
+FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
+                        FPDF_BYTESTRING password) {
+  CPDF_Parser* pParser = new CPDF_Parser;
+  pParser->SetPassword(password);
+  CPDF_CustomAccess* pFile = new CPDF_CustomAccess(pFileAccess);
+  FX_DWORD err_code = pParser->StartParse(pFile);
+  if (err_code) {
+    delete pParser;
+    ProcessParseError(err_code);
+    return NULL;
+  }
+  CPDF_Document* pDoc = NULL;
+  pDoc = pParser ? pParser->GetDocument() : NULL;
+  CheckUnSupportError(pDoc, err_code);
+  return pParser->GetDocument();
+}
+
+DLLEXPORT FPDF_BOOL STDCALL FPDF_GetFileVersion(FPDF_DOCUMENT doc,
+                                                int* fileVersion) {
+  if (!doc || !fileVersion)
+    return FALSE;
+  *fileVersion = 0;
+  CPDF_Document* pDoc = (CPDF_Document*)doc;
+  CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
+  if (!pParser)
+    return FALSE;
+  *fileVersion = pParser->GetFileVersion();
+  return TRUE;
+}
+
+// jabdelmalek: changed return type from FX_DWORD to build on Linux (and match
+// header).
+DLLEXPORT unsigned long STDCALL FPDF_GetDocPermissions(FPDF_DOCUMENT document) {
+  if (document == NULL)
+    return 0;
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+  CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
+  CPDF_Dictionary* pDict = pParser->GetEncryptDict();
+  if (pDict == NULL)
+    return (FX_DWORD)-1;
+
+  return pDict->GetInteger("P");
+}
+
+DLLEXPORT int STDCALL FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) {
+  if (document == NULL)
+    return -1;
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+  CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
+  CPDF_Dictionary* pDict = pParser->GetEncryptDict();
+  if (pDict == NULL)
+    return -1;
+
+  return pDict->GetInteger("R");
+}
+
+DLLEXPORT int STDCALL FPDF_GetPageCount(FPDF_DOCUMENT document) {
+  if (document == NULL)
+    return 0;
+  return ((CPDF_Document*)document)->GetPageCount();
+}
+
+DLLEXPORT FPDF_PAGE STDCALL FPDF_LoadPage(FPDF_DOCUMENT document,
+                                          int page_index) {
+  if (document == NULL)
+    return NULL;
+  if (page_index < 0 || page_index >= FPDF_GetPageCount(document))
+    return NULL;
+
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+  if (pDoc == NULL)
+    return NULL;
+  CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
+  if (pDict == NULL)
+    return NULL;
+  CPDF_Page* pPage = new CPDF_Page;
+  pPage->Load(pDoc, pDict);
+  pPage->ParseContent();
+  return pPage;
+}
+
+DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
+  if (!page)
+    return 0.0;
+  return ((CPDF_Page*)page)->GetPageWidth();
+}
+
+DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
+  if (!page)
+    return 0.0;
+  return ((CPDF_Page*)page)->GetPageHeight();
+}
+
+void DropContext(void* data) {
+  delete (CRenderContext*)data;
+}
+
+#if defined(_DEBUG) || defined(DEBUG)
+#define DEBUG_TRACE
+#endif
+
+#if defined(_WIN32)
+DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
+                                       FPDF_PAGE page,
+                                       int start_x,
+                                       int start_y,
+                                       int size_x,
+                                       int size_y,
+                                       int rotate,
+                                       int flags) {
+  if (page == NULL)
+    return;
+  CPDF_Page* pPage = (CPDF_Page*)page;
+
+  CRenderContext* pContext = new CRenderContext;
+  pPage->SetPrivateData((void*)1, pContext, DropContext);
+
+#ifndef _WIN32_WCE
+  CFX_DIBitmap* pBitmap = NULL;
+  FX_BOOL bBackgroundAlphaNeeded = FALSE;
+  bBackgroundAlphaNeeded = pPage->BackgroundAlphaNeeded();
+  if (bBackgroundAlphaNeeded) {
+    pBitmap = new CFX_DIBitmap;
+    pBitmap->Create(size_x, size_y, FXDIB_Argb);
+    pBitmap->Clear(0x00ffffff);
+#ifdef _SKIA_SUPPORT_
+    pContext->m_pDevice = new CFX_SkiaDevice;
+    ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
+#else
+    pContext->m_pDevice = new CFX_FxgeDevice;
+    ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)pBitmap);
+#endif
+  } else
+    pContext->m_pDevice = new CFX_WindowsDevice(dc);
+
+  FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
+                         rotate, flags, TRUE, NULL);
+
+  if (bBackgroundAlphaNeeded) {
+    if (pBitmap) {
+      CFX_WindowsDevice WinDC(dc);
+
+      if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
+        CFX_DIBitmap* pDst = new CFX_DIBitmap;
+        int pitch = pBitmap->GetPitch();
+        pDst->Create(size_x, size_y, FXDIB_Rgb32);
+        FXSYS_memset(pDst->GetBuffer(), -1, pitch * size_y);
+        pDst->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
+                              FXDIB_BLEND_NORMAL, NULL, FALSE, NULL);
+        WinDC.StretchDIBits(pDst, 0, 0, size_x, size_y);
+        delete pDst;
+      } else
+        WinDC.SetDIBits(pBitmap, 0, 0);
+    }
+  }
+#else
+  // get clip region
+  RECT rect, cliprect;
+  rect.left = start_x;
+  rect.top = start_y;
+  rect.right = start_x + size_x;
+  rect.bottom = start_y + size_y;
+  GetClipBox(dc, &cliprect);
+  IntersectRect(&rect, &rect, &cliprect);
+  int width = rect.right - rect.left;
+  int height = rect.bottom - rect.top;
+
+#ifdef DEBUG_TRACE
+  {
+    char str[128];
+    memset(str, 0, sizeof(str));
+    FXSYS_snprintf(str, sizeof(str) - 1, "Rendering DIB %d x %d", width,
+                   height);
+    CPDF_ModuleMgr::Get()->ReportError(999, str);
+  }
+#endif
+
+  // Create a DIB section
+  LPVOID pBuffer;
+  BITMAPINFOHEADER bmih;
+  FXSYS_memset(&bmih, 0, sizeof bmih);
+  bmih.biSize = sizeof bmih;
+  bmih.biBitCount = 24;
+  bmih.biHeight = -height;
+  bmih.biPlanes = 1;
+  bmih.biWidth = width;
+  pContext->m_hBitmap = CreateDIBSection(dc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
+                                         &pBuffer, NULL, 0);
+  if (pContext->m_hBitmap == NULL) {
+#if defined(DEBUG) || defined(_DEBUG)
+    char str[128];
+    memset(str, 0, sizeof(str));
+    FXSYS_snprintf(str, sizeof(str) - 1,
+                   "Error CreateDIBSection: %d x %d, error code = %d", width,
+                   height, GetLastError());
+    CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
+#else
+    CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
+#endif
+  }
+  FXSYS_memset(pBuffer, 0xff, height * ((width * 3 + 3) / 4 * 4));
+
+#ifdef DEBUG_TRACE
+  { CPDF_ModuleMgr::Get()->ReportError(999, "DIBSection created"); }
+#endif
+
+  // Create a device with this external buffer
+  pContext->m_pBitmap = new CFX_DIBitmap;
+  pContext->m_pBitmap->Create(width, height, FXDIB_Rgb, (uint8_t*)pBuffer);
+  pContext->m_pDevice = new CPDF_FxgeDevice;
+  ((CPDF_FxgeDevice*)pContext->m_pDevice)->Attach(pContext->m_pBitmap);
+
+#ifdef DEBUG_TRACE
+  CPDF_ModuleMgr::Get()->ReportError(999, "Ready for PDF rendering");
+#endif
+
+  // output to bitmap device
+  FPDF_RenderPage_Retail(pContext, page, start_x - rect.left,
+                         start_y - rect.top, size_x, size_y, rotate, flags);
+
+#ifdef DEBUG_TRACE
+  CPDF_ModuleMgr::Get()->ReportError(999, "Finished PDF rendering");
+#endif
+
+  // Now output to real device
+  HDC hMemDC = CreateCompatibleDC(dc);
+  if (hMemDC == NULL) {
+#if defined(DEBUG) || defined(_DEBUG)
+    char str[128];
+    memset(str, 0, sizeof(str));
+    FXSYS_snprintf(str, sizeof(str) - 1,
+                   "Error CreateCompatibleDC. Error code = %d", GetLastError());
+    CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, str);
+#else
+    CPDF_ModuleMgr::Get()->ReportError(FPDFERR_OUT_OF_MEMORY, NULL);
+#endif
+  }
+
+  HGDIOBJ hOldBitmap = SelectObject(hMemDC, pContext->m_hBitmap);
+
+#ifdef DEBUG_TRACE
+  CPDF_ModuleMgr::Get()->ReportError(999, "Ready for screen rendering");
+#endif
+
+  BitBlt(dc, rect.left, rect.top, width, height, hMemDC, 0, 0, SRCCOPY);
+  SelectObject(hMemDC, hOldBitmap);
+  DeleteDC(hMemDC);
+
+#ifdef DEBUG_TRACE
+  CPDF_ModuleMgr::Get()->ReportError(999, "Finished screen rendering");
+#endif
+
+#endif
+  if (bBackgroundAlphaNeeded) {
+    delete pBitmap;
+    pBitmap = NULL;
+  }
+  delete pContext;
+  pPage->RemovePrivateData((void*)1);
+}
+#endif
+
+DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
+                                             FPDF_PAGE page,
+                                             int start_x,
+                                             int start_y,
+                                             int size_x,
+                                             int size_y,
+                                             int rotate,
+                                             int flags) {
+  if (bitmap == NULL || page == NULL)
+    return;
+  CPDF_Page* pPage = (CPDF_Page*)page;
+
+  CRenderContext* pContext = new CRenderContext;
+  pPage->SetPrivateData((void*)1, pContext, DropContext);
+#ifdef _SKIA_SUPPORT_
+  pContext->m_pDevice = new CFX_SkiaDevice;
+
+  if (flags & FPDF_REVERSE_BYTE_ORDER)
+    ((CFX_SkiaDevice*)pContext->m_pDevice)
+        ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
+  else
+    ((CFX_SkiaDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
+#else
+  pContext->m_pDevice = new CFX_FxgeDevice;
+
+  if (flags & FPDF_REVERSE_BYTE_ORDER)
+    ((CFX_FxgeDevice*)pContext->m_pDevice)
+        ->Attach((CFX_DIBitmap*)bitmap, 0, TRUE);
+  else
+    ((CFX_FxgeDevice*)pContext->m_pDevice)->Attach((CFX_DIBitmap*)bitmap);
+#endif
+
+  FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
+                         rotate, flags, TRUE, NULL);
+
+  delete pContext;
+  pPage->RemovePrivateData((void*)1);
+}
+
+DLLEXPORT void STDCALL FPDF_ClosePage(FPDF_PAGE page) {
+  if (!page)
+    return;
+  CPDFSDK_PageView* pPageView =
+      (CPDFSDK_PageView*)(((CPDF_Page*)page))->GetPrivateData((void*)page);
+  if (pPageView && pPageView->IsLocked()) {
+    pPageView->TakeOverPage();
+    return;
+  }
+  delete (CPDF_Page*)page;
+}
+
+DLLEXPORT void STDCALL FPDF_CloseDocument(FPDF_DOCUMENT document) {
+  if (!document)
+    return;
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+  CPDF_Parser* pParser = (CPDF_Parser*)pDoc->GetParser();
+  if (pParser == NULL) {
+    delete pDoc;
+    return;
+  }
+  delete pParser;
+}
+
+DLLEXPORT unsigned long STDCALL FPDF_GetLastError() {
+  return GetLastError();
+}
+
+DLLEXPORT void STDCALL FPDF_DeviceToPage(FPDF_PAGE page,
+                                         int start_x,
+                                         int start_y,
+                                         int size_x,
+                                         int size_y,
+                                         int rotate,
+                                         int device_x,
+                                         int device_y,
+                                         double* page_x,
+                                         double* page_y) {
+  if (page == NULL || page_x == NULL || page_y == NULL)
+    return;
+  CPDF_Page* pPage = (CPDF_Page*)page;
+
+  CPDF_Matrix page2device;
+  pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
+                          rotate);
+  CPDF_Matrix device2page;
+  device2page.SetReverse(page2device);
+
+  FX_FLOAT page_x_f, page_y_f;
+  device2page.Transform((FX_FLOAT)(device_x), (FX_FLOAT)(device_y), page_x_f,
+                        page_y_f);
+
+  *page_x = (page_x_f);
+  *page_y = (page_y_f);
+}
+
+DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
+                                         int start_x,
+                                         int start_y,
+                                         int size_x,
+                                         int size_y,
+                                         int rotate,
+                                         double page_x,
+                                         double page_y,
+                                         int* device_x,
+                                         int* device_y) {
+  if (page == NULL || device_x == NULL || device_y == NULL)
+    return;
+  CPDF_Page* pPage = (CPDF_Page*)page;
+
+  CPDF_Matrix page2device;
+  pPage->GetDisplayMatrix(page2device, start_x, start_y, size_x, size_y,
+                          rotate);
+
+  FX_FLOAT device_x_f, device_y_f;
+  page2device.Transform(((FX_FLOAT)page_x), ((FX_FLOAT)page_y), device_x_f,
+                        device_y_f);
+
+  *device_x = FXSYS_round(device_x_f);
+  *device_y = FXSYS_round(device_y_f);
+}
+
+DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width,
+                                                int height,
+                                                int alpha) {
+  nonstd::unique_ptr<CFX_DIBitmap> pBitmap(new CFX_DIBitmap);
+  if (!pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32)) {
+    return NULL;
+  }
+  return pBitmap.release();
+}
+
+DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_CreateEx(int width,
+                                                  int height,
+                                                  int format,
+                                                  void* first_scan,
+                                                  int stride) {
+  FXDIB_Format fx_format;
+  switch (format) {
+    case FPDFBitmap_Gray:
+      fx_format = FXDIB_8bppRgb;
+      break;
+    case FPDFBitmap_BGR:
+      fx_format = FXDIB_Rgb;
+      break;
+    case FPDFBitmap_BGRx:
+      fx_format = FXDIB_Rgb32;
+      break;
+    case FPDFBitmap_BGRA:
+      fx_format = FXDIB_Argb;
+      break;
+    default:
+      return NULL;
+  }
+  CFX_DIBitmap* pBitmap = new CFX_DIBitmap;
+  pBitmap->Create(width, height, fx_format, (uint8_t*)first_scan, stride);
+  return pBitmap;
+}
+
+DLLEXPORT void STDCALL FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
+                                           int left,
+                                           int top,
+                                           int width,
+                                           int height,
+                                           FPDF_DWORD color) {
+  if (bitmap == NULL)
+    return;
+#ifdef _SKIA_SUPPORT_
+  CFX_SkiaDevice device;
+#else
+  CFX_FxgeDevice device;
+#endif
+  device.Attach((CFX_DIBitmap*)bitmap);
+  if (!((CFX_DIBitmap*)bitmap)->HasAlpha())
+    color |= 0xFF000000;
+  FX_RECT rect(left, top, left + width, top + height);
+  device.FillRect(&rect, color);
+}
+
+DLLEXPORT void* STDCALL FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) {
+  if (bitmap == NULL)
+    return NULL;
+  return ((CFX_DIBitmap*)bitmap)->GetBuffer();
+}
+
+DLLEXPORT int STDCALL FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) {
+  if (bitmap == NULL)
+    return 0;
+  return ((CFX_DIBitmap*)bitmap)->GetWidth();
+}
+
+DLLEXPORT int STDCALL FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) {
+  if (bitmap == NULL)
+    return 0;
+  return ((CFX_DIBitmap*)bitmap)->GetHeight();
+}
+
+DLLEXPORT int STDCALL FPDFBitmap_GetStride(FPDF_BITMAP bitmap) {
+  if (bitmap == NULL)
+    return 0;
+  return ((CFX_DIBitmap*)bitmap)->GetPitch();
+}
+
+DLLEXPORT void STDCALL FPDFBitmap_Destroy(FPDF_BITMAP bitmap) {
+  delete (CFX_DIBitmap*)bitmap;
+}
+
+void FPDF_RenderPage_Retail(CRenderContext* pContext,
+                            FPDF_PAGE page,
+                            int start_x,
+                            int start_y,
+                            int size_x,
+                            int size_y,
+                            int rotate,
+                            int flags,
+                            FX_BOOL bNeedToRestore,
+                            IFSDK_PAUSE_Adapter* pause) {
+  CPDF_Page* pPage = (CPDF_Page*)page;
+  if (pPage == NULL)
+    return;
+
+  if (!pContext->m_pOptions)
+    pContext->m_pOptions = new CPDF_RenderOptions;
+
+  if (flags & FPDF_LCD_TEXT)
+    pContext->m_pOptions->m_Flags |= RENDER_CLEARTYPE;
+  else
+    pContext->m_pOptions->m_Flags &= ~RENDER_CLEARTYPE;
+  if (flags & FPDF_NO_NATIVETEXT)
+    pContext->m_pOptions->m_Flags |= RENDER_NO_NATIVETEXT;
+  if (flags & FPDF_RENDER_LIMITEDIMAGECACHE)
+    pContext->m_pOptions->m_Flags |= RENDER_LIMITEDIMAGECACHE;
+  if (flags & FPDF_RENDER_FORCEHALFTONE)
+    pContext->m_pOptions->m_Flags |= RENDER_FORCE_HALFTONE;
+  if (flags & FPDF_RENDER_NO_SMOOTHTEXT)
+    pContext->m_pOptions->m_Flags |= RENDER_NOTEXTSMOOTH;
+  if (flags & FPDF_RENDER_NO_SMOOTHIMAGE)
+    pContext->m_pOptions->m_Flags |= RENDER_NOIMAGESMOOTH;
+  if (flags & FPDF_RENDER_NO_SMOOTHPATH)
+    pContext->m_pOptions->m_Flags |= RENDER_NOPATHSMOOTH;
+  // Grayscale output
+  if (flags & FPDF_GRAYSCALE) {
+    pContext->m_pOptions->m_ColorMode = RENDER_COLOR_GRAY;
+    pContext->m_pOptions->m_ForeColor = 0;
+    pContext->m_pOptions->m_BackColor = 0xffffff;
+  }
+  const CPDF_OCContext::UsageType usage =
+      (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
+  pContext->m_pOptions->m_AddFlags = flags >> 8;
+  pContext->m_pOptions->m_pOCContext =
+      new CPDF_OCContext(pPage->m_pDocument, usage);
+
+  CFX_AffineMatrix matrix;
+  pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate);
+
+  FX_RECT clip;
+  clip.left = start_x;
+  clip.right = start_x + size_x;
+  clip.top = start_y;
+  clip.bottom = start_y + size_y;
+  pContext->m_pDevice->SaveState();
+  pContext->m_pDevice->SetClip_Rect(&clip);
+
+  pContext->m_pContext = new CPDF_RenderContext;
+  pContext->m_pContext->Create(pPage);
+  pContext->m_pContext->AppendObjectList(pPage, &matrix);
+
+  if (flags & FPDF_ANNOT) {
+    pContext->m_pAnnots = new CPDF_AnnotList(pPage);
+    FX_BOOL bPrinting = pContext->m_pDevice->GetDeviceClass() != FXDC_DISPLAY;
+    pContext->m_pAnnots->DisplayAnnots(pPage, pContext->m_pContext, bPrinting,
+                                       &matrix, TRUE, NULL);
+  }
+
+  pContext->m_pRenderer = new CPDF_ProgressiveRenderer(
+      pContext->m_pContext, pContext->m_pDevice, pContext->m_pOptions);
+  pContext->m_pRenderer->Start(pause);
+  if (bNeedToRestore)
+    pContext->m_pDevice->RestoreState();
+}
+
+DLLEXPORT int STDCALL FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
+                                              int page_index,
+                                              double* width,
+                                              double* height) {
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+  if (pDoc == NULL)
+    return FALSE;
+
+  CPDF_Dictionary* pDict = pDoc->GetPage(page_index);
+  if (pDict == NULL)
+    return FALSE;
+
+  CPDF_Page page;
+  page.Load(pDoc, pDict);
+  *width = page.GetPageWidth();
+  *height = page.GetPageHeight();
+
+  return TRUE;
+}
+
+DLLEXPORT FPDF_BOOL STDCALL
+FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) {
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+  if (!pDoc)
+    return TRUE;
+  CPDF_ViewerPreferences viewRef(pDoc);
+  return viewRef.PrintScaling();
+}
+
+DLLEXPORT int STDCALL FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) {
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+  if (!pDoc)
+    return 1;
+  CPDF_ViewerPreferences viewRef(pDoc);
+  return viewRef.NumCopies();
+}
+
+DLLEXPORT FPDF_PAGERANGE STDCALL
+FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) {
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+  if (!pDoc)
+    return NULL;
+  CPDF_ViewerPreferences viewRef(pDoc);
+  return viewRef.PrintPageRange();
+}
+
+DLLEXPORT FPDF_DUPLEXTYPE STDCALL
+FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+  if (!pDoc)
+    return DuplexUndefined;
+  CPDF_ViewerPreferences viewRef(pDoc);
+  CFX_ByteString duplex = viewRef.Duplex();
+  if (FX_BSTRC("Simplex") == duplex)
+    return Simplex;
+  if (FX_BSTRC("DuplexFlipShortEdge") == duplex)
+    return DuplexFlipShortEdge;
+  if (FX_BSTRC("DuplexFlipLongEdge") == duplex)
+    return DuplexFlipLongEdge;
+  return DuplexUndefined;
+}
+
+DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
+  if (!document)
+    return 0;
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+
+  CPDF_Dictionary* pRoot = pDoc->GetRoot();
+  if (!pRoot)
+    return 0;
+
+  CPDF_NameTree nameTree(pDoc, FX_BSTRC("Dests"));
+  int count = nameTree.GetCount();
+  CPDF_Dictionary* pDest = pRoot->GetDict(FX_BSTRC("Dests"));
+  if (pDest)
+    count += pDest->GetCount();
+  return count;
+}
+
+DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDestByName(FPDF_DOCUMENT document,
+                                                    FPDF_BYTESTRING name) {
+  if (!document)
+    return NULL;
+  if (!name || name[0] == 0)
+    return NULL;
+
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+  CPDF_NameTree name_tree(pDoc, FX_BSTRC("Dests"));
+  return name_tree.LookupNamedDest(pDoc, name);
+}
+
+DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document,
+                                              int index,
+                                              void* buffer,
+                                              long* buflen) {
+  if (!buffer)
+    *buflen = 0;
+  if (!document || index < 0)
+    return NULL;
+  CPDF_Document* pDoc = (CPDF_Document*)document;
+
+  CPDF_Dictionary* pRoot = pDoc->GetRoot();
+  if (!pRoot)
+    return NULL;
+
+  CPDF_Object* pDestObj = NULL;
+  CFX_ByteString bsName;
+  CPDF_NameTree nameTree(pDoc, FX_BSTRC("Dests"));
+  int count = nameTree.GetCount();
+  if (index >= count) {
+    CPDF_Dictionary* pDest = pRoot->GetDict(FX_BSTRC("Dests"));
+    if (!pDest)
+      return NULL;
+    if (index >= count + pDest->GetCount())
+      return NULL;
+    index -= count;
+    FX_POSITION pos = pDest->GetStartPos();
+    int i = 0;
+    while (pos) {
+      pDestObj = pDest->GetNextElement(pos, bsName);
+      if (!pDestObj)
+        continue;
+      if (i == index)
+        break;
+      i++;
+    }
+  } else {
+    pDestObj = nameTree.LookupValue(index, bsName);
+  }
+  if (!pDestObj)
+    return NULL;
+  if (pDestObj->GetType() == PDFOBJ_DICTIONARY) {
+    pDestObj = ((CPDF_Dictionary*)pDestObj)->GetArray(FX_BSTRC("D"));
+    if (!pDestObj)
+      return NULL;
+  }
+  if (pDestObj->GetType() != PDFOBJ_ARRAY)
+    return NULL;
+  CFX_WideString wsName = PDF_DecodeText(bsName);
+  CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
+  unsigned int len = utf16Name.GetLength();
+  if (!buffer) {
+    *buflen = len;
+  } else if (*buflen >= len) {
+    memcpy(buffer, utf16Name.c_str(), len);
+    *buflen = len;
+  } else {
+    *buflen = -1;
+  }
+  return (FPDF_DEST)pDestObj;
+}