• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
11 #include "SkDOM.h"
12 #include "SkSVGDOM.h"
13 #include "SkOSPath.h"
14 #include "SkPath.h"
15 #include "SkPicture.h"
16 #include "SkStream.h"
17 #include <stack>
18 
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:
PathFinderView(const char name[]=nullptr)30     PathFinderView(const char name[] = nullptr)
31         : SkCanvas(4096, 4096, nullptr)
32         , fFilename(name) {
33         SkFILEStream stream(fFilename.c_str());
34         if (!stream.isValid()) {
35             SkDebugf("invalid input file at \"%s\"\n", fFilename.c_str());
36             return;
37         }
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);
58         }
59     }
60 
~PathFinderView()61     ~PathFinderView() override {}
62 
63 private:
64     // Called through SkPicture::playback during construction.
onDrawPath(const SkPath & path,const SkPaint & paint)65     void onDrawPath(const SkPath& path, const SkPaint& paint) override {
66         fPaths.push_back() = {path, paint, this->getTotalMatrix()};
67     }
68 
69     // overrides from SkEventSink
onQuery(SkEvent * evt)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 
handleKeystroke(SkUnichar key)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                 }
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');
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');
118                 }
119                 return true;
120             }
121             case 'D':
122                 SkDebugf("SampleApp --pathfinder %s", fFilename.c_str());
123                 if (!fTrail.empty()) {
124                     SkDebugf(" --keys ");
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 
onDrawContent(SkCanvas * canvas)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 
CreateSamplePathFinderView(const char filename[])162 SampleView* CreateSamplePathFinderView(const char filename[]) {
163     return new PathFinderView(filename);
164 }
165