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