diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2020-09-23 23:28:30 +0200 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2020-09-25 18:57:46 +0200 |
commit | 3de1b009bd187afa1dd49b10644c1920641e1596 (patch) | |
tree | 0f8d41b037809f0e2fd801daada1d47a4f9b6004 /oox | |
parent | 0120fecc22b36a55a2a25573a7a9632319b2b0ff (diff) |
fix Graphic duplication in import and add GraphicMapper
When importing writerfilter, we change to oox when
importing images. This transition doesn't store any
previous contexts and all instances are reset. The
problem occurs when we have identical images because
the transition erases all caches we have to determine
if an image has already been imported or not, which
causes that we import the same image multiple times
which create unnecessary copies.
This introduces the XGraphicMapper, which can be used
to store the XGraphic for a key and can be transferred
between writerfilter to oox. With this we can remember
which images were already imported and don't create
unnecessary internal copies which decreases memory.
This also includes a test which checks that the import
and export doesn't produce unnecessary copies of
identical images. The test checks that for OOXML, ODF
and MS Binary formats.
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103283
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
(cherry picked from commit d0efd878dc41e3913a2d91ff4b5c335c1d71a85c)
Change-Id: I33dc19218c565937fab77e132b3a996c51358b6e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103407
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'oox')
-rw-r--r-- | oox/source/helper/graphichelper.cxx | 52 | ||||
-rw-r--r-- | oox/source/shape/ShapeContextHandler.cxx | 6 | ||||
-rw-r--r-- | oox/source/shape/ShapeContextHandler.hxx | 2 | ||||
-rw-r--r-- | oox/source/shape/ShapeFilterBase.cxx | 5 |
4 files changed, 53 insertions, 12 deletions
diff --git a/oox/source/helper/graphichelper.cxx b/oox/source/helper/graphichelper.cxx index 8e56f58ccabb..e43f7ee655fa 100644 --- a/oox/source/helper/graphichelper.cxx +++ b/oox/source/helper/graphichelper.cxx @@ -30,6 +30,7 @@ #include <com/sun/star/graphic/GraphicProvider.hpp> #include <com/sun/star/graphic/XGraphicProvider.hpp> #include <com/sun/star/util/MeasureUnit.hpp> +#include <com/sun/star/graphic/GraphicMapper.hpp> #include <osl/diagnose.h> #include <sal/log.hxx> #include <comphelper/seqstream.hxx> @@ -320,16 +321,24 @@ void GraphicHelper::importEmbeddedGraphics(const std::vector<OUString>& rStreamN std::vector<OUString> aMissingStreamNames; std::vector< uno::Reference<io::XInputStream> > aMissingStreams; + initializeGraphicMapperIfNeeded(); + + SAL_WARN_IF(!mxGraphicMapper.is(), "oox", "GraphicHelper::importEmbeddedGraphic - graphic mapper not available"); + for (const auto& rStreamName : rStreamNames) { - if(rStreamName.isEmpty()) + + if (rStreamName.isEmpty()) { SAL_WARN("oox", "GraphicHelper::importEmbeddedGraphics - empty stream name"); continue; } - EmbeddedGraphicMap::const_iterator aIt = maEmbeddedGraphics.find(rStreamName); - if (aIt == maEmbeddedGraphics.end()) + Reference<XGraphic> xGraphic; + + xGraphic = mxGraphicMapper->findGraphic(rStreamName); + + if (!xGraphic.is()) { aMissingStreamNames.push_back(rStreamName); aMissingStreams.push_back(mxStorage->openInputStream(rStreamName)); @@ -338,11 +347,14 @@ void GraphicHelper::importEmbeddedGraphics(const std::vector<OUString>& rStreamN std::vector< uno::Reference<graphic::XGraphic> > aGraphics = importGraphics(aMissingStreams); + assert(aGraphics.size() == aMissingStreamNames.size()); for (size_t i = 0; i < aGraphics.size(); ++i) { if (aGraphics[i].is()) - maEmbeddedGraphics[aMissingStreamNames[i]] = aGraphics[i]; + { + mxGraphicMapper->putGraphic(aMissingStreamNames[i], aGraphics[i]); + } } } @@ -350,22 +362,26 @@ Reference< XGraphic > GraphicHelper::importEmbeddedGraphic( const OUString& rStr { Reference< XGraphic > xGraphic; OSL_ENSURE( !rStreamName.isEmpty(), "GraphicHelper::importEmbeddedGraphic - empty stream name" ); + if( !rStreamName.isEmpty() ) { - EmbeddedGraphicMap::const_iterator aIt = maEmbeddedGraphics.find( rStreamName ); - if( aIt == maEmbeddedGraphics.end() ) + initializeGraphicMapperIfNeeded(); + + SAL_WARN_IF(!mxGraphicMapper.is(), "oox", "GraphicHelper::importEmbeddedGraphic - graphic mapper not available"); + + xGraphic = mxGraphicMapper->findGraphic(rStreamName); + if (!xGraphic.is()) { // Lazy-loading doesn't work with TIFF or WMF at the moment. WmfExternal aHeader; if ( (rStreamName.endsWith(".tiff") || rStreamName.endsWith(".wmf") ) && !pExtHeader) pExtHeader = &aHeader; - xGraphic = importGraphic(mxStorage->openInputStream(rStreamName), pExtHeader); - if( xGraphic.is() ) - maEmbeddedGraphics[ rStreamName ] = xGraphic; + auto xStream = mxStorage->openInputStream(rStreamName); + xGraphic = importGraphic(xStream, pExtHeader); + if (xGraphic.is()) + mxGraphicMapper->putGraphic(rStreamName, xGraphic); } - else - xGraphic = aIt->second; } return xGraphic; } @@ -383,6 +399,20 @@ awt::Size GraphicHelper::getOriginalSize( const Reference< XGraphic >& xGraphic return aSizeHmm; } +void GraphicHelper::setGraphicMapper(css::uno::Reference<css::graphic::XGraphicMapper> const & rGraphicMapper) +{ + mxGraphicMapper = rGraphicMapper; +} + +void GraphicHelper::initializeGraphicMapperIfNeeded() const +{ + if (!mxGraphicMapper.is()) + { + auto* pNonConstThis = const_cast<GraphicHelper*>(this); + pNonConstThis->mxGraphicMapper = graphic::GraphicMapper::create(mxContext); + } +} + } // namespace oox /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/oox/source/shape/ShapeContextHandler.cxx b/oox/source/shape/ShapeContextHandler.cxx index 1a1aafa5cf38..82f30c84ff09 100644 --- a/oox/source/shape/ShapeContextHandler.cxx +++ b/oox/source/shape/ShapeContextHandler.cxx @@ -595,6 +595,12 @@ void SAL_CALL ShapeContextHandler::setMediaDescriptor(const uno::Sequence<beans: maMediaDescriptor = rMediaDescriptor; } +void SAL_CALL ShapeContextHandler::setGraphicMapper(css::uno::Reference<css::graphic::XGraphicMapper> const & rxGraphicMapper) +{ + auto pShapeFilterBase = static_cast<ShapeFilterBase*>(mxFilterBase.get()); + pShapeFilterBase->setGraphicMapper(rxGraphicMapper); +} + OUString ShapeContextHandler::getImplementationName() { return "com.sun.star.comp.oox.ShapeContextHandler"; diff --git a/oox/source/shape/ShapeContextHandler.hxx b/oox/source/shape/ShapeContextHandler.hxx index 2de45f533637..b1204470346e 100644 --- a/oox/source/shape/ShapeContextHandler.hxx +++ b/oox/source/shape/ShapeContextHandler.hxx @@ -122,6 +122,8 @@ public: virtual css::uno::Sequence<css::beans::PropertyValue> SAL_CALL getMediaDescriptor() override; virtual void SAL_CALL setMediaDescriptor(const css::uno::Sequence<css::beans::PropertyValue>& rMediaDescriptor) override; + void SAL_CALL setGraphicMapper(css::uno::Reference<css::graphic::XGraphicMapper> const & rGraphicMapper) override; + private: ShapeContextHandler(ShapeContextHandler const &) = delete; void operator =(ShapeContextHandler const &) = delete; diff --git a/oox/source/shape/ShapeFilterBase.cxx b/oox/source/shape/ShapeFilterBase.cxx index 562504090f76..255dd65a7b94 100644 --- a/oox/source/shape/ShapeFilterBase.cxx +++ b/oox/source/shape/ShapeFilterBase.cxx @@ -100,7 +100,10 @@ ShapeGraphicHelper::ShapeGraphicHelper( const ShapeFilterBase& rFilter ) : GraphicHelper* ShapeFilterBase::implCreateGraphicHelper() const { - return new ShapeGraphicHelper( *this ); + GraphicHelper* pGraphicHelper = new ShapeGraphicHelper(*this); + if (mxGraphicMapper.is()) + pGraphicHelper->setGraphicMapper(mxGraphicMapper); + return pGraphicHelper; } ::Color ShapeFilterBase::getSchemeColor( sal_Int32 nToken ) const |