You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

81 lines
2.2 KiB

  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package driver
  15. import (
  16. "regexp"
  17. "strings"
  18. "github.com/google/pprof/third_party/svgpan"
  19. )
  20. var (
  21. viewBox = regexp.MustCompile(`<svg\s*width="[^"]+"\s*height="[^"]+"\s*viewBox="[^"]+"`)
  22. graphID = regexp.MustCompile(`<g id="graph\d"`)
  23. svgClose = regexp.MustCompile(`</svg>`)
  24. )
  25. // massageSVG enhances the SVG output from DOT to provide better
  26. // panning inside a web browser. It uses the svgpan library, which is
  27. // embedded into the svgpan.JSSource variable.
  28. func massageSVG(svg string) string {
  29. // Work around for dot bug which misses quoting some ampersands,
  30. // resulting on unparsable SVG.
  31. svg = strings.Replace(svg, "&;", "&amp;;", -1)
  32. // Dot's SVG output is
  33. //
  34. // <svg width="___" height="___"
  35. // viewBox="___" xmlns=...>
  36. // <g id="graph0" transform="...">
  37. // ...
  38. // </g>
  39. // </svg>
  40. //
  41. // Change it to
  42. //
  43. // <svg width="100%" height="100%"
  44. // xmlns=...>
  45. // <script type="text/ecmascript"><![CDATA[` ..$(svgpan.JSSource)... `]]></script>`
  46. // <g id="viewport" transform="translate(0,0)">
  47. // <g id="graph0" transform="...">
  48. // ...
  49. // </g>
  50. // </g>
  51. // </svg>
  52. if loc := viewBox.FindStringIndex(svg); loc != nil {
  53. svg = svg[:loc[0]] +
  54. `<svg width="100%" height="100%"` +
  55. svg[loc[1]:]
  56. }
  57. if loc := graphID.FindStringIndex(svg); loc != nil {
  58. svg = svg[:loc[0]] +
  59. `<script type="text/ecmascript"><![CDATA[` + string(svgpan.JSSource) + `]]></script>` +
  60. `<g id="viewport" transform="scale(0.5,0.5) translate(0,0)">` +
  61. svg[loc[0]:]
  62. }
  63. if loc := svgClose.FindStringIndex(svg); loc != nil {
  64. svg = svg[:loc[0]] +
  65. `</g>` +
  66. svg[loc[0]:]
  67. }
  68. return svg
  69. }