blob: 7c7dca1576e8ce1d9a4dbdaebe88501c46f4003d [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
csmartdalton8c679092017-03-27 12:32:29 -060019/**
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 */
28class PathFinderView : public SampleView, public SkCanvas {
29public:
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 Daltond4124bc2017-09-28 11:59:46 -060035 SkDebugf("invalid input file at \"%s\"\n", fFilename.c_str());
csmartdalton8c679092017-03-27 12:32:29 -060036 return;
37 }
Chris Daltond4124bc2017-09-28 11:59:46 -060038 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);
csmartdalton8c679092017-03-27 12:32:29 -060058 }
csmartdalton8c679092017-03-27 12:32:29 -060059 }
60
61 ~PathFinderView() override {}
62
63private:
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 }
97 this->inval(nullptr);
98 }
99 return true;
100 case 'x':
101 if (fPaths.count() > 1) {
102 int midpt = (fPaths.count() + 1) / 2;
103 fPathHistory.emplace(fPaths, fTossedPaths);
104 fTossedPaths.reset(fPaths.begin() + midpt, fPaths.count() - midpt);
105 fPaths.resize_back(midpt);
106 fTrail.push_back('x');
107 this->inval(nullptr);
108 }
109 return true;
110 case 'Z': {
111 if (!fPathHistory.empty()) {
112 fPaths = fPathHistory.top().first;
113 fTossedPaths = fPathHistory.top().second;
114 fPathHistory.pop();
115 char ch;
116 do {
117 ch = fTrail.back();
118 fTrail.pop_back();
119 } while (ch != 'x');
120 this->inval(nullptr);
121 }
122 return true;
123 }
124 case 'D':
125 SkDebugf("SampleApp --pathfinder %s", fFilename.c_str());
126 if (!fTrail.empty()) {
Chris Dalton1d014102017-10-18 10:57:02 -0600127 SkDebugf(" --keys ");
csmartdalton8c679092017-03-27 12:32:29 -0600128 for (char ch : fTrail) {
129 SkDebugf("%c", ch);
130 }
131 }
132 SkDebugf("\n");
133 for (const FoundPath& foundPath : fPaths) {
134 foundPath.fPath.dump();
135 }
136 return true;
137 }
138 return false;
139 }
140
141 void onDrawContent(SkCanvas* canvas) override {
142 for (const FoundPath& path : fPaths) {
143 SkAutoCanvasRestore acr(canvas, true);
144 canvas->concat(path.fViewMatrix);
145 canvas->drawPath(path.fPath, path.fPaint);
146 }
147 }
148
149 struct FoundPath {
150 SkPath fPath;
151 SkPaint fPaint;
152 SkMatrix fViewMatrix;
153 };
154
155 SkString fFilename;
156 SkTArray<FoundPath> fPaths;
157 SkTArray<FoundPath> fTossedPaths;
158 SkTArray<char> fTrail;
159
160 std::stack<std::pair<SkTArray<FoundPath>, SkTArray<FoundPath>>> fPathHistory;
161
162 typedef SampleView INHERITED;
163};
164
165SampleView* CreateSamplePathFinderView(const char filename[]) {
166 return new PathFinderView(filename);
167}