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 <com/sun/star/xml/sax/XParser.hpp>
28 #include <com/sun/star/xml/sax/Parser.hpp>
29 #include <com/sun/star/xml/sax/InputSource.hpp>
30 #include <svgdocumenthandler.hxx>
31 #include <comphelper/diagnose_ex.hxx>
32 #include <rtl/ref.hxx>
34 #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
> 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
> context
):
76 context_(std::move(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
{ uno::Any(OUString("DoSmeplease")) };
97 xInit
->initialize(args
);
99 // connect parser and filter
100 xParser
->setDocumentHandler(xSvgDocHdl
);
102 // finally, parse the stream to a hierarchy of
103 // SVGGraphicPrimitive2D which will be embedded to the
104 // primitive sequence. Their decompositions will in the
105 // end create local low-level primitives, thus SVG will
106 // be processable from all our processors
107 xParser
->parseStream(myInputSource
);
109 catch(const uno::Exception
&)
111 TOOLS_INFO_EXCEPTION( "svg", "Parse error");
118 uno::Sequence
< uno::Reference
< ::graphic::XPrimitive2D
> > XSvgParser::getDecomposition(
119 const uno::Reference
< ::io::XInputStream
>& xSVGStream
,
120 const OUString
& aAbsolutePath
)
122 drawinglayer::primitive2d::Primitive2DContainer aRetval
;
126 // local document handler
127 rtl::Reference
<SvgDocHdl
> pSvgDocHdl
= new SvgDocHdl(aAbsolutePath
);
128 parseSvgXML(xSVGStream
, pSvgDocHdl
);
130 // decompose to primitives
131 for(std::unique_ptr
<SvgNode
> const & pCandidate
: pSvgDocHdl
->getSvgDocument().getSvgNodeVector())
133 if (Display::None
!= pCandidate
->getDisplay())
135 pCandidate
->decomposeSvgNode(aRetval
, false);
141 OSL_ENSURE(false, "Invalid stream (!)");
144 return aRetval
.toSequence();
147 uno::Any SAL_CALL
XSvgParser::getDrawCommands(
148 uno::Reference
<io::XInputStream
> const & xSvgStream
,
149 const OUString
& aAbsolutePath
)
153 if (!xSvgStream
.is())
156 rtl::Reference
<SvgDocHdl
> pSvgDocHdl
= new SvgDocHdl(aAbsolutePath
);
157 parseSvgXML(xSvgStream
, pSvgDocHdl
);
159 // decompose to primitives
160 for (std::unique_ptr
<SvgNode
> const & pCandidate
: pSvgDocHdl
->getSvgDocument().getSvgNodeVector())
162 if (Display::None
!= pCandidate
->getDisplay())
164 mpVisitor
= std::make_shared
<SvgDrawVisitor
>();
165 pCandidate
->accept(*mpVisitor
);
166 std::shared_ptr
<gfx::DrawRoot
> pDrawRoot(mpVisitor
->getDrawRoot());
167 sal_uInt64 nPointer
= reinterpret_cast<sal_uInt64
>(pDrawRoot
.get());
168 aAnyResult
<<= sal_uInt64(nPointer
);
175 OUString SAL_CALL
XSvgParser::getImplementationName()
177 return "svgio::svgreader::XSvgParser";
180 sal_Bool SAL_CALL
XSvgParser::supportsService(const OUString
& rServiceName
)
182 return cppu::supportsService(this, rServiceName
);
185 uno::Sequence
< OUString
> SAL_CALL
XSvgParser::getSupportedServiceNames()
187 return { "com.sun.star.graphic.SvgTools" };
190 } // end of namespace svgio::svgreader
192 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
193 svgio_XSvgParser_get_implementation(
194 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const&)
196 return cppu::acquire(new svgio::svgreader::XSvgParser(context
));
199 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */