[Libreoffice-commits] core.git: Branch 'feature/drawinglayercore' - 3 commits - include/vcl vcl/CppunitTest_vcl_type_serializer_test.mk vcl/inc vcl/qa vcl/source

Tomaž Vajngerl (via logerrit) logerrit at kemper.freedesktop.org
Thu Apr 30 08:50:03 UTC 2020


 include/vcl/filter/pdfdocument.hxx          |   29 -----
 vcl/CppunitTest_vcl_type_serializer_test.mk |   11 -
 vcl/inc/TypeSerializer.hxx                  |    4 
 vcl/qa/cppunit/TypeSerializerTest.cxx       |  161 +++++++++++++++++++++++++++-
 vcl/source/gdi/TypeSerializer.cxx           |  117 ++++++++++++++++++++
 5 files changed, 285 insertions(+), 37 deletions(-)

New commits:
commit d028229068c89f44c74a645fe8f0c3400c05706f
Author:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Thu Apr 30 10:48:44 2020 +0200
Commit:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
CommitDate: Thu Apr 30 10:48:44 2020 +0200

    vcl: add Graphic serialization (writing) to TypeSerializer + tests
    
    Change-Id: I3c4845550e776c4c2c891d94db71bacea27c9a37

diff --git a/vcl/inc/TypeSerializer.hxx b/vcl/inc/TypeSerializer.hxx
index befd4edd7660..136af57a399a 100644
--- a/vcl/inc/TypeSerializer.hxx
+++ b/vcl/inc/TypeSerializer.hxx
@@ -24,6 +24,7 @@
 #include <tools/GenericTypeSerializer.hxx>
 #include <vcl/gradient.hxx>
 #include <vcl/gfxlink.hxx>
+#include <vcl/graph.hxx>
 
 class VCL_DLLPUBLIC TypeSerializer : public tools::GenericTypeSerializer
 {
@@ -35,6 +36,9 @@ public:
 
     void readGfxLink(GfxLink& rGfxLink);
     void writeGfxLink(const GfxLink& rGfxLink);
+
+    void readGraphic(Graphic& rGraphic);
+    void writeGraphic(const Graphic& rGraphic);
 };
 
 #endif
diff --git a/vcl/qa/cppunit/TypeSerializerTest.cxx b/vcl/qa/cppunit/TypeSerializerTest.cxx
index e5d3a259f803..0c737a4c4f03 100644
--- a/vcl/qa/cppunit/TypeSerializerTest.cxx
+++ b/vcl/qa/cppunit/TypeSerializerTest.cxx
@@ -130,6 +130,43 @@ void TypeSerializerTest::testGraphic()
         CPPUNIT_ASSERT_EQUAL(std::string("c2bed2099ce617f1cc035701de5186f0d43e3064"),
                              toHexString(aHash));
     }
+
+    // Test TypeSerializer - Native Format 5
+    {
+        SvMemoryStream aMemoryStream;
+        aMemoryStream.SetVersion(SOFFICE_FILEFORMAT_50);
+        aMemoryStream.SetCompressMode(SvStreamCompressFlags::NATIVE);
+        {
+            TypeSerializer aSerializer(aMemoryStream);
+            aSerializer.writeGraphic(aGraphic);
+        }
+        aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+
+        CPPUNIT_ASSERT_EQUAL(sal_uInt64(290), aMemoryStream.remainingSize());
+        std::vector<unsigned char> aHash = calculateHash(aMemoryStream);
+        CPPUNIT_ASSERT_EQUAL(std::string("ee55ab6faa73b61b68bc3d5628d95f0d3c528e2a"),
+                             toHexString(aHash));
+
+        aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+        sal_uInt32 nType;
+        aMemoryStream.ReadUInt32(nType);
+        CPPUNIT_ASSERT_EQUAL(COMPAT_FORMAT('N', 'A', 'T', '5'), nType);
+    }
+
+    // Test TypeSerializer - Normal
+    {
+        SvMemoryStream aMemoryStream;
+        {
+            TypeSerializer aSerializer(aMemoryStream);
+            aSerializer.writeGraphic(aGraphic);
+        }
+        aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+
+        CPPUNIT_ASSERT_EQUAL(sal_uInt64(233), aMemoryStream.remainingSize());
+        std::vector<unsigned char> aHash = calculateHash(aMemoryStream);
+        CPPUNIT_ASSERT_EQUAL(std::string("c2bed2099ce617f1cc035701de5186f0d43e3064"),
+                             toHexString(aHash));
+    }
 }
 
 void TypeSerializerTest::testGraphic_Bitmap_NoGfxLink()
@@ -155,6 +192,26 @@ void TypeSerializerTest::testGraphic_Bitmap_NoGfxLink()
         aMemoryStream.ReadUInt16(nType);
         CPPUNIT_ASSERT_EQUAL(sal_uInt16(0x4D42), nType); // Magic written with WriteDIBBitmapEx
     }
+
+    // Test TypeSerializer
+    {
+        SvMemoryStream aMemoryStream;
+        {
+            TypeSerializer aSerializer(aMemoryStream);
+            aSerializer.writeGraphic(aGraphic);
+        }
+        aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+
+        CPPUNIT_ASSERT_EQUAL(sal_uInt64(383), aMemoryStream.remainingSize());
+        std::vector<unsigned char> aHash = calculateHash(aMemoryStream);
+        CPPUNIT_ASSERT_EQUAL(std::string("da831418499146d51bf245fadf60b9111faa76c2"),
+                             toHexString(aHash));
+
+        aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+        sal_uInt16 nType;
+        aMemoryStream.ReadUInt16(nType);
+        CPPUNIT_ASSERT_EQUAL(sal_uInt16(0x4D42), nType); // Magic written with WriteDIBBitmapEx
+    }
 }
 
 } // namespace
diff --git a/vcl/source/gdi/TypeSerializer.cxx b/vcl/source/gdi/TypeSerializer.cxx
index ad2f1400da85..8266c6ac9813 100644
--- a/vcl/source/gdi/TypeSerializer.cxx
+++ b/vcl/source/gdi/TypeSerializer.cxx
@@ -20,6 +20,8 @@
 #include <TypeSerializer.hxx>
 #include <tools/vcompat.hxx>
 #include <sal/log.hxx>
+#include <comphelper/fileformat.h>
+#include <vcl/gdimtf.hxx>
 
 TypeSerializer::TypeSerializer(SvStream& rStream)
     : GenericTypeSerializer(rStream)
@@ -148,4 +150,119 @@ void TypeSerializer::writeGfxLink(const GfxLink& rGfxLink)
     }
 }
 
+namespace
+{
+constexpr sal_uInt32 constSvgMagic((sal_uInt32('s') << 24) | (sal_uInt32('v') << 16)
+                                   | (sal_uInt32('g') << 8) | sal_uInt32('0'));
+constexpr sal_uInt32 constWmfMagic((sal_uInt32('w') << 24) | (sal_uInt32('m') << 16)
+                                   | (sal_uInt32('f') << 8) | sal_uInt32('0'));
+constexpr sal_uInt32 constEmfMagic((sal_uInt32('e') << 24) | (sal_uInt32('m') << 16)
+                                   | (sal_uInt32('f') << 8) | sal_uInt32('0'));
+constexpr sal_uInt32 constPdfMagic((sal_uInt32('s') << 24) | (sal_uInt32('v') << 16)
+                                   | (sal_uInt32('g') << 8) | sal_uInt32('0'));
+
+#define NATIVE_FORMAT_50 COMPAT_FORMAT('N', 'A', 'T', '5')
+
+} // end anonymous namespace
+
+void TypeSerializer::readGraphic(Graphic& /*rGraphic*/) {}
+
+void TypeSerializer::writeGraphic(const Graphic& rGraphic)
+{
+    Graphic aGraphic(rGraphic);
+
+    if (!aGraphic.makeAvailable())
+        return;
+
+    auto pGfxLink = aGraphic.GetSharedGfxLink();
+
+    if (mrStream.GetVersion() >= SOFFICE_FILEFORMAT_50
+        && (mrStream.GetCompressMode() & SvStreamCompressFlags::NATIVE) && pGfxLink
+        && pGfxLink->IsNative())
+    {
+        // native format
+        mrStream.WriteUInt32(NATIVE_FORMAT_50);
+
+        // write compat info, destructor writes stuff into the header
+        {
+            VersionCompat aCompat(mrStream, StreamMode::WRITE, 1);
+        }
+        pGfxLink->SetPrefMapMode(aGraphic.GetPrefMapMode());
+        pGfxLink->SetPrefSize(aGraphic.GetPrefSize());
+        writeGfxLink(*pGfxLink);
+    }
+    else
+    {
+        // own format
+        const SvStreamEndian nOldFormat = mrStream.GetEndian();
+        mrStream.SetEndian(SvStreamEndian::LITTLE);
+
+        switch (aGraphic.GetType())
+        {
+            case GraphicType::NONE:
+            case GraphicType::Default:
+                break;
+
+            case GraphicType::Bitmap:
+            {
+                auto pVectorGraphicData = aGraphic.getVectorGraphicData();
+                if (pVectorGraphicData)
+                {
+                    // stream out Vector Graphic defining data (length, byte array and evtl. path)
+                    // this is used e.g. in swapping out graphic data and in transporting it over UNO API
+                    // as sequence of bytes, but AFAIK not written anywhere to any kind of file, so it should be
+                    // no problem to extend it; only used at runtime
+                    switch (pVectorGraphicData->getVectorGraphicDataType())
+                    {
+                        case VectorGraphicDataType::Wmf:
+                        {
+                            mrStream.WriteUInt32(constWmfMagic);
+                            break;
+                        }
+                        case VectorGraphicDataType::Emf:
+                        {
+                            mrStream.WriteUInt32(constEmfMagic);
+                            break;
+                        }
+                        case VectorGraphicDataType::Svg:
+                        {
+                            mrStream.WriteUInt32(constSvgMagic);
+                            break;
+                        }
+                        case VectorGraphicDataType::Pdf:
+                        {
+                            mrStream.WriteUInt32(constPdfMagic);
+                            break;
+                        }
+                    }
+
+                    sal_uInt32 nSize = pVectorGraphicData->getVectorGraphicDataArrayLength();
+                    mrStream.WriteUInt32(nSize);
+                    mrStream.WriteBytes(
+                        pVectorGraphicData->getVectorGraphicDataArray().getConstArray(), nSize);
+                    mrStream.WriteUniOrByteString(pVectorGraphicData->getPath(),
+                                                  mrStream.GetStreamCharSet());
+                }
+                else if (aGraphic.IsAnimated())
+                {
+                    WriteAnimation(mrStream, aGraphic.GetAnimation());
+                }
+                else
+                {
+                    WriteDIBBitmapEx(aGraphic.GetBitmapEx(), mrStream);
+                }
+            }
+            break;
+
+            default:
+            {
+                if (aGraphic.IsSupportedGraphic())
+                    WriteGDIMetaFile(mrStream, rGraphic.GetGDIMetaFile());
+            }
+            break;
+        }
+        mrStream.SetEndian(nOldFormat);
+    }
+}
+
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
commit 4243b8faa366413735a2228dfc3d41ff7ac83884
Author:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Thu Apr 30 10:42:56 2020 +0200
Commit:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
CommitDate: Thu Apr 30 10:42:56 2020 +0200

    vcl: test Graphic serialization to a stream - WriteGraphic funct.
    
    Change-Id: Ic365d27e9ef1c676e47de22b8949b5679c0a2841

diff --git a/vcl/CppunitTest_vcl_type_serializer_test.mk b/vcl/CppunitTest_vcl_type_serializer_test.mk
index aa4a36e2ee32..b2d78c34a6a1 100644
--- a/vcl/CppunitTest_vcl_type_serializer_test.mk
+++ b/vcl/CppunitTest_vcl_type_serializer_test.mk
@@ -33,18 +33,9 @@ $(eval $(call gb_CppunitTest_use_libraries,vcl_type_serializer_test, \
 ))
 
 $(eval $(call gb_CppunitTest_use_sdk_api,vcl_type_serializer_test))
-
+$(eval $(call gb_CppunitTest_use_rdb,vcl_type_serializer_test,services))
 $(eval $(call gb_CppunitTest_use_ure,vcl_type_serializer_test))
 $(eval $(call gb_CppunitTest_use_vcl,vcl_type_serializer_test))
-
-$(eval $(call gb_CppunitTest_use_components,vcl_type_serializer_test,\
-    configmgr/source/configmgr \
-    i18npool/util/i18npool \
-    ucb/source/core/ucb1 \
-    ucb/source/ucp/file/ucpfile1 \
-    uui/util/uui \
-))
-
 $(eval $(call gb_CppunitTest_use_configuration,vcl_type_serializer_test))
 
 # vim: set noet sw=4 ts=4:
diff --git a/vcl/qa/cppunit/TypeSerializerTest.cxx b/vcl/qa/cppunit/TypeSerializerTest.cxx
index cf5985211076..e5d3a259f803 100644
--- a/vcl/qa/cppunit/TypeSerializerTest.cxx
+++ b/vcl/qa/cppunit/TypeSerializerTest.cxx
@@ -7,23 +7,57 @@
  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  */
 
+#include <config_features.h>
+
 #include <cppunit/TestAssert.h>
 #include <cppunit/TestFixture.h>
 #include <cppunit/extensions/HelperMacros.h>
 #include <cppunit/plugin/TestPlugIn.h>
-#include <config_features.h>
-#include <vcl/dllapi.h>
+
+#include <unotest/directories.hxx>
+#include <vcl/graph.hxx>
+#include <vcl/graphicfilter.hxx>
+#include <comphelper/hash.hxx>
+#include <tools/vcompat.hxx>
+#include <comphelper/fileformat.h>
 
 #include <TypeSerializer.hxx>
 
 namespace
 {
+constexpr char DATA_DIRECTORY[] = "/vcl/qa/cppunit/data/";
+
+std::vector<unsigned char> calculateHash(SvStream& rStream)
+{
+    rStream.Seek(STREAM_SEEK_TO_BEGIN);
+    comphelper::Hash aHashEngine(comphelper::HashType::SHA1);
+    const sal_uInt32 nSize(rStream.remainingSize());
+    std::vector<sal_uInt8> aData(nSize);
+    aHashEngine.update(aData.data(), nSize);
+    return aHashEngine.finalize();
+}
+
+std::string toHexString(const std::vector<unsigned char>& a)
+{
+    std::stringstream aStrm;
+    for (auto& i : a)
+    {
+        aStrm << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(i);
+    }
+
+    return aStrm.str();
+}
+
 class TypeSerializerTest : public CppUnit::TestFixture
 {
     void testGradient();
+    void testGraphic();
+    void testGraphic_Bitmap_NoGfxLink();
 
     CPPUNIT_TEST_SUITE(TypeSerializerTest);
     CPPUNIT_TEST(testGradient);
+    CPPUNIT_TEST(testGraphic);
+    CPPUNIT_TEST(testGraphic_Bitmap_NoGfxLink);
     CPPUNIT_TEST_SUITE_END();
 };
 
@@ -57,6 +91,72 @@ void TypeSerializerTest::testGradient()
     CPPUNIT_ASSERT_EQUAL(sal_uInt16(30), aReadGradient.GetSteps());
 }
 
