1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/config.h>
12 #include <test/bootstrapfixture.hxx>
16 #include <comphelper/seqstream.hxx>
17 #include <comphelper/processfactory.hxx>
18 #include <tools/stream.hxx>
20 #include <com/sun/star/graphic/SvgTools.hpp>
21 #include <com/sun/star/graphic/XSvgParser.hpp>
23 #include <basegfx/DrawCommands.hxx>
24 #include <basegfx/polygon/b2dpolypolygontools.hxx>
30 class TestParsing
: public test::BootstrapFixture
32 void testSimpleRectangle();
34 uno::Reference
<io::XInputStream
> parseSvg(const OUString
& aSource
);
37 CPPUNIT_TEST_SUITE(TestParsing
);
38 CPPUNIT_TEST(testSimpleRectangle
);
39 CPPUNIT_TEST(testPath
);
40 CPPUNIT_TEST_SUITE_END();
43 uno::Reference
<io::XInputStream
> TestParsing::parseSvg(const OUString
& aSource
)
45 SvFileStream
aFileStream(aSource
, StreamMode::READ
);
46 std::size_t nSize
= aFileStream
.remainingSize();
47 std::unique_ptr
<sal_Int8
[]> pBuffer(new sal_Int8
[nSize
+ 1]);
48 aFileStream
.ReadBytes(pBuffer
.get(), nSize
);
51 uno::Sequence
<sal_Int8
> aData(pBuffer
.get(), nSize
+ 1);
52 uno::Reference
<io::XInputStream
> aInputStream(new comphelper::SequenceInputStream(aData
));
57 void TestParsing::testSimpleRectangle()
59 OUString aSvgFile
= "/svgio/qa/cppunit/data/VisiotorTest-Rect.svg";
60 OUString aUrl
= m_directories
.getURLFromSrc(aSvgFile
);
61 OUString aPath
= m_directories
.getPathFromSrc(aSvgFile
);
63 uno::Reference
<io::XInputStream
> xStream
= parseSvg(aUrl
);
64 CPPUNIT_ASSERT(xStream
.is());
66 uno::Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext());
67 const uno::Reference
<graphic::XSvgParser
> xSvgParser
= graphic::SvgTools::create(xContext
);
69 uno::Any aAny
= xSvgParser
->getDrawCommands(xStream
, aPath
);
70 CPPUNIT_ASSERT(aAny
.has
<sal_uInt64
>());
71 auto* pDrawRoot
= reinterpret_cast<gfx::DrawRoot
*>(aAny
.get
<sal_uInt64
>());
73 basegfx::B2DRange
aSurfaceRectangle(0, 0, 120, 120);
75 CPPUNIT_ASSERT_EQUAL(size_t(1), pDrawRoot
->maChildren
.size());
76 CPPUNIT_ASSERT_EQUAL(aSurfaceRectangle
, pDrawRoot
->maRectangle
);
78 auto* pDrawBase
= pDrawRoot
->maChildren
[0].get();
79 CPPUNIT_ASSERT_EQUAL(gfx::DrawCommandType::Rectangle
, pDrawRoot
->maChildren
[0]->getType());
80 auto* pDrawRect
= static_cast<gfx::DrawRectangle
*>(pDrawBase
);
81 CPPUNIT_ASSERT_EQUAL(basegfx::B2DRange(10, 10, 110, 110), pDrawRect
->maRectangle
);
82 CPPUNIT_ASSERT_EQUAL(3.0, pDrawRect
->mnStrokeWidth
);
83 CPPUNIT_ASSERT(bool(pDrawRect
->mpStrokeColor
));
84 CPPUNIT_ASSERT_EQUAL(COL_LIGHTRED
, Color(*pDrawRect
->mpStrokeColor
));
85 CPPUNIT_ASSERT(bool(pDrawRect
->mpFillColor
));
86 CPPUNIT_ASSERT_EQUAL(Color(0x00cc00), Color(*pDrawRect
->mpFillColor
));
87 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.1, pDrawRect
->mnOpacity
, 1E-12);
90 void TestParsing::testPath()
92 OUString aSvgFile
= "/svgio/qa/cppunit/data/path.svg";
93 OUString aUrl
= m_directories
.getURLFromSrc(aSvgFile
);
94 OUString aPath
= m_directories
.getPathFromSrc(aSvgFile
);
96 uno::Reference
<io::XInputStream
> xStream
= parseSvg(aUrl
);
97 CPPUNIT_ASSERT(xStream
.is());
99 uno::Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext());
100 const uno::Reference
<graphic::XSvgParser
> xSvgParser
= graphic::SvgTools::create(xContext
);
102 uno::Any aAny
= xSvgParser
->getDrawCommands(xStream
, aPath
);
103 CPPUNIT_ASSERT(aAny
.has
<sal_uInt64
>());
104 auto* pDrawRoot
= reinterpret_cast<gfx::DrawRoot
*>(aAny
.get
<sal_uInt64
>());
106 CPPUNIT_ASSERT_EQUAL(size_t(1), pDrawRoot
->maChildren
.size());
108 auto* pDrawBase
= pDrawRoot
->maChildren
[0].get();
109 CPPUNIT_ASSERT_EQUAL(gfx::DrawCommandType::Path
, pDrawBase
->getType());
110 auto* pDrawPath
= static_cast<gfx::DrawPath
*>(pDrawBase
);
112 CPPUNIT_ASSERT_EQUAL(OUString("m1 1h42v24h-42v-24z"),
113 basegfx::utils::exportToSvgD(pDrawPath
->maPolyPolygon
, true, true, false));
114 CPPUNIT_ASSERT_EQUAL(0.0, pDrawPath
->mnStrokeWidth
);
115 CPPUNIT_ASSERT(bool(pDrawPath
->mpStrokeColor
));
116 CPPUNIT_ASSERT_EQUAL(COL_WHITE
, Color(*pDrawPath
->mpStrokeColor
));
117 CPPUNIT_ASSERT(bool(pDrawPath
->mpFillColor
));
118 CPPUNIT_ASSERT_EQUAL(Color(0x007aff), Color(*pDrawPath
->mpFillColor
));
119 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.1, pDrawPath
->mnOpacity
, 1E-12);
122 CPPUNIT_TEST_SUITE_REGISTRATION(TestParsing
);
125 CPPUNIT_PLUGIN_IMPLEMENT();
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */