tdf#152153 DOCX export: fix lost images at embedded documents

Handling of image counter was incorrect if the
document contains embedded documents, overwriting
images with the other ones.

See also commit cf2dc247ff5f726238856e9b46a4926a30430e14
"DOCX export: image deduplication and clean up" and
 commit 3f6df3835fec71ea61894f9a3bbfe5e4a06a5495
"DOCX export: fix image counters for multiple documents".

Change-Id: I3ce3e370f96fa8b9feca3bc73f06ddca933215d4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143036
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
diff --git a/include/oox/export/drawingml.hxx b/include/oox/export/drawingml.hxx
index 3f74f12..bf2ed44 100644
--- a/include/oox/export/drawingml.hxx
+++ b/include/oox/export/drawingml.hxx
@@ -143,7 +143,7 @@ class OOX_DLLPUBLIC DrawingML
{

private:
    static int mnImageCounter;
    static std::stack<sal_Int32> mnImageCounter;
    static int mnWdpImageCounter;
    static std::map<OUString, OUString> maWdpCache;
    static sal_Int32 mnDrawingMLCount;
diff --git a/oox/source/export/drawingml.cxx b/oox/source/export/drawingml.cxx
index f7bf0ff..16affd2 100644
--- a/oox/source/export/drawingml.cxx
+++ b/oox/source/export/drawingml.cxx
@@ -235,7 +235,7 @@ void WriteGradientPath(const awt::Gradient& rGradient, const FSHelperPtr& pFS, c
}

// not thread safe
int DrawingML::mnImageCounter = 1;
std::stack<sal_Int32> DrawingML::mnImageCounter;
int DrawingML::mnWdpImageCounter = 1;
std::map<OUString, OUString> DrawingML::maWdpCache;
sal_Int32 DrawingML::mnDrawingMLCount = 0;
@@ -268,7 +268,6 @@ sal_Int16 DrawingML::GetScriptType(const OUString& rStr)

void DrawingML::ResetCounters()
{
    mnImageCounter = 1;
    mnWdpImageCounter = 1;
    maWdpCache.clear();
}
@@ -281,11 +280,13 @@ void DrawingML::ResetMlCounters()

void DrawingML::PushExportGraphics()
{
    mnImageCounter.push(1);
    maExportGraphics.emplace();
}

void DrawingML::PopExportGraphics()
{
    mnImageCounter.pop();
    maExportGraphics.pop();
}

@@ -1394,7 +1395,7 @@ OUString DrawingML::WriteImage( const Graphic& rGraphic , bool bRelPathToMedia )
        Reference<XOutputStream> xOutStream = mpFB->openFragmentStream(
            OUStringBuffer()
                .appendAscii(GetComponentDir())
                .append("/media/image" + OUString::number(mnImageCounter))
                .append("/media/image" + OUString::number(mnImageCounter.top()))
                .appendAscii(pExtension)
                .makeStringAndClear(),
            sMediaType);
@@ -1410,7 +1411,7 @@ OUString DrawingML::WriteImage( const Graphic& rGraphic , bool bRelPathToMedia )
        sPath = OUStringBuffer()
                    .appendAscii(sRelationCompPrefix.getStr())
                    .appendAscii(sRelPathToMedia.getStr())
                    .append(static_cast<sal_Int32>(mnImageCounter++))
                    .append(static_cast<sal_Int32>(mnImageCounter.top()++))
                    .appendAscii(pExtension)
                    .makeStringAndClear();

@@ -1489,7 +1490,7 @@ void DrawingML::WriteMediaNonVisualProperties(const css::uno::Reference<css::dra
        Reference<XOutputStream> xOutStream = mpFB->openFragmentStream(OUStringBuffer()
                                                                       .appendAscii(GetComponentDir())
                                                                       .append("/media/media" +
                                                                            OUString::number(mnImageCounter) +
                                                                            OUString::number(mnImageCounter.top()) +
                                                                            aExtension)
                                                                       .makeStringAndClear(),
                                                                       aMimeType);
@@ -1501,7 +1502,7 @@ void DrawingML::WriteMediaNonVisualProperties(const css::uno::Reference<css::dra

        // create the relation
        OUString aPath = OUStringBuffer().appendAscii(GetRelationCompPrefix())
                                         .append("media/media" + OUString::number(mnImageCounter++) + aExtension)
                                         .append("media/media" + OUString::number(mnImageCounter.top()++) + aExtension)
                                         .makeStringAndClear();
        aVideoFileRelId = mpFB->addRelation(mpFS->getOutputStream(), oox::getRelationship(eMediaType), aPath);
        aMediaRelId = mpFB->addRelation(mpFS->getOutputStream(), oox::getRelationship(Relationship::MEDIA), aPath);
diff --git a/sw/qa/extras/ooxmlexport/data/embedded_images.odt b/sw/qa/extras/ooxmlexport/data/embedded_images.odt
new file mode 100644
index 0000000..26166fa
--- /dev/null
+++ b/sw/qa/extras/ooxmlexport/data/embedded_images.odt
Binary files differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx
index 57b8e68..22c411d 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx
@@ -1010,6 +1010,26 @@ CPPUNIT_TEST_FIXTURE(Test, testTdf143726)
    assertXPath(pXmlStyles, "/w:styles/w:style[@w:styleId='ContentsHeading']/w:name", "val", "TOC Heading");
}

CPPUNIT_TEST_FIXTURE(Test, testTdf152153)
{
    loadAndReload("embedded_images.odt");

    uno::Reference<packages::zip::XZipFileAccess2> xNameAccess
        = packages::zip::ZipFileAccess::createWithURL(comphelper::getComponentContext(m_xSFactory),
                                                      maTempFile.GetURL());
    const uno::Sequence<OUString> aNames(xNameAccess->getElementNames());
    int nImageFiles = 0;
    for (const auto& rElementName : aNames)
        if (rElementName.startsWith("word/media/image"))
            nImageFiles++;

    // Without the accompanying fix in place, this test would have failed with:
    // - Expected: 4
    // - Actual  : 3
    // i.e. the once embedded picture wouldn't have been saved.
    CPPUNIT_ASSERT_EQUAL(4, nImageFiles);
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */