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 <cppuhelper/implbase2.hxx>
26 #include <cppuhelper/supportsservice.hxx>
27 #include <comphelper/sequence.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 <tools/diagnose_ex.h>
33 #include <rtl/ref.hxx>
35 #include <svgvisitor.hxx>
37 using namespace ::com::sun::star
;
39 namespace svgio::svgreader
43 class XSvgParser
: public ::cppu::WeakAggImplHelper2
< graphic::XSvgParser
, lang::XServiceInfo
>
46 std::shared_ptr
<SvgDrawVisitor
> mpVisitor
;
48 uno::Reference
< uno::XComponentContext
> context_
;
49 bool parseSvgXML(uno::Reference
<io::XInputStream
> const & xSVGStream
,
50 uno::Reference
<xml::sax::XDocumentHandler
> const & xSvgDocHdl
);
53 uno::Reference
< uno::XComponentContext
> const & context
);
54 XSvgParser(const XSvgParser
&) = delete;
55 XSvgParser
& operator=(const XSvgParser
&) = delete;
58 virtual uno::Sequence
< uno::Reference
< ::graphic::XPrimitive2D
> > SAL_CALL
getDecomposition(
59 const uno::Reference
< ::io::XInputStream
>& xSVGStream
,
60 const OUString
& aAbsolutePath
) override
;
62 virtual uno::Any SAL_CALL
getDrawCommands(
63 uno::Reference
<io::XInputStream
> const & xSvgStream
,
64 const OUString
& aAbsolutePath
) override
;
67 virtual OUString SAL_CALL
getImplementationName() override
;
68 virtual sal_Bool SAL_CALL
supportsService(const OUString
&) override
;
69 virtual uno::Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
74 XSvgParser::XSvgParser(
75 uno::Reference
< uno::XComponentContext
> const & context
):
80 bool XSvgParser::parseSvgXML(uno::Reference
<io::XInputStream
> const & xSVGStream
, uno::Reference
<xml::sax::XDocumentHandler
> const & xSvgDocHdl
)
84 // prepare ParserInputSource
85 xml::sax::InputSource myInputSource
;
86 myInputSource
.aInputStream
= xSVGStream
;
89 uno::Reference
< xml::sax::XParser
> xParser(
90 xml::sax::Parser::create(context_
));
91 // fdo#60471 need to enable internal entities because
92 // certain ... popular proprietary products write SVG files
93 // that use entities to define XML namespaces.
94 uno::Reference
<lang::XInitialization
> const xInit(xParser
,
95 uno::UNO_QUERY_THROW
);
96 uno::Sequence
<uno::Any
> args(1);
97 args
[0] <<= OUString("DoSmeplease");
98 xInit
->initialize(args
);
100 // connect parser and filter
101 xParser
->setDocumentHandler(xSvgDocHdl
);
103 // finally, parse the stream to a hierarchy of
104 // SVGGraphicPrimitive2D which will be embedded to the
105 // primitive sequence. Their decompositions will in the
106 // end create local low-level primitives, thus SVG will
107 // be processable from all our processors
108 xParser
->parseStream(myInputSource
);
110 catch(const uno::Exception
&)
112 TOOLS_INFO_EXCEPTION( "svg", "Parse error");
119 uno::Sequence
< uno::Reference
< ::graphic::XPrimitive2D
> > XSvgParser::getDecomposition(
120 const uno::Reference
< ::io::XInputStream
>& xSVGStream
,
121 const OUString
& aAbsolutePath
)
123 drawinglayer::primitive2d::Primitive2DContainer aRetval
;
127 // local document handler
128 rtl::Reference
<SvgDocHdl
> pSvgDocHdl
= new SvgDocHdl(aAbsolutePath
);
129 parseSvgXML(xSVGStream
, pSvgDocHdl
);
131 // decompose to primitives
132 for(std::unique_ptr
<SvgNode
> const & pCandidate
: pSvgDocHdl
->getSvgDocument().getSvgNodeVector())
134 if (Display::None
!= pCandidate
->getDisplay())
136 pCandidate
->decomposeSvgNode(aRetval
, false);
142 OSL_ENSURE(false, "Invalid stream (!)");
145 return comphelper::containerToSequence(aRetval
);
148 uno::Any SAL_CALL
XSvgParser::getDrawCommands(
149 uno::Reference
<io::XInputStream
> const & xSvgStream
,
150 const OUString
& aAbsolutePath
)
154 if (!xSvgStream
.is())
157 rtl::Reference
<SvgDocHdl
> pSvgDocHdl
= new SvgDocHdl(aAbsolutePath
);
158 parseSvgXML(xSvgStream
, pSvgDocHdl
);
160 // decompose to primitives
161 for (std::unique_ptr
<SvgNode
> const & pCandidate
: pSvgDocHdl
->getSvgDocument().getSvgNodeVector())
163 if (Display::None
!= pCandidate
->getDisplay())
165 mpVisitor
= std::make_shared
<SvgDrawVisitor
>();
166 pCandidate
->accept(*mpVisitor
);
167 std::shared_ptr
<gfx::DrawRoot
> pDrawRoot(mpVisitor
->getDrawRoot());
168 sal_uInt64 nPointer
= reinterpret_cast<sal_uInt64
>(pDrawRoot
.get());
169 aAnyResult
<<= sal_uInt64(nPointer
);
176 OUString SAL_CALL
XSvgParser::getImplementationName()
178 return "svgio::svgreader::XSvgParser";
181 sal_Bool SAL_CALL
XSvgParser::supportsService(const OUString
& rServiceName
)
183 return cppu::supportsService(this, rServiceName
);
186 uno::Sequence
< OUString
> SAL_CALL
XSvgParser::getSupportedServiceNames()
188 return { "com.sun.star.graphic.SvgTools" };
191 } // end of namespace svgio::svgreader
193 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
194 svgio_XSvgParser_get_implementation(
195 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
197 return cppu::acquire(new svgio::svgreader::XSvgParser(context
));
200 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */