Ver código fonte

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 anos atrás
pai
commit
2bf1f9507e

+ 1
- 2
internal/driver/commands.go Ver arquivo

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

third_party/svg/svg.go → internal/driver/svg.go Ver arquivo

@@ -12,12 +12,13 @@
12 12
 // See the License for the specific language governing permissions and
13 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 17
 import (
19 18
 	"regexp"
20 19
 	"strings"
20
+
21
+	"github.com/google/pprof/third_party/svgpan"
21 22
 )
22 23
 
23 24
 var (
@@ -26,15 +27,15 @@ var (
26 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 34
 	// Work around for dot bug which misses quoting some ampersands,
34 35
 	// resulting on unparsable SVG.
35 36
 	svg = strings.Replace(svg, "&;", "&amp;;", -1)
36 37
 
37
-	//Dot's SVG output is
38
+	// Dot's SVG output is
38 39
 	//
39 40
 	//    <svg width="___" height="___"
40 41
 	//     viewBox="___" xmlns=...>
@@ -48,7 +49,7 @@ func Massage(svg string) string {
48 49
 	//    <svg width="100%" height="100%"
49 50
 	//     xmlns=...>
50 51
 
51
-	//    <script type="text/ecmascript"><![CDATA[` ..$(svgPanJS)... `]]></script>`
52
+	//    <script type="text/ecmascript"><![CDATA[` ..$(svgpan.JSSource)... `]]></script>`
52 53
 	//    <g id="viewport" transform="translate(0,0)">
53 54
 	//    <g id="graph0" transform="...">
54 55
 	//    ...
@@ -64,7 +65,7 @@ func Massage(svg string) string {
64 65
 
65 66
 	if loc := graphID.FindStringIndex(svg); loc != nil {
66 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 69
 			`<g id="viewport" transform="scale(0.5,0.5) translate(0,0)">` +
69 70
 			svg[loc[0]:]
70 71
 	}

+ 27
- 0
third_party/svgpan/LICENSE Ver arquivo

@@ -0,0 +1,27 @@
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 Ver arquivo

@@ -1,17 +1,19 @@
1 1
 // SVG pan and zoom library.
2 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 18
  *  - Mouse panning
17 19
  *  - Mouse zooming (using the wheel)
@@ -26,6 +28,10 @@ const svgPanJS = `
26 28
  *
27 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 35
  * 1.2.1, Mon Jul  4 00:33:18 CEST 2011, Andrea Leofreddi
30 36
  *	- Fixed a regression with mouse wheel (now working on Firefox 5)
31 37
  *	- Working with viewBox attribute (#4)
@@ -43,28 +49,30 @@ const svgPanJS = `
43 49
  *
44 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 54
  * Redistribution and use in source and binary forms, with or without modification, are
49 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 69
  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
62 70
  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
63 71
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
64 72
  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
65 73
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
66 74
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67
- * 
75
+ *
68 76
  * The views and conclusions contained in the software and documentation are those of the
69 77
  * authors and should not be interpreted as representing official policies, either expressed
70 78
  * or implied, of Andrea Leofreddi.
@@ -72,19 +80,20 @@ const svgPanJS = `
72 80
 
73 81
 "use strict";
74 82
 
75
-/// CONFIGURATION 
83
+/// CONFIGURATION
76 84
 /// ====>
77 85
 
78 86
 var enablePan = 1; // 1 or 0: enable or disable panning (default enabled)
79 87
 var enableZoom = 1; // 1 or 0: enable or disable zooming (default enabled)
80 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 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 98
 setupHandlers(root);
90 99
 
@@ -109,22 +118,20 @@ function setupHandlers(root){
109 118
  * Retrieves the root element for SVG manipulation. The element is then cached into the svgRoot global variable.
110 119
  */
111 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 137
 	return svgRoot;
@@ -185,11 +192,11 @@ function handleMouseWheel(evt) {
185 192
 	var delta;
186 193
 
187 194
 	if(evt.wheelDelta)
188
-		delta = evt.wheelDelta / 3600; // Chrome/Safari
195
+		delta = evt.wheelDelta / 360; // Chrome/Safari
189 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 201
 	var g = getRoot(svgDoc);
195 202
 	
@@ -250,8 +257,8 @@ function handleMouseDown(evt) {
250 257
 	var g = getRoot(svgDoc);
251 258
 
252 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 263
 		// Pan mode
257 264
 		state = 'pan';
@@ -287,5 +294,4 @@ function handleMouseUp(evt) {
287 294
 		state = '';
288 295
 	}
289 296
 }
290
-
291 297
 `