diff options
author | Ashod Nakashian <ashod.nakashian@collabora.co.uk> | 2018-05-24 23:01:15 -0400 |
---|---|---|
committer | Ashod Nakashian <ashod.nakashian@collabora.co.uk> | 2018-06-04 12:36:28 -0400 |
commit | dd3c2e606cd03fc0fe4f157a787bfab050b9f7f5 (patch) | |
tree | 38c6394b4e437c32205242c883340cf05bcab6a0 | |
parent | e7a4f2a27ad9a6d4f7da9734f846137b50338340 (diff) |
pdf: preserve the original page dimensions on import
Also allow for rendering PDFs to images at custom
resolution, instead of hard-coded (old hard-coded
value of 96 dpi is now default arguments).
Reviewed-on: https://gerrit.libreoffice.org/54786
Reviewed-by: Jan Holesovsky <kendy@collabora.com>
Tested-by: Jan Holesovsky <kendy@collabora.com>
(cherry picked from commit 40478fae4fe73319c5f336c69c6f5f5b0fed9991)
Change-Id: Ia5b52f72d6ce7130a2debc7c6f86504aa041bdc8
-rw-r--r-- | include/vcl/pdfread.hxx | 10 | ||||
-rw-r--r-- | sd/source/filter/pdf/sdpdffilter.cxx | 13 | ||||
-rw-r--r-- | vcl/source/filter/ipdf/pdfread.cxx | 33 |
3 files changed, 39 insertions, 17 deletions
diff --git a/include/vcl/pdfread.hxx b/include/vcl/pdfread.hxx index c9e62bb5cb21..0fc3249cd6a3 100644 --- a/include/vcl/pdfread.hxx +++ b/include/vcl/pdfread.hxx @@ -35,11 +35,15 @@ namespace vcl VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Bitmap& rBitmap, size_t nPageIndex, css::uno::Sequence<sal_Int8>& rPdfData, sal_uInt64 nPos = STREAM_SEEK_TO_BEGIN, - sal_uInt64 nSize = STREAM_SEEK_TO_END); -VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Graphic& rGraphic); + sal_uInt64 nSize = STREAM_SEEK_TO_END, + const double fResolutionDPI = 96.); + +VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Graphic& rGraphic, + const double fResolutionDPI = 96.); VCL_DLLPUBLIC size_t ImportPDF(const OUString& rURL, std::vector<Bitmap>& rBitmaps, - css::uno::Sequence<sal_Int8>& rPdfData); + css::uno::Sequence<sal_Int8>& rPdfData, + const double fResolutionDPI = 96.); } #endif // INCLUDED_VCL_SOURCE_FILTER_IPDF_PDFREAD_HXX diff --git a/sd/source/filter/pdf/sdpdffilter.cxx b/sd/source/filter/pdf/sdpdffilter.cxx index 71eecabfb3b0..cc7be1936e61 100644 --- a/sd/source/filter/pdf/sdpdffilter.cxx +++ b/sd/source/filter/pdf/sdpdffilter.cxx @@ -98,9 +98,12 @@ bool SdPdfFilter::Import() const OUString aFileName( mrMedium.GetURLObject().GetMainURL(INetURLObject::DecodeMechanism::NONE)); + // Rendering resolution. + const double dResolutionDPI = 96.; + uno::Sequence<sal_Int8> aPdfData; std::vector<Bitmap> aBitmaps; - if (vcl::ImportPDF(aFileName, aBitmaps, aPdfData) == 0) + if (vcl::ImportPDF(aFileName, aBitmaps, aPdfData, dResolutionDPI) == 0) return false; // Prepare the link with the PDF stream. @@ -128,8 +131,12 @@ bool SdPdfFilter::Import() // Create the page and insert the Graphic. SdPage* pPage = mrDocument.GetSdPage(nPageNumber++, PageKind::Standard); - const Size aGrfSize(OutputDevice::LogicToLogic( - aGraphic.GetPrefSize(), aGraphic.GetPrefMapMode(), MapMode(MapUnit::Map100thMM))); + Size aGrfSize(OutputDevice::LogicToLogic(aGraphic.GetPrefSize(), aGraphic.GetPrefMapMode(), + MapMode(MapUnit::Map100thMM))); + + // Resize to original size based on 72 dpi to preserve page size. + aGrfSize = Size(aGrfSize.Width() * 72. / dResolutionDPI, + aGrfSize.Height() * 72. / dResolutionDPI); // Make the page size match the rendered image. pPage->SetSize(aGrfSize); diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx index 52a6f7a3af89..3b0dce163e0c 100644 --- a/vcl/source/filter/ipdf/pdfread.cxx +++ b/vcl/source/filter/ipdf/pdfread.cxx @@ -54,11 +54,15 @@ int CompatibleWriter::WriteBlockCallback(FPDF_FILEWRITE* pFileWrite, const void* } /// Convert to inch, then assume 96 DPI. -double pointToPixel(double fPoint) { return fPoint / 72 * 96; } +inline double pointToPixel(const double fPoint, const double fResolutionDPI) +{ + return fPoint * fResolutionDPI / 72.; +} /// Does PDF to bitmap conversion using pdfium. size_t generatePreview(SvStream& rStream, std::vector<Bitmap>& rBitmaps, sal_uInt64 nPos, - sal_uInt64 nSize, const size_t nFirstPage, int nPages) + sal_uInt64 nSize, const size_t nFirstPage, int nPages, + const double fResolutionDPI = 96.) { FPDF_LIBRARY_CONFIG aConfig; aConfig.version = 2; @@ -90,8 +94,8 @@ size_t generatePreview(SvStream& rStream, std::vector<Bitmap>& rBitmaps, sal_uIn break; // Returned unit is points, convert that to pixel. - const size_t nPageWidth = pointToPixel(FPDF_GetPageWidth(pPdfPage)); - const size_t nPageHeight = pointToPixel(FPDF_GetPageHeight(pPdfPage)); + const size_t nPageWidth = pointToPixel(FPDF_GetPageWidth(pPdfPage), fResolutionDPI); + const size_t nPageHeight = pointToPixel(FPDF_GetPageHeight(pPdfPage), fResolutionDPI); FPDF_BITMAP pPdfBitmap = FPDFBitmap_Create(nPageWidth, nPageHeight, /*alpha=*/1); if (!pPdfBitmap) break; @@ -194,12 +198,15 @@ bool getCompatibleStream(SvStream& rInStream, SvStream& rOutStream, sal_uInt64 n } #else size_t generatePreview(SvStream&, std::vector<Bitmap>&, sal_uInt64 nPos, sal_uInt64 nSize, - size_t nFirstPage = 0, int nLastPage = 0) + size_t nFirstPage = 0, int nLastPage = 0, const double fResolutionDPI = 96.) { (void)rStream; (void)rBitmap; (void)nPos; (void)nSize; + (void)nFirstPage; + (void)nLastPage; + (void)fResolutionDPI; return 0; } @@ -217,11 +224,13 @@ bool getCompatibleStream(SvStream& rInStream, SvStream& rOutStream, sal_uInt64 n namespace vcl { bool ImportPDF(SvStream& rStream, Bitmap& rBitmap, size_t nPageIndex, - css::uno::Sequence<sal_Int8>& rPdfData, sal_uInt64 nPos, sal_uInt64 nSize) + css::uno::Sequence<sal_Int8>& rPdfData, sal_uInt64 nPos, sal_uInt64 nSize, + const double fResolutionDPI) { // Get the preview of the first page. std::vector<Bitmap> aBitmaps; - if (generatePreview(rStream, aBitmaps, nPos, nSize, nPageIndex, 1) != 1 || aBitmaps.empty()) + if (generatePreview(rStream, aBitmaps, nPos, nSize, nPageIndex, 1, fResolutionDPI) != 1 + || aBitmaps.empty()) return false; rBitmap = aBitmaps[0]; @@ -239,11 +248,11 @@ bool ImportPDF(SvStream& rStream, Bitmap& rBitmap, size_t nPageIndex, return true; } -bool ImportPDF(SvStream& rStream, Graphic& rGraphic) +bool ImportPDF(SvStream& rStream, Graphic& rGraphic, const double fResolutionDPI) { uno::Sequence<sal_Int8> aPdfData; Bitmap aBitmap; - const bool bRet = ImportPDF(rStream, aBitmap, 0, aPdfData); + const bool bRet = ImportPDF(rStream, aBitmap, 0, aPdfData, fResolutionDPI); rGraphic = aBitmap; rGraphic.setPdfData(std::make_shared<css::uno::Sequence<sal_Int8>>(aPdfData)); rGraphic.setPageNumber(0); // We currently import only the first page. @@ -251,12 +260,14 @@ bool ImportPDF(SvStream& rStream, Graphic& rGraphic) } size_t ImportPDF(const OUString& rURL, std::vector<Bitmap>& rBitmaps, - css::uno::Sequence<sal_Int8>& rPdfData) + css::uno::Sequence<sal_Int8>& rPdfData, const double fResolutionDPI) { std::unique_ptr<SvStream> xStream( ::utl::UcbStreamHelper::CreateStream(rURL, StreamMode::READ | StreamMode::SHARE_DENYNONE)); - if (generatePreview(*xStream, rBitmaps, STREAM_SEEK_TO_BEGIN, STREAM_SEEK_TO_END, 0, -1) == 0) + if (generatePreview(*xStream, rBitmaps, STREAM_SEEK_TO_BEGIN, STREAM_SEEK_TO_END, 0, -1, + fResolutionDPI) + == 0) return 0; // Save the original PDF stream for later use. |