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 "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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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 | |
Florin Malita | 7006e15 | 2020-11-10 15:24:59 -0500 | [diff] [blame^] | 36 | fDom = SkSVGDOM::MakeFromStream(svgStream); |
fmalita | e1baa7c | 2016-09-14 12:04:30 -0700 | [diff] [blame] | 37 | if (fDom) { |
| 38 | fDom->setContainerSize(SkSize::Make(this->width(), this->height())); |
| 39 | } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 40 | } |
| 41 | |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 42 | void onDrawContent(SkCanvas* canvas) override { |
| 43 | if (fDom) { |
| 44 | fDom->render(canvas); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void onSizeChange() override { |
| 49 | if (fDom) { |
| 50 | fDom->setContainerSize(SkSize::Make(this->width(), this->height())); |
| 51 | } |
| 52 | |
| 53 | this->INHERITED::onSizeChange(); |
| 54 | } |
| 55 | |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 56 | SkString name() override { return fLabel; } |
fmalita | b83cdbc | 2016-08-04 08:39:41 -0700 | [diff] [blame] | 57 | |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 58 | private: |
| 59 | sk_sp<SkSVGDOM> fDom; |
fmalita | 851d68a | 2016-08-15 07:48:47 -0700 | [diff] [blame] | 60 | SkString fPath; |
fmalita | b83cdbc | 2016-08-04 08:39:41 -0700 | [diff] [blame] | 61 | SkString fLabel; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 62 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 63 | using INHERITED = Sample; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | } // anonymous namespace |
| 67 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 68 | Sample* CreateSampleSVGFileView(const SkString& filename); |
| 69 | Sample* CreateSampleSVGFileView(const SkString& filename) { |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 70 | return new SVGFileView(filename); |
| 71 | } |
Hal Canary | 0f66681 | 2018-03-22 15:21:12 -0400 | [diff] [blame] | 72 | #endif // SK_XML |