From c659c2c30d2a33392f62bb9a27ff04a07d216976 Mon Sep 17 00:00:00 2001 From: Florin Malita Date: Thu, 5 Apr 2018 11:57:21 -0400 Subject: Viewer SVG support Change-Id: I93ee61271ebe960063bec16ba472b3fd243ee149 Reviewed-on: https://skia-review.googlesource.com/118885 Reviewed-by: Mike Reed Commit-Queue: Florin Malita --- tools/viewer/SvgSlide.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ tools/viewer/SvgSlide.h | 34 ++++++++++++++++++++++++++++++++++ tools/viewer/Viewer.cpp | 16 ++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 tools/viewer/SvgSlide.cpp create mode 100644 tools/viewer/SvgSlide.h (limited to 'tools/viewer') diff --git a/tools/viewer/SvgSlide.cpp b/tools/viewer/SvgSlide.cpp new file mode 100644 index 0000000000..41dc3187a5 --- /dev/null +++ b/tools/viewer/SvgSlide.cpp @@ -0,0 +1,43 @@ +/* + * Copyright 2018 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include "SvgSlide.h" + +#include "SkCanvas.h" +#include "SkStream.h" +#include "SkSVGDOM.h" + +SvgSlide::SvgSlide(const SkString& name, const SkString& path) + : fPath(path) { + fName = name; +} + +void SvgSlide::load(SkScalar w, SkScalar h) { + fWinSize = SkSize::Make(w, h); + + if (const auto svgStream = SkStream::MakeFromFile(fPath.c_str())) { + fDom = SkSVGDOM::MakeFromStream(*svgStream); + if (fDom) { + fDom->setContainerSize(fWinSize); + } + } +} + +void SvgSlide::unload() { + fDom.reset(); +} + +SkISize SvgSlide::getDimensions() const { + // We always scale to fill the window. + return fWinSize.toCeil(); +} + +void SvgSlide::draw(SkCanvas* canvas) { + if (fDom) { + fDom->render(canvas); + } +} diff --git a/tools/viewer/SvgSlide.h b/tools/viewer/SvgSlide.h new file mode 100644 index 0000000000..9fc3a5b717 --- /dev/null +++ b/tools/viewer/SvgSlide.h @@ -0,0 +1,34 @@ +/* + * Copyright 2018 Google Inc. + * + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#ifndef SvgSlide_DEFINED +#define SvgSlide_DEFINED + +#include "Slide.h" + +class SkSVGDOM; + +class SvgSlide final : public Slide { +public: + SvgSlide(const SkString& name, const SkString& path); + + void load(SkScalar winWidth, SkScalar winHeight) override; + void unload() override; + + SkISize getDimensions() const override; + + void draw(SkCanvas*) override; +private: + const SkString fPath; + + SkSize fWinSize = SkSize::MakeEmpty(); + sk_sp fDom; + + typedef Slide INHERITED; +}; + +#endif // SvgSlide_DEFINED diff --git a/tools/viewer/Viewer.cpp b/tools/viewer/Viewer.cpp index ad02a6574b..fb659555e2 100644 --- a/tools/viewer/Viewer.cpp +++ b/tools/viewer/Viewer.cpp @@ -15,6 +15,7 @@ #include "SkottieSlide.h" #include "SKPSlide.h" #include "SlideDir.h" +#include "SvgSlide.h" #include "GrContext.h" #include "SkCanvas.h" @@ -577,6 +578,21 @@ void Viewer::initSlides() { std::move(dirSlides))); } } + + // SVGs + for (const auto& svg : FLAGS_svgs) { + SkOSFile::Iter it(svg.c_str(), ".svg"); + + SkString svgName; + while (it.next(&svgName)) { + if (SkCommandLineFlags::ShouldSkip(FLAGS_match, svgName.c_str())) { + continue; + } + auto slide = sk_make_sp(svgName, SkOSPath::Join(svg.c_str(), + svgName.c_str())); + fSlides.push_back(std::move(slide)); + } + } } -- cgit v1.2.3