sd/qa/unit/SdrPdfImportTest.cxx |    2 +-
 svx/source/svdraw/svdpdf.cxx    |   28 +++++++++++++++++-----------
 2 files changed, 18 insertions(+), 12 deletions(-)

New commits:
commit 01950b80970da85f9a10bacbe9d3a34c61efe2f9
Author:     Caolán McNamara <caolan.mcnam...@collabora.com>
AuthorDate: Wed Aug 6 09:42:30 2025 +0100
Commit:     Miklos Vajna <vmik...@collabora.com>
CommitDate: Wed Sep 17 14:15:52 2025 +0200

    match pdf text size and vertical positioning
    
    Change-Id: Icaf9630ee287b817685cc74de0fcf85522937c11
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190984
    Reviewed-by: Miklos Vajna <vmik...@collabora.com>
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com>

diff --git a/sd/qa/unit/SdrPdfImportTest.cxx b/sd/qa/unit/SdrPdfImportTest.cxx
index da4298d5fe7c..14c88f88d7fa 100644
--- a/sd/qa/unit/SdrPdfImportTest.cxx
+++ b/sd/qa/unit/SdrPdfImportTest.cxx
@@ -129,7 +129,7 @@ CPPUNIT_TEST_FIXTURE(SdrPdfImportTest, testImportSimpleText)
     CPPUNIT_ASSERT(pImportedObject);
 
     // Check the object position
-    CPPUNIT_ASSERT_EQUAL(tools::Rectangle(Point(2011, 2102), Size(2106 + 1, 
298 + 1)),
+    CPPUNIT_ASSERT_EQUAL(tools::Rectangle(Point(2011, 2018), Size(2107, 470)),
                          pImportedObject->GetLogicRect());
 
     // Object should be a text object containing one paragraph with
diff --git a/svx/source/svdraw/svdpdf.cxx b/svx/source/svdraw/svdpdf.cxx
index cd5a53ad6005..fd12fdd68084 100644
--- a/svx/source/svdraw/svdpdf.cxx
+++ b/svx/source/svdraw/svdpdf.cxx
@@ -104,6 +104,12 @@ ImpSdrPdfImport::ImpSdrPdfImport(SdrModel& rModel, 
SdrLayerID nLay, const tools:
     mpVD->SetLineColor();
     mpVD->SetFillColor();
     maOldLineColor.SetRed(mpVD->GetLineColor().GetRed() + 1);
+
+    // Get TextBounds relative to baseline
+    vcl::Font aFnt = mpVD->GetFont();
+    aFnt.SetAlignment(ALIGN_BASELINE);
+    mpVD->SetFont(aFnt);
+
     mpLineAttr = std::make_unique<SfxItemSetFixed<XATTR_LINE_FIRST, 
XATTR_LINE_LAST>>(
         rModel.GetItemPool());
     mpFillAttr = std::make_unique<SfxItemSetFixed<XATTR_FILL_FIRST, 
XATTR_FILL_LAST>>(
@@ -780,20 +786,20 @@ void ImpSdrPdfImport::InsertTextObject(const Point& rPos, 
const Size& rSize, con
 
     FontMetric aFontMetric(mpVD->GetFontMetric());
     vcl::Font aFont(mpVD->GetFont());
-    TextAlign eAlignment(aFont.GetAlignment());
+    assert(aFont.GetAlignment() == ALIGN_BASELINE);
+
+    tools::Rectangle aOurRect;
+    (void)mpVD->GetTextBoundRect(aOurRect, rStr);
 
-    // sal_Int32 nTextWidth = static_cast<sal_Int32>(mpVD->GetTextWidth(rStr) 
* mfScaleX);
-    sal_Int32 nTextHeight = static_cast<sal_Int32>(mpVD->GetTextHeight() * 
mfScaleY);
+    auto nDiff = aFontMetric.GetDescent() - aOurRect.Bottom();
+    Point aPos(rPos.X(), rPos.Y() + nDiff);
 
-    Point aPosition(basegfx::fround<tools::Long>(rPos.X() * mfScaleX + 
maOfs.X()),
-                    basegfx::fround<tools::Long>(rPos.Y() * mfScaleY + 
maOfs.Y()));
-    Size aSize(basegfx::fround<tools::Long>(rSize.Width() * mfScaleX),
-               basegfx::fround<tools::Long>(rSize.Height() * mfScaleY));
+    Size aBoundsSize(rSize.Width(), -aFontMetric.GetLineHeight());
 
-    if (eAlignment == ALIGN_BASELINE)
-        aPosition.AdjustY(basegfx::fround<tools::Long>(aFontMetric.GetAscent() 
* -mfScaleY));
-    else if (eAlignment == ALIGN_BOTTOM)
-        aPosition.AdjustY(-nTextHeight);
+    Point aPosition(basegfx::fround<tools::Long>(aPos.X() * mfScaleX + 
maOfs.X()),
+                    basegfx::fround<tools::Long>(aPos.Y() * mfScaleY + 
maOfs.Y()));
+    Size aSize(basegfx::fround<tools::Long>(aBoundsSize.Width() * mfScaleX),
+               basegfx::fround<tools::Long>(aBoundsSize.Height() * mfScaleY));
 
     tools::Rectangle aTextRect(aPosition, aSize);
     rtl::Reference<SdrRectObj> pText = new SdrRectObj(*mpModel, 
SdrObjKind::Text, aTextRect);

Reply via email to