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" |
| 11 | #include "SkStream.h" |
| 12 | #include "SkSVGDOM.h" |
| 13 | #include "SkView.h" |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | class SVGFileView : public SampleView { |
| 18 | public: |
| 19 | SVGFileView(const char path[]) { |
| 20 | SkFILEStream svgStream(path); |
| 21 | if (!svgStream.isValid()) { |
| 22 | SkDebugf("file not found: \"path\"\n", path); |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | SkDOM xmlDom; |
| 27 | if (!xmlDom.build(svgStream)) { |
| 28 | SkDebugf("XML parsing failed: \"path\"\n", path); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | fDom = SkSVGDOM::MakeFromDOM(xmlDom, SkSize::Make(this->width(), this->height())); |
| 33 | } |
| 34 | |
| 35 | virtual ~SVGFileView() = default; |
| 36 | |
| 37 | protected: |
| 38 | void onDrawContent(SkCanvas* canvas) override { |
| 39 | if (fDom) { |
| 40 | fDom->render(canvas); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void onSizeChange() override { |
| 45 | if (fDom) { |
| 46 | fDom->setContainerSize(SkSize::Make(this->width(), this->height())); |
| 47 | } |
| 48 | |
| 49 | this->INHERITED::onSizeChange(); |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | sk_sp<SkSVGDOM> fDom; |
| 54 | |
| 55 | typedef SampleView INHERITED; |
| 56 | }; |
| 57 | |
| 58 | } // anonymous namespace |
| 59 | |
| 60 | SampleView* CreateSampleSVGFileView(const char filename[]); |
| 61 | SampleView* CreateSampleSVGFileView(const char filename[]) { |
| 62 | return new SVGFileView(filename); |
| 63 | } |