filter/source/pdf/pdfexport.cxx | 39 +++++++++++++++++++++++++++++++++++++++ filter/source/pdf/pdfexport.hxx | 1 + 2 files changed, 40 insertions(+)
New commits: commit f2d44962b4f9c0e91375d829800a9b603eb454d8 Author: Miklos Vajna <vmik...@collabora.com> AuthorDate: Thu Nov 24 14:30:22 2022 +0100 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Wed Dec 7 07:46:10 2022 +0000 Related: tdf#54053 PDF export: add UNO API to customize the watermark rotation The watermark direction is currently either 0 degrees for landscape pages or 270 degrees for portrait pages. The problem is many people expect 45 degrees rotation angle or some custom angle in general, and we provide no way to control it. Fix the problem by adding a new "WatermarkRotateAngle" PDF export filter option to specify the rotation angle explicitly. Note that the watermark text is still centered, and the text size is decreased to still fit the page boundaries. To keep things simple, do this shrinking by going with a size that matches the shorter dimension of the page, instead of some more complex iterative approach. Example cmdline usage: soffice --convert-to pdf:writer_pdf_Export:'{"Watermark":{"type":"string","value":"draft"}, "WatermarkRotateAngle":{"type":"long","value":"450"}}' test.odt (cherry picked from commit 574db5efa9a2ab6d70faedf538be77a1eb8c597b) Conflicts: filter/CppunitTest_filter_pdf.mk filter/qa/pdf.cxx filter/source/pdf/pdfexport.cxx Change-Id: I1fee14c333e68c92cf4c65ec100e04dcf024f907 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143712 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> Reviewed-by: Miklos Vajna <vmik...@collabora.com> diff --git a/filter/source/pdf/pdfexport.cxx b/filter/source/pdf/pdfexport.cxx index 0be005e1434c..8a7948cdf79f 100644 --- a/filter/source/pdf/pdfexport.cxx +++ b/filter/source/pdf/pdfexport.cxx @@ -38,6 +38,7 @@ #include <unotools/configmgr.hxx> #include <cppuhelper/compbase.hxx> #include <cppuhelper/basemutex.hxx> +#include <basegfx/matrix/b2dhommatrix.hxx> #include "pdfexport.hxx" #include <strings.hrc> @@ -550,6 +551,14 @@ bool PDFExport::Export( const OUString& rFile, const Sequence< PropertyValue >& moWatermarkFontHeight = nFontHeight; } } + else if (rProp.Name == "WatermarkRotateAngle") + { + sal_Int32 nRotateAngle{}; + if (rProp.Value >>= nRotateAngle) + { + moWatermarkRotateAngle = Degree10(nRotateAngle); + } + } else if (rProp.Name == "WatermarkFontName") { OUString aFontName{}; @@ -1156,6 +1165,17 @@ void PDFExport::ImplWriteWatermark( vcl::PDFWriter& rWriter, const Size& rPageSi aFont.SetOrientation( 2700_deg10 ); } + if (moWatermarkRotateAngle) + { + aFont.SetOrientation(*moWatermarkRotateAngle); + if (rPageSize.Width() < rPageSize.Height()) + { + // Set text width based on the shorter side, so rotation can't push text outside the + // page boundaries. + nTextWidth = rPageSize.Width(); + } + } + // adjust font height for text to fit OutputDevice* pDev = rWriter.GetReferenceDevice(); pDev->Push(); @@ -1209,6 +1229,25 @@ void PDFExport::ImplWriteWatermark( vcl::PDFWriter& rWriter, const Size& rPageSi (rPageSize.Height()-w)/2 ); aTextRect = tools::Rectangle( aTextPoint, Size( nTextHeight, w ) ); } + + if (moWatermarkRotateAngle) + { + // First set the text's starting point to the center of the page. + tools::Rectangle aPageRectangle(Point(0, 0), rPageSize); + aTextPoint = aPageRectangle.Center(); + // Then adjust it so that the text remains centered, based on the rotation angle. + basegfx::B2DPolygon aTextPolygon + = basegfx::utils::createPolygonFromRect(basegfx::B2DRectangle(0, -nTextHeight, w, 0)); + basegfx::B2DHomMatrix aMatrix; + aMatrix.rotate(-1 * toRadians(*moWatermarkRotateAngle)); + aTextPolygon.transform(aMatrix); + basegfx::B2DPoint aPolygonCenter = aTextPolygon.getB2DRange().getCenter(); + aTextPoint.AdjustX(-aPolygonCenter.getX()); + aTextPoint.AdjustY(-aPolygonCenter.getY()); + + aTextRect = aPageRectangle; + } + rWriter.SetClipRegion(); rWriter.BeginTransparencyGroup(); rWriter.DrawText( aTextPoint, msWatermark ); diff --git a/filter/source/pdf/pdfexport.hxx b/filter/source/pdf/pdfexport.hxx index 6fc98f6acd47..8d839f3b18a8 100644 --- a/filter/source/pdf/pdfexport.hxx +++ b/filter/source/pdf/pdfexport.hxx @@ -81,6 +81,7 @@ private: Color maWatermarkColor; std::optional<sal_Int32> moWatermarkFontHeight; OUString maWatermarkFontName; + std::optional<Degree10> moWatermarkRotateAngle; OUString msTiledWatermark; // these variable are here only to have a location in filter/pdf to set the default