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 | |
Hal Canary | 0f66681 | 2018-03-22 15:21:12 -0400 | [diff] [blame] | 8 | #include "SkTypes.h" |
| 9 | |
| 10 | #ifdef SK_XML |
| 11 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 12 | #include "Sample.h" |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 13 | #include "SkCanvas.h" |
| 14 | #include "SkDOM.h" |
fmalita | b83cdbc | 2016-08-04 08:39:41 -0700 | [diff] [blame] | 15 | #include "SkOSFile.h" |
Ben Wagner | bf111d7 | 2016-11-07 18:05:29 -0500 | [diff] [blame] | 16 | #include "SkOSPath.h" |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 17 | #include "SkStream.h" |
| 18 | #include "SkSVGDOM.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 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 62 | bool onQuery(Sample::Event* evt) override { |
| 63 | if (Sample::TitleQ(*evt)) { |
| 64 | Sample::TitleR(evt, fLabel.c_str()); |
fmalita | b83cdbc | 2016-08-04 08:39:41 -0700 | [diff] [blame] | 65 | return true; |
| 66 | } |
| 67 | |
| 68 | return this->INHERITED::onQuery(evt); |
| 69 | } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 70 | private: |
| 71 | sk_sp<SkSVGDOM> fDom; |
fmalita | 851d68a | 2016-08-15 07:48:47 -0700 | [diff] [blame] | 72 | SkString fPath; |
fmalita | b83cdbc | 2016-08-04 08:39:41 -0700 | [diff] [blame] | 73 | SkString fLabel; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 74 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 75 | typedef Sample INHERITED; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | } // anonymous namespace |
| 79 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 80 | Sample* CreateSampleSVGFileView(const SkString& filename); |
| 81 | Sample* CreateSampleSVGFileView(const SkString& filename) { |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 82 | return new SVGFileView(filename); |
| 83 | } |
Hal Canary | 0f66681 | 2018-03-22 15:21:12 -0400 | [diff] [blame] | 84 | #endif // SK_XML |