diff options
author | Miklos Vajna <vmiklos@collabora.com> | 2022-01-04 16:28:03 +0100 |
---|---|---|
committer | Miklos Vajna <vmiklos@collabora.com> | 2022-01-04 19:02:03 +0100 |
commit | 77d2bbaa18e0be5347d7bd7167b245264789e0a4 (patch) | |
tree | 26f3f2f5ecb92105b79c9d2e9e3bb9ebf8a9eb79 | |
parent | 8e78d0f335240a5ca42d53ab2f1f4e0c652ade88 (diff) |
sc export chart as graphic: handle PDF
The context menu for Calc charts would call into
GraphicFilter::ExportGraphic(), which has explicit code for e.g. SVG,
but not for PDF. The graphic exporter to PDF is only meant to work with
images backed with PDF data, not with all shapes.
Fix the problem by explicitly handling PDF in
GraphicHelper::SaveShapeAsGraphic() in svx/, and invoking the normal PDF
export in that case. Continue to fall back to XGraphicExportFilter for
other formats.
This requires passing down the current document from sc/.
Change-Id: Ia5f78bffa1d26989bb0ad3ed265b922e609f076f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/127969
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
-rw-r--r-- | include/svx/graphichelper.hxx | 12 | ||||
-rw-r--r-- | sc/source/ui/drawfunc/chartsh.cxx | 9 | ||||
-rw-r--r-- | svx/CppunitTest_svx_core.mk | 48 | ||||
-rw-r--r-- | svx/Module_svx.mk | 1 | ||||
-rw-r--r-- | svx/qa/unit/core.cxx | 84 | ||||
-rw-r--r-- | svx/qa/unit/data/chart.ods | bin | 0 -> 19734 bytes | |||
-rw-r--r-- | svx/source/core/graphichelper.cxx | 80 |
7 files changed, 211 insertions, 23 deletions
diff --git a/include/svx/graphichelper.hxx b/include/svx/graphichelper.hxx index 2f723b830493..1d3c6ee58227 100644 --- a/include/svx/graphichelper.hxx +++ b/include/svx/graphichelper.hxx @@ -24,6 +24,10 @@ #include <svx/svxdllapi.h> namespace com::sun::star::drawing { class XShape; } +namespace com::sun::star::lang +{ +class XComponent; +} namespace weld { class Widget; } namespace weld { class Window; } @@ -34,7 +38,13 @@ public: static void GetPreferredExtension( OUString& rExtension, const Graphic& rGraphic ); static OUString GetImageType(const Graphic& rGraphic); static OUString ExportGraphic(weld::Window* pWin, const Graphic& rGraphic, const OUString& rGraphicName); - static void SaveShapeAsGraphic(weld::Window* pWin, const css::uno::Reference< css::drawing::XShape >& xShape); + static void + SaveShapeAsGraphicToPath(const css::uno::Reference<css::lang::XComponent>& xComponent, + const css::uno::Reference<css::drawing::XShape>& xShape, + const OUString& rMimeType, const OUString& rPath); + static void SaveShapeAsGraphic(weld::Window* pWin, + const css::uno::Reference<css::lang::XComponent>& xComponent, + const css::uno::Reference<css::drawing::XShape>& xShape); static short HasToSaveTransformedImage(weld::Widget* pWin); }; diff --git a/sc/source/ui/drawfunc/chartsh.cxx b/sc/source/ui/drawfunc/chartsh.cxx index e1fc24eecf74..ac54c435eeea 100644 --- a/sc/source/ui/drawfunc/chartsh.cxx +++ b/sc/source/ui/drawfunc/chartsh.cxx @@ -137,8 +137,15 @@ void ScChartShell::ExecuteExportAsGraphic( SfxRequest& ) if( dynamic_cast<const SdrOle2Obj*>( pObject) ) { vcl::Window* pWin = GetViewData().GetActiveWin(); + css::uno::Reference<css::lang::XComponent> xComponent; + const SfxObjectShell* pShell = GetObjectShell(); + if (pShell) + { + xComponent = pShell->GetModel(); + } Reference< drawing::XShape > xSourceDoc( pObject->getUnoShape() ); - GraphicHelper::SaveShapeAsGraphic(pWin ? pWin->GetFrameWeld() : nullptr, xSourceDoc); + GraphicHelper::SaveShapeAsGraphic(pWin ? pWin->GetFrameWeld() : nullptr, xComponent, + xSourceDoc); } } diff --git a/svx/CppunitTest_svx_core.mk b/svx/CppunitTest_svx_core.mk new file mode 100644 index 000000000000..aac47ba1db0c --- /dev/null +++ b/svx/CppunitTest_svx_core.mk @@ -0,0 +1,48 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +#************************************************************************* +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +#************************************************************************* + +$(eval $(call gb_CppunitTest_CppunitTest,svx_core)) + +$(eval $(call gb_CppunitTest_use_externals,svx_core,\ + boost_headers \ +)) + +$(eval $(call gb_CppunitTest_add_exception_objects,svx_core, \ + svx/qa/unit/core \ +)) + +$(eval $(call gb_CppunitTest_use_libraries,svx_core, \ + comphelper \ + cppu \ + sal \ + svx \ + svxcore \ + test \ + tl \ + unotest \ + utl \ + vcl \ +)) + +$(eval $(call gb_CppunitTest_use_sdk_api,svx_core)) + +$(eval $(call gb_CppunitTest_use_ure,svx_core)) +$(eval $(call gb_CppunitTest_use_vcl,svx_core)) + +$(eval $(call gb_CppunitTest_use_rdb,svx_core,services)) + +$(eval $(call gb_CppunitTest_use_custom_headers,svx_core,\ + officecfg/registry \ +)) + +$(eval $(call gb_CppunitTest_use_configuration,svx_core)) + +# vim: set noet sw=4 ts=4: diff --git a/svx/Module_svx.mk b/svx/Module_svx.mk index 3fc4e889173e..b7d573d08f35 100644 --- a/svx/Module_svx.mk +++ b/svx/Module_svx.mk @@ -40,6 +40,7 @@ $(eval $(call gb_Module_add_check_targets,svx,\ CppunitTest_svx_gallery_test \ CppunitTest_svx_removewhichrange \ CppunitTest_svx_styles \ + CppunitTest_svx_core \ )) # screenshots diff --git a/svx/qa/unit/core.cxx b/svx/qa/unit/core.cxx new file mode 100644 index 000000000000..fbacceed341f --- /dev/null +++ b/svx/qa/unit/core.cxx @@ -0,0 +1,84 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <test/bootstrapfixture.hxx> +#include <unotest/macros_test.hxx> + +#include <com/sun/star/frame/Desktop.hpp> +#include <com/sun/star/drawing/XDrawPagesSupplier.hpp> + +#include <svx/graphichelper.hxx> +#include <unotools/tempfile.hxx> +#include <vcl/filter/PDFiumLibrary.hxx> + +using namespace ::com::sun::star; + +namespace +{ +/// Tests for svx/source/core/ code. +class Test : public test::BootstrapFixture, public unotest::MacrosTest +{ +private: + uno::Reference<lang::XComponent> mxComponent; + +public: + void setUp() override; + void tearDown() override; + uno::Reference<lang::XComponent>& getComponent() { return mxComponent; } +}; + +void Test::setUp() +{ + test::BootstrapFixture::setUp(); + + mxDesktop.set(frame::Desktop::create(mxComponentContext)); +} + +void Test::tearDown() +{ + if (mxComponent.is()) + mxComponent->dispose(); + + test::BootstrapFixture::tearDown(); +} + +constexpr OUStringLiteral DATA_DIRECTORY = u"/svx/qa/unit/data/"; + +CPPUNIT_TEST_FIXTURE(Test, testChartExportToPdf) +{ + // Given a Calc document with a chart in it: + OUString aURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "chart.ods"; + getComponent() = loadFromDesktop(aURL); + uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSupplier(getComponent(), uno::UNO_QUERY); + uno::Reference<drawing::XDrawPage> xDrawPage(xDrawPagesSupplier->getDrawPages()->getByIndex(0), + uno::UNO_QUERY); + uno::Reference<drawing::XShape> xShape(xDrawPage->getByIndex(0), uno::UNO_QUERY); + + // When exporting that chart to PDF: + utl::TempFile aTempFile; + GraphicHelper::SaveShapeAsGraphicToPath(getComponent(), xShape, "application/pdf", + aTempFile.GetURL()); + + // Then make sure we get a valid, non-empty PDF: + auto pPdfium = vcl::pdf::PDFiumLibrary::get(); + SvMemoryStream aMemory; + aMemory.WriteStream(*aTempFile.GetStream(StreamMode::READ)); + std::unique_ptr<vcl::pdf::PDFiumDocument> pPdfDocument + = pPdfium->openDocument(aMemory.GetData(), aMemory.GetSize()); + // Without the accompanying fix in place, this test would have failed, because the output was + // empty (0 bytes). + CPPUNIT_ASSERT(pPdfDocument); + int nPageCount = pPdfDocument->getPageCount(); + CPPUNIT_ASSERT_GREATER(0, nPageCount); +} +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svx/qa/unit/data/chart.ods b/svx/qa/unit/data/chart.ods Binary files differnew file mode 100644 index 000000000000..d9e9bdf18c1e --- /dev/null +++ b/svx/qa/unit/data/chart.ods diff --git a/svx/source/core/graphichelper.cxx b/svx/source/core/graphichelper.cxx index a6b51e62fc5d..f98d1ac0a63d 100644 --- a/svx/source/core/graphichelper.cxx +++ b/svx/source/core/graphichelper.cxx @@ -47,9 +47,13 @@ #include <com/sun/star/beans/XPropertyAccess.hpp> #include <com/sun/star/task/ErrorCodeIOException.hpp> #include <com/sun/star/graphic/XGraphic.hpp> +#include <com/sun/star/drawing/ShapeCollection.hpp> #include <map> +#include <sfx2/objsh.hxx> +#include <unotools/streamwrap.hxx> + using namespace css::uno; using namespace css::lang; using namespace css::graphic; @@ -365,7 +369,60 @@ OUString GraphicHelper::ExportGraphic(weld::Window* pParent, const Graphic& rGra return OUString(); } -void GraphicHelper::SaveShapeAsGraphic(weld::Window* pParent, const Reference< drawing::XShape >& xShape) +void GraphicHelper::SaveShapeAsGraphicToPath( + const css::uno::Reference<css::lang::XComponent>& xComponent, + const css::uno::Reference<css::drawing::XShape>& xShape, const OUString& aExportMimeType, + const OUString& sPath) +{ + Reference<XComponentContext> xContext(::comphelper::getProcessComponentContext()); + Reference<XInputStream> xGraphStream; + + if (xGraphStream.is()) + { + Reference<XSimpleFileAccess3> xFileAccess = SimpleFileAccess::create(xContext); + xFileAccess->writeFile(sPath, xGraphStream); + } + else if (xComponent.is() && aExportMimeType == "application/pdf") + { + css::uno::Reference<css::lang::XMultiServiceFactory> xMSF(xContext->getServiceManager(), + css::uno::UNO_QUERY); + css::uno::Reference<css::document::XExporter> xExporter( + xMSF->createInstance("com.sun.star.comp.PDF.PDFFilter"), css::uno::UNO_QUERY); + xExporter->setSourceDocument(xComponent); + + css::uno::Reference<css::drawing::XShapes> xShapes + = css::drawing::ShapeCollection::create(comphelper::getProcessComponentContext()); + xShapes->add(xShape); + css::uno::Sequence<PropertyValue> aFilterData{ + comphelper::makePropertyValue("Selection", xShapes), + }; + SvFileStream aStream(sPath, StreamMode::READWRITE | StreamMode::TRUNC); + css::uno::Reference<css::io::XOutputStream> xStream(new utl::OStreamWrapper(aStream)); + css::uno::Sequence<PropertyValue> aDescriptor{ + comphelper::makePropertyValue("FilterData", aFilterData), + comphelper::makePropertyValue("OutputStream", xStream) + }; + css::uno::Reference<css::document::XFilter> xFilter(xExporter, css::uno::UNO_QUERY); + xFilter->filter(aDescriptor); + } + else + { + Reference<css::drawing::XGraphicExportFilter> xGraphicExporter + = css::drawing::GraphicExportFilter::create(xContext); + + Sequence<PropertyValue> aDescriptor{ comphelper::makePropertyValue("MediaType", + aExportMimeType), + comphelper::makePropertyValue("URL", sPath) }; + + Reference<XComponent> xSourceDocument(xShape, UNO_QUERY_THROW); + xGraphicExporter->setSourceDocument(xSourceDocument); + xGraphicExporter->filter(aDescriptor); + } +} + +void GraphicHelper::SaveShapeAsGraphic(weld::Window* pParent, + const css::uno::Reference<css::lang::XComponent>& xComponent, + const Reference<drawing::XShape>& xShape) { try { @@ -406,26 +463,7 @@ void GraphicHelper::SaveShapeAsGraphic(weld::Window* pParent, const Reference< OUString sPath( xFilePicker->getFiles().getConstArray()[0] ); OUString aExportMimeType( aMimeTypeMap[xFilePicker->getCurrentFilter()] ); - Reference< XInputStream > xGraphStream; - - if( xGraphStream.is() ) - { - Reference<XSimpleFileAccess3> xFileAccess = SimpleFileAccess::create( xContext ); - xFileAccess->writeFile( sPath, xGraphStream ); - } - else - { - Reference<css::drawing::XGraphicExportFilter> xGraphicExporter = css::drawing::GraphicExportFilter::create( xContext ); - - Sequence<PropertyValue> aDescriptor{ - comphelper::makePropertyValue("MediaType", aExportMimeType), - comphelper::makePropertyValue("URL", sPath) - }; - - Reference< XComponent > xSourceDocument( xShape, UNO_QUERY_THROW ); - xGraphicExporter->setSourceDocument( xSourceDocument ); - xGraphicExporter->filter( aDescriptor ); - } + GraphicHelper::SaveShapeAsGraphicToPath(xComponent, xShape, aExportMimeType, sPath); } } catch( Exception& ) |