From c043f65f6675d03bb54822cfb1fdb29ffbe1e430 Mon Sep 17 00:00:00 2001 From: Andras Timar Date: Mon, 4 May 2026 09:35:00 +0200 Subject: [PATCH] tdf#167641 emfio: render PDF in EMR_COMMENT_MULTIFORMATS with Version=0 MS Office writes the EmrFormat.Version field as 0 in the EMR_COMMENT_MULTIFORMATS record carrying an embedded PDF. Per [MS-EMF] 2.3.3.4.2 EmrFormat, Version MUST be 0x00000001 only when Signature == EPS_SIGNATURE; for any other signature it MUST be ignored. The reader rejected anything but 1, so the embedded PDF was dropped on the floor. Real-world files also use an empty OutputRect (0,0,0,0); per [MS-EMF] 2.3.3.4.3 it is in logical coordinates, so fall back to the EMF's logical bounds (mrclBounds) when it is degenerate, so ResolveBitmapActions has somewhere to draw the PDF bitmap. The new fixture is the existing pdf-in-emf.pptx EMF with the two fields patched to reproduce the regression in 776 bytes. Signed-off-by: Andras Timar Change-Id: I432f75a1cac11d84448db07c1a1815cd0be60744 Reviewed-on: https://gerrit.collaboraoffice.com/c/online/+/1904 Tested-by: Jenkins CPCI Reviewed-by: Miklos Vajna (cherry picked from commit a5d7b2935718bf7f1f6cc0352c8d8681e6713f4b) Cherry-pick note: the test fails on macOS in CI Reviewed-on: https://gerrit.libreoffice.org/c/core/+/205084 Tested-by: Jenkins Reviewed-by: Xisco Fauli --- emfio/qa/cppunit/emf/EmfImportTest.cxx | 26 ++++++++++++++++++ .../cppunit/emf/data/TestPdfInEmfVersion0.emf | Bin 0 -> 776 bytes emfio/source/reader/emfreader.cxx | 19 +++++++++---- 3 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 emfio/qa/cppunit/emf/data/TestPdfInEmfVersion0.emf diff --git a/emfio/qa/cppunit/emf/EmfImportTest.cxx b/emfio/qa/cppunit/emf/EmfImportTest.cxx index 0227fe059a31..684668b73f22 100644 --- a/emfio/qa/cppunit/emf/EmfImportTest.cxx +++ b/emfio/qa/cppunit/emf/EmfImportTest.cxx @@ -2032,6 +2032,32 @@ CPPUNIT_TEST_FIXTURE(Test, testCreatePen) assertXPath(pDocument, aXPathPrefix + "mask//pointarray[1]/point", "y", u"8918"); } +#if !defined(MACOSX) +CPPUNIT_TEST_FIXTURE(Test, testPdfInEmfVersion0) +{ + // tdf#167641 EMF with EMR_COMMENT_MULTIFORMATS carrying a PDF, where the + // EmrFormat.Version field is 0 (as written by current MS Office) and the + // OutputRect is empty (0,0,0,0). [MS-EMF] 2.3.3.4.2 says Version must be + // ignored unless Signature == EPS_SIGNATURE; an empty OutputRect must fall + // back to the EMF's logical bounds. Without the fix, the embedded PDF was + // not rendered at all. + if (!vcl::pdf::PDFiumLibrary::get()) + { + return; + } + + Primitive2DSequence aSequence + = parseEmf(u"/emfio/qa/cppunit/emf/data/TestPdfInEmfVersion0.emf"); + CPPUNIT_ASSERT_EQUAL(1, static_cast(aSequence.getLength())); + drawinglayer::Primitive2dXmlDump dumper; + xmlDocUniquePtr pDocument = dumper.dumpAndParse(Primitive2DContainer(aSequence)); + CPPUNIT_ASSERT(pDocument); + + // Without the fix the metafile was empty (no bitmap was drawn). + assertXPath(pDocument, "//bitmap"_ostr, 1); +} +#endif + CPPUNIT_TEST_FIXTURE(Test, testPdfInEmf) { if (!vcl::pdf::PDFiumLibrary::get()) diff --git a/emfio/qa/cppunit/emf/data/TestPdfInEmfVersion0.emf b/emfio/qa/cppunit/emf/data/TestPdfInEmfVersion0.emf new file mode 100644 index 0000000000000000000000000000000000000000..b025f9f6b690a8a77db7aaef221347cea9a62619 GIT binary patch literal 776 zcmZuv%Syvg5S?04xriHG6mc<|x>3_Sn%0Gg*jf=4>tm(3i1apAYN9t0T7SV`a8blx z@hAKY@dKQ>CTXw^oJ{VSa}P6_6aZjENaKTE7-_t|%>o1Wj#~f)m@5LzP;rW|Oz0=V z^xn%N03#oz%OhYszz`(dLl+QiFu>G+9B)8GRRaRj#oA((o-2exVwmNrB2YuHM2*Z4 z_L~QdX@H$XmL=9>tJ!AalMd^Y%k5@s!!qikJoe|7%hH1>~$i% za@hAeXUxQZ#=os7N!NdS(QZLZM1)m#$(itZY;q(6KfXoBWJMG!>D`M*J+_I;7r7o$ zNjw*)JY=Fv#vLfxC7~7DAdP(9dzRFO9>i0z?#nT0mF2yYmZ06ze&-3i_ccO^kVg8CE)cPll1csn6Y`E> literal 0 HcmV?d00001 diff --git a/emfio/source/reader/emfreader.cxx b/emfio/source/reader/emfreader.cxx index b1ee598f4fba..de23fd6cd4d4 100644 --- a/emfio/source/reader/emfreader.cxx +++ b/emfio/source/reader/emfreader.cxx @@ -477,6 +477,14 @@ namespace emfio { tools::Rectangle aOutputRect = EmfReader::ReadRectangle(); + // [MS-EMF] 2.3.3.4.3 EMR_COMMENT_MULTIFORMATS: OutputRect is in logical + // coordinates. MS Office sometimes writes (0,0,0,0); fall back to the + // EMF's logical bounds so the embedded PDF actually gets drawn. + if (aOutputRect.GetWidth() <= 1 && aOutputRect.GetHeight() <= 1) + { + aOutputRect = mrclBounds; + } + sal_uInt32 nCountFormats(0); mpInputStream->ReadUInt32(nCountFormats); if (nCountFormats < 1) @@ -492,12 +500,11 @@ namespace emfio return; } - sal_uInt32 nVersion(0); - mpInputStream->ReadUInt32(nVersion); - if (nVersion != 1) - { - return; - } + // [MS-EMF] 2.3.3.4.2 EmrFormat: Version MUST be 0x00000001 only when + // Signature == EPS_SIGNATURE; for any other signature it MUST be ignored. + // MS Office writes Version=0 with Signature="PDF "; older code rejected that. + // Skip the Version field without reading it, since its value is irrelevant here. + mpInputStream->SeekRel(sizeof(sal_uInt32)); sal_uInt32 nSizeData(0); mpInputStream->ReadUInt32(nSizeData);