diff --git a/BUILD.gn b/BUILD.gn index 8b30c411d4..69ed2d4240 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1693,7 +1693,7 @@ if (skia_enable_tools) { deps = [ ":flags", ":skia", - ":tool_utils" + ":tool_utils", ] } @@ -2014,6 +2014,7 @@ if (skia_enable_tools) { "tools/viewer/SkottieSlide.cpp", "tools/viewer/SlideDir.cpp", "tools/viewer/StatsLayer.cpp", + "tools/viewer/SvgSlide.cpp", "tools/viewer/Viewer.cpp", ] libs = [] @@ -2022,6 +2023,7 @@ if (skia_enable_tools) { deps = [ ":experimental_skottie", ":experimental_sksg", + ":experimental_svg_model", ":flags", ":gm", ":gpu_tool_utils", 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)); + } + } }