diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2021-05-31 17:37:02 +0200 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2021-05-31 19:25:44 +0200 |
commit | e1546b790763c1004dc2e2e3581c666466d7cf9c (patch) | |
tree | 1680a35b77634676638f0ea82db554728e12af8b | |
parent | 2efe1e8977a83143696b8db49c7c26bbff91c04c (diff) |
sw XHTML / reqif export: export PNG images directly, without an OLE wrapper
Do this by default and add a ExportImagesAsOLE=true option for those who
want the old behavior.
Adapt existing tests to ask for the old behavior and add a new one that
depends on the new default.
Change-Id: If863215ce267d6accc85b5c8f7bdbd3f2b1e9187
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/116486
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
-rw-r--r-- | sw/qa/extras/htmlexport/htmlexport.cxx | 120 | ||||
-rw-r--r-- | sw/source/filter/html/htmlflywriter.cxx | 4 | ||||
-rw-r--r-- | sw/source/filter/html/wrthtml.cxx | 10 | ||||
-rw-r--r-- | sw/source/filter/html/wrthtml.hxx | 3 |
4 files changed, 105 insertions, 32 deletions
diff --git a/sw/qa/extras/htmlexport/htmlexport.cxx b/sw/qa/extras/htmlexport/htmlexport.cxx index 2dd946c03bfe..9ace986366f3 100644 --- a/sw/qa/extras/htmlexport/htmlexport.cxx +++ b/sw/qa/extras/htmlexport/htmlexport.cxx @@ -704,34 +704,55 @@ DECLARE_HTMLEXPORT_ROUNDTRIP_TEST(testReqIfOleImg, "reqif-ole-img.xhtml") CPPUNIT_ASSERT(aStream.indexOf("type=\"image/png\"") != -1); } -DECLARE_HTMLEXPORT_ROUNDTRIP_TEST(testReqIfPngImg, "reqif-png-img.xhtml") +CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testReqIfPngImg) { - uno::Reference<container::XNamed> xShape(getShape(1), uno::UNO_QUERY); - CPPUNIT_ASSERT(xShape.is()); + auto verify = [this](bool bExported) { + uno::Reference<container::XNamed> xShape(getShape(1), uno::UNO_QUERY); + CPPUNIT_ASSERT(xShape.is()); - if (!mbExported) - { - // Imported PNG image is not an object. - CPPUNIT_ASSERT_EQUAL(OUString("Image1"), xShape->getName()); - return; - } - - // All images are exported as objects in ReqIF mode. - CPPUNIT_ASSERT_EQUAL(OUString("Object1"), xShape->getName()); + if (!bExported) + { + // Imported PNG image is not an object. + CPPUNIT_ASSERT_EQUAL(OUString("Image1"), xShape->getName()); + return; + } - // This was <img>, not <object>, which is not valid in the reqif-xhtml - // subset. - SvStream* pStream = maTempFile.GetStream(StreamMode::READ); - CPPUNIT_ASSERT(pStream); - sal_uInt64 nLength = pStream->TellEnd(); - OString aStream(read_uInt8s_ToOString(*pStream, nLength)); - CPPUNIT_ASSERT(aStream.indexOf("<reqif-xhtml:object") != -1); + // All images are exported as objects in ReqIF mode. + CPPUNIT_ASSERT_EQUAL(OUString("Object1"), xShape->getName()); + + // This was <img>, not <object>, which is not valid in the reqif-xhtml + // subset. + SvStream* pStream = maTempFile.GetStream(StreamMode::READ); + CPPUNIT_ASSERT(pStream); + sal_uInt64 nLength = pStream->TellEnd(); + OString aStream(read_uInt8s_ToOString(*pStream, nLength)); + CPPUNIT_ASSERT(aStream.indexOf("<reqif-xhtml:object") != -1); + + // Make sure that both RTF and PNG versions are written. + CPPUNIT_ASSERT(aStream.indexOf("text/rtf") != -1); + // This failed when images with a query in their file:// URL failed to + // import. + CPPUNIT_ASSERT(aStream.indexOf("image/png") != -1); + }; - // Make sure that both RTF and PNG versions are written. - CPPUNIT_ASSERT(aStream.indexOf("text/rtf") != -1); - // This failed when images with a query in their file:// URL failed to - // import. - CPPUNIT_ASSERT(aStream.indexOf("image/png") != -1); + OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "reqif-png-img.xhtml"; + uno::Sequence<beans::PropertyValue> aLoadProperties = { + comphelper::makePropertyValue("FilterName", OUString("HTML (StarWriter)")), + comphelper::makePropertyValue("FilterOptions", OUString("xhtmlns=reqif-xhtml")), + }; + mxComponent = loadFromDesktop(aURL, "com.sun.star.text.TextDocument", aLoadProperties); + verify(/*bExported=*/false); + uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY); + uno::Sequence<beans::PropertyValue> aStoreProperties = { + comphelper::makePropertyValue("FilterName", OUString("HTML (StarWriter)")), + comphelper::makePropertyValue("FilterOptions", OUString("xhtmlns=reqif-xhtml")), + comphelper::makePropertyValue("ExportImagesAsOLE", true), + }; + xStorable->storeToURL(maTempFile.GetURL(), aStoreProperties); + mxComponent->dispose(); + mxComponent + = loadFromDesktop(maTempFile.GetURL(), "com.sun.star.text.TextDocument", aLoadProperties); + verify(/*bExported=*/true); } DECLARE_HTMLEXPORT_TEST(testReqIfJpgImg, "reqif-jpg-img.xhtml") @@ -911,10 +932,19 @@ DECLARE_HTMLEXPORT_TEST(testTransparentImage, "transparent-image.odt") CPPUNIT_ASSERT_MESSAGE(aMessage.toUtf8().getStr(), aSource.endsWith(".gif")); } -DECLARE_HTMLEXPORT_TEST(testTransparentImageReqIf, "transparent-image.odt") +CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testTransparentImageReqIf) { + OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "transparent-image.odt"; + mxComponent = loadFromDesktop(aURL, "com.sun.star.text.TextDocument", {}); + uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY); + uno::Sequence<beans::PropertyValue> aStoreProperties = { + comphelper::makePropertyValue("FilterName", OUString("HTML (StarWriter)")), + comphelper::makePropertyValue("FilterOptions", OUString("xhtmlns=reqif-xhtml")), + comphelper::makePropertyValue("ExportImagesAsOLE", true), + }; + xStorable->storeToURL(maTempFile.GetURL(), aStoreProperties); SvMemoryStream aStream; - wrapFragment(maTempFile, aStream); + HtmlExportTest::wrapFragment(maTempFile, aStream); xmlDocUniquePtr pDoc = parseXmlStream(&aStream); CPPUNIT_ASSERT(pDoc); @@ -1465,7 +1495,13 @@ CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testReqifImageToOle) dispatchCommand(mxComponent, ".uno:InsertGraphic", aArgs); // When exporting to XHTML: - ExportToReqif(); + uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY); + uno::Sequence<beans::PropertyValue> aStoreProperties = { + comphelper::makePropertyValue("FilterName", OUString("HTML (StarWriter)")), + comphelper::makePropertyValue("FilterOptions", OUString("xhtmlns=reqif-xhtml")), + comphelper::makePropertyValue("ExportImagesAsOLE", true), + }; + xStorable->storeToURL(maTempFile.GetURL(), aStoreProperties); // Then make sure we export that PNG as WMF in ReqIF mode: OUString aRtfUrl = GetOlePath(); @@ -1492,6 +1528,36 @@ CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testReqifImageToOle) CPPUNIT_ASSERT(aOle1Reader.m_nPresentationDataSize); } +CPPUNIT_TEST_FIXTURE(SwHtmlDomExportTest, testReqifEmbedPNGDirectly) +{ + // Given a document with an image: + loadURL("private:factory/swriter", nullptr); + OUString aImageURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "ole2.png"; + uno::Sequence<beans::PropertyValue> aArgs = { + comphelper::makePropertyValue("FileName", aImageURL), + }; + dispatchCommand(mxComponent, ".uno:InsertGraphic", aArgs); + + // When exporting to XHTML: + uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY); + uno::Sequence<beans::PropertyValue> aStoreProperties = { + comphelper::makePropertyValue("FilterName", OUString("HTML (StarWriter)")), + comphelper::makePropertyValue("FilterOptions", OUString("xhtmlns=reqif-xhtml")), + }; + xStorable->storeToURL(maTempFile.GetURL(), aStoreProperties); + + // Then make sure the PNG is embedded directly, without an RTF wrapper: + SvMemoryStream aStream; + HtmlExportTest::wrapFragment(maTempFile, aStream); + xmlDocUniquePtr pXmlDoc = parseXmlStream(&aStream); + CPPUNIT_ASSERT(pXmlDoc); + // Without the accompanying fix in place, this test would have failed with: + // - Expected: image/png + // - Actual : text/rtf + // i.e. even PNG was wrapped in an RTF. + assertXPath(pXmlDoc, "//reqif-xhtml:p/reqif-xhtml:object", "type", "image/png"); +} + CPPUNIT_PLUGIN_IMPLEMENT(); /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sw/source/filter/html/htmlflywriter.cxx b/sw/source/filter/html/htmlflywriter.cxx index d2ab65efc7dc..09dfa97a5220 100644 --- a/sw/source/filter/html/htmlflywriter.cxx +++ b/sw/source/filter/html/htmlflywriter.cxx @@ -1863,7 +1863,7 @@ static Writer& OutHTML_FrameFormatGrfNode( Writer& rWrt, const SwFrameFormat& rF if (xGraphic.is() && aMimeType.isEmpty()) xGraphic->getPropertyValue("MimeType") >>= aMimeType; - if (rHTMLWrt.mbReqIF) + if (rHTMLWrt.mbReqIF && rHTMLWrt.m_bExportImagesAsOLE) { // Write the original image as an RTF fragment. OUString aFileName; @@ -1893,7 +1893,7 @@ static Writer& OutHTML_FrameFormatGrfNode( Writer& rWrt, const SwFrameFormat& rF OutHTML_Image( rWrt, rFrameFormat, aGraphicURL, aGraphic, pGrfNd->GetTitle(), pGrfNd->GetTwipSize(), nFrameFlags, "graphic", nullptr, aMimeType ); - if (rHTMLWrt.mbReqIF) + if (rHTMLWrt.mbReqIF && rHTMLWrt.m_bExportImagesAsOLE) rWrt.Strm().WriteOString(OString("</" + rHTMLWrt.GetNamespace() + OOO_STRING_SVTOOLS_HTML_object ">")); return rWrt; diff --git a/sw/source/filter/html/wrthtml.cxx b/sw/source/filter/html/wrthtml.cxx index 81456897d47f..63857cf65c73 100644 --- a/sw/source/filter/html/wrthtml.cxx +++ b/sw/source/filter/html/wrthtml.cxx @@ -196,12 +196,16 @@ void SwHTMLWriter::SetupFilterOptions(SfxMedium& rMedium) comphelper::SequenceAsHashMap aStoreMap(rMedium.GetArgs()); auto it = aStoreMap.find("RTFOLEMimeType"); - if (it == aStoreMap.end()) + if (it != aStoreMap.end()) { - return; + it->second >>= m_aRTFOLEMimeType; } - it->second >>= m_aRTFOLEMimeType; + it = aStoreMap.find("ExportImagesAsOLE"); + if (it != aStoreMap.end()) + { + it->second >>= m_bExportImagesAsOLE; + } } void SwHTMLWriter::SetupFilterOptions(const OUString& rFilterOptions) diff --git a/sw/source/filter/html/wrthtml.hxx b/sw/source/filter/html/wrthtml.hxx index 38ccf0cf8c46..3e23616f6b21 100644 --- a/sw/source/filter/html/wrthtml.hxx +++ b/sw/source/filter/html/wrthtml.hxx @@ -408,6 +408,9 @@ public: OUString m_aRTFOLEMimeType; + /// ReqIF mode: export images as OLE objects. + bool m_bExportImagesAsOLE = false; + /// Construct an instance of SwHTMLWriter and optionally give it /// the filter options directly, which can also be set via SetupFilterOptions(). explicit SwHTMLWriter( const OUString& rBaseURL, const OUString& rFilterOptions = "" ); |