diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2022-11-23 13:39:40 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2022-11-23 15:47:03 +0100 |
commit | d1dd9b9733511ff451e264169537c08fa14c574f (patch) | |
tree | 7782a030dafe2398010c55da09d266580270eded | |
parent | 6c3cebd5aaec641807f5efed814fc5d1c64df149 (diff) |
Related: tdf#54053 PDF export: add UNO API to customize the watermark font name
The font name of the watermark text is currently hardwired to Helvetica,
a sans-serif font.
The problem is that this looks bad in case your document uses serif or
monospace fonts. Or perhaps your document uses a single font and you
want to make sure that the watermark uses exactly the same font.
Fix the problem by adding a new "WatermarkFontName" PDF export filter
option to specify the font name explicitly.
Example cmdline usage:
soffice --convert-to pdf:writer_pdf_Export:'{"Watermark":{"type":"string","value":"draft"}, "WatermarkFontName":{"type":"string","value":"Times"}}' test.odt
Change-Id: I2f065e1517236695b0a95369bdcd803125018587
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143143
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
-rw-r--r-- | filter/qa/pdf.cxx | 49 | ||||
-rw-r--r-- | filter/source/pdf/pdfexport.cxx | 11 | ||||
-rw-r--r-- | filter/source/pdf/pdfexport.hxx | 1 |
3 files changed, 58 insertions, 3 deletions
diff --git a/filter/qa/pdf.cxx b/filter/qa/pdf.cxx index 4c8a9a2dc7dc..c3edb1c5f841 100644 --- a/filter/qa/pdf.cxx +++ b/filter/qa/pdf.cxx @@ -196,7 +196,7 @@ CPPUNIT_TEST_FIXTURE(Test, testWatermarkFontHeight) return; mxComponent.set(loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument")); - // When exporting that as PDF with a red watermark: + // When exporting that as PDF with a 100pt-sized watermark: uno::Reference<css::lang::XMultiServiceFactory> xFactory = getMultiServiceFactory(); uno::Reference<document::XFilter> xFilter( xFactory->createInstance("com.sun.star.document.PDFFilter"), uno::UNO_QUERY); @@ -216,7 +216,7 @@ CPPUNIT_TEST_FIXTURE(Test, testWatermarkFontHeight) }; xFilter->filter(aDescriptor); - // Then make sure that the watermark color is correct: + // Then make sure that the watermark font size is correct: std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument = pPDFium->openDocument(aStream.GetData(), aStream.GetSize(), OString()); CPPUNIT_ASSERT(pPdfDocument); @@ -232,6 +232,51 @@ CPPUNIT_TEST_FIXTURE(Test, testWatermarkFontHeight) // i.e. the font size was automatic, could not specify an explicit size. CPPUNIT_ASSERT_EQUAL(nExpectedFontSize, nFontSize); } + +CPPUNIT_TEST_FIXTURE(Test, testWatermarkFontName) +{ + // Given an empty Writer document: + std::shared_ptr<vcl::pdf::PDFium> pPDFium = vcl::pdf::PDFiumLibrary::get(); + if (!pPDFium) + return; + mxComponent.set(loadFromDesktop("private:factory/swriter", "com.sun.star.text.TextDocument")); + + // When exporting that as PDF with a serif watermark: + uno::Reference<css::lang::XMultiServiceFactory> xFactory = getMultiServiceFactory(); + uno::Reference<document::XFilter> xFilter( + xFactory->createInstance("com.sun.star.document.PDFFilter"), uno::UNO_QUERY); + uno::Reference<document::XExporter> xExporter(xFilter, uno::UNO_QUERY); + xExporter->setSourceDocument(mxComponent); + SvMemoryStream aStream; + uno::Reference<io::XOutputStream> xOutputStream(new utl::OStreamWrapper(aStream)); + OUString aExpectedFontName("Liberation Serif"); + uno::Sequence<beans::PropertyValue> aFilterData{ + comphelper::makePropertyValue("Watermark", OUString("X")), + comphelper::makePropertyValue("WatermarkFontName", aExpectedFontName), + }; + uno::Sequence<beans::PropertyValue> aDescriptor{ + comphelper::makePropertyValue("FilterName", OUString("writer_pdf_Export")), + comphelper::makePropertyValue("FilterData", aFilterData), + comphelper::makePropertyValue("OutputStream", xOutputStream), + }; + xFilter->filter(aDescriptor); + + // Then make sure that the watermark font name is correct: + std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument + = pPDFium->openDocument(aStream.GetData(), aStream.GetSize(), OString()); + CPPUNIT_ASSERT(pPdfDocument); + std::unique_ptr<vcl::pdf::PDFiumPage> pPage = pPdfDocument->openPage(0); + CPPUNIT_ASSERT_EQUAL(1, pPage->getObjectCount()); + std::unique_ptr<vcl::pdf::PDFiumPageObject> pPageObject = pPage->getObject(0); + CPPUNIT_ASSERT_EQUAL(1, pPageObject->getFormObjectCount()); + std::unique_ptr<vcl::pdf::PDFiumPageObject> pFormObject = pPageObject->getFormObject(0); + OUString aFontName = pFormObject->getFontName(); + // Without the accompanying fix in place, this test would have failed with: + // - Expected: Liberation Serif + // - Actual : Helvetica + // i.e. the font name was sans, could not specify an explicit name. + CPPUNIT_ASSERT_EQUAL(aExpectedFontName, aFontName); +} } CPPUNIT_PLUGIN_IMPLEMENT(); diff --git a/filter/source/pdf/pdfexport.cxx b/filter/source/pdf/pdfexport.cxx index df86ab46393d..5bb6f0baa2fe 100644 --- a/filter/source/pdf/pdfexport.cxx +++ b/filter/source/pdf/pdfexport.cxx @@ -109,6 +109,7 @@ PDFExport::PDFExport( const Reference< XComponent >& rxSrcDoc, mbIsRedactMode ( false ), maWatermarkColor ( COL_LIGHTGREEN ), + maWatermarkFontName ( "Helvetica" ), mbHideViewerToolbar ( false ), mbHideViewerMenubar ( false ), @@ -576,6 +577,14 @@ bool PDFExport::Export( const OUString& rFile, const Sequence< PropertyValue >& moWatermarkFontHeight = nFontHeight; } } + else if (rProp.Name == "WatermarkFontName") + { + OUString aFontName{}; + if (rProp.Value >>= aFontName) + { + maWatermarkFontName = aFontName; + } + } else if ( rProp.Name == "TiledWatermark" ) rProp.Value >>= msTiledWatermark; // now all the security related properties... @@ -1168,7 +1177,7 @@ void PDFExport::ImplExportPage( vcl::PDFWriter& rWriter, vcl::PDFExtOutDevData& void PDFExport::ImplWriteWatermark( vcl::PDFWriter& rWriter, const Size& rPageSize ) { - vcl::Font aFont( "Helvetica", Size( 0, moWatermarkFontHeight ? *moWatermarkFontHeight : 3*rPageSize.Height()/4 ) ); + vcl::Font aFont( maWatermarkFontName, Size( 0, moWatermarkFontHeight ? *moWatermarkFontHeight : 3*rPageSize.Height()/4 ) ); aFont.SetItalic( ITALIC_NONE ); aFont.SetWidthType( WIDTH_NORMAL ); aFont.SetWeight( WEIGHT_NORMAL ); diff --git a/filter/source/pdf/pdfexport.hxx b/filter/source/pdf/pdfexport.hxx index 22f9db1841af..40ac7d3e2f9a 100644 --- a/filter/source/pdf/pdfexport.hxx +++ b/filter/source/pdf/pdfexport.hxx @@ -75,6 +75,7 @@ private: OUString msWatermark; Color maWatermarkColor; std::optional<sal_Int32> moWatermarkFontHeight; + OUString maWatermarkFontName; OUString msTiledWatermark; // these variable are here only to have a location in filter/pdf to set the default |