diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2020-09-22 07:29:34 +0200 |
---|---|---|
committer | Andras Timar <andras.timar@collabora.com> | 2020-10-06 08:24:17 +0200 |
commit | 8c75dc8cdae09aeae447862cdfc1fda948bc369d (patch) | |
tree | 92480eb2e78ef51a33fb2ac6e8c8d007cf4003e2 | |
parent | 3e94f095c4f5db0a132e94de471ba82b6cc1380f (diff) |
docx export: Use checksum as key to cache graphic, not Graphic*
The problem is when we have multiple identical images in the
document and try to export. Without this change, the images will
be duplicated because the cache is using Graphic*, for which the
instance changes all the time (is not unique). Using the checksum
will make sure that all the images with the same content will be
saved as one image into the document.
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103132
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
(cherry picked from commit 8679bf3ec608aec277fd677082aa5c38e63a65ce)
Change-Id: I980af2dba51060ce4320571aca14d21e26ed8976
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103406
Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice@gmail.com>
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
(cherry picked from commit 0120fecc22b36a55a2a25573a7a9632319b2b0ff)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103606
Reviewed-by: Andras Timar <andras.timar@collabora.com>
-rw-r--r-- | sw/source/filter/ww8/docxattributeoutput.cxx | 46 | ||||
-rw-r--r-- | sw/source/filter/ww8/docxattributeoutput.hxx | 8 |
2 files changed, 40 insertions, 14 deletions
diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx b/sw/source/filter/ww8/docxattributeoutput.cxx index 91aad561d3db..074cb2507737 100644 --- a/sw/source/filter/ww8/docxattributeoutput.cxx +++ b/sw/source/filter/ww8/docxattributeoutput.cxx @@ -4879,8 +4879,8 @@ void DocxAttributeOutput::PopRelIdCache() void DocxAttributeOutput::PushRelIdCache() { - m_aRelIdCache.push(std::map<const Graphic*, OString>()); - m_aSdrRelIdCache.push(std::map<BitmapChecksum, OUString>()); + m_aRelIdCache.emplace(); + m_aSdrRelIdCache.emplace(); } OUString DocxAttributeOutput::FindRelId(BitmapChecksum nChecksum) @@ -4899,6 +4899,29 @@ void DocxAttributeOutput::CacheRelId(BitmapChecksum nChecksum, const OUString& r m_aSdrRelIdCache.top()[nChecksum] = rRelId; } +OString DocxAttributeOutput::getExistingGraphicRelId(BitmapChecksum nChecksum) +{ + OString aResult; + + if (m_aRelIdCache.empty()) + return aResult; + + auto pIterator = m_aRelIdCache.top().find(nChecksum); + + if (pIterator != m_aRelIdCache.top().end()) + { + aResult = pIterator->second; + } + + return aResult; +} + +void DocxAttributeOutput::cacheGraphicRelId(BitmapChecksum nChecksum, OString const & rRelId) +{ + if (!m_aRelIdCache.empty()) + m_aRelIdCache.top().emplace(nChecksum, rRelId); +} + void DocxAttributeOutput::FlyFrameGraphic( const SwGrfNode* pGrfNode, const Size& rSize, const SwFlyFrameFormat* pOLEFrameFormat, SwOLENode* pOLENode, const SdrObject* pSdrObj ) { SAL_INFO("sw.ww8", "TODO DocxAttributeOutput::FlyFrameGraphic( const SwGrfNode* pGrfNode, const Size& rSize, const SwFlyFrameFormat* pOLEFrameFormat, SwOLENode* pOLENode, const SdrObject* pSdrObj ) - some stuff still missing" ); @@ -4928,25 +4951,24 @@ void DocxAttributeOutput::FlyFrameGraphic( const SwGrfNode* pGrfNode, const Size else { // inline, we also have to write the image itself - const Graphic* pGraphic = nullptr; + Graphic aGraphic; if (pGrfNode) - pGraphic = &pGrfNode->GetGrf(); + aGraphic = pGrfNode->GetGrf(); else - pGraphic = pOLENode->GetGraphic(); + aGraphic = *pOLENode->GetGraphic(); - if (!m_aRelIdCache.empty() && m_aRelIdCache.top().find(pGraphic) != m_aRelIdCache.top().end()) - // We already have a RelId for this Graphic. - aRelId = m_aRelIdCache.top()[pGraphic]; - else + BitmapChecksum aChecksum = aGraphic.GetChecksum(); + aRelId = getExistingGraphicRelId(aChecksum); + + if (aRelId.isEmpty()) { // Not in cache, then need to write it. m_rDrawingML.SetFS( m_pSerializer ); // to be sure that we write to the right stream - OUString aImageId = m_rDrawingML.WriteImage( *pGraphic ); + OUString aImageId = m_rDrawingML.WriteImage(aGraphic); aRelId = OUStringToOString( aImageId, RTL_TEXTENCODING_UTF8 ); - if (!m_aRelIdCache.empty()) - m_aRelIdCache.top()[pGraphic] = aRelId; + cacheGraphicRelId(aChecksum, aRelId); } nImageType = XML_embed; diff --git a/sw/source/filter/ww8/docxattributeoutput.hxx b/sw/source/filter/ww8/docxattributeoutput.hxx index 7ecbf3202214..2d32c29bfd6d 100644 --- a/sw/source/filter/ww8/docxattributeoutput.hxx +++ b/sw/source/filter/ww8/docxattributeoutput.hxx @@ -922,8 +922,12 @@ private: bool m_setFootnote; - /// RelId <-> Graphic* cache, so that in case of alternate content, the same graphic only gets written once. - std::stack< std::map<const Graphic*, OString> > m_aRelIdCache; + OString getExistingGraphicRelId(BitmapChecksum aChecksum); + void cacheGraphicRelId(BitmapChecksum nChecksum, OString const & rRelId); + + /// RelId <-> BitmapChecksum cache, so that in case of alternate content, the same graphic only gets written once. + std::stack< std::map<BitmapChecksum, OString> > m_aRelIdCache; + /// RelId <-> BitmapChecksum cache, similar to m_aRelIdCache, but used for non-Writer graphics, handled in oox. std::stack< std::map<BitmapChecksum, OUString> > m_aSdrRelIdCache; |