filter/source/pdf/pdfexport.cxx |   46 ++++++++++++++++++++++++++++++----------
 filter/source/pdf/pdfexport.hxx |    2 +
 2 files changed, 37 insertions(+), 11 deletions(-)

New commits:
commit c1682e413712751b21a21d48c03a0eeba427d558
Author:     Miklos Vajna <[email protected]>
AuthorDate: Wed Nov 23 09:59:05 2022 +0100
Commit:     Miklos Vajna <[email protected]>
CommitDate: Wed Dec 7 07:39:52 2022 +0000

    Related: tdf#54053 PDF export: add UNO API to customize the watermark font 
size
    
    The font height of the watermark text is currently automatic: we start
    with a value based on the page size and then decrease it till the text
    fits.
    
    The problem is that sometimes you want a smaller value, but specifying
    this was not possible.
    
    Fix the problem by adding a new "WatermarkFontHeight" PDF export filter
    option to specify the font size explicitly.
    
    Example cmdline usage:
    
    soffice --convert-to 
pdf:writer_pdf_Export:'{"Watermark":{"type":"string","value":"draft"}, 
"WatermarkFontHeight":{"type":"long","value":"100"}}' test.odt
    
    (cherry picked from commit 175e514c93b3696faa8c331c8b8f56e832ceb4c1)
    
    Conflicts:
            filter/qa/pdf.cxx
    
    Change-Id: Iceab943b7a995badfcdc6b12ecc9264e39e4a3a2
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143710
    Tested-by: Jenkins CollaboraOffice <[email protected]>
    Reviewed-by: Miklos Vajna <[email protected]>

diff --git a/filter/source/pdf/pdfexport.cxx b/filter/source/pdf/pdfexport.cxx
index f70fbba5b281..af21c7b1a9c8 100644
--- a/filter/source/pdf/pdfexport.cxx
+++ b/filter/source/pdf/pdfexport.cxx
@@ -541,6 +541,14 @@ bool PDFExport::Export( const OUString& rFile, const 
Sequence< PropertyValue >&
                         maWatermarkColor = Color(ColorTransparency, nColor);
                     }
                 }
+                else if (rProp.Name == "WatermarkFontHeight")
+                {
+                    sal_Int32 nFontHeight{};
+                    if (rProp.Value >>= nFontHeight)
+                    {
+                        moWatermarkFontHeight = nFontHeight;
+                    }
+                }
                 else if ( rProp.Name == "TiledWatermark" )
                     rProp.Value >>= msTiledWatermark;
                 // now all the security related properties...
