LibreOffice Module svgio (master) 1
xsvgparser.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <com/sun/star/graphic/XSvgParser.hpp>
23#include <com/sun/star/lang/XServiceInfo.hpp>
24#include <com/sun/star/lang/XInitialization.hpp>
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>
33#include <rtl/ref.hxx>
34#include <tools/stream.hxx>
36
37#include <svgvisitor.hxx>
38#include <utility>
39
40using namespace ::com::sun::star;
41
42namespace svgio::svgreader
43{
44 namespace {
45
46 class XSvgParser : public ::cppu::WeakAggImplHelper2< graphic::XSvgParser, lang::XServiceInfo >
47 {
48 private:
49 std::shared_ptr<SvgDrawVisitor> mpVisitor;
50
51 uno::Reference< uno::XComponentContext > context_;
52 bool parseSvgXML(uno::Reference<io::XInputStream> const & xSVGStream,
53 uno::Reference<xml::sax::XDocumentHandler> const & xSvgDocHdl);
54 public:
55 explicit XSvgParser(
56 uno::Reference< uno::XComponentContext > context);
57 XSvgParser(const XSvgParser&) = delete;
58 XSvgParser& operator=(const XSvgParser&) = delete;
59
60 // XSvgParser
61 virtual uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > SAL_CALL getDecomposition(
62 const uno::Reference< ::io::XInputStream >& xSVGStream,
63 const OUString& aAbsolutePath) override;
64
65 virtual uno::Any SAL_CALL getDrawCommands(
66 uno::Reference<io::XInputStream> const & xSvgStream,
67 const OUString& aAbsolutePath) override;
68
69 // XServiceInfo
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;
73 };
74
75 }
76
77 XSvgParser::XSvgParser(
78 uno::Reference< uno::XComponentContext > context):
79 context_(std::move(context))
80 {
81 }
82
83 bool XSvgParser::parseSvgXML(uno::Reference<io::XInputStream> const & xSVGStream, uno::Reference<xml::sax::XDocumentHandler> const & xSvgDocHdl)
84 {
85 try
86 {
87 // prepare ParserInputSource
88 xml::sax::InputSource myInputSource;
89 myInputSource.aInputStream = xSVGStream;
90
91 // get parser
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);
101
102 // connect parser and filter
103 xParser->setDocumentHandler(xSvgDocHdl);
104
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);
111 }
112 catch(const uno::Exception&)
113 {
114 TOOLS_INFO_EXCEPTION( "svg", "Parse error");
115 return false;
116 }
117
118 return true;
119 }
120
121 uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > XSvgParser::getDecomposition(
122 const uno::Reference< ::io::XInputStream >& xSVGStream,
123 const OUString& aAbsolutePath )
124 {
126
127 if(xSVGStream.is())
128 {
129 // local document handler
130 rtl::Reference<SvgDocHdl> pSvgDocHdl = new SvgDocHdl(aAbsolutePath);
131 parseSvgXML(xSVGStream, pSvgDocHdl);
132
133 // decompose to primitives
134 for(std::unique_ptr<SvgNode> const & pCandidate : pSvgDocHdl->getSvgDocument().getSvgNodeVector())
135 {
136 if (Display::None != pCandidate->getDisplay())
137 {
138 pCandidate->decomposeSvgNode(aRetval, false);
139 }
140 }
141 }
142 else
143 {
144 OSL_ENSURE(false, "Invalid stream (!)");
145 }
146
147 return aRetval.toSequence();
148 }
149
150 uno::Any SAL_CALL XSvgParser::getDrawCommands(
151 uno::Reference<io::XInputStream> const & xSvgStream,
152 const OUString& aAbsolutePath)
153 {
154 uno::Any aAnyResult;
155
156 if (!xSvgStream.is())
157 return aAnyResult;
158
159 rtl::Reference<SvgDocHdl> pSvgDocHdl = new SvgDocHdl(aAbsolutePath);
160 parseSvgXML(xSvgStream, pSvgDocHdl);
161
162 // decompose to primitives
163 for (std::unique_ptr<SvgNode> const & pCandidate : pSvgDocHdl->getSvgDocument().getSvgNodeVector())
164 {
165 if (Display::None != pCandidate->getDisplay())
166 {
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);
172 }
173 }
174
175 return aAnyResult;
176 }
177
178 OUString SAL_CALL XSvgParser::getImplementationName()
179 {
180 return "svgio::svgreader::XSvgParser";
181 }
182
183 sal_Bool SAL_CALL XSvgParser::supportsService(const OUString& rServiceName)
184 {
185 return cppu::supportsService(this, rServiceName);
186 }
187
188 uno::Sequence< OUString > SAL_CALL XSvgParser::getSupportedServiceNames()
189 {
190 return { "com.sun.star.graphic.SvgTools" };
191 }
192
193} // end of namespace svgio::svgreader
194
195extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
197 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
198{
199 return cppu::acquire(new svgio::svgreader::XSvgParser(context));
200}
201
202extern "C" bool TestImportSVG(SvStream& rStream)
203{
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;
207}
208
209/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Reference< XInputStream > xStream
css::uno::Sequence< css::uno::Reference< css::graphic::XPrimitive2D > > toSequence() const
#define TOOLS_INFO_EXCEPTION(area, stream)
Reference< XComponentContext > getProcessComponentContext()
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
args
unsigned char sal_Bool
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * svgio_XSvgParser_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: xsvgparser.cxx:196
std::shared_ptr< SvgDrawVisitor > mpVisitor
Definition: xsvgparser.cxx:49
uno::Reference< uno::XComponentContext > context_
Definition: xsvgparser.cxx:51
bool TestImportSVG(SvStream &rStream)
Definition: xsvgparser.cxx:202