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