fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 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 "SampleCode.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkDOM.h" |
fmalita | b83cdbc | 2016-08-04 08:39:41 -0700 | [diff] [blame] | 11 | #include "SkOSFile.h" |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 12 | #include "SkStream.h" |
| 13 | #include "SkSVGDOM.h" |
| 14 | #include "SkView.h" |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | class SVGFileView : public SampleView { |
| 19 | public: |
fmalita | b83cdbc | 2016-08-04 08:39:41 -0700 | [diff] [blame] | 20 | SVGFileView(const char path[]) |
| 21 | : fLabel(SkStringPrintf("[%s]", SkOSPath::Basename(path).c_str())) { |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 22 | SkFILEStream svgStream(path); |
| 23 | if (!svgStream.isValid()) { |
| 24 | SkDebugf("file not found: \"path\"\n", path); |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | SkDOM xmlDom; |
| 29 | if (!xmlDom.build(svgStream)) { |
| 30 | SkDebugf("XML parsing failed: \"path\"\n", path); |
| 31 | return; |
| 32 | } |
| 33 | |
| 34 | fDom = SkSVGDOM::MakeFromDOM(xmlDom, SkSize::Make(this->width(), this->height())); |
| 35 | } |
| 36 | |
| 37 | virtual ~SVGFileView() = default; |
| 38 | |
| 39 | protected: |
| 40 | void onDrawContent(SkCanvas* canvas) override { |
| 41 | if (fDom) { |
| 42 | fDom->render(canvas); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | void onSizeChange() override { |
| 47 | if (fDom) { |
| 48 | fDom->setContainerSize(SkSize::Make(this->width(), this->height())); |
| 49 | } |
| 50 | |
| 51 | this->INHERITED::onSizeChange(); |
| 52 | } |
| 53 | |
fmalita | b83cdbc | 2016-08-04 08:39:41 -0700 | [diff] [blame] | 54 | bool onQuery(SkEvent* evt) override { |
| 55 | if (SampleCode::TitleQ(*evt)) { |
| 56 | SampleCode::TitleR(evt, fLabel.c_str()); |
| 57 | return true; |
| 58 | } |
| 59 | |
| 60 | return this->INHERITED::onQuery(evt); |
| 61 | } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 62 | private: |
| 63 | sk_sp<SkSVGDOM> fDom; |
fmalita | b83cdbc | 2016-08-04 08:39:41 -0700 | [diff] [blame] | 64 | SkString fLabel; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 65 | |
| 66 | typedef SampleView INHERITED; |
| 67 | }; |
| 68 | |
| 69 | } // anonymous namespace |
| 70 | |
| 71 | SampleView* CreateSampleSVGFileView(const char filename[]); |
| 72 | SampleView* CreateSampleSVGFileView(const char filename[]) { |
| 73 | return new SVGFileView(filename); |
| 74 | } |