@@ -1127,7 +1135,7 @@ void PDFExport::ImplExportPage( vcl::PDFWriter& rWriter, 
vcl::PDFExtOutDevData&
 
 void PDFExport::ImplWriteWatermark( vcl::PDFWriter& rWriter, const Size& 
rPageSize )
 {
-    vcl::Font aFont( "Helvetica", Size( 0, 3*rPageSize.Height()/4 ) );
+    vcl::Font aFont( "Helvetica", Size( 0, moWatermarkFontHeight ? 
*moWatermarkFontHeight : 3*rPageSize.Height()/4 ) );
     aFont.SetItalic( ITALIC_NONE );
     aFont.SetWidthType( WIDTH_NORMAL );
     aFont.SetWeight( WEIGHT_NORMAL );
@@ -1145,19 +1153,26 @@ void PDFExport::ImplWriteWatermark( vcl::PDFWriter& 
rWriter, const Size& rPageSi
     pDev->SetFont( aFont );
     pDev->SetMapMode( MapMode( MapUnit::MapPoint ) );
     int w = 0;
-    while( ( w = pDev->GetTextWidth( msWatermark ) ) > nTextWidth )
+    if (moWatermarkFontHeight)
     {
-        if (w == 0)
-            break;
-        tools::Long nNewHeight = aFont.GetFontHeight() * nTextWidth / w;
-        if( nNewHeight == aFont.GetFontHeight() )
+        w = pDev->GetTextWidth(msWatermark);
+    }
+    else
+    {
+        while( ( w = pDev->GetTextWidth( msWatermark ) ) > nTextWidth )
         {
-            nNewHeight--;
-            if( nNewHeight <= 0 )
+            if (w == 0)
                 break;
+            tools::Long nNewHeight = aFont.GetFontHeight() * nTextWidth / w;
+            if( nNewHeight == aFont.GetFontHeight() )
+            {
+                nNewHeight--;
+                if( nNewHeight <= 0 )
+                    break;
+            }
+            aFont.SetFontHeight( nNewHeight );
+            pDev->SetFont( aFont );
         }
-        aFont.SetFontHeight( nNewHeight );
-        pDev->SetFont( aFont );
     }
     tools::Long nTextHeight = pDev->GetTextHeight();
     // leave some maneuvering room for rounding issues, also
diff --git a/filter/source/pdf/pdfexport.hxx b/filter/source/pdf/pdfexport.hxx
index 166893d4fca2..489c3ec2e7a1 100644
--- a/filter/source/pdf/pdfexport.hxx
+++ b/filter/source/pdf/pdfexport.hxx
@@ -79,6 +79,7 @@ private:
 
     OUString            msWatermark;
     Color               maWatermarkColor;
+    std::optional<sal_Int32> moWatermarkFontHeight;
     OUString            msTiledWatermark;
 
     // these variable are here only to have a location in filter/pdf to set 
the default
commit 6804814e2145231e5bd649f655426a11001d42b9
Author:     Miklos Vajna <[email protected]>
AuthorDate: Thu Nov 17 13:11:27 2022 +0100
Commit:     Miklos Vajna <[email protected]>
CommitDate: Wed Dec 7 07:39:43 2022 +0000

    Related: tdf#54053 PDF export: add UNO API to customize the watermark color
    
    PDF export has a watermark feature, but its color is hardcoded to light
    green, which won't fit all documents.
    
    On the other hand, the input from Heiko in
    <https://bugs.documentfoundation.org/show_bug.cgi?id=54053#c12> is to
    keep the dialog clean, so adding one more UI option is not great.
    
    Fix the problem by only adding the option at an UNO API level for now,
    this relaxes the hardcoded color without cluttering the UI.
    
    Also available on the cmdline for e.g. 0xff0000 / red color:
    
    soffice --convert-to 
pdf:writer_pdf_Export:'{"Watermark":{"type":"string","value":"draft"}, 
"WatermarkColor":{"type":"long","value":"16711680"}}' test.odt
    
    (cherry picked from commit 21c4749d0205d1ba90494edc2527ff9d11f86f87)
    
    Conflicts:
            filter/qa/pdf.cxx
    
    Change-Id: I38a3650df86fd34dfc09e3a5643511baa371aa83
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143709
    Tested-by: Miklos Vajna <[email protected]>
    Reviewed-by: Miklos Vajna <[email protected]>

diff --git a/filter/source/pdf/pdfexport.cxx b/filter/source/pdf/pdfexport.cxx
index 604326b4bdbf..f70fbba5b281 100644
--- a/filter/source/pdf/pdfexport.cxx
+++ b/filter/source/pdf/pdfexport.cxx
@@ -106,6 +106,7 @@ PDFExport::PDFExport( const Reference< XComponent >& 
rxSrcDoc,
     mbRemoveTransparencies      ( false ),
 
     mbIsRedactMode              ( false ),
+    maWatermarkColor            ( COL_LIGHTGREEN ),
 
     mbHideViewerToolbar         ( false ),
     mbHideViewerMenubar         ( false ),
@@ -532,6 +533,14 @@ bool PDFExport::Export( const OUString& rFile, const 
Sequence< PropertyValue >&
                     rProp.Value >>= mbAddStream;
                 else if ( rProp.Name == "Watermark" )
                     rProp.Value >>= msWatermark;
+                else if ( rProp.Name == "WatermarkColor" )
+                {
+                    sal_Int32 nColor{};
+                    if (rProp.Value >>= nColor)
+                    {
+                        maWatermarkColor = Color(ColorTransparency, nColor);
+                    }
+                }
                 else if ( rProp.Name == "TiledWatermark" )
                     rProp.Value >>= msTiledWatermark;
                 // now all the security related properties...
@@ -1159,7 +1168,7 @@ void PDFExport::ImplWriteWatermark( vcl::PDFWriter& 
rWriter, const Size& rPageSi
     rWriter.Push();
     rWriter.SetMapMode( MapMode( MapUnit::MapPoint ) );
     rWriter.SetFont( aFont );
-    rWriter.SetTextColor( COL_LIGHTGREEN );
+    rWriter.SetTextColor(maWatermarkColor);
     Point aTextPoint;
     tools::Rectangle aTextRect;
     if( rPageSize.Width() > rPageSize.Height() )
diff --git a/filter/source/pdf/pdfexport.hxx b/filter/source/pdf/pdfexport.hxx
index 967668e7421b..166893d4fca2 100644
--- a/filter/source/pdf/pdfexport.hxx
+++ b/filter/source/pdf/pdfexport.hxx
@@ -78,6 +78,7 @@ private:
     bool                mbIsRedactMode;
 
     OUString            msWatermark;
+    Color               maWatermarkColor;
     OUString            msTiledWatermark;
 
     // these variable are here only to have a location in filter/pdf to set 
the default

Reply via email to