csmartdalton | 8c67909 | 2017-03-27 12:32:29 -0600 | [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 | |
| 8 | #include "SampleCode.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkCommandLineFlags.h" |
Chris Dalton | d4124bc | 2017-09-28 11:59:46 -0600 | [diff] [blame] | 11 | #include "SkDOM.h" |
| 12 | #include "SkSVGDOM.h" |
csmartdalton | 8c67909 | 2017-03-27 12:32:29 -0600 | [diff] [blame] | 13 | #include "SkOSPath.h" |
| 14 | #include "SkPath.h" |
| 15 | #include "SkPicture.h" |
| 16 | #include "SkStream.h" |
| 17 | #include <stack> |
| 18 | |
csmartdalton | 8c67909 | 2017-03-27 12:32:29 -0600 | [diff] [blame] | 19 | /** |
| 20 | * This is a simple utility designed to extract the paths from an SKP file and then isolate a single |
| 21 | * one of them. Use the 'x' and 'X' keys to guide a binary search: |
| 22 | * |
| 23 | * 'x': Throw out half the paths. |
| 24 | * 'X': Toggle which half gets tossed and which half is kept. |
| 25 | * 'Z': Back up one level. |
| 26 | * 'D': Dump the path. |
| 27 | */ |
| 28 | class PathFinderView : public SampleView, public SkCanvas { |
| 29 | public: |
| 30 | PathFinderView(const char name[] = nullptr) |
| 31 | : SkCanvas(4096, 4096, nullptr) |
| 32 | , fFilename(name) { |
| 33 | SkFILEStream stream(fFilename.c_str()); |
| 34 | if (!stream.isValid()) { |
Chris Dalton | d4124bc | 2017-09-28 11:59:46 -0600 | [diff] [blame] | 35 | SkDebugf("invalid input file at \"%s\"\n", fFilename.c_str()); |
csmartdalton | 8c67909 | 2017-03-27 12:32:29 -0600 | [diff] [blame] | 36 | return; |
| 37 | } |
Chris Dalton | d4124bc | 2017-09-28 11:59:46 -0600 | [diff] [blame] | 38 | if (fFilename.endsWith(".svg")) { |
| 39 | SkDOM xml; |
| 40 | if (!xml.build(stream)) { |
| 41 | SkDebugf("XML parsing failed: \"%s\"\n", fFilename.c_str()); |
| 42 | return; |
| 43 | } |
| 44 | sk_sp<SkSVGDOM> svg = SkSVGDOM::MakeFromDOM(xml); |
| 45 | if (!svg) { |
| 46 | SkDebugf("couldn't load svg at \"%s\"\n", fFilename.c_str()); |
| 47 | return; |
| 48 | } |
| 49 | svg->setContainerSize(SkSize::Make(500, 500)); |
| 50 | svg->render(this); |
| 51 | } else { |
| 52 | sk_sp<SkPicture> pic = SkPicture::MakeFromStream(&stream); |
| 53 | if (!pic) { |
| 54 | SkDebugf("couldn't load skp at \"%s\"\n", fFilename.c_str()); |
| 55 | return; |
| 56 | } |
| 57 | pic->playback(this); |
csmartdalton | 8c67909 | 2017-03-27 12:32:29 -0600 | [diff] [blame] | 58 | } |
csmartdalton | 8c67909 | 2017-03-27 12:32:29 -0600 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | ~PathFinderView() override {} |
| 62 | |
| 63 | private: |
| 64 | // Called through SkPicture::playback during construction. |
| 65 | void onDrawPath(const SkPath& path, const SkPaint& paint) override { |
| 66 | fPaths.push_back() = {path, paint, this->getTotalMatrix()}; |
| 67 | } |
| 68 | |
| 69 | // overrides from SkEventSink |
| 70 | bool onQuery(SkEvent* evt) override { |
| 71 | if (SampleCode::TitleQ(*evt)) { |
| 72 | SkString name("PATHFINDER:"); |
| 73 | const char* basename = strrchr(fFilename.c_str(), SkOSPath::SEPARATOR); |
| 74 | name.append(basename ? basename+1: fFilename.c_str()); |
| 75 | SampleCode::TitleR(evt, name.c_str()); |
| 76 | return true; |
| 77 | } |
| 78 | SkUnichar key; |
| 79 | if (SampleCode::CharQ(*evt, &key)) { |
| 80 | if (this->handleKeystroke(key)) { |
| 81 | return true; |
| 82 | } |
| 83 | } |
| 84 | return this->INHERITED::onQuery(evt); |
| 85 | } |
| 86 | |
| 87 | bool handleKeystroke(SkUnichar key) { |
| 88 | switch (key) { |
| 89 | case 'X': |
| 90 | if (!fTossedPaths.empty()) { |
| 91 | SkTSwap(fPaths, fTossedPaths); |
| 92 | if ('X' == fTrail.back()) { |
| 93 | fTrail.pop_back(); |
| 94 | } else { |
| 95 | fTrail.push_back('X'); |
| 96 | } |
csmartdalton | 8c67909 | 2017-03-27 12:32:29 -0600 | [diff] [blame] | 97 | } |
| 98 | return true; |
| 99 | case 'x': |
| 100 | if (fPaths.count() > 1) { |
| 101 | int midpt = (fPaths.count() + 1) / 2; |
| 102 | fPathHistory.emplace(fPaths, fTossedPaths); |
| 103 | fTossedPaths.reset(fPaths.begin() + midpt, fPaths.count() - midpt); |
| 104 | fPaths.resize_back(midpt); |
| 105 | fTrail.push_back('x'); |
csmartdalton | 8c67909 | 2017-03-27 12:32:29 -0600 | [diff] [blame] | 106 | } |
| 107 | return true; |
| 108 | case 'Z': { |
| 109 | if (!fPathHistory.empty()) { |
| 110 | fPaths = fPathHistory.top().first; |
| 111 | fTossedPaths = fPathHistory.top().second; |
| 112 | fPathHistory.pop(); |
| 113 | char ch; |
| 114 | do { |
| 115 | ch = fTrail.back(); |
| 116 | fTrail.pop_back(); |
| 117 | } while (ch != 'x'); |
csmartdalton | 8c67909 | 2017-03-27 12:32:29 -0600 | [diff] [blame] | 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | case 'D': |
| 122 | SkDebugf("SampleApp --pathfinder %s", fFilename.c_str()); |
| 123 | if (!fTrail.empty()) { |
Chris Dalton | 1d01410 | 2017-10-18 10:57:02 -0600 | [diff] [blame] | 124 | SkDebugf(" --keys "); |
csmartdalton | 8c67909 | 2017-03-27 12:32:29 -0600 | [diff] [blame] | 125 | for (char ch : fTrail) { |
| 126 | SkDebugf("%c", ch); |
| 127 | } |
| 128 | } |
| 129 | SkDebugf("\n"); |
| 130 | for (const FoundPath& foundPath : fPaths) { |
| 131 | foundPath.fPath.dump(); |
| 132 | } |
| 133 | return true; |
| 134 | } |
| 135 | return false; |
| 136 | } |
| 137 | |
| 138 | void onDrawContent(SkCanvas* canvas) override { |
| 139 | for (const FoundPath& path : fPaths) { |
| 140 | SkAutoCanvasRestore acr(canvas, true); |
| 141 | canvas->concat(path.fViewMatrix); |
| 142 | canvas->drawPath(path.fPath, path.fPaint); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | struct FoundPath { |
| 147 | SkPath fPath; |
| 148 | SkPaint fPaint; |
| 149 | SkMatrix fViewMatrix; |
| 150 | }; |
| 151 | |
| 152 | SkString fFilename; |
| 153 | SkTArray<FoundPath> fPaths; |
| 154 | SkTArray<FoundPath> fTossedPaths; |
| 155 | SkTArray<char> fTrail; |
| 156 | |
| 157 | std::stack<std::pair<SkTArray<FoundPath>, SkTArray<FoundPath>>> fPathHistory; |
| 158 | |
| 159 | typedef SampleView INHERITED; |
| 160 | }; |
| 161 | |
| 162 | SampleView* CreateSamplePathFinderView(const char filename[]) { |
| 163 | return new PathFinderView(filename); |
| 164 | } |