Merge to XFA: Use stdint.h types throughout PDFium.
[pdfium.git] / fpdfsdk / src / fpdfview.cpp
index c0b39ed..fa6bb2e 100644 (file)
@@ -4,15 +4,17 @@
  
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
+
+#include "../../core/include/fxcrt/fx_safe_types.h"
+#include "../../public/fpdf_ext.h"
+#include "../../public/fpdf_formfill.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/fpdfview.h"
 #include "../include/fsdk_rendercontext.h"
-#include "../include/fpdf_progressive.h"
-#include "../include/fpdf_ext.h"
-#include "../../../core/src/fxcrt/fx_safe_types.h"
-#include "../../third_party/base/numerics/safe_conversions_impl.h"
-#include "../include/fpdfformfill.h"
 #include "../include/fpdfxfa/fpdfxfa_doc.h"
 #include "../include/fpdfxfa/fpdfxfa_app.h"
 #include "../include/fpdfxfa/fpdfxfa_page.h"
@@ -102,7 +104,7 @@ CPDF_CustomAccess::CPDF_CustomAccess(FPDF_FILEACCESS* pFileAccess)
        m_BufferOffset = (FX_DWORD)-1;
 }
 
-FX_BOOL CPDF_CustomAccess::GetByte(FX_DWORD pos, FX_BYTE& ch)
+FX_BOOL CPDF_CustomAccess::GetByte(FX_DWORD pos, uint8_t& ch)
 {
        if (pos >= m_FileAccess.m_FileLen) return FALSE;
        if (m_BufferOffset == (FX_DWORD)-1 || pos < m_BufferOffset || pos >= m_BufferOffset + 512) {
@@ -175,74 +177,29 @@ FPDF_BOOL FSDK_IsSandBoxPolicyEnabled(FPDF_DWORD policy)
        return FALSE;
 }
 
+CCodec_ModuleMgr*      g_pCodecModule = NULL;
 
-#ifndef _T
-#define _T(x) x
-#endif
-
-#ifdef API5
-       CPDF_ModuleMgr* g_pModuleMgr = NULL;
-#else
-       CCodec_ModuleMgr*       g_pCodecModule = NULL;
-#endif
-
-//extern CPDFSDK_FormFillApp* g_pFormFillApp;
-
-#if _FX_OS_ == _FX_LINUX_EMBEDDED_
-class CFontMapper : public IPDF_FontMapper
+DLLEXPORT void STDCALL FPDF_InitLibrary()
 {
-public:
-       CFontMapper();
-       virtual ~CFontMapper();
-
-       virtual 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
-                                               );
-
-       FT_Face m_SysFace;
-};
+    g_pCodecModule = CCodec_ModuleMgr::Create();
 
-CFontMapper* g_pFontMapper = NULL;
-#endif         // #if _FX_OS_ == _FX_LINUX_EMBEDDED_
+    CFX_GEModule::Create();
+    CFX_GEModule::Get()->SetCodecModule(g_pCodecModule);
 
-DLLEXPORT void STDCALL FPDF_InitLibrary()
-{
-       g_pCodecModule = CCodec_ModuleMgr::Create();
-       
-       CFX_GEModule::Create();
-       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::Create();
+    CPDF_ModuleMgr::Get()->SetCodecModule(g_pCodecModule);
+    CPDF_ModuleMgr::Get()->InitPageModule();
+    CPDF_ModuleMgr::Get()->InitRenderModule();
 
-       CPDFXFA_App* pAppProvider = FPDFXFA_GetApp();
-       pAppProvider->Initialize();
+    CPDFXFA_App::GetInstance()->Initialize();
 }
 
-
 DLLEXPORT void STDCALL FPDF_DestroyLibrary()
 {
-       FPDFXFA_ReleaseApp();
-
-#if _FX_OS_ == _FX_LINUX_EMBEDDED_
-       if (g_pFontMapper) delete g_pFontMapper;
-#endif
-#ifdef API5
-       g_pModuleMgr->Destroy();
-#else
-       CPDF_ModuleMgr::Destroy();
-       CFX_GEModule::Destroy();
-       g_pCodecModule->Destroy();
-#endif
+    CPDFXFA_App::ReleaseInstance();
+    CPDF_ModuleMgr::Destroy();
+    CFX_GEModule::Destroy();
+    g_pCodecModule->Destroy();
 }
 
 #ifndef _WIN32
@@ -298,12 +255,12 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BY
        if (!pPDFDoc)
                return NULL;
 
