Skip to content

Commit

Permalink
Adding image.RegisterFormat hook
Browse files Browse the repository at this point in the history
  • Loading branch information
kenshaw committed Feb 2, 2024
1 parent 0e9bbec commit aafcded
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 73 deletions.
40 changes: 39 additions & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package resvg_test
import (
"bytes"
"fmt"
"image"
"image/color"
"image/png"
"log"
"os"
Expand All @@ -11,7 +13,7 @@ import (
)

func Example() {
img, err := resvg.Render(svgData)
img, _, err := image.Decode(bytes.NewReader(svgData))
if err != nil {
log.Fatal(err)
}
Expand All @@ -28,6 +30,42 @@ func Example() {
// 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", 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", 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
}

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" />
Expand Down
Loading

0 comments on commit aafcded

Please sign in to comment.