sd/qa/unit/SdrPdfImportTest.cxx | 2 - sd/qa/unit/export-tests.cxx | 7 ++++- svx/source/svdraw/svdpdf.cxx | 47 ++++++++++++++++++++-------------------- 3 files changed, 30 insertions(+), 26 deletions(-)
New commits: commit c91e8234917f2190f42578301555687b0e22cc5b Author: Caolán McNamara <caolan.mcnam...@collabora.com> AuthorDate: Mon Sep 15 17:05:17 2025 +0100 Commit: Miklos Vajna <vmik...@collabora.com> CommitDate: Thu Sep 18 09:37:12 2025 +0200 horizontally position the text rect to get the ink in the same place ditch auto expand of the text shape. Change-Id: I3a7cd2d6bbedc4db6640a6f1bbe6ad1ae43a4631 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190987 Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoff...@gmail.com> Reviewed-by: Miklos Vajna <vmik...@collabora.com> diff --git a/sd/qa/unit/SdrPdfImportTest.cxx b/sd/qa/unit/SdrPdfImportTest.cxx index 14c88f88d7fa..a3fde2477975 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, 2018), Size(2107, 470)), + CPPUNIT_ASSERT_EQUAL(tools::Rectangle(Point(2004, 2018), Size(2165, 470)), pImportedObject->GetLogicRect()); // Object should be a text object containing one paragraph with diff --git a/sd/qa/unit/export-tests.cxx b/sd/qa/unit/export-tests.cxx index 10e1931027dd..fd2fe86f7b5b 100644 --- a/sd/qa/unit/export-tests.cxx +++ b/sd/qa/unit/export-tests.cxx @@ -1057,9 +1057,12 @@ CPPUNIT_TEST_FIXTURE(SdExportTest, testExplodedPdfTextPos) saveAndReload(u"OpenDocument Drawing Flat XML"_ustr); xmlDocUniquePtr pXml = parseLayout(); + sal_Int32 x = getXPath(pXml, "//textarray[1]", "x").toInt32(); + // was 2028 originally + CPPUNIT_ASSERT_DOUBLES_EQUAL(2003, x, 0); sal_Int32 y = getXPath(pXml, "//textarray[1]", "y").toInt32(); - // was 3092 before - CPPUNIT_ASSERT_DOUBLES_EQUAL(3055, y, 0); + // was 3092 originally + CPPUNIT_ASSERT_DOUBLES_EQUAL(3063, y, 0); } CPPUNIT_TEST_FIXTURE(SdExportTest, testEmbeddedText) diff --git a/svx/source/svdraw/svdpdf.cxx b/svx/source/svdraw/svdpdf.cxx index f7287d729027..f343bb8ac4c2 100644 --- a/svx/source/svdraw/svdpdf.cxx +++ b/svx/source/svdraw/svdpdf.cxx @@ -781,28 +781,39 @@ void ImpSdrPdfImport::ImportText(std::unique_ptr<vcl::pdf::PDFiumPageObject> con InsertTextObject(aRect.TopLeft(), aRect.GetSize(), sText); } -void ImpSdrPdfImport::InsertTextObject(const Point& rPos, const Size& rSize, const OUString& rStr) +void ImpSdrPdfImport::InsertTextObject(const Point& rPos, const Size& /*rSize*/, + const OUString& rStr) { - // calc text box size, add 5% to make it fit safely - FontMetric aFontMetric(mpVD->GetFontMetric()); vcl::Font aFont(mpVD->GetFont()); assert(aFont.GetAlignment() == ALIGN_BASELINE); + /* Get our text bounds of this text, which is nominally relative to a 0 + left margin, so we know where the inked rect begins relative to the + start of the text area, and we can adjust the pdf rectangle by that so + the SdrRectObj text will get rendered at the same x position. + + Similarly find the relative vertical distance from the inked bounds + to the baseline and adjust the pdf rect so the SdrRectObj will render + the text at the same y position. + */ tools::Rectangle aOurRect; (void)mpVD->GetTextBoundRect(aOurRect, rStr); - auto nDiff = aFontMetric.GetDescent() - aOurRect.Bottom(); - Point aPos(rPos.X(), rPos.Y() + nDiff); + auto nXDiff = aOurRect.Left(); + auto nYDiff = aFontMetric.GetDescent() - aOurRect.Bottom(); - Size aBoundsSize(rSize.Width(), -aFontMetric.GetLineHeight()); + Point aPos(rPos.X() - nXDiff, rPos.Y() + nYDiff); + // As per ImpEditEngine::CalcParaWidth the width of the text box has to be 1 unit wider than the text + auto nTextWidth = mpVD->GetTextWidth(rStr) + 1; + Size aSize(nTextWidth, -aFontMetric.GetLineHeight()); - 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)); + Point aSdrPos(basegfx::fround<tools::Long>(aPos.X() * mfScaleX + maOfs.X()), + basegfx::fround<tools::Long>(aPos.Y() * mfScaleY + maOfs.Y())); + Size aSdrSize(basegfx::fround<tools::Long>(aSize.Width() * mfScaleX), + basegfx::fround<tools::Long>(aSize.Height() * mfScaleY)); - tools::Rectangle aTextRect(aPosition, aSize); + tools::Rectangle aTextRect(aSdrPos, aSdrSize); rtl::Reference<SdrRectObj> pText = new SdrRectObj(*mpModel, SdrObjKind::Text, aTextRect); pText->SetMergedItem(makeSdrTextUpperDistItem(0)); @@ -810,17 +821,7 @@ void ImpSdrPdfImport::InsertTextObject(const Point& rPos, const Size& rSize, con pText->SetMergedItem(makeSdrTextRightDistItem(0)); pText->SetMergedItem(makeSdrTextLeftDistItem(0)); - if (aFont.GetAverageFontWidth()) - { - pText->ClearMergedItem(SDRATTR_TEXT_AUTOGROWWIDTH); - pText->SetMergedItem(makeSdrTextAutoGrowHeightItem(false)); - // don't let the margins eat the space needed for the text - pText->SetMergedItem(SdrTextFitToSizeTypeItem(drawing::TextFitToSizeType_ALLLINES)); - } - else - { - pText->SetMergedItem(makeSdrTextAutoGrowWidthItem(true)); - } + pText->SetMergedItem(makeSdrTextAutoGrowHeightItem(false)); pText->SetLayer(mnLayer); pText->NbcSetText(rStr); @@ -836,7 +837,7 @@ void ImpSdrPdfImport::InsertTextObject(const Point& rPos, const Size& rSize, con } Degree100 nAngle = to<Degree100>(aFont.GetOrientation()); if (nAngle) - pText->SdrAttrObj::NbcRotate(aPosition, nAngle); + pText->SdrAttrObj::NbcRotate(aSdrPos, nAngle); InsertObj(pText.get(), false); }