summaryrefslogtreecommitdiff
path: root/sw
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2020-09-23 23:28:30 +0200
committerTomaž Vajngerl <quikee@gmail.com>2020-09-25 18:57:46 +0200
commit3de1b009bd187afa1dd49b10644c1920641e1596 (patch)
tree0f8d41b037809f0e2fd801daada1d47a4f9b6004 /sw
parent0120fecc22b36a55a2a25573a7a9632319b2b0ff (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 'sw')
-rw-r--r--sw/qa/extras/globalfilter/data/multiple_identical_graphics.odtbin0 -> 13826 bytes
-rw-r--r--sw/qa/extras/globalfilter/globalfilter.cxx83
2 files changed, 83 insertions, 0 deletions
diff --git a/sw/qa/extras/globalfilter/data/multiple_identical_graphics.odt b/sw/qa/extras/globalfilter/data/multiple_identical_graphics.odt
new file mode 100644
index 000000000000..20f40798edfe
--- /dev/null
+++ b/sw/qa/extras/globalfilter/data/multiple_identical_graphics.odt
Binary files differ
diff --git a/sw/qa/extras/globalfilter/globalfilter.cxx b/sw/qa/extras/globalfilter/globalfilter.cxx
index f0d61355ea86..fe9b58e16413 100644
--- a/sw/qa/extras/globalfilter/globalfilter.cxx
+++ b/sw/qa/extras/globalfilter/globalfilter.cxx
@@ -40,6 +40,7 @@ public:
void testLinkedGraphicRT();
void testImageWithSpecialID();
void testGraphicShape();
+ void testMultipleIdenticalGraphics();
void testCharHighlight();
void testCharHighlightODF();
void testCharHighlightBody();
@@ -62,6 +63,7 @@ public:
CPPUNIT_TEST(testLinkedGraphicRT);
CPPUNIT_TEST(testImageWithSpecialID);
CPPUNIT_TEST(testGraphicShape);
+ CPPUNIT_TEST(testMultipleIdenticalGraphics);
CPPUNIT_TEST(testCharHighlight);
CPPUNIT_TEST(testCharHighlightODF);
CPPUNIT_TEST(testMSCharBackgroundEditing);
@@ -396,6 +398,87 @@ void Test::testGraphicShape()
}
}
+namespace
+{
+
+std::vector<uno::Reference<graphic::XGraphic>>
+ lcl_getGraphics(const uno::Reference<lang::XComponent>& xComponent)
+{
+ std::vector<uno::Reference<graphic::XGraphic>> aGraphics;
+ uno::Reference<drawing::XShape> xShape;
+
+ uno::Reference<drawing::XDrawPageSupplier> xDrawPageSupplier(xComponent, uno::UNO_QUERY);
+ uno::Reference<drawing::XDrawPage> xDrawPage = xDrawPageSupplier->getDrawPage();
+ for (sal_Int32 i = 0; i < xDrawPage->getCount(); ++i)
+ {
+ uno::Reference<beans::XPropertySet> xShapeProperties(xDrawPage->getByIndex(i), uno::UNO_QUERY);
+ uno::Reference<graphic::XGraphic> xGraphic;
+ xShapeProperties->getPropertyValue("Graphic") >>= xGraphic;
+ if (xGraphic.is())
+ {
+ aGraphics.push_back(xGraphic);
+ }
+ }
+
+ return aGraphics;
+}
+
+}
+
+void Test::testMultipleIdenticalGraphics()
+{
+ // We have multiple identical graphics. When we save them we want
+ // them to be saved de-duplicated and the same should still be true
+ // after loading them again. This test check that the de-duplication
+ // works as expected.
+
+ const OUString aFilterNames[] {
+ "writer8",
+ //"Rich Text Format", // doesn't work correctly for now
+ "MS Word 97",
+ "Office Open XML Text",
+ };
+
+ for (OUString const & rFilterName : aFilterNames)
+ {
+ if (mxComponent.is())
+ mxComponent->dispose();
+
+ mxComponent = loadFromDesktop(m_directories.getURLFromSrc("/sw/qa/extras/globalfilter/data/multiple_identical_graphics.odt"), "com.sun.star.text.TextDocument");
+
+ // Export the document and import again for a check
+ utl::MediaDescriptor aMediaDescriptor;
+ aMediaDescriptor["FilterName"] <<= rFilterName;
+ utl::TempFile aTempFile;
+ aTempFile.EnableKillingFile();
+ uno::Reference<frame::XStorable> xStorable(mxComponent, uno::UNO_QUERY);
+ xStorable->storeToURL(aTempFile.GetURL(), aMediaDescriptor.getAsConstPropertyValueList());
+ mxComponent->dispose();
+
+ mxComponent = loadFromDesktop(aTempFile.GetURL(), "com.sun.star.text.TextDocument");
+
+ // Check whether graphic exported well
+ const OString sFailedMessage = OStringLiteral("Failed on filter: ") + rFilterName.toUtf8();
+ auto aGraphics = lcl_getGraphics(mxComponent);
+
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(sFailedMessage.getStr(), size_t(5), aGraphics.size());
+
+ // Get all GfxLink addresses, we expect all of them to be the same
+ // indicating we use the same graphic instance for all shapes
+ std::vector<sal_Int64> aGfxLinkAddresses;
+ for (auto const & rxGraphic : aGraphics)
+ {
+ GfxLink* pLink = Graphic(rxGraphic).GetSharedGfxLink().get();
+ aGfxLinkAddresses.emplace_back(reinterpret_cast<sal_Int64>(pLink));
+ }
+
+ // Check all addresses are the same
+ bool bResult = std::equal(aGfxLinkAddresses.begin() + 1, aGfxLinkAddresses.end(), aGfxLinkAddresses.begin());
+ const OString sGraphicNotTheSameFailedMessage = OStringLiteral("Graphics not the same for filter: '") + rFilterName.toUtf8() + OStringLiteral("'");
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(sGraphicNotTheSameFailedMessage.getStr(), true, bResult);
+ }
+}
+
void Test::testCharHighlightBody()
{
// MS Word has two kind of character backgrounds called character shading and highlighting