-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
123 lines (115 loc) · 2.97 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package resvg_test
import (
"bytes"
"fmt"
"image"
"image/color"
"image/png"
"log"
"os"
"github.com/xo/resvg"
)
func Example() {
img, _, err := image.Decode(bytes.NewReader(svgData))
if err != nil {
log.Fatal(err)
}
b := img.Bounds()
fmt.Printf("width: %d height: %d\n", b.Max.X, b.Max.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
// Output:
// width: 400 height: 180
}
func Example_render() {
img, err := resvg.Render(svgData)
if err != nil {
log.Fatal(err)
}
b := img.Bounds()
fmt.Printf("width: %d height: %d\n", b.Max.X, b.Max.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect_render.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
// Output:
// width: 400 height: 180
}
func Example_background() {
img, err := resvg.Render(svgData, resvg.WithBackground(color.White))
if err != nil {
log.Fatal(err)
}
b := img.Bounds()
fmt.Printf("width: %d height: %d\n", b.Max.X, b.Max.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect_white.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
// Output:
// width: 400 height: 180
}
func Example_scaleBestFit() {
a, err := resvg.Render(svgData, resvg.WithScaleMode(resvg.ScaleBestFit), resvg.WithWidth(300))
if err != nil {
log.Fatal(err)
}
ab := a.Bounds()
fmt.Printf("width: %d height: %d\n", ab.Max.X, ab.Max.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, a); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect_bestfit_a.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
b, err := resvg.Render(svgData, resvg.WithScaleMode(resvg.ScaleBestFit), resvg.WithHeight(135))
if err != nil {
log.Fatal(err)
}
bb := b.Bounds()
fmt.Printf("width: %d height: %d\n", bb.Max.X, bb.Max.Y)
buf.Reset()
if err := png.Encode(buf, b); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect_bestfit_b.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
// Output:
// width: 300 height: 135
// width: 300 height: 135
}
func Example_distort() {
img, err := resvg.Render(svgData, resvg.WithWidth(200), resvg.WithHeight(700))
if err != nil {
log.Fatal(err)
}
b := img.Bounds()
fmt.Printf("width: %d height: %d\n", b.Max.X, b.Max.Y)
buf := new(bytes.Buffer)
if err := png.Encode(buf, img); err != nil {
log.Fatal(err)
}
if err := os.WriteFile("rect_distort.png", buf.Bytes(), 0o644); err != nil {
log.Fatal(err)
}
// Output:
// width: 200 height: 700
}
var svgData = []byte(`<?xml version="1.0" encoding="iso-8859-1"?>
<svg width="400" height="180" xmlns="http://www.w3.org/2000/svg" version="1.1">
<rect x="50" y="20" width="150" height="150" style="fill:blue;stroke:pink;stroke-width:5;fill-opacity:0.1;stroke-opacity:0.9" />
Sorry, your browser does not support inline SVG.
</svg>`)