include/vcl/pdfread.hxx | 20 +++++++++++++++++++- sd/source/ui/view/sdview4.cxx | 6 ++++++ vcl/source/filter/ipdf/pdfread.cxx | 9 +++++++-- vcl/source/pdf/PdfConfig.cxx | 5 ++++- 4 files changed, 36 insertions(+), 4 deletions(-)
New commits: commit 266e6b76763d16bcb1dcf67bab2478a7d2d7b8fc Author: Tor Lillqvist <[email protected]> AuthorDate: Wed Feb 2 16:29:34 2022 +0200 Commit: Miklos Vajna <[email protected]> CommitDate: Thu Feb 3 14:43:53 2022 +0100 Make inserted or pasted PDF sharper on macOS When inserting a PDF file as an image or pasting PDF data from the clipboard on a Retina iMac the resulting rendered image did not look sharp. Using a surprisingly large extra scaling factor helps. The exact reasons for this are unknown. It isn't enough to use a scaling factor of just 2 (which is the HiDI ("Retina") scale factor on my iMac). Possibly the fuzziness is related to what Pdfium uses to render text. Also, look at CountDPIScaleFactor() in vcl/source/window/window.cxx. The GetDPIScaleFactor() function lies on macOS even more than it does on other platforms. It claims that the DPI scale percentage is always 100. But in fact most Macs nowadays have a Retina display so it would make more sense, in theory, to at least always return 200 instead. That wouldn't be any more wrong. But that causes regressions in the UI rendering, like needlessly large icons in the toolbars. Change-Id: Idc694f742c4ac32a5a134f8d206cf4eee467c39a Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129369 Tested-by: Jenkins Reviewed-by: Miklos Vajna <[email protected]> diff --git a/include/vcl/pdfread.hxx b/include/vcl/pdfread.hxx index 084bd3f913b5..72508a548293 100644 --- a/include/vcl/pdfread.hxx +++ b/include/vcl/pdfread.hxx @@ -1,4 +1,4 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ /* * This file is part of the LibreOffice project. * @@ -42,6 +42,24 @@ importPdfVectorGraphicData(SvStream& rStream, /// Imports a PDF stream into rGraphic. VCL_DLLPUBLIC bool ImportPDF(SvStream& rStream, Graphic& rGraphic); +// When inserting a PDF file as an image or pasting PDF data from the clipboard, at least on a +// Retina iMac, the resulting rendered image does not look sharp without this surprisingly large +// extra scaling factor. Exact reasons unknown. And it isn't enough to have it be just 2 (which is +// the actual Retina factor on my iMac). Possibly the fuzziness is related to what Pdfium uses to +// render text. + +// Also, look at CountDPIScaleFactor() in vcl/source/window/window.cxx. The GetDPIScaleFactor() API +// lies on macOS even more than it does on other platforms, it claims that the DPI scale factor is +// always 1. But in fact most Macs nowadays have a HiDPI ("Retina") display. But we can't just "fix" +// things by making GetDPIScaleFactor() always return 2 on macOS, even if that wouldn't be any more +// wrong, because that then causes other regressions that I have no time to look into now. + +#ifdef MACOSX +constexpr int PDF_INSERT_MAGIC_SCALE_FACTOR = 8; +#else +constexpr int PDF_INSERT_MAGIC_SCALE_FACTOR = 1; +#endif + struct PDFGraphicAnnotation { OUString maAuthor; diff --git a/sd/source/ui/view/sdview4.cxx b/sd/source/ui/view/sdview4.cxx index 80a653b7b9c8..5b7d5387d2c8 100644 --- a/sd/source/ui/view/sdview4.cxx +++ b/sd/source/ui/view/sdview4.cxx @@ -30,6 +30,8 @@ #include <sfx2/fcontnr.hxx> #include <sfx2/docfile.hxx> #include <sfx2/sfxsids.hrc> +#include <vcl/outdev.hxx> +#include <vcl/pdfread.hxx> #include <vcl/svapp.hxx> #include <vcl/weld.hxx> #include <svx/svdpagv.hxx> @@ -197,6 +199,10 @@ SdrGrafObj* View::InsertGraphic( const Graphic& rGraphic, sal_Int8& rAction, } sal_Int32 nPreferredDPI = mrDoc.getImagePreferredDPI(); + + if (rGraphic.GetGfxLink().GetType() == GfxLinkType::NativePdf && nPreferredDPI == 0 && vcl::PDF_INSERT_MAGIC_SCALE_FACTOR > 1) + nPreferredDPI = Application::GetDefaultDevice()->GetDPIX() * vcl::PDF_INSERT_MAGIC_SCALE_FACTOR; + if (nPreferredDPI > 0) { auto nWidth = o3tl::convert(aSizePixel.Width() / double(nPreferredDPI), o3tl::Length::in, o3tl::Length::mm100); diff --git a/vcl/source/filter/ipdf/pdfread.cxx b/vcl/source/filter/ipdf/pdfread.cxx index a69d10c7cafc..0780e1c9baaa 100644 --- a/vcl/source/filter/ipdf/pdfread.cxx +++ b/vcl/source/filter/ipdf/pdfread.cxx @@ -7,6 +7,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include <vcl/outdev.hxx> +#include <vcl/svapp.hxx> #include <vcl/pdfread.hxx> #include <tools/UnitConversion.hxx> @@ -156,8 +158,11 @@ size_t RenderPDFBitmaps(const void* pBuffer, int nSize, std::vector<BitmapEx>& r } // Returned unit is points, convert that to pixel. - const size_t nPageWidth = pointToPixel(nPageWidthPoints, fResolutionDPI); - const size_t nPageHeight = pointToPixel(nPageHeightPoints, fResolutionDPI); + + const size_t nPageWidth + = pointToPixel(nPageWidthPoints, fResolutionDPI) * PDF_INSERT_MAGIC_SCALE_FACTOR; + const size_t nPageHeight + = pointToPixel(nPageHeightPoints, fResolutionDPI) * PDF_INSERT_MAGIC_SCALE_FACTOR; std::unique_ptr<vcl::pdf::PDFiumBitmap> pPdfBitmap = pPdfium->createBitmap(nPageWidth, nPageHeight, /*nAlpha=*/1); if (!pPdfBitmap) diff --git a/vcl/source/pdf/PdfConfig.cxx b/vcl/source/pdf/PdfConfig.cxx index e74d30cfe22a..0e6a0436033d 100644 --- a/vcl/source/pdf/PdfConfig.cxx +++ b/vcl/source/pdf/PdfConfig.cxx @@ -11,6 +11,9 @@ #include <pdf/PdfConfig.hxx> #include <cstdlib> +#include <vcl/svapp.hxx> +#include <vcl/outdev.hxx> + namespace vcl::pdf { /// Get the default PDF rendering resolution in DPI. @@ -26,7 +29,7 @@ double getDefaultPdfResolutionDpi() } // Fallback to a sensible default. - return 96.; + return Application::GetDefaultDevice()->GetDPIX(); } }
