• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "tools/viewer/SvgSlide.h"
9 
10 #if defined(SK_ENABLE_SVG)
11 
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkStream.h"
14 #include "modules/skresources/include/SkResources.h"
15 #include "modules/svg/include/SkSVGDOM.h"
16 #include "modules/svg/include/SkSVGNode.h"
17 #include "src/utils/SkOSPath.h"
18 
SvgSlide(const SkString & name,const SkString & path)19 SvgSlide::SvgSlide(const SkString& name, const SkString& path)
20     : fPath(path)
21 {
22     fName = name;
23 }
24 
load(SkScalar w,SkScalar h)25 void SvgSlide::load(SkScalar w, SkScalar h) {
26     auto stream = SkStream::MakeFromFile(fPath.c_str());
27 
28     if (!stream) {
29         SkDebugf("Could not open %s.\n", fPath.c_str());
30         return;
31     }
32 
33     fWinSize = SkSize::Make(w, h);
34 
35     auto rp = skresources::DataURIResourceProviderProxy::Make(
36                   skresources::FileResourceProvider::Make(SkOSPath::Dirname(fPath.c_str()),
37                                                           /*predecode=*/true),
38                   /*predecode=*/true);
39     fDom = SkSVGDOM::Builder().setResourceProvider(std::move(rp)).make(*stream);
40     if (fDom) {
41         fDom->setContainerSize(fWinSize);
42     }
43 }
44 
unload()45 void SvgSlide::unload() {
46     fDom.reset();
47 }
48 
resize(SkScalar w,SkScalar h)49 void SvgSlide::resize(SkScalar w, SkScalar h) {
50     fWinSize = { w, h };
51     if (fDom) {
52         fDom->setContainerSize(fWinSize);
53     }
54 }
55 
getDimensions() const56 SkISize SvgSlide::getDimensions() const {
57     // We always scale to fill the window.
58     return fWinSize.toCeil();
59 }
60 
draw(SkCanvas * canvas)61 void SvgSlide::draw(SkCanvas* canvas) {
62     if (fDom) {
63         fDom->render(canvas);
64     }
65 }
66 
67 #endif // defined(SK_ENABLE_SVG)
68