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