Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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/SkRect.h" |
| 15 | #include "include/core/SkStream.h" |
| 16 | #include "samplecode/Sample.h" |
| 17 | #include "src/core/SkOSFile.h" |
| 18 | #include "src/utils/SkOSPath.h" |
| 19 | #include "src/xml/SkDOM.h" |
| 20 | #include "tools/Resources.h" |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 21 | |
| 22 | namespace { |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 23 | class AnimatedSVGSample : public Sample { |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 24 | static constexpr auto kAnimationIterations = 5; |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 25 | enum State { |
| 26 | kZoomIn, |
| 27 | kScroll, |
| 28 | kZoomOut |
| 29 | }; |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 30 | sk_sp<SkSVGDOM> fDom; |
| 31 | const char* fResource = nullptr; |
| 32 | const char* fName = nullptr; |
| 33 | State fState = kZoomIn; |
| 34 | int fAnimationLoop = kAnimationIterations; |
| 35 | SkScalar fDelta = 1; |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 36 | |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 37 | public: |
| 38 | AnimatedSVGSample(const char* r, const char* n) : fResource(r), fName(n) {} |
| 39 | |
| 40 | private: |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 41 | void onOnceBeforeDraw() override { |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 42 | SkASSERT(fResource); |
| 43 | auto data = GetResourceAsData(fResource); |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 44 | if (!data) { |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 45 | SkDebugf("Resource not found: \"%s\"\n", fResource); |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 46 | return; |
| 47 | } |
Mike Reed | 0933bc9 | 2017-12-09 01:27:41 +0000 | [diff] [blame] | 48 | SkMemoryStream svgStream(std::move(data)); |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 49 | |
| 50 | SkDOM xmlDom; |
| 51 | if (!xmlDom.build(svgStream)) { |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 52 | SkDebugf("XML parsing failed: \"%s\"\n", fResource); |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
| 56 | fDom = SkSVGDOM::MakeFromDOM(xmlDom); |
| 57 | if (fDom) { |
| 58 | fDom->setContainerSize(SkSize::Make(this->width(), this->height())); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void onDrawContent(SkCanvas* canvas) override { |
| 63 | if (fDom) { |
| 64 | canvas->setMatrix(SkMatrix::MakeScale(3)); |
| 65 | canvas->clipRect(SkRect::MakeLTRB(0, 0, 400, 400)); |
| 66 | switch (fState) { |
| 67 | case kZoomIn: |
| 68 | fDelta += 0.2f; |
| 69 | canvas->concat(SkMatrix::MakeScale(fDelta)); |
| 70 | break; |
| 71 | case kScroll: |
| 72 | if (fAnimationLoop > kAnimationIterations/2) { |
| 73 | fDelta += 80.f; |
| 74 | } else { |
| 75 | fDelta -= 80.f; |
| 76 | } |
| 77 | canvas->concat(SkMatrix::MakeScale(fDelta)); |
| 78 | canvas->translate(fDelta, 0); |
| 79 | break; |
| 80 | case kZoomOut: |
| 81 | fDelta += 0.2f; |
| 82 | canvas->concat(SkMatrix::MakeScale(fDelta)); |
| 83 | break; |
| 84 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 85 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 86 | fDom->render(canvas); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void onSizeChange() override { |
| 91 | if (fDom) { |
| 92 | fDom->setContainerSize(SkSize::Make(this->width(), this->height())); |
| 93 | } |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 94 | } |
| 95 | |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 96 | SkString name() override { return SkASSERT(fName), SkString(fName); } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 97 | |
Hal Canary | 4124807 | 2019-07-11 16:32:53 -0400 | [diff] [blame] | 98 | bool onAnimate(double nanos) override { |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 99 | if (!fDom) { |
| 100 | return false; |
| 101 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 102 | |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 103 | --fAnimationLoop; |
| 104 | if (fAnimationLoop == 0) { |
| 105 | fAnimationLoop = kAnimationIterations; |
| 106 | switch (fState) { |
| 107 | case kZoomIn: |
| 108 | fState = kScroll; |
| 109 | fDelta = 0; |
| 110 | break; |
| 111 | case kScroll: |
| 112 | fState = kZoomOut; |
| 113 | fDelta = 2; |
| 114 | break; |
| 115 | case kZoomOut: |
| 116 | fState = kZoomIn; |
| 117 | fDelta = 1; |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | return true; |
| 122 | } |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 123 | }; |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 124 | } // namespace |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 125 | |
Hal Canary | d7639af | 2019-07-17 09:08:11 -0400 | [diff] [blame] | 126 | DEF_SAMPLE( return new AnimatedSVGSample("Cowboy.svg", "SampleCowboy"); ) |
Jim Van Verth | 712fe73 | 2017-09-25 16:53:49 -0400 | [diff] [blame] | 127 | |
Hal Canary | 0f66681 | 2018-03-22 15:21:12 -0400 | [diff] [blame] | 128 | #endif // SK_XML |