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 | |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 12 | #include "SampleCode.h" |
| 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" |
| 19 | #include "SkView.h" |
| 20 | |
| 21 | namespace { |
| 22 | |
| 23 | class SVGFileView : public SampleView { |
| 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()) { |
fmalita | 851d68a | 2016-08-15 07:48:47 -0700 | [diff] [blame] | 33 | SkDebugf("file not found: \"path\"\n", fPath.c_str()); |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 34 | return; |
| 35 | } |
| 36 | |
| 37 | SkDOM xmlDom; |
| 38 | if (!xmlDom.build(svgStream)) { |
fmalita | 851d68a | 2016-08-15 07:48:47 -0700 | [diff] [blame] | 39 | SkDebugf("XML parsing failed: \"path\"\n", fPath.c_str()); |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 40 | return; |
| 41 | } |
| 42 | |
fmalita | e1baa7c | 2016-09-14 12:04:30 -0700 | [diff] [blame] | 43 | fDom = SkSVGDOM::MakeFromDOM(xmlDom); |
| 44 | if (fDom) { |
| 45 | fDom->setContainerSize(SkSize::Make(this->width(), this->height())); |
| 46 | } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 47 | } |
| 48 | |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 49 | void onDrawContent(SkCanvas* canvas) override { |
| 50 | if (fDom) { |
| 51 | fDom->render(canvas); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void onSizeChange() override { |
| 56 | if (fDom) { |
| 57 | fDom->setContainerSize(SkSize::Make(this->width(), this->height())); |
| 58 | } |
| 59 | |
| 60 | this->INHERITED::onSizeChange(); |
| 61 | } |
| 62 | |
fmalita | b83cdbc | 2016-08-04 08:39:41 -0700 | [diff] [blame] | 63 | bool onQuery(SkEvent* evt) override { |
| 64 | if (SampleCode::TitleQ(*evt)) { |
| 65 | SampleCode::TitleR(evt, fLabel.c_str()); |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | return this->INHERITED::onQuery(evt); |
| 70 | } |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 71 | private: |
| 72 | sk_sp<SkSVGDOM> fDom; |
fmalita | 851d68a | 2016-08-15 07:48:47 -0700 | [diff] [blame] | 73 | SkString fPath; |
fmalita | b83cdbc | 2016-08-04 08:39:41 -0700 | [diff] [blame] | 74 | SkString fLabel; |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 75 | |
| 76 | typedef SampleView INHERITED; |
| 77 | }; |
| 78 | |
| 79 | } // anonymous namespace |
| 80 | |
fmalita | 851d68a | 2016-08-15 07:48:47 -0700 | [diff] [blame] | 81 | SampleView* CreateSampleSVGFileView(const SkString& filename); |
| 82 | SampleView* CreateSampleSVGFileView(const SkString& filename) { |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 83 | return new SVGFileView(filename); |
| 84 | } |
Hal Canary | 0f66681 | 2018-03-22 15:21:12 -0400 | [diff] [blame^] | 85 | #endif // SK_XML |