match pdf text size and vertical positioning

Change-Id: Icaf9630ee287b817685cc74de0fcf85522937c11
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/190984
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/191772
Tested-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
This commit is contained in:
Caolán McNamara
2025-10-02 15:44:47 +02:00
parent a44927269b
commit 6bcbed0f57
2 changed files with 18 additions and 12 deletions
+1 -1
View File
@@ -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
+17 -11
View File
@@ -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<SfxItemSet>(
SfxItemSet::makeFixedSfxItemSet<XATTR_LINE_FIRST, XATTR_LINE_LAST>(rModel.GetItemPool()));
mpFillAttr = std::make_unique<SfxItemSet>(
@@ -783,20 +789,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);
// sal_Int32 nTextWidth = static_cast<sal_Int32>(mpVD->GetTextWidth(rStr) * mfScaleX);
sal_Int32 nTextHeight = static_cast<sal_Int32>(mpVD->GetTextHeight() * mfScaleY);
tools::Rectangle aOurRect;
(void)mpVD->GetTextBoundRect(aOurRect, rStr);
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));
auto nDiff = aFontMetric.GetDescent() - aOurRect.Bottom();
Point aPos(rPos.X(), rPos.Y() + nDiff);
if (eAlignment == ALIGN_BASELINE)
aPosition.AdjustY(basegfx::fround<tools::Long>(aFontMetric.GetAscent() * -mfScaleY));
else if (eAlignment == ALIGN_BOTTOM)
aPosition.AdjustY(-nTextHeight);
Size aBoundsSize(rSize.Width(), -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));
tools::Rectangle aTextRect(aPosition, aSize);
rtl::Reference<SdrRectObj> pText = new SdrRectObj(*mpModel, aTextRect, SdrObjKind::Text);