include/vcl/filter/PDFiumLibrary.hxx | 3 ++ vcl/source/filter/ipdf/pdfread.cxx | 45 ++++++++++------------------------- vcl/source/pdf/PDFiumLibrary.cxx | 36 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 31 deletions(-)
New commits: commit ff79f6fa9b09f0b501e8096999b7a16c57070388 Author: Miklos Vajna <vmik...@collabora.com> AuthorDate: Wed Oct 21 21:01:57 2020 +0200 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Thu Oct 22 09:06:33 2020 +0200 pdfium: add a wrapper for FPDF_SaveWithVersion() And use it in getCompatibleStream(). Change-Id: I48ab2a17c0780b78c6af6dbff50dba81f8041f43 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/104642 Tested-by: Jenkins Reviewed-by: Miklos Vajna <vmik...@collabora.com> diff --git a/include/vcl/filter/PDFiumLibrary.hxx b/include/vcl/filter/PDFiumLibrary.hxx index 96f258127693..1900c07d8e8c 100644 --- a/include/vcl/filter/PDFiumLibrary.hxx +++ b/include/vcl/filter/PDFiumLibrary.hxx @@ -30,6 +30,8 @@ #include <fpdf_doc.h> +class SvMemoryStream; + namespace vcl::pdf { constexpr char constDictionaryKeyTitle[] = "T"; @@ -236,6 +238,7 @@ public: basegfx::B2DSize getPageSize(int nIndex); int getPageCount(); int getFileVersion(); + bool saveWithVersion(SvMemoryStream& rStream, int nFileVersion); std::unique_ptr<PDFiumPage> openPage(int nIndex); }; diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx index 43cb80d50cee..f4ed0acf5b43 100644 --- a/vcl/source/filter/ipdf/pdfread.cxx +++ b/vcl/source/filter/ipdf/pdfread.cxx @@ -14,7 +14,6 @@ #if HAVE_FEATURE_PDFIUM #include <fpdfview.h> #include <fpdf_edit.h> -#include <fpdf_save.h> #include <tools/UnitConversion.hxx> #endif @@ -32,19 +31,6 @@ namespace { #if HAVE_FEATURE_PDFIUM -/// Callback class to be used with FPDF_SaveWithVersion(). -struct CompatibleWriter : public FPDF_FILEWRITE -{ - SvMemoryStream m_aStream; -}; - -int CompatibleWriterCallback(FPDF_FILEWRITE* pFileWrite, const void* pData, unsigned long nSize) -{ - auto pImpl = static_cast<CompatibleWriter*>(pFileWrite); - pImpl->m_aStream.WriteBytes(pData, nSize); - return 1; -} - /// Convert to inch, then assume 96 DPI. inline double pointToPixel(const double fPoint, const double fResolutionDPI) { @@ -93,24 +79,21 @@ bool getCompatibleStream(SvStream& rInStream, SvStream& rOutStream) SvMemoryStream aInBuffer; aInBuffer.WriteStream(rInStream, nSize); - // Load the buffer using pdfium. - FPDF_DOCUMENT pPdfDocument - = FPDF_LoadMemDocument(aInBuffer.GetData(), aInBuffer.GetSize(), /*password=*/nullptr); - if (!pPdfDocument) - return false; - - CompatibleWriter aWriter; - aWriter.version = 1; - aWriter.WriteBlock = &CompatibleWriterCallback; - - // 16 means PDF-1.6. - if (!FPDF_SaveWithVersion(pPdfDocument, &aWriter, 0, 16)) - return false; - - FPDF_CloseDocument(pPdfDocument); + SvMemoryStream aSaved; + { + // Load the buffer using pdfium. + std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument + = pPdfium->openDocument(aInBuffer.GetData(), aInBuffer.GetSize()); + if (!pPdfDocument) + return false; + + // 16 means PDF-1.6. + if (!pPdfDocument->saveWithVersion(aSaved, 16)) + return false; + } - aWriter.m_aStream.Seek(STREAM_SEEK_TO_BEGIN); - rOutStream.WriteStream(aWriter.m_aStream); + aSaved.Seek(STREAM_SEEK_TO_BEGIN); + rOutStream.WriteStream(aSaved); } return rOutStream.good(); diff --git a/vcl/source/pdf/PDFiumLibrary.cxx b/vcl/source/pdf/PDFiumLibrary.cxx index 41e061bb1f6c..10fa42f143cf 100644 --- a/vcl/source/pdf/PDFiumLibrary.cxx +++ b/vcl/source/pdf/PDFiumLibrary.cxx @@ -18,12 +18,35 @@ #include <fpdf_annot.h> #include <fpdf_edit.h> #include <fpdf_text.h> +#include <fpdf_save.h> #include <osl/endian.h> #include <vcl/bitmap.hxx> +#include <tools/stream.hxx> #include <bitmapwriteaccess.hxx> +namespace +{ +/// Callback class to be used with FPDF_SaveWithVersion(). +struct CompatibleWriter : public FPDF_FILEWRITE +{ + CompatibleWriter(SvMemoryStream& rStream) + : m_rStream(rStream) + { + } + + SvMemoryStream& m_rStream; +}; + +int CompatibleWriterCallback(FPDF_FILEWRITE* pFileWrite, const void* pData, unsigned long nSize) +{ + auto pImpl = static_cast<CompatibleWriter*>(pFileWrite); + pImpl->m_rStream.WriteBytes(pData, nSize); + return 1; +} +} + namespace vcl::pdf { OUString convertPdfDateToISO8601(OUString const& rInput) @@ -196,6 +219,19 @@ int PDFiumDocument::getFileVersion() return nFileVersion; } +bool PDFiumDocument::saveWithVersion(SvMemoryStream& rStream, int nFileVersion) +{ + CompatibleWriter aWriter(rStream); + aWriter.version = 1; + aWriter.WriteBlock = &CompatibleWriterCallback; + if (!FPDF_SaveWithVersion(mpPdfDocument, &aWriter, 0, nFileVersion)) + { + return false; + } + + return true; +} + int PDFiumPage::getObjectCount() { return FPDFPage_CountObjects(mpPage); } std::unique_ptr<PDFiumPageObject> PDFiumPage::getObject(int nIndex) _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits