From 834df8b3d10fb15e9bf2ef657a7fac4747544852 Mon Sep 17 00:00:00 2001 From: Miklos Vajna Date: Thu, 27 Apr 2017 09:14:26 +0200 Subject: xmlsecurity pdfverify: go via vcl graphic filter Initially some proof of concept code was implemented here, but then I copied that to VCL and it got fixes there, so call into vcl from here instead of invoking pdfium directly. Change-Id: I8590cbe94876ff6aff3f870535a99062304f3996 Reviewed-on: https://gerrit.libreoffice.org/37016 Reviewed-by: Miklos Vajna Tested-by: Jenkins --- xmlsecurity/Module_xmlsecurity.mk | 2 - xmlsecurity/workben/pdfverify.cxx | 86 +++------------------------------------ 2 files changed, 6 insertions(+), 82 deletions(-) (limited to 'xmlsecurity') diff --git a/xmlsecurity/Module_xmlsecurity.mk b/xmlsecurity/Module_xmlsecurity.mk index cb68c595f82d..d3fafeb7e4ec 100644 --- a/xmlsecurity/Module_xmlsecurity.mk +++ b/xmlsecurity/Module_xmlsecurity.mk @@ -46,13 +46,11 @@ $(eval $(call gb_Module_add_screenshot_targets,xmlsecurity,\ )) ifneq (,$(filter DESKTOP,$(BUILD_TYPE))) -ifeq ($(ENABLE_PDFIUM),TRUE) $(eval $(call gb_Module_add_targets,xmlsecurity,\ Executable_pdfverify \ )) -endif endif endif diff --git a/xmlsecurity/workben/pdfverify.cxx b/xmlsecurity/workben/pdfverify.cxx index 7746a6e7b716..7b64e42c2a49 100644 --- a/xmlsecurity/workben/pdfverify.cxx +++ b/xmlsecurity/workben/pdfverify.cxx @@ -9,15 +9,6 @@ #include -#ifdef WNT -#include -#endif -#include -#include -#ifdef WNT -#include -#endif - #include #include #include @@ -26,10 +17,9 @@ #include #include #include -#include -#include #include #include +#include #include @@ -39,88 +29,24 @@ using namespace com::sun::star; namespace { -/// Convert to inch, then assume 96 DPI. -double pointToPixel(double fPoint) -{ - return fPoint / 72 * 96; -} - /// Does PDF to PNG conversion using pdfium. void generatePreview(const OString& rPdfPath, const OString& rPngPath) { - FPDF_LIBRARY_CONFIG aConfig; - aConfig.version = 2; - aConfig.m_pUserFontPaths = nullptr; - aConfig.m_pIsolate = nullptr; - aConfig.m_v8EmbedderSlot = 0; - FPDF_InitLibraryWithConfig(&aConfig); - - // Read input into a buffer. + GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter(); + Graphic aGraphic; OUString aInURL; osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(rPdfPath), aInURL); SvFileStream aInStream(aInURL, StreamMode::READ); - SvMemoryStream aInBuffer; - aInBuffer.WriteStream(aInStream); - - // Load the buffer using pdfium. - FPDF_DOCUMENT pPdfDocument = FPDF_LoadMemDocument(aInBuffer.GetData(), aInBuffer.GetSize(), /*password=*/nullptr); - if (!pPdfDocument) - return; - - // Render the first page. - FPDF_PAGE pPdfPage = FPDF_LoadPage(pPdfDocument, /*page_index=*/0); - if (!pPdfPage) - return; - - // Returned unit is points, convert that to pixel. - int nPageWidth = pointToPixel(FPDF_GetPageWidth(pPdfPage)); - int nPageHeight = pointToPixel(FPDF_GetPageHeight(pPdfPage)); - FPDF_BITMAP pPdfBitmap = FPDFBitmap_Create(nPageWidth, nPageHeight, /*alpha=*/1); - if (!pPdfBitmap) + WMF_EXTERNALHEADER* pExtHeader = nullptr; + if (rFilter.ImportGraphic(aGraphic, OUString(), aInStream, GRFILTER_FORMAT_DONTKNOW, nullptr, GraphicFilterImportFlags::NONE, pExtHeader) != GRFILTER_OK) return; - FPDF_DWORD nColor = FPDFPage_HasTransparency(pPdfPage) ? 0x00000000 : 0xFFFFFFFF; - FPDFBitmap_FillRect(pPdfBitmap, 0, 0, nPageWidth, nPageHeight, nColor); - FPDF_RenderPageBitmap(pPdfBitmap, pPdfPage, /*start_x=*/0, /*start_y=*/0, nPageWidth, nPageHeight, /*rotate=*/0, /*flags=*/0); - - // Save the buffer as a PNG file. - Bitmap aBitmap(Size(nPageWidth, nPageHeight), 32); - { - Bitmap::ScopedWriteAccess pWriteAccess(aBitmap); - const char* pPdfBuffer = static_cast(FPDFBitmap_GetBuffer(pPdfBitmap)); -#ifndef MACOSX - std::memcpy(pWriteAccess->GetBuffer(), pPdfBuffer, nPageWidth * nPageHeight * 4); -#else - // ARGB -> BGRA - for (int nRow = 0; nRow < nPageHeight; ++nRow) - { - int nStride = FPDFBitmap_GetStride(pPdfBitmap); - const char* pPdfLine = pPdfBuffer + (nStride * nRow); - Scanline pRow = pWriteAccess->GetBuffer() + (nPageWidth * nRow * 4); - for (int nCol = 0; nCol < nPageWidth; ++nCol) - { - pRow[nCol * 4] = pPdfLine[(nCol * 4) + 3]; - pRow[(nCol * 4) + 1] = pPdfLine[(nCol * 4) + 2]; - pRow[(nCol * 4) + 2] = pPdfLine[(nCol * 4) + 1]; - pRow[(nCol * 4) + 3] = pPdfLine[nCol * 4]; - } - } -#endif - } - BitmapEx aBitmapEx(aBitmap); -#if defined(WNT) || defined(MACOSX) - aBitmapEx.Mirror(BmpMirrorFlags::Vertical); -#endif + BitmapEx aBitmapEx = aGraphic.GetBitmapEx(); vcl::PNGWriter aWriter(aBitmapEx); OUString aOutURL; osl::FileBase::getFileURLFromSystemPath(OUString::fromUtf8(rPngPath), aOutURL); SvFileStream aOutStream(aOutURL, StreamMode::WRITE); aWriter.Write(aOutStream); - - FPDFBitmap_Destroy(pPdfBitmap); - FPDF_ClosePage(pPdfPage); - FPDF_CloseDocument(pPdfDocument); - FPDF_DestroyLibrary(); } int pdfVerify(int nArgc, char** pArgv) -- cgit