• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016 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 "SkTypes.h"
9 
10 #ifdef SK_XML
11 
12 #include "Sample.h"
13 #include "SkCanvas.h"
14 #include "SkDOM.h"
15 #include "SkOSFile.h"
16 #include "SkOSPath.h"
17 #include "SkStream.h"
18 #include "SkSVGDOM.h"
19 
20 namespace {
21 
22 class SVGFileView : public Sample {
23 public:
SVGFileView(const SkString & path)24     SVGFileView(const SkString& path)
25         : fPath(path), fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path.c_str()).c_str())) {}
26     ~SVGFileView() override = default;
27 
28 protected:
onOnceBeforeDraw()29     void onOnceBeforeDraw() override {
30         SkFILEStream svgStream(fPath.c_str());
31         if (!svgStream.isValid()) {
32             SkDebugf("file not found: \"path\"\n", fPath.c_str());
33             return;
34         }
35 
36         SkDOM xmlDom;
37         if (!xmlDom.build(svgStream)) {
38             SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str());
39             return;
40         }
41 
42         fDom = SkSVGDOM::MakeFromDOM(xmlDom);
43         if (fDom) {
44             fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
45         }
46     }
47 
onDrawContent(SkCanvas * canvas)48     void onDrawContent(SkCanvas* canvas) override {
49         if (fDom) {
50             fDom->render(canvas);
51         }
52     }
53 
onSizeChange()54     void onSizeChange() override {
55         if (fDom) {
56             fDom->setContainerSize(SkSize::Make(this->width(), this->height()));
57         }
58 
59         this->INHERITED::onSizeChange();
60     }
61 
onQuery(Sample::Event * evt)62     bool onQuery(Sample::Event* evt) override {
63         if (Sample::TitleQ(*evt)) {
64             Sample::TitleR(evt, fLabel.c_str());
65             return true;
66         }
67 
68         return this->INHERITED::onQuery(evt);
69     }
70 private:
71     sk_sp<SkSVGDOM> fDom;
72     SkString        fPath;
73     SkString        fLabel;
74 
75     typedef Sample INHERITED;
76 };
77 
78 } // anonymous namespace
79 
80 Sample* CreateSampleSVGFileView(const SkString& filename);
CreateSampleSVGFileView(const SkString & filename)81 Sample* CreateSampleSVGFileView(const SkString& filename) {
82     return new SVGFileView(filename);
83 }
84 #endif  // SK_XML
85