golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/cmd/vendor/github.com/google/pprof/third_party/svg/svg.go (about)

     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  
    15  // Package svg provides tools related to handling of SVG files
    16  package svg
    17  
    18  import (
    19  	"regexp"
    20  	"strings"
    21  )
    22  
    23  var (
    24  	viewBox  = regexp.MustCompile(`<svg\s*width="[^"]+"\s*height="[^"]+"\s*viewBox="[^"]+"`)
    25  	graphID  = regexp.MustCompile(`<g id="graph\d"`)
    26  	svgClose = regexp.MustCompile(`</svg>`)
    27  )
    28  
    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 {
    33  	// Work around for dot bug which misses quoting some ampersands,
    34  	// resulting on unparsable SVG.
    35  	svg = strings.Replace(svg, "&;", "&amp;;", -1)
    36  
    37  	//Dot's SVG output is
    38  	//
    39  	//    <svg width="___" height="___"
    40  	//     viewBox="___" xmlns=...>
    41  	//    <g id="graph0" transform="...">
    42  	//    ...
    43  	//    </g>
    44  	//    </svg>
    45  	//
    46  	// Change it to
    47  	//
    48  	//    <svg width="100%" height="100%"
    49  	//     xmlns=...>
    50  
    51  	//    <script type="text/ecmascript"><![CDATA[` ..$(svgPanJS)... `]]></script>`
    52  	//    <g id="viewport" transform="translate(0,0)">
    53  	//    <g id="graph0" transform="...">
    54  	//    ...
    55  	//    </g>
    56  	//    </g>
    57  	//    </svg>
    58  
    59  	if loc := viewBox.FindStringIndex(svg); loc != nil {
    60  		svg = svg[:loc[0]] +
    61  			`<svg width="100%" height="100%"` +
    62  			svg[loc[1]:]
    63  	}
    64  
    65  	if loc := graphID.FindStringIndex(svg); loc != nil {
    66  		svg = svg[:loc[0]] +
    67  			`<script type="text/ecmascript"><![CDATA[` + string(svgPanJS) + `]]></script>` +
    68  			`<g id="viewport" transform="scale(0.5,0.5) translate(0,0)">` +
    69  			svg[loc[0]:]
    70  	}
    71  
    72  	if loc := svgClose.FindStringIndex(svg); loc != nil {
    73  		svg = svg[:loc[0]] +
    74  			`</g>` +
    75  			svg[loc[0]:]
    76  	}
    77  
    78  	return svg
    79  }