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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <com/sun/star/graphic/XSvgParser.hpp>
23 #include <com/sun/star/lang/XServiceInfo.hpp>
24 #include <com/sun/star/lang/XInitialization.hpp>
25 #include <comphelper/processfactory.hxx>
26 #include <cppuhelper/implbase.hxx>
27 #include <cppuhelper/supportsservice.hxx>
28 #include <com/sun/star/xml/sax/XParser.hpp>
29 #include <com/sun/star/xml/sax/Parser.hpp>
30 #include <com/sun/star/xml/sax/InputSource.hpp>
31 #include <svgdocumenthandler.hxx>
32 #include <comphelper/diagnose_ex.hxx>
33 #include <rtl/ref.hxx>
34 #include <tools/stream.hxx>
35 #include <unotools/streamwrap.hxx>
37 #include <svgvisitor.hxx>
40 using namespace ::com::sun::star
;
42 namespace svgio::svgreader
46 class XSvgParser
: public ::cppu::WeakImplHelper
< graphic::XSvgParser
, lang::XServiceInfo
>
49 std::shared_ptr
<SvgDrawVisitor
> mpVisitor
;
51 uno::Reference
< uno::XComponentContext
> context_
;
52 bool parseSvgXML(uno::Reference
<io::XInputStream
> const & xSVGStream
,
53 uno::Reference
<xml::sax::XDocumentHandler
> const & xSvgDocHdl
);
56 uno::Reference
< uno::XComponentContext
> context
);
57 XSvgParser(const XSvgParser
&) = delete;
58 XSvgParser
& operator=(const XSvgParser
&) = delete;
61 virtual uno::Sequence
< uno::Reference
< ::graphic::XPrimitive2D
> > SAL_CALL
getDecomposition(
62 const uno::Reference
< ::io::XInputStream
>& xSVGStream
,
63 const OUString
& aAbsolutePath
) override
;
65 virtual uno::Any SAL_CALL
getDrawCommands(
66 uno::Reference
<io::XInputStream
> const & xSvgStream
,
67 const OUString
& aAbsolutePath
) override
;
70 virtual OUString SAL_CALL
getImplementationName() override
;
71 virtual sal_Bool SAL_CALL
supportsService(const OUString
&) override
;
72 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
77 XSvgParser::XSvgParser(
78 uno::Reference
< uno::XComponentContext
> context
):
79 context_(std::move(context
))
83 bool XSvgParser::parseSvgXML(uno::Reference
<io::XInputStream
> const & xSVGStream
, uno::Reference
<xml::sax::XDocumentHandler
> const & xSvgDocHdl
)
87 // prepare ParserInputSource
88 xml::sax::InputSource myInputSource
;
89 myInputSource
.aInputStream
= xSVGStream
;
92 uno::Reference
< xml::sax::XParser
> xParser(
93 xml::sax::Parser::create(context_
));
94 // fdo#60471 need to enable internal entities because
95 // certain ... popular proprietary products write SVG files
96 // that use entities to define XML namespaces.
97 uno::Reference
<lang::XInitialization
> const xInit(xParser
,
98 uno::UNO_QUERY_THROW
);
99 uno::Sequence
<uno::Any
> args
{ uno::Any(OUString("DoSmeplease")) };
100 xInit
->initialize(args
);
102 // connect parser and filter
103 xParser
->setDocumentHandler(xSvgDocHdl
);
105 // finally, parse the stream to a hierarchy of
106 // SVGGraphicPrimitive2D which will be embedded to the
107 // primitive sequence. Their decompositions will in the
108 // end create local low-level primitives, thus SVG will
109 // be processable from all our processors
110 xParser
->parseStream(myInputSource
);
112 catch(const uno::Exception
&)
114 TOOLS_INFO_EXCEPTION( "svg", "Parse error");
121 uno::Sequence
< uno::Reference
< ::graphic::XPrimitive2D
> > XSvgParser::getDecomposition(
122 const uno::Reference
< ::io::XInputStream
>& xSVGStream
,
123 const OUString
& aAbsolutePath
)
125 drawinglayer::primitive2d::Primitive2DContainer aRetval
;
129 // local document handler
130 rtl::Reference
<SvgDocHdl
> pSvgDocHdl
= new SvgDocHdl(aAbsolutePath
);
131 parseSvgXML(xSVGStream
, pSvgDocHdl
);
133 // decompose to primitives
134 for(std::unique_ptr
<SvgNode
> const & pCandidate
: pSvgDocHdl
->getSvgDocument().getSvgNodeVector())
136 if (Display::None
!= pCandidate
->getDisplay())
138 pCandidate
->decomposeSvgNode(aRetval
, false);
144 OSL_ENSURE(false, "Invalid stream (!)");
147 return aRetval
.toSequence();
150 uno::Any SAL_CALL
XSvgParser::getDrawCommands(
151 uno::Reference
<io::XInputStream
> const & xSvgStream
,
152 const OUString
& aAbsolutePath
)
156 if (!xSvgStream
.is())
159 rtl::Reference
<SvgDocHdl
> pSvgDocHdl
= new SvgDocHdl(aAbsolutePath
);
160 parseSvgXML(xSvgStream
, pSvgDocHdl
);
162 // decompose to primitives
163 for (std::unique_ptr
<SvgNode
> const & pCandidate
: pSvgDocHdl
->getSvgDocument().getSvgNodeVector())
165 if (Display::None
!= pCandidate
->getDisplay())
167 mpVisitor
= std::make_shared
<SvgDrawVisitor
>();
168 pCandidate
->accept(*mpVisitor
);
169 std::shared_ptr
<gfx::DrawRoot
> pDrawRoot(mpVisitor
->getDrawRoot());
170 sal_uInt64 nPointer
= reinterpret_cast<sal_uInt64
>(pDrawRoot
.get());
171 aAnyResult
<<= sal_uInt64(nPointer
);
178 OUString SAL_CALL
XSvgParser::getImplementationName()
180 return "svgio::svgreader::XSvgParser";
183 sal_Bool SAL_CALL
XSvgParser::supportsService(const OUString
& rServiceName
)
185 return cppu::supportsService(this, rServiceName
);
188 uno::Sequence
< OUString
> SAL_CALL
XSvgParser::getSupportedServiceNames()
190 return { "com.sun.star.graphic.SvgTools" };
193 } // end of namespace svgio::svgreader
195 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
196 svgio_XSvgParser_get_implementation(
197 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
199 return cppu::acquire(new svgio::svgreader::XSvgParser(context
));
202 extern "C" bool TestImportSVG(SvStream
& rStream
)
204 css::uno::Reference
<css::io::XInputStream
> xStream(new utl::OInputStreamWrapper(rStream
));
205 rtl::Reference
<svgio::svgreader::XSvgParser
> xSvgParser(new svgio::svgreader::XSvgParser(comphelper::getProcessComponentContext()));
206 return xSvgParser
->getDecomposition(xStream
, OUString()).getLength() != 0;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */