瀏覽代碼

Update svgpan, add license, separate pprof-specific code. (#284)

The third_party svg directory has been a mix of pprof-specific code and
svgpan itself. This change moves the pprof-specific code out and adds a
license file. It also updates svgpan to 1.2.2 from
https://github.com/aleofreddi/svgpan while we are here. It also renames
the directory to svgpan to have all directories under third_party be
named precisely over the third party package they hold.
Alexey Alexandrov 7 年之前
父節點
當前提交
2bf1f9507e
共有 4 個文件被更改,包括 86 次插入53 次删除
  1. 1
    2
      internal/driver/commands.go
  2. 10
    9
      internal/driver/svg.go
  3. 27
    0
      third_party/svgpan/LICENSE
  4. 48
    42
      third_party/svgpan/svgpan.go

+ 1
- 2
internal/driver/commands.go 查看文件

28
 
28
 
29
 	"github.com/google/pprof/internal/plugin"
29
 	"github.com/google/pprof/internal/plugin"
30
 	"github.com/google/pprof/internal/report"
30
 	"github.com/google/pprof/internal/report"
31
-	"github.com/google/pprof/third_party/svg"
32
 )
31
 )
33
 
32
 
34
 // commands describes the commands accepted by pprof.
33
 // commands describes the commands accepted by pprof.
398
 		if err := generateSVG(input, baseSVG, ui); err != nil {
397
 		if err := generateSVG(input, baseSVG, ui); err != nil {
399
 			return err
398
 			return err
400
 		}
399
 		}
401
-		_, err := output.Write([]byte(svg.Massage(baseSVG.String())))
400
+		_, err := output.Write([]byte(massageSVG(baseSVG.String())))
402
 		return err
401
 		return err
403
 	}
402
 	}
404
 }
403
 }

third_party/svg/svg.go → internal/driver/svg.go 查看文件

12
 // See the License for the specific language governing permissions and
12
 // See the License for the specific language governing permissions and
13
 // limitations under the License.
13
 // limitations under the License.
14
 
14
 
15
-// Package svg provides tools related to handling of SVG files
16
-package svg
15
+package driver
17
 
16
 
18
 import (
17
 import (
19
 	"regexp"
18
 	"regexp"
20
 	"strings"
19
 	"strings"
20
+
21
+	"github.com/google/pprof/third_party/svgpan"
21
 )
22
 )
22
 
23
 
23
 var (
24
 var (
26
 	svgClose = regexp.MustCompile(`</svg>`)
27
 	svgClose = regexp.MustCompile(`</svg>`)
27
 )
28
 )
28
 
29
 
29
-// Massage enhances the SVG output from DOT to provide better
30
-// panning inside a web browser. It uses the SVGPan library, which is
31
-// embedded into the svgPanJS variable.
32
-func Massage(svg string) string {
30
+// massageSVG enhances the SVG output from DOT to provide better
31
+// panning inside a web browser. It uses the svgpan library, which is
32
+// embedded into the svgpan.JSSource variable.
33
+func massageSVG(svg string) string {
33
 	// Work around for dot bug which misses quoting some ampersands,
34
 	// Work around for dot bug which misses quoting some ampersands,
34
 	// resulting on unparsable SVG.
35
 	// resulting on unparsable SVG.
35
 	svg = strings.Replace(svg, "&;", "&amp;;", -1)
36
 	svg = strings.Replace(svg, "&;", "&amp;;", -1)
36
 
37
 
37
-	//Dot's SVG output is
38
+	// Dot's SVG output is
38
 	//
39
 	//
39
 	//    <svg width="___" height="___"
40
 	//    <svg width="___" height="___"
40
 	//     viewBox="___" xmlns=...>
41
 	//     viewBox="___" xmlns=...>
48
 	//    <svg width="100%" height="100%"
49
 	//    <svg width="100%" height="100%"
49
 	//     xmlns=...>
50
 	//     xmlns=...>
50
 
51
 
51
-	//    <script type="text/ecmascript"><![CDATA[` ..$(svgPanJS)... `]]></script>`
52
+	//    <script type="text/ecmascript"><![CDATA[` ..$(svgpan.JSSource)... `]]></script>`
52
 	//    <g id="viewport" transform="translate(0,0)">
53
 	//    <g id="viewport" transform="translate(0,0)">
53
 	//    <g id="graph0" transform="...">
54
 	//    <g id="graph0" transform="...">
54
 	//    ...
55
 	//    ...
64
 
65
 
65
 	if loc := graphID.FindStringIndex(svg); loc != nil {
66
 	if loc := graphID.FindStringIndex(svg); loc != nil {
66
 		svg = svg[:loc[0]] +
67
 		svg = svg[:loc[0]] +
67
-			`<script type="text/ecmascript"><![CDATA[` + string(svgPanJS) + `]]></script>` +
68
+			`<script type="text/ecmascript"><![CDATA[` + string(svgpan.JSSource) + `]]></script>` +
68
 			`<g id="viewport" transform="scale(0.5,0.5) translate(0,0)">` +
69
 			`<g id="viewport" transform="scale(0.5,0.5) translate(0,0)">` +
69
 			svg[loc[0]:]
70
 			svg[loc[0]:]
70
 	}
71
 	}

+ 27
- 0
third_party/svgpan/LICENSE 查看文件

1
+Copyright 2009-2017 Andrea Leofreddi <a.leofreddi@vleo.net>. All rights reserved.
2
+
3
+Redistribution and use in source and binary forms, with or without modification, are
4
+permitted provided that the following conditions are met:
5
+
6
+   1. Redistributions of source code must retain the above copyright
7
+      notice, this list of conditions and the following disclaimer.
8
+   2. Redistributions in binary form must reproduce the above copyright
9
+      notice, this list of conditions and the following disclaimer in the
10
+      documentation and/or other materials provided with the distribution.
11
+   3. Neither the name of the copyright holder nor the names of its
12
+      contributors may be used to endorse or promote products derived from
13
+      this software without specific prior written permission.
14
+
15
+THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
16
+OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17
+AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR
18
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21
+ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
22
+NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
+
25
+The views and conclusions contained in the software and documentation are those of the
26
+authors and should not be interpreted as representing official policies, either expressed
27
+or implied, of Andrea Leofreddi.

third_party/svg/svgpan.go → third_party/svgpan/svgpan.go 查看文件

1
 // SVG pan and zoom library.
1
 // SVG pan and zoom library.
2
 // See copyright notice in string constant below.
2
 // See copyright notice in string constant below.
3
 
3
 
4
-package svg
4
+package svgpan
5
 
5
 
6
-// https://www.cyberz.org/projects/SVGPan/SVGPan.js
6
+// https://github.com/aleofreddi/svgpan
7
 
7
 
8
-const svgPanJS = `
9
-/** 
10
- *  SVGPan library 1.2.1
8
+// JSSource returns the svgpan.js file
9
+const JSSource = `
10
+/**
11
+ *  SVGPan library 1.2.2
11
  * ======================
12
  * ======================
12
  *
13
  *
13
- * Given an unique existing element with id "viewport" (or when missing, the first g 
14
- * element), including the the library into any SVG adds the following capabilities:
14
+ * Given an unique existing element with id "viewport" (or when missing, the
15
+ * first g-element), including the the library into any SVG adds the following
16
+ * capabilities:
15
  *
17
  *
16
  *  - Mouse panning
18
  *  - Mouse panning
17
  *  - Mouse zooming (using the wheel)
19
  *  - Mouse zooming (using the wheel)
26
  *
28
  *
27
  * Releases:
29
  * Releases:
28
  *
30
  *
31
+ * 1.2.2, Tue Aug 30 17:21:56 CEST 2011, Andrea Leofreddi
32
+ *	- Fixed viewBox on root tag (#7)
33
+ *	- Improved zoom speed (#2)
34
+ *
29
  * 1.2.1, Mon Jul  4 00:33:18 CEST 2011, Andrea Leofreddi
35
  * 1.2.1, Mon Jul  4 00:33:18 CEST 2011, Andrea Leofreddi
30
  *	- Fixed a regression with mouse wheel (now working on Firefox 5)
36
  *	- Fixed a regression with mouse wheel (now working on Firefox 5)
31
  *	- Working with viewBox attribute (#4)
37
  *	- Working with viewBox attribute (#4)
43
  *
49
  *
44
  * This code is licensed under the following BSD license:
50
  * This code is licensed under the following BSD license:
45
  *
51
  *
46
- * Copyright 2009-2010 Andrea Leofreddi <a.leofreddi@itcharm.com>. All rights reserved.
47
- * 
52
+ * Copyright 2009-2017 Andrea Leofreddi <a.leofreddi@vleo.net>. All rights reserved.
53
+ *
48
  * Redistribution and use in source and binary forms, with or without modification, are
54
  * Redistribution and use in source and binary forms, with or without modification, are
49
  * permitted provided that the following conditions are met:
55
  * permitted provided that the following conditions are met:
50
- * 
51
- *    1. Redistributions of source code must retain the above copyright notice, this list of
52
- *       conditions and the following disclaimer.
53
- * 
54
- *    2. Redistributions in binary form must reproduce the above copyright notice, this list
55
- *       of conditions and the following disclaimer in the documentation and/or other materials
56
- *       provided with the distribution.
57
- * 
58
- * THIS SOFTWARE IS PROVIDED BY Andrea Leofreddi ` + "``AS IS''" + ` AND ANY EXPRESS OR IMPLIED
59
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
60
- * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Andrea Leofreddi OR
56
+ *
57
+ *    1. Redistributions of source code must retain the above copyright
58
+ *       notice, this list of conditions and the following disclaimer.
59
+ *    2. Redistributions in binary form must reproduce the above copyright
60
+ *       notice, this list of conditions and the following disclaimer in the
61
+ *       documentation and/or other materials provided with the distribution.
62
+ *    3. Neither the name of the copyright holder nor the names of its
63
+ *       contributors may be used to endorse or promote products derived from
64
+ *       this software without specific prior written permission.
65
+ *
66
+ * THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY EXPRESS
67
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
68
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS OR
61
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
69
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
70
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
63
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
71
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
64
  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
72
  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
65
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
73
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
66
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67
- * 
75
+ *
68
  * The views and conclusions contained in the software and documentation are those of the
76
  * The views and conclusions contained in the software and documentation are those of the
69
  * authors and should not be interpreted as representing official policies, either expressed
77
  * authors and should not be interpreted as representing official policies, either expressed
70
  * or implied, of Andrea Leofreddi.
78
  * or implied, of Andrea Leofreddi.
72
 
80
 
73
 "use strict";
81
 "use strict";
74
 
82
 
75
-/// CONFIGURATION 
83
+/// CONFIGURATION
76
 /// ====>
84
 /// ====>
77
 
85
 
78
 var enablePan = 1; // 1 or 0: enable or disable panning (default enabled)
86
 var enablePan = 1; // 1 or 0: enable or disable panning (default enabled)
79
 var enableZoom = 1; // 1 or 0: enable or disable zooming (default enabled)
87
 var enableZoom = 1; // 1 or 0: enable or disable zooming (default enabled)
80
 var enableDrag = 0; // 1 or 0: enable or disable dragging (default disabled)
88
 var enableDrag = 0; // 1 or 0: enable or disable dragging (default disabled)
89
+var zoomScale = 0.2; // Zoom sensitivity
81
 
90
 
82
 /// <====
91
 /// <====
83
-/// END OF CONFIGURATION 
92
+/// END OF CONFIGURATION
84
 
93
 
85
 var root = document.documentElement;
94
 var root = document.documentElement;
86
 
95
 
87
-var state = 'none', svgRoot, stateTarget, stateOrigin, stateTf;
96
+var state = 'none', svgRoot = null, stateTarget, stateOrigin, stateTf;
88
 
97
 
89
 setupHandlers(root);
98
 setupHandlers(root);
90
 
99
 
109
  * Retrieves the root element for SVG manipulation. The element is then cached into the svgRoot global variable.
118
  * Retrieves the root element for SVG manipulation. The element is then cached into the svgRoot global variable.
110
  */
119
  */
111
 function getRoot(root) {
120
 function getRoot(root) {
112
-	if(typeof(svgRoot) == "undefined") {
113
-		var g = null;
114
-
115
-		g = root.getElementById("viewport");
121
+	if(svgRoot == null) {
122
+		var r = root.getElementById("viewport") ? root.getElementById("viewport") : root.documentElement, t = r;
116
 
123
 
117
-		if(g == null)
118
-			g = root.getElementsByTagName('g')[0];
124
+		while(t != root) {
125
+			if(t.getAttribute("viewBox")) {
126
+				setCTM(r, t.getCTM());
119
 
127
 
120
-		if(g == null)
121
-			alert('Unable to obtain SVG root element');
128
+				t.removeAttribute("viewBox");
129
+			}
122
 
130
 
123
-		setCTM(g, g.getCTM());
131
+			t = t.parentNode;
132
+		}
124
 
133
 
125
-		g.removeAttribute("viewBox");
126
-
127
-		svgRoot = g;
134
+		svgRoot = r;
128
 	}
135
 	}
129
 
136
 
130
 	return svgRoot;
137
 	return svgRoot;
185
 	var delta;
192
 	var delta;
186
 
193
 
187
 	if(evt.wheelDelta)
194
 	if(evt.wheelDelta)
188
-		delta = evt.wheelDelta / 3600; // Chrome/Safari
195
+		delta = evt.wheelDelta / 360; // Chrome/Safari
189
 	else
196
 	else
190
-		delta = evt.detail / -90; // Mozilla
197
+		delta = evt.detail / -9; // Mozilla
191
 
198
 
192
-	var z = 1 + delta; // Zoom factor: 0.9/1.1
199
+	var z = Math.pow(1 + zoomScale, delta);
193
 
200
 
194
 	var g = getRoot(svgDoc);
201
 	var g = getRoot(svgDoc);
195
 	
202
 	
250
 	var g = getRoot(svgDoc);
257
 	var g = getRoot(svgDoc);
251
 
258
 
252
 	if(
259
 	if(
253
-		evt.target.tagName == "svg" 
254
-		|| !enableDrag // Pan anyway when drag is disabled and the user clicked on an element 
260
+		evt.target.tagName == "svg"
261
+		|| !enableDrag // Pan anyway when drag is disabled and the user clicked on an element
255
 	) {
262
 	) {
256
 		// Pan mode
263
 		// Pan mode
257
 		state = 'pan';
264
 		state = 'pan';
287
 		state = '';
294
 		state = '';
288
 	}
295
 	}
289
 }
296
 }
290
-
291
 `
297
 `