-       CPDFXFA_App* pProvider = FPDFXFA_GetApp();
+       CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
        CPDFXFA_Document* pDocument = FX_NEW CPDFXFA_Document(pPDFDoc, pProvider);
        return pDocument;
 }
 
-DLLEXPORT FX_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document, int& docType)
+DLLEXPORT FX_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document, int* docType)
 {
        if (!document)
                return FALSE;
@@ -327,9 +284,9 @@ DLLEXPORT FX_BOOL STDCALL FPDF_HasXFAField(FPDF_DOCUMENT document, int& docType)
        FX_BOOL bDynamicXFA = pRoot->GetBoolean("NeedsRendering", FALSE);
 
        if (bDynamicXFA)
-               docType = DOCTYPE_DYNIMIC_XFA;
+               *docType = DOCTYPE_DYNIMIC_XFA;
        else
-               docType = DOCTYPE_STATIC_XFA;
+               *docType = DOCTYPE_STATIC_XFA;
 
        return TRUE;
 }
@@ -342,10 +299,10 @@ DLLEXPORT  FPDF_BOOL STDCALL FPDF_LoadXFA(FPDF_DOCUMENT document)
 
 extern void CheckUnSupportError(CPDF_Document * pDoc, FX_DWORD err_code);
 
-class CMemFile FX_FINAL: public IFX_FileRead, public CFX_Object
+class CMemFile final : public IFX_FileRead
 {
 public:
-       CMemFile(FX_BYTE* pBuf, FX_FILESIZE size):m_pBuf(pBuf),m_size(size) {}
+       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;}
@@ -363,14 +320,14 @@ public:
            return TRUE;
        }
 private:
-       FX_BYTE* m_pBuf;
+       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 = FX_NEW CPDF_Parser;
        pParser->SetPassword(password);
-       CMemFile* pMemFile = FX_NEW CMemFile((FX_BYTE*)data_buf, size);
+       CMemFile* pMemFile = new CMemFile((uint8_t*)data_buf, size);
        FX_DWORD err_code = pParser->StartParse(pMemFile);
        if (err_code) {
                delete pParser;
@@ -384,10 +341,8 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadMemDocument(const void* data_buf, int s
        if (!pPDFDoc)
                return NULL;
 
-       CPDFXFA_App* pProvider = FPDFXFA_GetApp();
+       CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
        CPDFXFA_Document* pDocument = FX_NEW CPDFXFA_Document(pPDFDoc, pProvider);
-       //pDocument->LoadXFADoc();
-
        return pDocument;
 }
 
@@ -409,10 +364,8 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAc
        if (!pPDFDoc)
                return NULL;
 
-       CPDFXFA_App* pProvider = FPDFXFA_GetApp();
+       CPDFXFA_App* pProvider = CPDFXFA_App::GetInstance();
        CPDFXFA_Document* pDocument = FX_NEW CPDFXFA_Document(pPDFDoc, pProvider);
-       //pDocument->LoadXFADoc();
-
        return pDocument;
 }
 
@@ -748,9 +701,11 @@ DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page, int start_x, int start_
 
 DLLEXPORT FPDF_BITMAP STDCALL FPDFBitmap_Create(int width, int height, int alpha)
 {
-       CFX_DIBitmap* pBitmap = FX_NEW CFX_DIBitmap;
-       pBitmap->Create(width, height, alpha ? FXDIB_Argb : FXDIB_Rgb32);
-       return pBitmap;
+    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)
@@ -1046,10 +1001,10 @@ FPDF_RESULT     FPDF_BStr_Clear(FPDF_BSTR* str)
        return 0;
 }
 
-DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index, void* buffer, long& buflen)
+DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index, void* buffer, long* buflen)
 {
     if (!buffer)
-        buflen = 0;
+        *buflen = 0;
     if (!document || index < 0) return NULL;
        CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
 
@@ -1086,12 +1041,12 @@ DLLEXPORT FPDF_DEST STDCALL FPDF_GetNamedDest(FPDF_DOCUMENT document, int index,
     CFX_ByteString utf16Name = wsName.UTF16LE_Encode();
     unsigned int len = utf16Name.GetLength();
     if (!buffer) {
-        buflen = len;
-    } else if (buflen >= len) {
+        *buflen = len;
+    } else if (*buflen >= len) {
         memcpy(buffer, utf16Name.c_str(), len);
-        buflen = len;
+        *buflen = len;
     } else {
-        buflen = -1;
+        *buflen = -1;
     }
     return (FPDF_DEST)pDestObj;
 }