diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2019-02-24 18:08:38 +0100 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2019-03-06 16:33:06 +0100 |
commit | 7894fd2b442eff45ecf14088ebd17ee9f8678752 (patch) | |
tree | cf1525a0c7118b6c84a0abd555a36277f8d8e91b /svgio | |
parent | de5dc664fc923b9704860f51267c438cad28cbe4 (diff) |
svgio visitor, add draw commands and create the from svg
Adds a visitor for svgio for visiting svg nodes and create something
useful from them.
Basic draw commands - a tree of draw commands (with sub-pixel
precision support) just to store a simple definition for drawing.
Adds a svg draw visitor and create draw commands from the svg
structure and expose the commands through UNO API.
Change-Id: I073e891a2cffdd76d4e3b838590e3a19c998e9bf
Reviewed-on: https://gerrit.libreoffice.org/68770
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'svgio')
-rw-r--r-- | svgio/CppunitTest_svgio_read.mk | 67 | ||||
-rw-r--r-- | svgio/Library_svgio.mk | 1 | ||||
-rw-r--r-- | svgio/Module_svgio.mk | 1 | ||||
-rw-r--r-- | svgio/inc/svgnode.hxx | 12 | ||||
-rw-r--r-- | svgio/inc/svgvisitor.hxx | 41 | ||||
-rw-r--r-- | svgio/qa/cppunit/SvgRead.cxx | 102 | ||||
-rw-r--r-- | svgio/source/svgreader/svgnode.cxx | 4 | ||||
-rw-r--r-- | svgio/source/svgreader/svgvisitor.cxx | 105 | ||||
-rw-r--r-- | svgio/source/svguno/xsvgparser.cxx | 116 |
9 files changed, 413 insertions, 36 deletions
diff --git a/svgio/CppunitTest_svgio_read.mk b/svgio/CppunitTest_svgio_read.mk new file mode 100644 index 000000000000..20ebc0eee176 --- /dev/null +++ b/svgio/CppunitTest_svgio_read.mk @@ -0,0 +1,67 @@ +# -*- Mode: makefile-gmake; tab-width: 4; indent-tabs-mode: t -*- +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# + +$(eval $(call gb_CppunitTest_CppunitTest,svgio_read)) + +$(eval $(call gb_CppunitTest_add_exception_objects,svgio_read,\ + svgio/qa/cppunit/SvgRead \ +)) + +$(eval $(call gb_CppunitTest_set_include,svgio_read,\ + $$(INCLUDE) \ + -I$(SRCDIR)/svgio/inc \ +)) + +$(eval $(call gb_CppunitTest_use_externals,svgio_read,\ + boost_headers \ + libxml2 \ +)) + +$(eval $(call gb_CppunitTest_use_library_objects,svgio_read,\ + svgio \ +)) + +$(eval $(call gb_CppunitTest_use_libraries,svgio_read, \ + basegfx \ + comphelper \ + cppu \ + cppuhelper \ + drawinglayer \ + editeng \ + i18nlangtag \ + sal \ + salhelper \ + sax \ + sot \ + svl \ + svt \ + svx \ + svxcore \ + test \ + tl \ + tk \ + ucbhelper \ + unotest \ + utl \ + vcl \ + xo \ +)) + +$(eval $(call gb_CppunitTest_use_sdk_api,svgio_read)) +$(eval $(call gb_CppunitTest_use_ure,svgio_read)) +$(eval $(call gb_CppunitTest_use_vcl,svgio_read)) +$(eval $(call gb_CppunitTest_use_rdb,svgio_read,services)) + +$(eval $(call gb_CppunitTest_use_custom_headers,svgio_read,\ + officecfg/registry \ +)) + +$(eval $(call gb_CppunitTest_use_configuration,svgio_read)) + +# vim: set noet sw=4 ts=4: diff --git a/svgio/Library_svgio.mk b/svgio/Library_svgio.mk index 44d8ef9c05b5..94f78b91e5ce 100644 --- a/svgio/Library_svgio.mk +++ b/svgio/Library_svgio.mk @@ -77,6 +77,7 @@ $(eval $(call gb_Library_add_exception_objects,svgio,\ svgio/source/svgreader/svgtextpathnode \ svgio/source/svgreader/svgtspannode \ svgio/source/svgreader/svgusenode \ + svgio/source/svgreader/svgvisitor \ svgio/source/svguno/svguno \ svgio/source/svguno/xsvgparser \ )) diff --git a/svgio/Module_svgio.mk b/svgio/Module_svgio.mk index 29ef97d7087b..26b659f59991 100644 --- a/svgio/Module_svgio.mk +++ b/svgio/Module_svgio.mk @@ -24,6 +24,7 @@ $(eval $(call gb_Module_add_targets,svgio,\ $(eval $(call gb_Module_add_check_targets,svgio,\ CppunitTest_svgio \ + CppunitTest_svgio_read \ )) # vim: set noet ts=4 sw=4: diff --git a/svgio/inc/svgnode.hxx b/svgio/inc/svgnode.hxx index 22df883b22b3..c10179bf236a 100644 --- a/svgio/inc/svgnode.hxx +++ b/svgio/inc/svgnode.hxx @@ -80,6 +80,8 @@ namespace svgio // which members should be initialized Display getDisplayFromContent(const OUString& aContent); + class Visitor; + class SvgNode : public InfoProvider { private: @@ -137,6 +139,8 @@ namespace svgio SvgNode(const SvgNode&) = delete; SvgNode& operator=(const SvgNode&) = delete; + void accept(Visitor& rVisitor); + /// scan helper to read and interpret a local CssStyle to mpLocalCssStyle void readLocalCssStyle(const OUString& aContent); @@ -182,6 +186,14 @@ namespace svgio /// alternative parent void setAlternativeParent(const SvgNode* pAlternativeParent = nullptr) { mpAlternativeParent = pAlternativeParent; } }; + + class Visitor + { + public: + virtual ~Visitor() = default; + virtual void visit(SvgNode const & pNode) = 0; + }; + } // end of namespace svgreader } // end of namespace svgio diff --git a/svgio/inc/svgvisitor.hxx b/svgio/inc/svgvisitor.hxx new file mode 100644 index 000000000000..ea56e4cd189a --- /dev/null +++ b/svgio/inc/svgvisitor.hxx @@ -0,0 +1,41 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#ifndef INCLUDED_SVGIO_INC_SVGVISITOR_HXX +#define INCLUDED_SVGIO_INC_SVGVISITOR_HXX + +#include <basegfx/DrawCommands.hxx> +#include <memory> +#include "svgnode.hxx" + +namespace svgio +{ +namespace svgreader +{ +class SvgDrawVisitor : public Visitor +{ +private: + std::shared_ptr<DrawRoot> mpDrawRoot; + std::shared_ptr<DrawBase> mpCurrent; + +public: + SvgDrawVisitor(); + + void visit(svgio::svgreader::SvgNode const& rNode) override; + void goToChildren(svgio::svgreader::SvgNode const& rNode); + + std::shared_ptr<DrawRoot> const& getDrawRoot() { return mpDrawRoot; } +}; +} +} + +#endif // INCLUDED_SVGIO_INC_SVGVISITOR_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svgio/qa/cppunit/SvgRead.cxx b/svgio/qa/cppunit/SvgRead.cxx new file mode 100644 index 000000000000..9077f92db7b6 --- /dev/null +++ b/svgio/qa/cppunit/SvgRead.cxx @@ -0,0 +1,102 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <sal/config.h> + +#include <test/bootstrapfixture.hxx> + +#include <memory> + +#include <comphelper/seqstream.hxx> +#include <comphelper/sequence.hxx> +#include <comphelper/processfactory.hxx> +#include <tools/stream.hxx> + +#include <com/sun/star/graphic/SvgTools.hpp> +#include <com/sun/star/graphic/Primitive2DTools.hpp> +#include <com/sun/star/graphic/XPrimitive2D.hpp> + +#include <drawinglayer/primitive2d/baseprimitive2d.hxx> + +#include <com/sun/star/graphic/XSvgParser.hpp> +#include <com/sun/star/lang/XServiceInfo.hpp> +#include <com/sun/star/lang/XInitialization.hpp> +#include <com/sun/star/xml/sax/XParser.hpp> +#include <com/sun/star/xml/sax/Parser.hpp> +#include <com/sun/star/xml/sax/InputSource.hpp> +#include <cppuhelper/implbase2.hxx> +#include <cppuhelper/supportsservice.hxx> +#include <comphelper/sequence.hxx> + +#include <svgdocumenthandler.hxx> +#include <svgrectnode.hxx> +#include <svgsvgnode.hxx> +#include <svggnode.hxx> + +#include <basegfx/DrawCommands.hxx> + +namespace +{ +using namespace css; + +class Test : public test::BootstrapFixture +{ + void test(); + uno::Reference<io::XInputStream> parseSvg(const OUString& aSource); + +public: + CPPUNIT_TEST_SUITE(Test); + CPPUNIT_TEST(test); + CPPUNIT_TEST_SUITE_END(); +}; + +uno::Reference<io::XInputStream> Test::parseSvg(const OUString& aSource) +{ + SvFileStream aFileStream(aSource, StreamMode::READ); + std::size_t nSize = aFileStream.remainingSize(); + std::unique_ptr<sal_Int8[]> pBuffer(new sal_Int8[nSize + 1]); + aFileStream.ReadBytes(pBuffer.get(), nSize); + pBuffer[nSize] = 0; + + uno::Sequence<sal_Int8> aData(pBuffer.get(), nSize + 1); + uno::Reference<io::XInputStream> aInputStream(new comphelper::SequenceInputStream(aData)); + + return aInputStream; +} + +void Test::test() +{ + OUString aSvgFile = "/svgio/qa/cppunit/data/Rect.svg"; + OUString aUrl = m_directories.getURLFromSrc(aSvgFile); + OUString aPath = m_directories.getPathFromSrc(aSvgFile); + + uno::Reference<io::XInputStream> xStream = parseSvg(aUrl); + CPPUNIT_ASSERT(xStream.is()); + + uno::Reference<uno::XComponentContext> xContext(comphelper::getProcessComponentContext()); + const uno::Reference<graphic::XSvgParser> xSvgParser = graphic::SvgTools::create(xContext); + + uno::Any aAny = xSvgParser->getDrawCommands(xStream, aPath); + CPPUNIT_ASSERT(aAny.has<sal_uInt64>()); + DrawRoot* pDrawRoot = reinterpret_cast<DrawRoot*>(aAny.get<sal_uInt64>()); + + CPPUNIT_ASSERT_EQUAL(size_t(1), pDrawRoot->maChildren.size()); + CPPUNIT_ASSERT_EQUAL(basegfx::B2DRange(0, 0, 120, 120), pDrawRoot->maRectangle); + + CPPUNIT_ASSERT_EQUAL(DrawCommandType::Rectangle, pDrawRoot->maChildren[0]->getType()); + CPPUNIT_ASSERT_EQUAL(basegfx::B2DRange(10, 10, 110, 110), + static_cast<DrawRectangle*>(pDrawRoot->maChildren[0].get())->maRectangle); +} + +CPPUNIT_TEST_SUITE_REGISTRATION(Test); +} + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svgio/source/svgreader/svgnode.cxx b/svgio/source/svgreader/svgnode.cxx index 88b9a72ec196..cb30c3111131 100644 --- a/svgio/source/svgreader/svgnode.cxx +++ b/svgio/source/svgreader/svgnode.cxx @@ -673,6 +673,10 @@ namespace svgio return XmlSpace_default; } + void SvgNode::accept(Visitor & rVisitor) + { + rVisitor.visit(*this); + } } // end of namespace svgreader } // end of namespace svgio diff --git a/svgio/source/svgreader/svgvisitor.cxx b/svgio/source/svgreader/svgvisitor.cxx new file mode 100644 index 000000000000..841a1cb7022e --- /dev/null +++ b/svgio/source/svgreader/svgvisitor.cxx @@ -0,0 +1,105 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + */ + +#include <sal/config.h> +#include <sal/log.hxx> + +#include <svgdocumenthandler.hxx> +#include <svgrectnode.hxx> +#include <svgsvgnode.hxx> +#include <svggnode.hxx> +#include <svgpathnode.hxx> + +#include <svgvisitor.hxx> + +namespace svgio +{ +namespace svgreader +{ +SvgDrawVisitor::SvgDrawVisitor() + : mpDrawRoot(std::make_shared<DrawRoot>()) + , mpCurrent(mpDrawRoot) +{ +} + +void SvgDrawVisitor::visit(svgio::svgreader::SvgNode const& rNode) +{ + switch (rNode.getType()) + { + case svgio::svgreader::SVGTokenSvg: + { + auto const& rSvgNode = static_cast<svgio::svgreader::SvgSvgNode const&>(rNode); + + double x = rSvgNode.getX().getNumber(); + double y = rSvgNode.getY().getNumber(); + double w = rSvgNode.getWidth().getNumber(); + double h = rSvgNode.getHeight().getNumber(); + + static_cast<DrawRoot*>(mpCurrent.get())->maRectangle + = basegfx::B2DRange(x, y, x + w, y + h); + } + break; + case svgio::svgreader::SVGTokenG: + { + auto const& rGNode = static_cast<svgio::svgreader::SvgGNode const&>(rNode); + + if (rGNode.getTransform() != nullptr) + { + basegfx::B2DHomMatrix rMatrix = *rGNode.getTransform(); + + printf("G [%f %f %f - %f %f %f - %f %f %f]\n", rMatrix.get(0, 0), rMatrix.get(0, 1), + rMatrix.get(0, 2), rMatrix.get(1, 0), rMatrix.get(1, 1), rMatrix.get(1, 2), + rMatrix.get(2, 0), rMatrix.get(2, 1), rMatrix.get(2, 2)); + } + } + break; + case svgio::svgreader::SVGTokenRect: + { + auto const& rRectNode = static_cast<svgio::svgreader::SvgRectNode const&>(rNode); + + double x = rRectNode.getX().getNumber(); + double y = rRectNode.getY().getNumber(); + double w = rRectNode.getWidth().getNumber(); + double h = rRectNode.getHeight().getNumber(); + + auto pRectangle + = std::make_shared<DrawRectangle>(basegfx::B2DRange(x, y, x + w, y + h)); + mpCurrent->maChildren.push_back(pRectangle); + } + break; + case svgio::svgreader::SVGTokenPath: + { + auto const& rPathNode = static_cast<svgio::svgreader::SvgPathNode const&>(rNode); + auto pPath = rPathNode.getPath(); + if (pPath) + { + auto pDrawPath = std::make_shared<DrawPath>(*pPath); + mpCurrent->maChildren.push_back(pDrawPath); + } + } + break; + + default: + break; + } + goToChildren(rNode); +} + +void SvgDrawVisitor::goToChildren(svgio::svgreader::SvgNode const& rNode) +{ + for (auto& rChild : rNode.getChildren()) + { + rChild->accept(*this); + } +} +} +} // end of namespace svgio::svgreader + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svgio/source/svguno/xsvgparser.cxx b/svgio/source/svguno/xsvgparser.cxx index 2f2a51c5c318..3ed675763f72 100644 --- a/svgio/source/svguno/xsvgparser.cxx +++ b/svgio/source/svguno/xsvgparser.cxx @@ -32,6 +32,8 @@ #include <drawinglayer/geometry/viewinformation2d.hxx> #include <svgdocumenthandler.hxx> +#include <svgvisitor.hxx> + #include "xsvgparser.hxx" using namespace ::com::sun::star; @@ -43,9 +45,11 @@ namespace svgio class XSvgParser : public ::cppu::WeakAggImplHelper2< graphic::XSvgParser, lang::XServiceInfo > { private: - uno::Reference< uno::XComponentContext > context_; + std::shared_ptr<SvgDrawVisitor> mpVisitor; - protected: + uno::Reference< uno::XComponentContext > context_; + bool parseSvgXML(uno::Reference<io::XInputStream> const & xSVGStream, + uno::Reference<xml::sax::XDocumentHandler> const & xSvgDocHdl); public: explicit XSvgParser( uno::Reference< uno::XComponentContext > const & context); @@ -57,6 +61,10 @@ namespace svgio const uno::Reference< ::io::XInputStream >& xSVGStream, const OUString& aAbsolutePath) override; + virtual uno::Any SAL_CALL getDrawCommands( + uno::Reference<io::XInputStream> const & xSvgStream, + const OUString& aAbsolutePath) override; + // XServiceInfo virtual OUString SAL_CALL getImplementationName() override; virtual sal_Bool SAL_CALL supportsService(const OUString&) override; @@ -97,6 +105,45 @@ namespace svgio { } + bool XSvgParser::parseSvgXML(uno::Reference<io::XInputStream> const & xSVGStream, uno::Reference<xml::sax::XDocumentHandler> const & xSvgDocHdl) + { + try + { + // prepare ParserInputSrouce + xml::sax::InputSource myInputSource; + myInputSource.aInputStream = xSVGStream; + + // get parser + uno::Reference< xml::sax::XParser > xParser( + xml::sax::Parser::create(context_)); + // fdo#60471 need to enable internal entities because + // certain ... popular proprietary products write SVG files + // that use entities to define XML namespaces. + uno::Reference<lang::XInitialization> const xInit(xParser, + uno::UNO_QUERY_THROW); + uno::Sequence<uno::Any> args(1); + args[0] <<= OUString("DoSmeplease"); + xInit->initialize(args); + + // connect parser and filter + xParser->setDocumentHandler(xSvgDocHdl); + + // finally, parse the stream to a hierarchy of + // SVGGraphicPrimitive2D which will be embedded to the + // primitive sequence. Their decompositions will in the + // end create local low-level primitives, thus SVG will + // be processable from all our processors + xParser->parseStream(myInputSource); + } + catch(const uno::Exception& e) + { + SAL_WARN( "svg", "Parse error! : " << e); + return false; + } + + return true; + } + uno::Sequence< uno::Reference< ::graphic::XPrimitive2D > > XSvgParser::getDecomposition( const uno::Reference< ::io::XInputStream >& xSVGStream, const OUString& aAbsolutePath ) @@ -107,40 +154,8 @@ namespace svgio { // local document handler SvgDocHdl* pSvgDocHdl = new SvgDocHdl(aAbsolutePath); - uno::Reference< xml::sax::XDocumentHandler > xSvgDocHdl(pSvgDocHdl); - - try - { - // prepare ParserInputSrouce - xml::sax::InputSource myInputSource; - myInputSource.aInputStream = xSVGStream; - - // get parser - uno::Reference< xml::sax::XParser > xParser( - xml::sax::Parser::create(context_)); - // fdo#60471 need to enable internal entities because - // certain ... popular proprietary products write SVG files - // that use entities to define XML namespaces. - uno::Reference<lang::XInitialization> const xInit(xParser, - uno::UNO_QUERY_THROW); - uno::Sequence<uno::Any> args(1); - args[0] <<= OUString("DoSmeplease"); - xInit->initialize(args); - - // connect parser and filter - xParser->setDocumentHandler(xSvgDocHdl); - - // finally, parse the stream to a hierarchy of - // SVGGraphicPrimitive2D which will be embedded to the - // primitive sequence. Their decompositions will in the - // end create local low-level primitives, thus SVG will - // be processable from all our processors - xParser->parseStream(myInputSource); - } - catch(const uno::Exception& e) - { - SAL_WARN( "svg", "Parse error! : " << e); - } + uno::Reference<xml::sax::XDocumentHandler> xSvgDocHdl(pSvgDocHdl); + parseSvgXML(xSVGStream, xSvgDocHdl); // decompose to primitives for(std::unique_ptr<SvgNode> const & pCandidate : pSvgDocHdl->getSvgDocument().getSvgNodeVector()) @@ -159,6 +174,35 @@ namespace svgio return comphelper::containerToSequence(aRetval); } + uno::Any SAL_CALL XSvgParser::getDrawCommands( + uno::Reference<io::XInputStream> const & xSvgStream, + const OUString& aAbsolutePath) + { + uno::Any aAnyResult; + + if (!xSvgStream.is()) + return aAnyResult; + + SvgDocHdl* pSvgDocHdl = new SvgDocHdl(aAbsolutePath); + uno::Reference<xml::sax::XDocumentHandler> xSvgDocHdl(pSvgDocHdl); + parseSvgXML(xSvgStream, xSvgDocHdl); + + // decompose to primitives + for (std::unique_ptr<SvgNode> const & pCandidate : pSvgDocHdl->getSvgDocument().getSvgNodeVector()) + { + if (Display_none != pCandidate->getDisplay()) + { + mpVisitor = std::make_shared<SvgDrawVisitor>(); + pCandidate->accept(*mpVisitor); + std::shared_ptr<DrawRoot> pDrawRoot(mpVisitor->getDrawRoot()); + sal_uInt64 nPointer = reinterpret_cast<sal_uInt64>(pDrawRoot.get()); + aAnyResult <<= sal_uInt64(nPointer); + } + } + + return aAnyResult; + } + OUString SAL_CALL XSvgParser::getImplementationName() { return XSvgParser_getImplementationName(); |