将pdf库改为动态调用
This commit is contained in:
parent
85fa9e7b18
commit
4143f57e9c
|
@ -28,6 +28,7 @@
|
|||
<ClCompile Include="..\..\..\imgfmt\HGOfd.cpp" />
|
||||
<ClCompile Include="..\..\..\imgfmt\HGOfdImpl.cpp" />
|
||||
<ClCompile Include="..\..\..\imgfmt\HGPdf.cpp" />
|
||||
<ClCompile Include="..\..\..\imgfmt\HGPdfImpl.cpp" />
|
||||
<ClCompile Include="..\..\..\imgfmt\HGPng.cpp" />
|
||||
<ClCompile Include="..\..\..\imgfmt\HGTiff.cpp" />
|
||||
<ClCompile Include="..\..\..\utility\HGString.cpp" />
|
||||
|
@ -40,6 +41,7 @@
|
|||
<ClInclude Include="..\..\..\imgfmt\HGOfd.h" />
|
||||
<ClInclude Include="..\..\..\imgfmt\HGOfdImpl.hpp" />
|
||||
<ClInclude Include="..\..\..\imgfmt\HGPdf.h" />
|
||||
<ClInclude Include="..\..\..\imgfmt\HGPdfImpl.hpp" />
|
||||
<ClInclude Include="..\..\..\imgfmt\HGPng.h" />
|
||||
<ClInclude Include="..\..\..\imgfmt\HGTiff.h" />
|
||||
<ClInclude Include="..\..\..\utility\HGString.h" />
|
||||
|
@ -125,7 +127,7 @@
|
|||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableUAC>false</EnableUAC>
|
||||
<ModuleDefinitionFile>HGImgFmt.def</ModuleDefinitionFile>
|
||||
<AdditionalDependencies>../Debug/HGBase.lib;../../../third_party/libjpeg/windows/lib/x86/jpeg.lib;../../../third_party/libnsbmp/windows/lib/x86/libnsbmp.lib;../../../third_party/libpng/windows/lib/x86/libpng16.lib;../../../third_party/zlib/windows/lib/x86/zlib.lib;../../../third_party/libtiff/windows/lib/x86/tiff.lib;../../../third_party/mupdf/windows/lib/x86/libmupdf.lib;../../../third_party/pdflib/windows/lib/x86/pdflib.lib;../../../third_party/libzip/windows/lib/x86/zip.lib;../../../third_party/tinyxml2/windows/lib/x86/tinyxml2d.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>../Debug/HGBase.lib;../../../third_party/libjpeg/windows/lib/x86/jpeg.lib;../../../third_party/libnsbmp/windows/lib/x86/libnsbmp.lib;../../../third_party/libpng/windows/lib/x86/libpng16.lib;../../../third_party/zlib/windows/lib/x86/zlib.lib;../../../third_party/libtiff/windows/lib/x86/tiff.lib;../../../third_party/libzip/windows/lib/x86/zip.lib;../../../third_party/tinyxml2/windows/lib/x86/tinyxml2d.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalOptions>/ignore:4098,4099,4075 /LTCG %(AdditionalOptions)</AdditionalOptions>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
|
@ -149,7 +151,7 @@
|
|||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableUAC>false</EnableUAC>
|
||||
<ModuleDefinitionFile>HGImgFmt.def</ModuleDefinitionFile>
|
||||
<AdditionalDependencies>../Release/HGBase.lib;../../../third_party/libjpeg/windows/lib/x86/jpeg.lib;../../../third_party/libnsbmp/windows/lib/x86/libnsbmp.lib;../../../third_party/libpng/windows/lib/x86/libpng16.lib;../../../third_party/zlib/windows/lib/x86/zlib.lib;../../../third_party/libtiff/windows/lib/x86/tiff.lib;../../../third_party/mupdf/windows/lib/x86/libmupdf.lib;../../../third_party/pdflib/windows/lib/x86/pdflib.lib;../../../third_party/libzip/windows/lib/x86/zip.lib;../../../third_party/tinyxml2/windows/lib/x86/tinyxml2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>../Release/HGBase.lib;../../../third_party/libjpeg/windows/lib/x86/jpeg.lib;../../../third_party/libnsbmp/windows/lib/x86/libnsbmp.lib;../../../third_party/libpng/windows/lib/x86/libpng16.lib;../../../third_party/zlib/windows/lib/x86/zlib.lib;../../../third_party/libtiff/windows/lib/x86/tiff.lib;../../../third_party/libzip/windows/lib/x86/zip.lib;../../../third_party/tinyxml2/windows/lib/x86/tinyxml2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalOptions>/ignore:4099 /LTCG %(AdditionalOptions)</AdditionalOptions>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
|
@ -177,7 +179,7 @@ copy $(OutDir)HGImgFmt.dll $(SolutionDir)..\..\..\release\win\x86\Release\</Comm
|
|||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableUAC>false</EnableUAC>
|
||||
<ModuleDefinitionFile>HGImgFmt.def</ModuleDefinitionFile>
|
||||
<AdditionalDependencies>../x64/Debug/HGBase.lib;../../../third_party/libjpeg/windows/lib/x64/jpeg.lib;../../../third_party/libnsbmp/windows/lib/x64/libnsbmp.lib;../../../third_party/libpng/windows/lib/x64/libpng16.lib;../../../third_party/zlib/windows/lib/x64/zlib.lib;../../../third_party/libtiff/windows/lib/x64/tiff.lib;../../../third_party/mupdf/windows/lib/x64/libmupdf.lib;../../../third_party/pdflib/windows/lib/x64/pdflib.lib;../../../third_party/libzip/windows/lib/x64/zip.lib;../../../third_party/tinyxml2/windows/lib/x64/tinyxml2d.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>../x64/Debug/HGBase.lib;../../../third_party/libjpeg/windows/lib/x64/jpeg.lib;../../../third_party/libnsbmp/windows/lib/x64/libnsbmp.lib;../../../third_party/libpng/windows/lib/x64/libpng16.lib;../../../third_party/zlib/windows/lib/x64/zlib.lib;../../../third_party/libtiff/windows/lib/x64/tiff.lib;../../../third_party/libzip/windows/lib/x64/zip.lib;../../../third_party/tinyxml2/windows/lib/x64/tinyxml2d.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalOptions>/ignore:4098,4099,4075 /LTCG %(AdditionalOptions)</AdditionalOptions>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
|
@ -201,7 +203,7 @@ copy $(OutDir)HGImgFmt.dll $(SolutionDir)..\..\..\release\win\x86\Release\</Comm
|
|||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<EnableUAC>false</EnableUAC>
|
||||
<ModuleDefinitionFile>HGImgFmt.def</ModuleDefinitionFile>
|
||||
<AdditionalDependencies>../x64/Release/HGBase.lib;../../../third_party/libjpeg/windows/lib/x64/jpeg.lib;../../../third_party/libnsbmp/windows/lib/x64/libnsbmp.lib;../../../third_party/libpng/windows/lib/x64/libpng16.lib;../../../third_party/zlib/windows/lib/x64/zlib.lib;../../../third_party/libtiff/windows/lib/x64/tiff.lib;../../../third_party/mupdf/windows/lib/x64/libmupdf.lib;../../../third_party/pdflib/windows/lib/x64/pdflib.lib;../../../third_party/libzip/windows/lib/x64/zip.lib;../../../third_party/tinyxml2/windows/lib/x64/tinyxml2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>../x64/Release/HGBase.lib;../../../third_party/libjpeg/windows/lib/x64/jpeg.lib;../../../third_party/libnsbmp/windows/lib/x64/libnsbmp.lib;../../../third_party/libpng/windows/lib/x64/libpng16.lib;../../../third_party/zlib/windows/lib/x64/zlib.lib;../../../third_party/libtiff/windows/lib/x64/tiff.lib;../../../third_party/libzip/windows/lib/x64/zip.lib;../../../third_party/tinyxml2/windows/lib/x64/tinyxml2.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalOptions>/ignore:4099 /LTCG %(AdditionalOptions)</AdditionalOptions>
|
||||
</Link>
|
||||
<PostBuildEvent>
|
||||
|
|
336
imgfmt/HGPdf.cpp
336
imgfmt/HGPdf.cpp
|
@ -1,116 +1,5 @@
|
|||
#include "HGPdf.h"
|
||||
#include "../base/HGInc.h"
|
||||
#include "mupdf/pdf.h"
|
||||
#include "mupdf/fitz.h"
|
||||
#include "pdflib.h"
|
||||
#include "../utility/HGString.h"
|
||||
#include <string>
|
||||
|
||||
struct HGPdfReaderImpl
|
||||
{
|
||||
HGPdfReaderImpl()
|
||||
{
|
||||
m_pContext = NULL;
|
||||
m_pDoc = NULL;
|
||||
}
|
||||
|
||||
~HGPdfReaderImpl()
|
||||
{
|
||||
if (NULL != m_pDoc)
|
||||
{
|
||||
fz_drop_document(m_pContext, m_pDoc);
|
||||
m_pDoc = NULL;
|
||||
}
|
||||
|
||||
if (NULL != m_pContext)
|
||||
{
|
||||
fz_drop_context(m_pContext);
|
||||
m_pContext = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
fz_context* m_pContext;
|
||||
fz_document *m_pDoc;
|
||||
};
|
||||
|
||||
struct HGPdfImageWriterImpl
|
||||
{
|
||||
HGPdfImageWriterImpl()
|
||||
{
|
||||
m_pPdf = NULL;
|
||||
}
|
||||
|
||||
~HGPdfImageWriterImpl()
|
||||
{
|
||||
if (NULL != m_pPdf)
|
||||
{
|
||||
PDF_TRY(m_pPdf)
|
||||
{
|
||||
PDF_end_document(m_pPdf, "");
|
||||
}
|
||||
PDF_CATCH(m_pPdf)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
PDF_delete(m_pPdf);
|
||||
m_pPdf = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
PDF* m_pPdf;
|
||||
};
|
||||
|
||||
#if defined(HG_CMP_MSC)
|
||||
static WCHAR* GetUnicodeStr(const char* text)
|
||||
{
|
||||
int len = ::MultiByteToWideChar(CP_ACP, 0, text, -1, NULL, 0);
|
||||
WCHAR* pUnicode = new WCHAR[len];
|
||||
::MultiByteToWideChar(CP_ACP, 0, text, -1, pUnicode, len);
|
||||
return pUnicode;
|
||||
}
|
||||
static uint32_t GetUnicodeStrLen(const WCHAR* pUnicode)
|
||||
{
|
||||
return (uint32_t)wcslen(pUnicode);
|
||||
}
|
||||
#else
|
||||
static uint16_t* GetUnicodeStr(const char* text)
|
||||
{
|
||||
if (0 == *text)
|
||||
{
|
||||
uint16_t* pUnicode = new uint16_t[1];
|
||||
*pUnicode = 0;
|
||||
return pUnicode;
|
||||
}
|
||||
|
||||
uint16_t* pUnicode = new uint16_t[strlen(text) + 2];
|
||||
memset(pUnicode, 0, sizeof(uint16_t) * (strlen(text) + 2));
|
||||
|
||||
iconv_t cd = iconv_open("UNICODE//IGNORE", "UTF-8");
|
||||
if ((iconv_t)-1 != cd)
|
||||
{
|
||||
char* inbuf = (char*)text;
|
||||
size_t inbytes = strlen(text);
|
||||
char* outbuf = (char*)pUnicode;
|
||||
size_t outsize = sizeof(uint16_t) * (strlen(text) + 1);
|
||||
|
||||
iconv(cd, &inbuf, &inbytes, &outbuf, &outsize);
|
||||
iconv_close(cd);
|
||||
}
|
||||
|
||||
return pUnicode;
|
||||
}
|
||||
static uint32_t GetUnicodeStrLen(const uint16_t* pUnicode)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
while (0 != pUnicode[i])
|
||||
{
|
||||
++i;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
#endif
|
||||
#include "HGPdfImpl.hpp"
|
||||
|
||||
HGResult HGAPI HGImgFmt_CheckPdfFile(const HGChar* fileName, HGBool* isPdf)
|
||||
{
|
||||
|
@ -132,55 +21,21 @@ HGResult HGAPI HGImgFmt_CheckPdfFile(const HGChar* fileName, HGBool* isPdf)
|
|||
|
||||
HGResult HGAPI HGImgFmt_OpenPdfReader(const HGChar* fileName, HGPdfReader* reader)
|
||||
{
|
||||
if (NULL == fileName || NULL == reader)
|
||||
if (NULL == reader)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
HGBool isPdfFile = HGFALSE;
|
||||
FILE* file = fopen(fileName, "r");
|
||||
if (NULL != file)
|
||||
HGPdfReaderImpl* pdfReaderImpl = new HGPdfReaderImpl;
|
||||
HGResult ret = pdfReaderImpl->Open(fileName);
|
||||
if (HGBASE_ERR_OK != ret)
|
||||
{
|
||||
HGByte data[4];
|
||||
size_t len = fread(data, 1, 4, file);
|
||||
if (4 == len && 0 == memcmp(data, "%PDF", 4))
|
||||
{
|
||||
isPdfFile = HGTRUE;
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
delete pdfReaderImpl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (!isPdfFile)
|
||||
{
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
fz_context* pContext = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
|
||||
if (NULL == pContext)
|
||||
{
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
HGResult ret = HGBASE_ERR_FAIL;
|
||||
fz_try(pContext)
|
||||
{
|
||||
fz_register_document_handlers(pContext);
|
||||
fz_document* pDoc = fz_open_document(pContext, StdStringToUtf8(fileName).c_str());
|
||||
|
||||
HGPdfReaderImpl* pdfReaderImpl = new HGPdfReaderImpl;
|
||||
pdfReaderImpl->m_pContext = pContext;
|
||||
pdfReaderImpl->m_pDoc = pDoc;
|
||||
*reader = (HGPdfReader)pdfReaderImpl;
|
||||
ret = HGBASE_ERR_OK;
|
||||
}
|
||||
fz_catch(pContext)
|
||||
{
|
||||
fz_drop_context(pContext);
|
||||
pContext = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
*reader = (HGPdfReader)pdfReaderImpl;
|
||||
return HGBASE_ERR_OK;
|
||||
}
|
||||
|
||||
HGResult HGAPI HGImgFmt_ClosePdfReader(HGPdfReader reader)
|
||||
|
@ -191,154 +46,65 @@ HGResult HGAPI HGImgFmt_ClosePdfReader(HGPdfReader reader)
|
|||
}
|
||||
|
||||
HGPdfReaderImpl* pdfReaderImpl = (HGPdfReaderImpl*)reader;
|
||||
HGResult ret = pdfReaderImpl->Close();
|
||||
if (HGBASE_ERR_OK != ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
delete pdfReaderImpl;
|
||||
return HGBASE_ERR_OK;
|
||||
}
|
||||
|
||||
HGResult HGAPI HGImgFmt_GetPdfPageCount(HGPdfReader reader, HGUInt* count)
|
||||
{
|
||||
if (NULL == reader || NULL == count)
|
||||
if (NULL == reader)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
HGPdfReaderImpl* pdfReaderImpl = (HGPdfReaderImpl*)reader;
|
||||
|
||||
HGResult ret = HGBASE_ERR_FAIL;
|
||||
fz_try(pdfReaderImpl->m_pContext)
|
||||
{
|
||||
*count = (uint32_t)fz_count_pages(pdfReaderImpl->m_pContext, pdfReaderImpl->m_pDoc);
|
||||
ret = HGBASE_ERR_OK;
|
||||
}
|
||||
fz_catch(pdfReaderImpl->m_pContext)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return ret;
|
||||
return pdfReaderImpl->GetPageCount(count);
|
||||
}
|
||||
|
||||
HGResult HGAPI HGImgFmt_GetPdfPageInfo(HGPdfReader reader, HGUInt page, HGPdfPageInfo* info)
|
||||
{
|
||||
if (NULL == reader || NULL == info)
|
||||
if (NULL == reader)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
HGPdfReaderImpl* pdfReaderImpl = (HGPdfReaderImpl*)reader;
|
||||
|
||||
HGResult ret = HGBASE_ERR_FAIL;
|
||||
fz_page* fzpage = NULL;
|
||||
|
||||
fz_try(pdfReaderImpl->m_pContext)
|
||||
{
|
||||
fzpage = fz_load_page(pdfReaderImpl->m_pContext, pdfReaderImpl->m_pDoc, (int)page);
|
||||
pdf_page* page = pdf_page_from_fz_page(pdfReaderImpl->m_pContext, fzpage);
|
||||
if (NULL != page)
|
||||
{
|
||||
fz_rect pdfRect = pdf_bound_page(pdfReaderImpl->m_pContext, page);
|
||||
info->width = ceil(pdfRect.x1);
|
||||
info->height = ceil(pdfRect.y1);
|
||||
ret = HGBASE_ERR_OK;
|
||||
}
|
||||
}
|
||||
fz_catch(pdfReaderImpl->m_pContext)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
if (NULL != fzpage)
|
||||
fz_drop_page(pdfReaderImpl->m_pContext, fzpage);
|
||||
return ret;
|
||||
return pdfReaderImpl->GetPageInfo(page, info);
|
||||
}
|
||||
|
||||
HGResult HGAPI HGImgFmt_LoadImageFromPdfReader(HGPdfReader reader, HGUInt page, HGFloat xScale, HGFloat yScale,
|
||||
HGUInt imgType, HGUInt imgOrigin, HGImage* image)
|
||||
{
|
||||
if (NULL == reader || NULL == image)
|
||||
if (NULL == reader)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
HGPdfReaderImpl* pdfReaderImpl = (HGPdfReaderImpl*)reader;
|
||||
|
||||
HGResult ret = HGBASE_ERR_FAIL;
|
||||
fz_pixmap* pix = NULL;
|
||||
|
||||
fz_try(pdfReaderImpl->m_pContext)
|
||||
{
|
||||
fz_matrix ctm = fz_scale(xScale, yScale);
|
||||
pix = fz_new_pixmap_from_page_number(pdfReaderImpl->m_pContext, pdfReaderImpl->m_pDoc, (int)page,
|
||||
ctm, fz_device_rgb(pdfReaderImpl->m_pContext), 0);
|
||||
|
||||
int width = fz_pixmap_width(pdfReaderImpl->m_pContext, pix);
|
||||
int height = fz_pixmap_height(pdfReaderImpl->m_pContext, pix);
|
||||
|
||||
*image = NULL;
|
||||
HGImageInfo imgInfo = { (uint32_t)width, (uint32_t)height, HGBASE_IMGTYPE_RGB, (uint32_t)pix->stride, HGBASE_IMGORIGIN_TOP };
|
||||
HGBase_CreateImageFromData(pix->samples, &imgInfo, NULL, imgType, imgOrigin, image);
|
||||
if (NULL != *image)
|
||||
{
|
||||
ret = HGBASE_ERR_OK;
|
||||
}
|
||||
}
|
||||
fz_catch(pdfReaderImpl->m_pContext)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
if (NULL != pix)
|
||||
fz_drop_pixmap(pdfReaderImpl->m_pContext, pix);
|
||||
return ret;
|
||||
return pdfReaderImpl->LoadImage(page, xScale, yScale, imgType, imgOrigin, image);
|
||||
}
|
||||
|
||||
HGResult HGAPI HGImgFmt_OpenPdfImageWriter(const HGChar* fileName, HGPdfImageWriter* writer)
|
||||
{
|
||||
if (NULL == fileName || NULL == writer)
|
||||
if (NULL == writer)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
PDF* p = PDF_new();
|
||||
if (NULL == p)
|
||||
{
|
||||
return HGBASE_ERR_OUTOFMEMORY;
|
||||
}
|
||||
|
||||
#if defined(HG_CMP_MSC)
|
||||
WCHAR* pUnicode = GetUnicodeStr(fileName);
|
||||
#else
|
||||
uint16_t* pUnicode = GetUnicodeStr(fileName);
|
||||
#endif
|
||||
|
||||
PDF_TRY(p)
|
||||
{
|
||||
PDF_set_parameter(p, "compatibility", "1.4");
|
||||
PDF_set_parameter(p, "errorpolicy", "return");
|
||||
PDF_set_parameter(p, "hypertextencoding", "host");
|
||||
|
||||
#if defined(HG_CMP_MSC)
|
||||
if (-1 == PDF_begin_document(p, (const char*)pUnicode, (int)(GetUnicodeStrLen(pUnicode) * sizeof(WCHAR)), ""))
|
||||
#else
|
||||
if (-1 == PDF_begin_document(p, (const char*)pUnicode, (int)(GetUnicodeStrLen(pUnicode) * sizeof(uint16_t)), ""))
|
||||
#endif
|
||||
{
|
||||
delete[] pUnicode;
|
||||
PDF_delete(p);
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
}
|
||||
PDF_CATCH(p)
|
||||
{
|
||||
delete[] pUnicode;
|
||||
PDF_delete(p);
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
delete[] pUnicode;
|
||||
|
||||
HGPdfImageWriterImpl* pdfImageWriterImpl = new HGPdfImageWriterImpl;
|
||||
pdfImageWriterImpl->m_pPdf = p;
|
||||
HGResult ret = pdfImageWriterImpl->Open(fileName);
|
||||
if (HGBASE_ERR_OK != ret)
|
||||
{
|
||||
delete pdfImageWriterImpl;
|
||||
return ret;
|
||||
}
|
||||
|
||||
*writer = (HGPdfImageWriter)pdfImageWriterImpl;
|
||||
return HGBASE_ERR_OK;
|
||||
}
|
||||
|
@ -351,51 +117,23 @@ HGResult HGAPI HGImgFmt_ClosePdfImageWriter(HGPdfImageWriter writer)
|
|||
}
|
||||
|
||||
HGPdfImageWriterImpl* pdfImageWriterImpl = (HGPdfImageWriterImpl*)writer;
|
||||
HGResult ret = pdfImageWriterImpl->Close();
|
||||
if (HGBASE_ERR_OK != ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
delete pdfImageWriterImpl;
|
||||
return HGBASE_ERR_OK;
|
||||
}
|
||||
|
||||
HGResult HGAPI HGImgFmt_SaveJpegImageToPdfImageWriter(HGPdfImageWriter writer, HGImage image, const HGJpegSaveInfo* info, HGUInt quality)
|
||||
{
|
||||
if (NULL == writer || NULL == image)
|
||||
if (NULL == writer)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
HGBuffer buffer = NULL;
|
||||
HGResult ret = HGImgFmt_SaveJpegImageToBuffer(image, info, quality, &buffer);
|
||||
if (HGBASE_ERR_OK != ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
HGByte* imgBuf = NULL;
|
||||
HGBase_GetBufferData(buffer, &imgBuf);
|
||||
HGUSize size = 0;
|
||||
HGBase_GetBufferSize(buffer, &size);
|
||||
|
||||
HGPdfImageWriterImpl* pdfImageWriterImpl = (HGPdfImageWriterImpl*)writer;
|
||||
|
||||
PDF_TRY(pdfImageWriterImpl->m_pPdf)
|
||||
{
|
||||
PDF_create_pvf(pdfImageWriterImpl->m_pPdf, "virtual_file", 0, imgBuf, size, "");
|
||||
}
|
||||
PDF_CATCH(pdfImageWriterImpl->m_pPdf)
|
||||
{
|
||||
return HGBASE_ERR_OUTOFMEMORY;
|
||||
}
|
||||
|
||||
int img = PDF_load_image(pdfImageWriterImpl->m_pPdf, "auto", "virtual_file", 0, "page 1");
|
||||
if (-1 != img)
|
||||
{
|
||||
PDF_begin_page_ext(pdfImageWriterImpl->m_pPdf, 0.0, 0.0, "");
|
||||
PDF_fit_image(pdfImageWriterImpl->m_pPdf, img, 0.0, 0.0, "adjustpage");
|
||||
PDF_end_page_ext(pdfImageWriterImpl->m_pPdf, "");
|
||||
PDF_close_image(pdfImageWriterImpl->m_pPdf, img);
|
||||
}
|
||||
|
||||
PDF_delete_pvf(pdfImageWriterImpl->m_pPdf, "virtual_file", 0);
|
||||
|
||||
HGBase_DestroyBuffer(buffer);
|
||||
return HGBASE_ERR_OK;
|
||||
return pdfImageWriterImpl->SaveJpegImage(image, info, quality);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,878 @@
|
|||
#include "HGPdfImpl.hpp"
|
||||
#include "../base/HGInc.h"
|
||||
#include "../base/HGUtility.h"
|
||||
#include "../utility/HGString.h"
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
template<typename dst_type, typename src_type>
|
||||
dst_type union_cast(src_type src)
|
||||
{
|
||||
union {
|
||||
src_type s;
|
||||
dst_type d;
|
||||
}u;
|
||||
u.s = src;
|
||||
return u.d;
|
||||
}
|
||||
|
||||
HGPdfReaderImpl::HGPdfReaderImpl()
|
||||
{
|
||||
m_dll = NULL;
|
||||
m_pContext = NULL;
|
||||
m_pDoc = NULL;
|
||||
}
|
||||
|
||||
HGPdfReaderImpl::~HGPdfReaderImpl()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
HGResult HGPdfReaderImpl::Open(const HGChar* fileName)
|
||||
{
|
||||
if (NULL != m_pDoc)
|
||||
{
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
if (NULL == fileName)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
HGBool isPdfFile = HGFALSE;
|
||||
FILE* file = fopen(fileName, "r");
|
||||
if (NULL != file)
|
||||
{
|
||||
HGByte data[4];
|
||||
size_t len = fread(data, 1, 4, file);
|
||||
if (4 == len && 0 == memcmp(data, "%PDF", 4))
|
||||
{
|
||||
isPdfFile = HGTRUE;
|
||||
}
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
if (!isPdfFile)
|
||||
{
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
assert(NULL == m_dll);
|
||||
|
||||
HGChar oldDir[256];
|
||||
HGBase_GetCurrentDir(oldDir, 256);
|
||||
HGChar moduleName[256];
|
||||
HGBase_GetModuleName(union_cast<void*>(&HGPdfReaderImpl::Open), moduleName, 256);
|
||||
HGChar newDir[256];
|
||||
HGBase_GetFilePath(moduleName, newDir, 256);
|
||||
|
||||
HGBase_SetCurrentDir(newDir);
|
||||
#if defined(HG_CMP_MSC)
|
||||
HGResult ret = HGBase_CreateDll("libmupdf.dll", &m_dll);
|
||||
#else
|
||||
HGResult ret = HGBase_CreateDll("libmupdf.so", &m_dll);
|
||||
#endif
|
||||
HGBase_SetCurrentDir(oldDir);
|
||||
if (HGBASE_ERR_OK != ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
fz_context* pContext = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
|
||||
if (NULL == pContext)
|
||||
{
|
||||
HGBase_DestroyDll(m_dll);
|
||||
m_dll = NULL;
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
ret = HGBASE_ERR_FAIL;
|
||||
fz_try(pContext)
|
||||
{
|
||||
fz_register_document_handlers(pContext);
|
||||
fz_document* pDoc = fz_open_document(pContext, StdStringToUtf8(fileName).c_str());
|
||||
|
||||
m_pContext = pContext;
|
||||
m_pDoc = pDoc;
|
||||
ret = HGBASE_ERR_OK;
|
||||
}
|
||||
fz_catch(pContext)
|
||||
{
|
||||
fz_drop_context(pContext);
|
||||
pContext = NULL;
|
||||
HGBase_DestroyDll(m_dll);
|
||||
m_dll = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
HGResult HGPdfReaderImpl::Close()
|
||||
{
|
||||
if (NULL == m_pDoc)
|
||||
{
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
fz_drop_document(m_pContext, m_pDoc);
|
||||
m_pDoc = NULL;
|
||||
fz_drop_context(m_pContext);
|
||||
m_pContext = NULL;
|
||||
HGBase_DestroyDll(m_dll);
|
||||
m_dll = NULL;
|
||||
return HGBASE_ERR_OK;
|
||||
}
|
||||
|
||||
HGResult HGPdfReaderImpl::GetPageCount(HGUInt* count)
|
||||
{
|
||||
if (NULL == m_pDoc)
|
||||
{
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
if (NULL == count)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
HGResult ret = HGBASE_ERR_FAIL;
|
||||
fz_try(m_pContext)
|
||||
{
|
||||
*count = (uint32_t)fz_count_pages(m_pContext, m_pDoc);
|
||||
ret = HGBASE_ERR_OK;
|
||||
}
|
||||
fz_catch(m_pContext)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
HGResult HGPdfReaderImpl::GetPageInfo(HGUInt page, HGPdfPageInfo* info)
|
||||
{
|
||||
if (NULL == m_pDoc)
|
||||
{
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
if (NULL == info)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
HGResult ret = HGBASE_ERR_FAIL;
|
||||
fz_page* fzpage = NULL;
|
||||
|
||||
fz_try(m_pContext)
|
||||
{
|
||||
fzpage = fz_load_page(m_pContext, m_pDoc, (int)page);
|
||||
pdf_page* page = pdf_page_from_fz_page(m_pContext, fzpage);
|
||||
if (NULL != page)
|
||||
{
|
||||
fz_rect pdfRect = pdf_bound_page(m_pContext, page);
|
||||
info->width = ceil(pdfRect.x1);
|
||||
info->height = ceil(pdfRect.y1);
|
||||
ret = HGBASE_ERR_OK;
|
||||
}
|
||||
}
|
||||
fz_catch(m_pContext)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
if (NULL != fzpage)
|
||||
fz_drop_page(m_pContext, fzpage);
|
||||
return ret;
|
||||
}
|
||||
|
||||
HGResult HGPdfReaderImpl::LoadImage(HGUInt page, HGFloat xScale, HGFloat yScale,
|
||||
HGUInt imgType, HGUInt imgOrigin, HGImage* image)
|
||||
{
|
||||
if (NULL == m_pDoc)
|
||||
{
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
if (NULL == image)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
HGResult ret = HGBASE_ERR_FAIL;
|
||||
fz_pixmap* pix = NULL;
|
||||
|
||||
fz_try(m_pContext)
|
||||
{
|
||||
fz_matrix ctm = fz_scale(xScale, yScale);
|
||||
pix = fz_new_pixmap_from_page_number(m_pContext, m_pDoc, (int)page,
|
||||
ctm, fz_device_rgb(m_pContext), 0);
|
||||
|
||||
int width = fz_pixmap_width(m_pContext, pix);
|
||||
int height = fz_pixmap_height(m_pContext, pix);
|
||||
|
||||
*image = NULL;
|
||||
HGImageInfo imgInfo = { (uint32_t)width, (uint32_t)height, HGBASE_IMGTYPE_RGB, (uint32_t)pix->stride, HGBASE_IMGORIGIN_TOP };
|
||||
HGBase_CreateImageFromData(pix->samples, &imgInfo, NULL, imgType, imgOrigin, image);
|
||||
if (NULL != *image)
|
||||
{
|
||||
ret = HGBASE_ERR_OK;
|
||||
}
|
||||
}
|
||||
fz_catch(m_pContext)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
if (NULL != pix)
|
||||
fz_drop_pixmap(m_pContext, pix);
|
||||
return ret;
|
||||
}
|
||||
|
||||
fz_context* HGPdfReaderImpl::fz_new_context_imp(const fz_alloc_context* alloc, const fz_locks_context* locks, size_t max_store, const char* version)
|
||||
{
|
||||
typedef fz_context* (*Func)(const fz_alloc_context*, const fz_locks_context*, size_t, const char*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_new_context_imp", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return func(alloc, locks, max_store, version);
|
||||
}
|
||||
|
||||
void HGPdfReaderImpl::fz_register_document_handlers(fz_context* ctx)
|
||||
{
|
||||
typedef void (*Func)(fz_context*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_register_document_handlers", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(ctx);
|
||||
}
|
||||
|
||||
fz_document* HGPdfReaderImpl::fz_open_document(fz_context* ctx, const char* filename)
|
||||
{
|
||||
typedef fz_document* (*Func)(fz_context*, const char*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_open_document", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return func(ctx, filename);
|
||||
}
|
||||
|
||||
void HGPdfReaderImpl::fz_drop_context(fz_context* ctx)
|
||||
{
|
||||
typedef void (*Func)(fz_context*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_drop_context", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(ctx);
|
||||
}
|
||||
|
||||
void HGPdfReaderImpl::fz_drop_document(fz_context* ctx, fz_document* doc)
|
||||
{
|
||||
typedef void (*Func)(fz_context*, fz_document*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_drop_document", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(ctx, doc);
|
||||
}
|
||||
|
||||
int HGPdfReaderImpl::fz_count_pages(fz_context* ctx, fz_document* doc)
|
||||
{
|
||||
typedef int (*Func)(fz_context*, fz_document*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_count_pages", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return func(ctx, doc);
|
||||
}
|
||||
|
||||
fz_page* HGPdfReaderImpl::fz_load_page(fz_context* ctx, fz_document* doc, int number)
|
||||
{
|
||||
typedef fz_page* (*Func)(fz_context*, fz_document* doc, int);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_load_page", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return func(ctx, doc, number);
|
||||
}
|
||||
|
||||
pdf_page* HGPdfReaderImpl::pdf_page_from_fz_page(fz_context* ctx, fz_page* ptr)
|
||||
{
|
||||
typedef pdf_page* (*Func)(fz_context*, fz_page*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "pdf_page_from_fz_page", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return func(ctx, ptr);
|
||||
}
|
||||
|
||||
fz_rect HGPdfReaderImpl::pdf_bound_page(fz_context* ctx, pdf_page* page)
|
||||
{
|
||||
typedef fz_rect (*Func)(fz_context*, pdf_page*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "pdf_bound_page", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
fz_rect rect = {0, 0, 0, 0};
|
||||
return rect;
|
||||
}
|
||||
|
||||
return func(ctx, page);
|
||||
}
|
||||
|
||||
void HGPdfReaderImpl::fz_drop_page(fz_context* ctx, fz_page* page)
|
||||
{
|
||||
typedef void (*Func)(fz_context*, fz_page*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_drop_page", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(ctx, page);
|
||||
}
|
||||
|
||||
fz_matrix HGPdfReaderImpl::fz_scale(float sx, float sy)
|
||||
{
|
||||
typedef fz_matrix (*Func)(float, float);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_scale", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
fz_matrix matrix = { 0, 0, 0, 0, 0, 0 };
|
||||
return matrix;
|
||||
}
|
||||
|
||||
return func(sx, sy);
|
||||
}
|
||||
|
||||
fz_pixmap* HGPdfReaderImpl::fz_new_pixmap_from_page_number(fz_context* ctx, fz_document* doc, int number, fz_matrix ctm, fz_colorspace* cs, int alpha)
|
||||
{
|
||||
typedef fz_pixmap* (*Func)(fz_context*, fz_document*, int, fz_matrix, fz_colorspace*, int);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_new_pixmap_from_page_number", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return func(ctx, doc, number, ctm, cs, alpha);
|
||||
}
|
||||
|
||||
fz_colorspace* HGPdfReaderImpl::fz_device_rgb(fz_context* ctx)
|
||||
{
|
||||
typedef fz_colorspace* (*Func)(fz_context*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_device_rgb", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return func(ctx);
|
||||
}
|
||||
|
||||
int HGPdfReaderImpl::fz_pixmap_width(fz_context* ctx, const fz_pixmap* pix)
|
||||
{
|
||||
typedef int (*Func)(fz_context*, const fz_pixmap*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_pixmap_width", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return func(ctx, pix);
|
||||
}
|
||||
|
||||
int HGPdfReaderImpl::fz_pixmap_height(fz_context* ctx, const fz_pixmap* pix)
|
||||
{
|
||||
typedef int (*Func)(fz_context*, const fz_pixmap*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_pixmap_height", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return func(ctx, pix);
|
||||
}
|
||||
|
||||
void HGPdfReaderImpl::fz_drop_pixmap(fz_context* ctx, fz_pixmap* pix)
|
||||
{
|
||||
typedef void (*Func)(fz_context*, fz_pixmap*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_drop_pixmap", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(ctx, pix);
|
||||
}
|
||||
|
||||
fz_jmp_buf* HGPdfReaderImpl::fz_push_try(fz_context* ctx)
|
||||
{
|
||||
typedef fz_jmp_buf* (*Func)(fz_context*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_push_try", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return func(ctx);
|
||||
}
|
||||
|
||||
int HGPdfReaderImpl::fz_do_try(fz_context* ctx)
|
||||
{
|
||||
typedef int (*Func)(fz_context*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_do_try", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return func(ctx);
|
||||
}
|
||||
|
||||
int HGPdfReaderImpl::fz_do_catch(fz_context* ctx)
|
||||
{
|
||||
typedef int (*Func)(fz_context*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "fz_do_catch", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return func(ctx);
|
||||
}
|
||||
|
||||
|
||||
#if defined(HG_CMP_MSC)
|
||||
static WCHAR* GetUnicodeStr(const char* text)
|
||||
{
|
||||
int len = ::MultiByteToWideChar(CP_ACP, 0, text, -1, NULL, 0);
|
||||
WCHAR* pUnicode = new WCHAR[len];
|
||||
::MultiByteToWideChar(CP_ACP, 0, text, -1, pUnicode, len);
|
||||
return pUnicode;
|
||||
}
|
||||
static uint32_t GetUnicodeStrLen(const WCHAR* pUnicode)
|
||||
{
|
||||
return (uint32_t)wcslen(pUnicode);
|
||||
}
|
||||
#else
|
||||
static uint16_t* GetUnicodeStr(const char* text)
|
||||
{
|
||||
if (0 == *text)
|
||||
{
|
||||
uint16_t* pUnicode = new uint16_t[1];
|
||||
*pUnicode = 0;
|
||||
return pUnicode;
|
||||
}
|
||||
|
||||
uint16_t* pUnicode = new uint16_t[strlen(text) + 2];
|
||||
memset(pUnicode, 0, sizeof(uint16_t) * (strlen(text) + 2));
|
||||
|
||||
iconv_t cd = iconv_open("UNICODE//IGNORE", "UTF-8");
|
||||
if ((iconv_t)-1 != cd)
|
||||
{
|
||||
char* inbuf = (char*)text;
|
||||
size_t inbytes = strlen(text);
|
||||
char* outbuf = (char*)pUnicode;
|
||||
size_t outsize = sizeof(uint16_t) * (strlen(text) + 1);
|
||||
|
||||
iconv(cd, &inbuf, &inbytes, &outbuf, &outsize);
|
||||
iconv_close(cd);
|
||||
}
|
||||
|
||||
return pUnicode;
|
||||
}
|
||||
static uint32_t GetUnicodeStrLen(const uint16_t* pUnicode)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
while (0 != pUnicode[i])
|
||||
{
|
||||
++i;
|
||||
}
|
||||
|
||||
return i;
|
||||
}
|
||||
#endif
|
||||
|
||||
HGPdfImageWriterImpl::HGPdfImageWriterImpl()
|
||||
{
|
||||
m_dll = NULL;
|
||||
m_pPdf = NULL;
|
||||
}
|
||||
|
||||
HGPdfImageWriterImpl::~HGPdfImageWriterImpl()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
HGResult HGPdfImageWriterImpl::Open(const HGChar* fileName)
|
||||
{
|
||||
if (NULL != m_pPdf)
|
||||
{
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
if (NULL == fileName)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
assert(NULL == m_dll);
|
||||
|
||||
HGChar oldDir[256];
|
||||
HGBase_GetCurrentDir(oldDir, 256);
|
||||
HGChar moduleName[256];
|
||||
HGBase_GetModuleName(union_cast<void*>(&HGPdfImageWriterImpl::Open), moduleName, 256);
|
||||
HGChar newDir[256];
|
||||
HGBase_GetFilePath(moduleName, newDir, 256);
|
||||
|
||||
HGBase_SetCurrentDir(newDir);
|
||||
#if defined(HG_CMP_MSC)
|
||||
HGResult ret = HGBase_CreateDll("pdflib.dll", &m_dll);
|
||||
#else
|
||||
HGResult ret = HGBase_CreateDll("libpdf.so", &m_dll);
|
||||
#endif
|
||||
HGBase_SetCurrentDir(oldDir);
|
||||
if (HGBASE_ERR_OK != ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
PDF* p = PDF_new();
|
||||
if (NULL == p)
|
||||
{
|
||||
HGBase_DestroyDll(m_dll);
|
||||
m_dll = NULL;
|
||||
return HGBASE_ERR_OUTOFMEMORY;
|
||||
}
|
||||
|
||||
#if defined(HG_CMP_MSC)
|
||||
WCHAR* pUnicode = GetUnicodeStr(fileName);
|
||||
#else
|
||||
uint16_t* pUnicode = GetUnicodeStr(fileName);
|
||||
#endif
|
||||
|
||||
PDF_TRY(p)
|
||||
{
|
||||
PDF_set_parameter(p, "compatibility", "1.4");
|
||||
PDF_set_parameter(p, "errorpolicy", "return");
|
||||
PDF_set_parameter(p, "hypertextencoding", "host");
|
||||
|
||||
#if defined(HG_CMP_MSC)
|
||||
if (-1 == PDF_begin_document(p, (const char*)pUnicode, (int)(GetUnicodeStrLen(pUnicode) * sizeof(WCHAR)), ""))
|
||||
#else
|
||||
if (-1 == PDF_begin_document(p, (const char*)pUnicode, (int)(GetUnicodeStrLen(pUnicode) * sizeof(uint16_t)), ""))
|
||||
#endif
|
||||
{
|
||||
delete[] pUnicode;
|
||||
PDF_delete(p);
|
||||
HGBase_DestroyDll(m_dll);
|
||||
m_dll = NULL;
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
}
|
||||
PDF_CATCH(p)
|
||||
{
|
||||
delete[] pUnicode;
|
||||
PDF_delete(p);
|
||||
HGBase_DestroyDll(m_dll);
|
||||
m_dll = NULL;
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
delete[] pUnicode;
|
||||
m_pPdf = p;
|
||||
return HGBASE_ERR_OK;
|
||||
}
|
||||
|
||||
HGResult HGPdfImageWriterImpl::Close()
|
||||
{
|
||||
if (NULL == m_pPdf)
|
||||
{
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
PDF_TRY(m_pPdf)
|
||||
{
|
||||
PDF_end_document(m_pPdf, "");
|
||||
}
|
||||
PDF_CATCH(m_pPdf)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
PDF_delete(m_pPdf);
|
||||
m_pPdf = NULL;
|
||||
HGBase_DestroyDll(m_dll);
|
||||
m_dll = NULL;
|
||||
return HGBASE_ERR_OK;
|
||||
}
|
||||
|
||||
HGResult HGPdfImageWriterImpl::SaveJpegImage(HGImage image, const HGJpegSaveInfo* info, HGUInt quality)
|
||||
{
|
||||
if (NULL == m_pPdf)
|
||||
{
|
||||
return HGBASE_ERR_FAIL;
|
||||
}
|
||||
|
||||
if (NULL == image)
|
||||
{
|
||||
return HGBASE_ERR_INVALIDARG;
|
||||
}
|
||||
|
||||
HGBuffer buffer = NULL;
|
||||
HGResult ret = HGImgFmt_SaveJpegImageToBuffer(image, info, quality, &buffer);
|
||||
if (HGBASE_ERR_OK != ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
HGByte* imgBuf = NULL;
|
||||
HGBase_GetBufferData(buffer, &imgBuf);
|
||||
HGUSize size = 0;
|
||||
HGBase_GetBufferSize(buffer, &size);
|
||||
|
||||
PDF_TRY(m_pPdf)
|
||||
{
|
||||
PDF_create_pvf(m_pPdf, "virtual_file", 0, imgBuf, size, "");
|
||||
}
|
||||
PDF_CATCH(m_pPdf)
|
||||
{
|
||||
HGBase_DestroyBuffer(buffer);
|
||||
return HGBASE_ERR_OUTOFMEMORY;
|
||||
}
|
||||
|
||||
int img = PDF_load_image(m_pPdf, "auto", "virtual_file", 0, "page 1");
|
||||
if (-1 != img)
|
||||
{
|
||||
PDF_begin_page_ext(m_pPdf, 0.0, 0.0, "");
|
||||
PDF_fit_image(m_pPdf, img, 0.0, 0.0, "adjustpage");
|
||||
PDF_end_page_ext(m_pPdf, "");
|
||||
PDF_close_image(m_pPdf, img);
|
||||
}
|
||||
|
||||
PDF_delete_pvf(m_pPdf, "virtual_file", 0);
|
||||
|
||||
HGBase_DestroyBuffer(buffer);
|
||||
return HGBASE_ERR_OK;
|
||||
}
|
||||
|
||||
PDF* HGPdfImageWriterImpl::PDF_new(void)
|
||||
{
|
||||
typedef PDF* (PDFLIB_CALL *Func)();
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_new", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return func();
|
||||
}
|
||||
|
||||
void HGPdfImageWriterImpl::PDF_set_parameter(PDF* p, const char* key, const char* value)
|
||||
{
|
||||
typedef void (PDFLIB_CALL* Func)(PDF*, const char*, const char*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_set_parameter", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(p, key, value);
|
||||
}
|
||||
|
||||
int HGPdfImageWriterImpl::PDF_begin_document(PDF* p, const char* filename, int len, const char* optlist)
|
||||
{
|
||||
typedef int (PDFLIB_CALL* Func)(PDF*, const char*, int, const char*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_begin_document", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return func(p, filename, len, optlist);
|
||||
}
|
||||
|
||||
void HGPdfImageWriterImpl::PDF_delete(PDF* p)
|
||||
{
|
||||
typedef void (PDFLIB_CALL* Func)(PDF*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_delete", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(p);
|
||||
}
|
||||
|
||||
void HGPdfImageWriterImpl::PDF_end_document(PDF* p, const char* optlist)
|
||||
{
|
||||
typedef void (PDFLIB_CALL* Func)(PDF*, const char*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_end_document", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(p, optlist);
|
||||
}
|
||||
|
||||
void HGPdfImageWriterImpl::PDF_create_pvf(PDF* p, const char* filename, int len, const void* data, size_t size, const char* optlist)
|
||||
{
|
||||
typedef void (PDFLIB_CALL* Func)(PDF*, const char*, int, const void*, size_t, const char*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_create_pvf", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(p, filename, len, data, size, optlist);
|
||||
}
|
||||
|
||||
int HGPdfImageWriterImpl::PDF_load_image(PDF* p, const char* imagetype, const char* filename, int len, const char* optlist)
|
||||
{
|
||||
typedef int (PDFLIB_CALL* Func)(PDF*, const char*, const char*, int, const char*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_load_image", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return func(p, imagetype, filename, len, optlist);
|
||||
}
|
||||
|
||||
void HGPdfImageWriterImpl::PDF_begin_page_ext(PDF* p, double width, double height, const char* optlist)
|
||||
{
|
||||
typedef void (PDFLIB_CALL* Func)(PDF*, double, double, const char*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_begin_page_ext", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(p, width, height, optlist);
|
||||
}
|
||||
|
||||
void HGPdfImageWriterImpl::PDF_fit_image(PDF* p, int image, double x, double y, const char* optlist)
|
||||
{
|
||||
typedef void (PDFLIB_CALL* Func)(PDF*, int, double, double, const char*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_fit_image", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(p, image, x, y, optlist);
|
||||
}
|
||||
|
||||
void HGPdfImageWriterImpl::PDF_end_page_ext(PDF* p, const char* optlist)
|
||||
{
|
||||
typedef void (PDFLIB_CALL* Func)(PDF*, const char*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_end_page_ext", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(p, optlist);
|
||||
}
|
||||
|
||||
void HGPdfImageWriterImpl::PDF_close_image(PDF* p, int image)
|
||||
{
|
||||
typedef void (PDFLIB_CALL* Func)(PDF*, int);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_close_image", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
func(p, image);
|
||||
}
|
||||
|
||||
int HGPdfImageWriterImpl::PDF_delete_pvf(PDF* p, const char* filename, int len)
|
||||
{
|
||||
typedef int (PDFLIB_CALL* Func)(PDF*, const char*, int);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "PDF_delete_pvf", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return func(p, filename, len);
|
||||
}
|
||||
|
||||
pdf_jmpbuf* HGPdfImageWriterImpl::pdf_jbuf(PDF* p)
|
||||
{
|
||||
typedef pdf_jmpbuf* (PDFLIB_CALL* Func)(PDF*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "pdf_jbuf", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return func(p);
|
||||
}
|
||||
|
||||
int HGPdfImageWriterImpl::pdf_catch(PDF* p)
|
||||
{
|
||||
typedef int (PDFLIB_CALL* Func)(PDF*);
|
||||
Func func = NULL;
|
||||
HGBase_GetDllProcAddress(m_dll, "pdf_catch", (HGPointer*)&func);
|
||||
if (NULL == func)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return func(p);
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
#ifndef __HGPDFIMPL_HPP__
|
||||
#define __HGPDFIMPL_HPP__
|
||||
|
||||
#include "HGPdf.h"
|
||||
#include "../base/HGDll.h"
|
||||
#include "mupdf/pdf.h"
|
||||
#include "mupdf/fitz.h"
|
||||
#include "pdflib.h"
|
||||
|
||||
class HGPdfReaderImpl
|
||||
{
|
||||
public:
|
||||
HGPdfReaderImpl();
|
||||
~HGPdfReaderImpl();
|
||||
|
||||
HGResult Open(const HGChar* fileName);
|
||||
HGResult Close();
|
||||
HGResult GetPageCount(HGUInt* count);
|
||||
HGResult GetPageInfo(HGUInt page, HGPdfPageInfo* info);
|
||||
HGResult LoadImage(HGUInt page, HGFloat xScale, HGFloat yScale,
|
||||
HGUInt imgType, HGUInt imgOrigin, HGImage* image);
|
||||
|
||||
private:
|
||||
fz_context* fz_new_context_imp(const fz_alloc_context* alloc, const fz_locks_context* locks, size_t max_store, const char* version);
|
||||
void fz_register_document_handlers(fz_context* ctx);
|
||||
fz_document* fz_open_document(fz_context* ctx, const char* filename);
|
||||
void fz_drop_context(fz_context* ctx);
|
||||
void fz_drop_document(fz_context* ctx, fz_document* doc);
|
||||
int fz_count_pages(fz_context* ctx, fz_document* doc);
|
||||
fz_page* fz_load_page(fz_context* ctx, fz_document* doc, int number);
|
||||
pdf_page* pdf_page_from_fz_page(fz_context* ctx, fz_page* ptr);
|
||||
fz_rect pdf_bound_page(fz_context* ctx, pdf_page* page);
|
||||
void fz_drop_page(fz_context* ctx, fz_page* page);
|
||||
fz_matrix fz_scale(float sx, float sy);
|
||||
fz_pixmap* fz_new_pixmap_from_page_number(fz_context* ctx, fz_document* doc, int number, fz_matrix ctm, fz_colorspace* cs, int alpha);
|
||||
fz_colorspace* fz_device_rgb(fz_context* ctx);
|
||||
int fz_pixmap_width(fz_context* ctx, const fz_pixmap* pix);
|
||||
int fz_pixmap_height(fz_context* ctx, const fz_pixmap* pix);
|
||||
void fz_drop_pixmap(fz_context* ctx, fz_pixmap* pix);
|
||||
fz_jmp_buf* fz_push_try(fz_context* ctx);
|
||||
int fz_do_try(fz_context* ctx);
|
||||
int fz_do_catch(fz_context* ctx);
|
||||
|
||||
private:
|
||||
HGDll m_dll;
|
||||
fz_context* m_pContext;
|
||||
fz_document* m_pDoc;
|
||||
};
|
||||
|
||||
class HGPdfImageWriterImpl
|
||||
{
|
||||
public:
|
||||
HGPdfImageWriterImpl();
|
||||
~HGPdfImageWriterImpl();
|
||||
|
||||
HGResult Open(const HGChar* fileName);
|
||||
HGResult Close();
|
||||
HGResult SaveJpegImage(HGImage image, const HGJpegSaveInfo* info, HGUInt quality);
|
||||
|
||||
private:
|
||||
PDF* PDF_new(void);
|
||||
void PDF_set_parameter(PDF* p, const char* key, const char* value);
|
||||
int PDF_begin_document(PDF* p, const char* filename, int len, const char* optlist);
|
||||
void PDF_delete(PDF* p);
|
||||
void PDF_end_document(PDF* p, const char* optlist);
|
||||
void PDF_create_pvf(PDF* p, const char* filename, int len, const void* data, size_t size, const char* optlist);
|
||||
int PDF_load_image(PDF* p, const char* imagetype, const char* filename, int len, const char* optlist);
|
||||
void PDF_begin_page_ext(PDF* p, double width, double height, const char* optlist);
|
||||
void PDF_fit_image(PDF* p, int image, double x, double y, const char* optlist);
|
||||
void PDF_end_page_ext(PDF* p, const char* optlist);
|
||||
void PDF_close_image(PDF* p, int image);
|
||||
int PDF_delete_pvf(PDF* p, const char* filename, int len);
|
||||
pdf_jmpbuf* pdf_jbuf(PDF* p);
|
||||
int pdf_catch(PDF* p);
|
||||
|
||||
private:
|
||||
HGDll m_dll;
|
||||
PDF* m_pPdf;
|
||||
};
|
||||
|
||||
#endif /* __HGPDFIMPL_HPP__ */
|
|
@ -5,6 +5,17 @@
|
|||
#include "../base/HGUtility.h"
|
||||
#include "../imgfmt/HGBmp.h"
|
||||
|
||||
template<typename dst_type, typename src_type>
|
||||
dst_type union_cast(src_type src)
|
||||
{
|
||||
union {
|
||||
src_type s;
|
||||
dst_type d;
|
||||
}u;
|
||||
u.s = src;
|
||||
return u.d;
|
||||
}
|
||||
|
||||
HGUInt HGOCRHanvon::m_refCount = 0;
|
||||
|
||||
HGOCRHanvon::HGOCRHanvon()
|
||||
|
@ -30,7 +41,7 @@ HGResult HGOCRHanvon::Init()
|
|||
HGChar oldDir[256];
|
||||
HGBase_GetCurrentDir(oldDir, 256);
|
||||
HGChar moduleName[256];
|
||||
HGBase_GetModuleName(&m_refCount, moduleName, 256);
|
||||
HGBase_GetModuleName(union_cast<void*>(&HGOCRHanvon::Init), moduleName, 256);
|
||||
HGChar newDir[256];
|
||||
HGBase_GetFilePath(moduleName, newDir, 256);
|
||||
|
||||
|
@ -216,7 +227,7 @@ void HGOCRHanvon::HWOCR_SDKExit(void* pstOcrHandle)
|
|||
return;
|
||||
}
|
||||
|
||||
return func(pstOcrHandle);
|
||||
func(pstOcrHandle);
|
||||
}
|
||||
|
||||
void HGOCRHanvon::HWOCR_InitPdf()
|
||||
|
@ -229,7 +240,7 @@ void HGOCRHanvon::HWOCR_InitPdf()
|
|||
return;
|
||||
}
|
||||
|
||||
return func();
|
||||
func();
|
||||
}
|
||||
|
||||
void HGOCRHanvon::HWOCR_ExitPdf()
|
||||
|
@ -242,7 +253,7 @@ void HGOCRHanvon::HWOCR_ExitPdf()
|
|||
return;
|
||||
}
|
||||
|
||||
return func();
|
||||
func();
|
||||
}
|
||||
|
||||
int HGOCRHanvon::HWOCR_RecognizeFile2Str(unsigned char* ImgFile, void* pstHandle, char** ppRst, int* pnLen)
|
||||
|
@ -268,7 +279,7 @@ void HGOCRHanvon::HWOCR_FreeStrRst(char** ppRst)
|
|||
return;
|
||||
}
|
||||
|
||||
return func(ppRst);
|
||||
func(ppRst);
|
||||
}
|
||||
|
||||
int HGOCRHanvon::HWOCR_RecognizeFile(unsigned char* ImgFile, unsigned char* OutFile, int nFileType, void* pstHandle, char* pLibDir, char* pTmpDir)
|
||||
|
|
Loading…
Reference in New Issue