[Libreoffice-commits] core.git: Branch 'distro/collabora/cp-6.2' - sw/source

Tomaž Vajngerl (via logerrit) logerrit at kemper.freedesktop.org
Tue Oct 6 06:24:52 UTC 2020


 sw/source/filter/ww8/docxattributeoutput.cxx |   46 +++++++++++++++++++--------
 sw/source/filter/ww8/docxattributeoutput.hxx |    8 +++-
 2 files changed, 40 insertions(+), 14 deletions(-)

New commits:
commit 8c75dc8cdae09aeae447862cdfc1fda948bc369d
Author:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Tue Sep 22 07:29:34 2020 +0200
Commit:     Andras Timar <andras.timar at collabora.com>
CommitDate: Tue Oct 6 08:24:17 2020 +0200

    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 at gmail.com>
    (cherry picked from commit 8679bf3ec608aec277fd677082aa5c38e63a65ce)
    
    Change-Id: I980af2dba51060ce4320571aca14d21e26ed8976
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103406
    Tested-by: Jenkins CollaboraOffice <jenkinscollaboraoffice at gmail.com>
    Reviewed-by: Tomaž Vajngerl <quikee at gmail.com>
    (cherry picked from commit 0120fecc22b36a55a2a25573a7a9632319b2b0ff)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/103606
    Reviewed-by: Andras Timar <andras.timar at collabora.com>

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;
 


More information about the Libreoffice-commits mailing list