OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkDOM.h" | 10 #include "SkDOM.h" |
11 #include "SkOSFile.h" | 11 #include "SkOSFile.h" |
12 #include "SkStream.h" | 12 #include "SkStream.h" |
13 #include "SkSVGDOM.h" | 13 #include "SkSVGDOM.h" |
14 #include "SkView.h" | 14 #include "SkView.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 class SVGFileView : public SampleView { | 18 class SVGFileView : public SampleView { |
19 public: | 19 public: |
20 SVGFileView(const char path[]) | 20 SVGFileView(const SkString& path) |
21 : fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path).c_str())) { | 21 : fPath(path), fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path.c_s tr()).c_str())) {} |
22 SkFILEStream svgStream(path); | 22 virtual ~SVGFileView() = default; |
23 | |
24 protected: | |
25 void onOnceBeforeDraw() override { | |
robertphillips
2016/08/14 20:15:29
Do you need this if check? Shouldn't 'fDom' always
f(malita)
2016/08/15 13:18:56
I was thinking it may trigger when navigating back
| |
26 if (fDom) { | |
27 return; | |
28 } | |
29 SkFILEStream svgStream(fPath.c_str()); | |
23 if (!svgStream.isValid()) { | 30 if (!svgStream.isValid()) { |
24 SkDebugf("file not found: \"path\"\n", path); | 31 SkDebugf("file not found: \"path\"\n", fPath.c_str()); |
25 return; | 32 return; |
26 } | 33 } |
27 | 34 |
28 SkDOM xmlDom; | 35 SkDOM xmlDom; |
29 if (!xmlDom.build(svgStream)) { | 36 if (!xmlDom.build(svgStream)) { |
30 SkDebugf("XML parsing failed: \"path\"\n", path); | 37 SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str()); |
31 return; | 38 return; |
32 } | 39 } |
33 | 40 |
34 fDom = SkSVGDOM::MakeFromDOM(xmlDom, SkSize::Make(this->width(), this->h eight())); | 41 fDom = SkSVGDOM::MakeFromDOM(xmlDom, SkSize::Make(this->width(), this->h eight())); |
35 } | 42 } |
36 | 43 |
37 virtual ~SVGFileView() = default; | |
38 | |
39 protected: | |
40 void onDrawContent(SkCanvas* canvas) override { | 44 void onDrawContent(SkCanvas* canvas) override { |
41 if (fDom) { | 45 if (fDom) { |
42 fDom->render(canvas); | 46 fDom->render(canvas); |
43 } | 47 } |
44 } | 48 } |
45 | 49 |
46 void onSizeChange() override { | 50 void onSizeChange() override { |
47 if (fDom) { | 51 if (fDom) { |
48 fDom->setContainerSize(SkSize::Make(this->width(), this->height())); | 52 fDom->setContainerSize(SkSize::Make(this->width(), this->height())); |
49 } | 53 } |
50 | 54 |
51 this->INHERITED::onSizeChange(); | 55 this->INHERITED::onSizeChange(); |
52 } | 56 } |
53 | 57 |
54 bool onQuery(SkEvent* evt) override { | 58 bool onQuery(SkEvent* evt) override { |
55 if (SampleCode::TitleQ(*evt)) { | 59 if (SampleCode::TitleQ(*evt)) { |
56 SampleCode::TitleR(evt, fLabel.c_str()); | 60 SampleCode::TitleR(evt, fLabel.c_str()); |
57 return true; | 61 return true; |
58 } | 62 } |
59 | 63 |
60 return this->INHERITED::onQuery(evt); | 64 return this->INHERITED::onQuery(evt); |
61 } | 65 } |
62 private: | 66 private: |
63 sk_sp<SkSVGDOM> fDom; | 67 sk_sp<SkSVGDOM> fDom; |
68 SkString fPath; | |
64 SkString fLabel; | 69 SkString fLabel; |
65 | 70 |
66 typedef SampleView INHERITED; | 71 typedef SampleView INHERITED; |
67 }; | 72 }; |
68 | 73 |
69 } // anonymous namespace | 74 } // anonymous namespace |
70 | 75 |
71 SampleView* CreateSampleSVGFileView(const char filename[]); | 76 SampleView* CreateSampleSVGFileView(const SkString& filename); |
72 SampleView* CreateSampleSVGFileView(const char filename[]) { | 77 SampleView* CreateSampleSVGFileView(const SkString& filename) { |
73 return new SVGFileView(filename); | 78 return new SVGFileView(filename); |
74 } | 79 } |
OLD | NEW |