blob: 18d200bad13e553cc4ed2c5bdb5d32ffe516307e [file] [log] [blame]
csmartdalton8c679092017-03-27 12:32:29 -06001/*
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 Daltond4124bc2017-09-28 11:59:46 -060011#include "SkDOM.h"
12#include "SkSVGDOM.h"
csmartdalton8c679092017-03-27 12:32:29 -060013#include "SkOSPath.h"
14#include "SkPath.h"
15#include "SkPicture.h"
16#include "SkStream.h"
17#include <stack>
18
19DEFINE_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 */
30class PathFinderView : public SampleView, public SkCanvas {
31public:
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 Daltond4124bc2017-09-28 11:59:46 -060037 SkDebugf("invalid input file at \"%s\"\n", fFilename.c_str());
csmartdalton8c679092017-03-27 12:32:29 -060038 return;
39 }
Chris Daltond4124bc2017-09-28 11:59:46 -060040 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);
csmartdalton8c679092017-03-27 12:32:29 -060060 }
csmartdalton8c679092017-03-27 12:32:29 -060061 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
71private:
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
173SampleView* CreateSamplePathFinderView(const char filename[]) {
174 return new PathFinderView(filename);
175}