diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2022-11-24 14:30:22 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2022-12-07 07:46:10 +0000 |
commit | f2d44962b4f9c0e91375d829800a9b603eb454d8 (patch) | |
tree | e6e3462a4b1d12e55342edbc178c6106e598181a | |
parent | 481bb66b2e12a2a885b1d963f719aa84069dc270 (diff) |
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 <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
-rw-r--r-- | filter/source/pdf/pdfexport.cxx | 39 | ||||
-rw-r--r-- | filter/source/pdf/pdfexport.hxx | 1 |
2 files changed, 40 insertions, 0 deletions
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 |