blob: d53c40bb5ee33761910577b587d1d611648f7648 [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 }
csmartdalton8c679092017-03-27 12:32:29 -060097 }
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');
csmartdalton8c679092017-03-27 12:32:29 -0600106 }
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');
csmartdalton8c679092017-03-27 12:32:29 -0600118 }
119 return true;
120 }
121 case 'D':
122 SkDebugf("SampleApp --pathfinder %s", fFilename.c_str());
123 if (!fTrail.empty()) {
Chris Dalton1d014102017-10-18 10:57:02 -0600124 SkDebugf(" --keys ");
csmartdalton8c679092017-03-27 12:32:29 -0600125 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
162SampleView* CreateSamplePathFinderView(const char filename[]) {
163 return new PathFinderView(filename);
164}