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/sequence.hxx>
18 #include <comphelper/processfactory.hxx>
19 #include <tools/stream.hxx>
21 #include <com/sun/star/graphic/SvgTools.hpp>
22 #include <com/sun/star/graphic/Primitive2DTools.hpp>
23 #include <com/sun/star/graphic/XPrimitive2D.hpp>
25 #include <drawinglayer/primitive2d/baseprimitive2d.hxx>
27 #include <com/sun/star/graphic/XSvgParser.hpp>
28 #include <com/sun/star/lang/XServiceInfo.hpp>
29 #include <com/sun/star/lang/XInitialization.hpp>
30 #include <com/sun/star/xml/sax/XParser.hpp>
31 #include <com/sun/star/xml/sax/Parser.hpp>
32 #include <com/sun/star/xml/sax/InputSource.hpp>
33 #include <cppuhelper/implbase2.hxx>
34 #include <cppuhelper/supportsservice.hxx>
35 #include <comphelper/sequence.hxx>
37 #include <svgdocumenthandler.hxx>
38 #include <svgrectnode.hxx>
39 #include <svgsvgnode.hxx>
40 #include <svggnode.hxx>
42 #include <basegfx/DrawCommands.hxx>
43 #include <basegfx/matrix/b2dhommatrixtools.hxx>
49 class TestParsing
: public test::BootstrapFixture
51 void testSimpleRectangle();
53 uno::Reference
<io::XInputStream
> parseSvg(const OUString
& aSource
);
56 CPPUNIT_TEST_SUITE(TestParsing
);
57 CPPUNIT_TEST(testSimpleRectangle
);
58 CPPUNIT_TEST(testPath
);
59 CPPUNIT_TEST_SUITE_END();
62 uno::Reference
<io::XInputStream
> TestParsing::parseSvg(const OUString
& aSource
)
64 SvFileStream
aFileStream(aSource
, StreamMode::READ
);
65 std::size_t nSize
= aFileStream
.remainingSize();
66 std::unique_ptr
<sal_Int8
[]> pBuffer(new sal_Int8
[nSize
+ 1]);
67 aFileStream
.ReadBytes(pBuffer
.get(), nSize
);
70 uno::Sequence
<sal_Int8
> aData(pBuffer
.get(), nSize
+ 1);
71 uno::Reference
<io::XInputStream
> aInputStream(new comphelper::SequenceInputStream(aData
));
76 void TestParsing::testSimpleRectangle()
78 OUString aSvgFile
= "/svgio/qa/cppunit/data/VisiotorTest-Rect.svg";
79 OUString aUrl
= m_directories
.getURLFromSrc(aSvgFile
);
80 OUString aPath
= m_directories
.getPathFromSrc(aSvgFile
);
82 uno::Reference
<io::XInputStream
> xStream
= parseSvg(aUrl
);
83 CPPUNIT_ASSERT(xStream
.is());
85 uno::Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext());
86 const uno::Reference
<graphic::XSvgParser
> xSvgParser
= graphic::SvgTools::create(xContext
);
88 uno::Any aAny
= xSvgParser
->getDrawCommands(xStream
, aPath
);
89 CPPUNIT_ASSERT(aAny
.has
<sal_uInt64
>());
90 auto* pDrawRoot
= reinterpret_cast<gfx::DrawRoot
*>(aAny
.get
<sal_uInt64
>());
92 basegfx::B2DRange
aSurfaceRectangle(0, 0, 120, 120);
94 CPPUNIT_ASSERT_EQUAL(size_t(1), pDrawRoot
->maChildren
.size());
95 CPPUNIT_ASSERT_EQUAL(aSurfaceRectangle
, pDrawRoot
->maRectangle
);
97 auto* pDrawBase
= pDrawRoot
->maChildren
[0].get();
98 CPPUNIT_ASSERT_EQUAL(gfx::DrawCommandType::Rectangle
, pDrawRoot
->maChildren
[0]->getType());
99 auto* pDrawRect
= static_cast<gfx::DrawRectangle
*>(pDrawBase
);
100 CPPUNIT_ASSERT_EQUAL(basegfx::B2DRange(10, 10, 110, 110), pDrawRect
->maRectangle
);
101 CPPUNIT_ASSERT_EQUAL(3.0, pDrawRect
->mnStrokeWidth
);
102 CPPUNIT_ASSERT(bool(pDrawRect
->mpStrokeColor
));
103 CPPUNIT_ASSERT_EQUAL(sal_Int32(0xff0000), sal_Int32(Color(*pDrawRect
->mpStrokeColor
)));
104 CPPUNIT_ASSERT(bool(pDrawRect
->mpFillColor
));
105 CPPUNIT_ASSERT_EQUAL(sal_Int32(0x00cc00), sal_Int32(Color(*pDrawRect
->mpFillColor
)));
106 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.1, pDrawRect
->mnOpacity
, 1E-12);
109 void TestParsing::testPath()
111 OUString aSvgFile
= "/svgio/qa/cppunit/data/path.svg";
112 OUString aUrl
= m_directories
.getURLFromSrc(aSvgFile
);
113 OUString aPath
= m_directories
.getPathFromSrc(aSvgFile
);
115 uno::Reference
<io::XInputStream
> xStream
= parseSvg(aUrl
);
116 CPPUNIT_ASSERT(xStream
.is());
118 uno::Reference
<uno::XComponentContext
> xContext(comphelper::getProcessComponentContext());
119 const uno::Reference
<graphic::XSvgParser
> xSvgParser
= graphic::SvgTools::create(xContext
);
121 uno::Any aAny
= xSvgParser
->getDrawCommands(xStream
, aPath
);
122 CPPUNIT_ASSERT(aAny
.has
<sal_uInt64
>());
123 auto* pDrawRoot
= reinterpret_cast<gfx::DrawRoot
*>(aAny
.get
<sal_uInt64
>());
125 CPPUNIT_ASSERT_EQUAL(size_t(1), pDrawRoot
->maChildren
.size());
127 auto* pDrawBase
= pDrawRoot
->maChildren
[0].get();
128 CPPUNIT_ASSERT_EQUAL(gfx::DrawCommandType::Path
, pDrawBase
->getType());
129 auto* pDrawPath
= static_cast<gfx::DrawPath
*>(pDrawBase
);
131 CPPUNIT_ASSERT_EQUAL(OUString("m1 1h42v24h-42v-24z"),
132 basegfx::utils::exportToSvgD(pDrawPath
->maPolyPolygon
, true, true, false));
133 CPPUNIT_ASSERT_EQUAL(0.0, pDrawPath
->mnStrokeWidth
);
134 CPPUNIT_ASSERT(bool(pDrawPath
->mpStrokeColor
));
135 CPPUNIT_ASSERT_EQUAL(sal_Int32(0xffffff), sal_Int32(Color(*pDrawPath
->mpStrokeColor
)));
136 CPPUNIT_ASSERT(bool(pDrawPath
->mpFillColor
));
137 CPPUNIT_ASSERT_EQUAL(sal_Int32(0x007aff), sal_Int32(Color(*pDrawPath
->mpFillColor
)));
138 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.1, pDrawPath
->mnOpacity
, 1E-12);
141 CPPUNIT_TEST_SUITE_REGISTRATION(TestParsing
);
144 CPPUNIT_PLUGIN_IMPLEMENT();
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */