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