sw/inc/EnhancedPDFExportHelper.hxx              |    4 ++
 sw/qa/core/text/itrform2.cxx                    |   45 ++++++++++++++++++++++++
 sw/source/core/text/EnhancedPDFExportHelper.cxx |   13 ++++++
 sw/source/core/text/itrform2.cxx                |   22 +++++++++++
 4 files changed, 82 insertions(+), 2 deletions(-)

New commits:
commit 9880a99b86be361cc8cc526ef22c17c94c431595
Author:     Miklos Vajna <vmik...@collabora.com>
AuthorDate: Wed Jul 10 08:57:19 2024 +0200
Commit:     Caolán McNamara <caolan.mcnam...@collabora.com>
CommitDate: Thu Jul 11 12:58:56 2024 +0200

    tdf#161970 sw content controls: fix PDF export for the ExportNotesInMargin 
case
    
    Have both a content control and a comment in a document, export to PDF
    while "Comments in margin" is enabled, now the default text of the
    content control has an incorrect position / size.
    
    The transform for normal writer comment was added in commit
    eb1c0480050a1328287c4a85468afbec50b0e29d (Resolves: fdo#36815 enable
    printing WYSIWYG sidewindow comments, 2014-04-16), and e.g. the
    hyperlink rectangles are transformed correctly, but the content control
    PDF export code added in commit 82d90529dc2b3cb8359dec78852cbd910a66d275
    (sw content controls, rich text: add initial PDF export, 2022-09-12)
    missed this.
    
    Fix the problem by extracting the actual transform to a new
    SwEnhancedPDFExportHelper::MapSwRectToPDFRect() and then using it in
    both SwEnhancedPDFExportHelper::SwRectToPDFRect() (e.g. hyperlinks) and
    SwContentControlPortion::DescribePDFControl().
    
    Note that the font size of the PDF widget also needs scaling down, also
    fix here.
    
    (cherry picked from commit 597806350152b746b192a66b4789ca7c8c5377a9)
    
    Change-Id: I9cccbd569070336e1720ba2a82e91793083722f1
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/170342
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>
    Tested-by: Caolán McNamara <caolan.mcnam...@collabora.com>

diff --git a/sw/inc/EnhancedPDFExportHelper.hxx 
b/sw/inc/EnhancedPDFExportHelper.hxx
index dd20f54cbef8..450c9c2fe71a 100644
--- a/sw/inc/EnhancedPDFExportHelper.hxx
+++ b/sw/inc/EnhancedPDFExportHelper.hxx
@@ -247,6 +247,10 @@ class SwEnhancedPDFExportHelper
     //scale and position rRectangle if we're scaling due to notes in margins.
     tools::Rectangle SwRectToPDFRect(const SwPageFrame* pCurrPage,
         const tools::Rectangle& rRectangle) const;
+
+    static tools::Rectangle MapSwRectToPDFRect(const SwPageFrame* pCurrPage,
+                                               const tools::Rectangle& 
rRectangle);
+    static double GetSwRectToPDFRectScale();
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/qa/core/text/itrform2.cxx b/sw/qa/core/text/itrform2.cxx
index c9f9e3a9adcc..ec375020c72c 100644
--- a/sw/qa/core/text/itrform2.cxx
+++ b/sw/qa/core/text/itrform2.cxx
@@ -15,6 +15,8 @@
 
 #include <comphelper/propertyvalue.hxx>
 #include <editeng/colritem.hxx>
+#include <sfx2/viewfrm.hxx>
+#include <sfx2/dispatch.hxx>
 
 #include <IDocumentLayoutAccess.hxx>
 #include <rootfrm.hxx>
@@ -25,6 +27,8 @@
 #include <wrtsh.hxx>
 #include <formatcontentcontrol.hxx>
 #include <textcontentcontrol.hxx>
+#include <view.hxx>
+#include <cmdid.h>
 
 namespace
 {
@@ -314,6 +318,47 @@ CPPUNIT_TEST_FIXTURE(Test, 
testContentControlPDFDropDownText)
     // i.e. only the 3 colors were exported, the default "test" text was not.
     CPPUNIT_ASSERT_EQUAL(4, pAnnotation->getOptionCount(pPdfDocument.get()));
 }