+void TypeSerializerTest::testGraphic()
+{
+    test::Directories aDirectories;
+    OUString aURL = aDirectories.getURLFromSrc(DATA_DIRECTORY) + "SimpleExample.svg";
+    SvFileStream aStream(aURL, StreamMode::READ);
+    GraphicFilter& rGraphicFilter = GraphicFilter::GetGraphicFilter();
+    Graphic aGraphic = rGraphicFilter.ImportUnloadedGraphic(aStream);
+    aGraphic.makeAvailable();
+
+    // Test WriteGraphic - Native Format 5
+    {
+        SvMemoryStream aMemoryStream;
+        aMemoryStream.SetVersion(SOFFICE_FILEFORMAT_50);
+        aMemoryStream.SetCompressMode(SvStreamCompressFlags::NATIVE);
+        WriteGraphic(aMemoryStream, aGraphic);
+        aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+
+        CPPUNIT_ASSERT_EQUAL(sal_uInt64(290), aMemoryStream.remainingSize());
+        std::vector<unsigned char> aHash = calculateHash(aMemoryStream);
+        CPPUNIT_ASSERT_EQUAL(std::string("ee55ab6faa73b61b68bc3d5628d95f0d3c528e2a"),
+                             toHexString(aHash));
+
+        aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+        sal_uInt32 nType;
+        aMemoryStream.ReadUInt32(nType);
+        CPPUNIT_ASSERT_EQUAL(COMPAT_FORMAT('N', 'A', 'T', '5'), nType);
+    }
+
+    // Test WriteGraphic - Normal
+    {
+        SvMemoryStream aMemoryStream;
+        WriteGraphic(aMemoryStream, aGraphic);
+        aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+
+        CPPUNIT_ASSERT_EQUAL(sal_uInt64(233), aMemoryStream.remainingSize());
+        std::vector<unsigned char> aHash = calculateHash(aMemoryStream);
+        CPPUNIT_ASSERT_EQUAL(std::string("c2bed2099ce617f1cc035701de5186f0d43e3064"),
+                             toHexString(aHash));
+    }
+}
+
+void TypeSerializerTest::testGraphic_Bitmap_NoGfxLink()
+{
+    Bitmap aBitmap(Size(10, 10), 24);
+    aBitmap.Erase(COL_LIGHTGRAYBLUE);
+    BitmapEx aBitmapEx(aBitmap);
+    Graphic aGraphic(aBitmapEx);
+
+    // Test WriteGraphic
+    {
+        SvMemoryStream aMemoryStream;
+        WriteGraphic(aMemoryStream, aGraphic);
+        aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+
+        CPPUNIT_ASSERT_EQUAL(sal_uInt64(383), aMemoryStream.remainingSize());
+        std::vector<unsigned char> aHash = calculateHash(aMemoryStream);
+        CPPUNIT_ASSERT_EQUAL(std::string("da831418499146d51bf245fadf60b9111faa76c2"),
+                             toHexString(aHash));
+
+        aMemoryStream.Seek(STREAM_SEEK_TO_BEGIN);
+        sal_uInt16 nType;
+        aMemoryStream.ReadUInt16(nType);
+        CPPUNIT_ASSERT_EQUAL(sal_uInt16(0x4D42), nType); // Magic written with WriteDIBBitmapEx
+    }
+}
+
 } // namespace
 
 CPPUNIT_TEST_SUITE_REGISTRATION(TypeSerializerTest);
commit 328b5c82222e17c14e3063f0c282471a9617057d
Author:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
AuthorDate: Thu Apr 30 10:38:42 2020 +0200
Commit:     Tomaž Vajngerl <tomaz.vajngerl at collabora.co.uk>
CommitDate: Thu Apr 30 10:38:42 2020 +0200

    cleanup namespaces in pdfdocument.hxx
    
    Change-Id: I861a9ef81cda439a17d6d085cf59ace7860f79d3

diff --git a/include/vcl/filter/pdfdocument.hxx b/include/vcl/filter/pdfdocument.hxx
index ebcabce6e404..42130eba9d15 100644
--- a/include/vcl/filter/pdfdocument.hxx
+++ b/include/vcl/filter/pdfdocument.hxx
@@ -19,37 +19,17 @@
 
 #include <vcl/dllapi.h>
 
-namespace com
-{
-namespace sun
-{
-namespace star
-{
-namespace security
+namespace com::sun::star::security
 {
 class XCertificate;
 }
-}
-}
-}
 
-namespace com
-{
-namespace sun
-{
-namespace star
-{
-namespace uno
+namespace com::sun::star::uno
 {
 template <class interface_type> class Reference;
 }
-}
-}
-}
 
-namespace vcl
-{
-namespace filter
+namespace vcl::filter
 {
 class PDFTrailerElement;
 class PDFReferenceElement;
@@ -429,8 +409,7 @@ public:
     //@}
 };
 
-} // namespace pdfio
-} // namespace xmlsecurity
+} // namespace vcl::filter
 
 #endif // INCLUDED_VCL_FILTER_PDFDOCUMENT_HXX
 


More information about the Libreoffice-commits mailing list