+
+CPPUNIT_TEST_FIXTURE(Test, testContentControlPDFComments)
+{
+    std::shared_ptr<vcl::pdf::PDFium> pPDFium = vcl::pdf::PDFiumLibrary::get();
+    if (!pPDFium)
+        return;
+
+    // Given a document with both a content control and a comment:
+    createSwDoc();
+    SwWrtShell* pWrtShell = getSwDocShell()->GetWrtShell();
+    pWrtShell->InsertContentControl(SwContentControlType::RICH_TEXT);
+    pWrtShell->SttEndDoc(/*bStt=*/false);
+    SwDocShell* pDocShell = getSwDocShell();
+    SwView* pView = pDocShell->GetView();
+    pView->GetViewFrame().GetDispatcher()->Execute(FN_POSTIT, 
SfxCallMode::SYNCHRON);
+
+    // When exporting to PDF, exporting notes in master (and not as widgets):
+    uno::Sequence<beans::PropertyValue> aFilterData = {
+        comphelper::makePropertyValue(u"ExportNotes"_ustr, false),
+        comphelper::makePropertyValue(u"ExportNotesInMargin"_ustr, true),
+    };
+    saveWithParams({
+        comphelper::makePropertyValue(u"FilterName"_ustr, 
u"writer_pdf_Export"_ustr),
+        comphelper::makePropertyValue(u"FilterData"_ustr, aFilterData),
+    });
+
+    // Then make sure the only widget for the content control has a correct 
position:
+    std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument = parsePDFExport();
+    std::unique_ptr<vcl::pdf::PDFiumPage> pPage = pPdfDocument->openPage(0);
+    pPage->onAfterLoadPage(pPdfDocument.get());
+    CPPUNIT_ASSERT_EQUAL(1, pPage->getAnnotationCount());
+    std::unique_ptr<vcl::pdf::PDFiumAnnotation> pAnnotation = 
pPage->getAnnotation(0);
+    basegfx::B2DPoint aAnnotTopLeft = pAnnotation->getRectangle().getMinimum();
+    // Without the accompanying fix in place, this test would have failed with:
+    // - Expected: (41.749, 639.401)
+    // - Actual  : (59.249,716.951)
+    // i.e. the content control rectangle was shifted towards the top right of 
the page, compared to
+    // where it's expected.
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(41.749, aAnnotTopLeft.getX(), 0.001);
+    CPPUNIT_ASSERT_DOUBLES_EQUAL(639.401, aAnnotTopLeft.getY(), 0.001);
+}
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/core/text/EnhancedPDFExportHelper.cxx 
b/sw/source/core/text/EnhancedPDFExportHelper.cxx
index de88acdd0cd8..4ccb43e053b8 100644
--- a/sw/source/core/text/EnhancedPDFExportHelper.cxx
+++ b/sw/source/core/text/EnhancedPDFExportHelper.cxx
@@ -2094,11 +2094,22 @@ tools::Rectangle 
SwEnhancedPDFExportHelper::SwRectToPDFRect(const SwPageFrame* p
     {
         return rRectangle;
     }
+    return MapSwRectToPDFRect(pCurrPage, rRectangle);
+}
+
+double SwEnhancedPDFExportHelper::GetSwRectToPDFRectScale()
+{
+    return 0.75;
+}
+
+tools::Rectangle SwEnhancedPDFExportHelper::MapSwRectToPDFRect(const 
SwPageFrame* pCurrPage,
+                                                               const 
tools::Rectangle& rRectangle)
+{
     //the page has been scaled by 75% and vertically centered, so adjust these
     //rectangles equivalently
     tools::Rectangle aRect(rRectangle);
     Size aRectSize(aRect.GetSize());
-    double fScale = 0.75;
+    double fScale = GetSwRectToPDFRectScale();
     aRectSize.setWidth( aRectSize.Width() * fScale );
     aRectSize.setHeight( aRectSize.Height() * fScale );
     tools::Long nOrigHeight = pCurrPage->getFrameArea().Height();
diff --git a/sw/source/core/text/itrform2.cxx b/sw/source/core/text/itrform2.cxx
index 221a23c5534a..94a505800517 100644
--- a/sw/source/core/text/itrform2.cxx
+++ b/sw/source/core/text/itrform2.cxx
@@ -62,6 +62,7 @@
 #include <docsh.hxx>
 #include <unocrsrhelper.hxx>
 #include <textcontentcontrol.hxx>
+#include <EnhancedPDFExportHelper.hxx>
 #include <com/sun/star/rdf/Statement.hpp>
 #include <com/sun/star/rdf/URI.hpp>
 #include <com/sun/star/rdf/URIs.hpp>
@@ -1090,10 +1091,19 @@ bool SwContentControlPortion::DescribePDFControl(const 
SwTextPaintInfo& rInf) co
         return false;
     }
 
+    bool bShrinkPageForPostIts = pPDFExtOutDevData->GetIsExportNotesInMargin()
+                                 && 
sw_GetPostIts(rDoc.getIDocumentFieldsAccess(), nullptr);
     const SwFont* pFont = rInf.GetFont();
     if (pFont)
     {
         pDescriptor->TextFont = pFont->GetActualFont();
+        if (bShrinkPageForPostIts)
+        {
+            // Page area is scaled down so we have space for comments. Scale 
down the font height
+            // for the content of the widgets, too.
+            double fScale = 
SwEnhancedPDFExportHelper::GetSwRectToPDFRectScale();
+            
pDescriptor->TextFont.SetFontHeight(pDescriptor->TextFont.GetFontHeight() * 
fScale);
+        }
 
         // Need to transport the color explicitly, so it's applied to both 
already filled in and
         // future content.
@@ -1137,7 +1147,17 @@ bool SwContentControlPortion::DescribePDFControl(const 
SwTextPaintInfo& rInf) co
     aLocation.AddLeft(-20);
     aLocation.AddRight(20);
 
-    pDescriptor->Location = aLocation.SVRect();
+    tools::Rectangle aRect = aLocation.SVRect();
+    if (bShrinkPageForPostIts)
+    {
+        // Map the rectangle of the form widget, similar to how it's done for 
e.g. hyperlinks.
+        const SwPageFrame* pPageFrame = pTextFrame->FindPageFrame();
+        if (pPageFrame)
+        {
+            aRect = SwEnhancedPDFExportHelper::MapSwRectToPDFRect(pPageFrame, 
aRect);
+        }
+    }
+    pDescriptor->Location = aRect;
 
     pPDFExtOutDevData->WrapBeginStructureElement(vcl::PDFWriter::Form);
     pPDFExtOutDevData->CreateControl(*pDescriptor);

Reply via email to