diff --git a/internal/svg/svg.go b/internal/svg/svg.go index 9e78efc3cb..a85d176689 100644 --- a/internal/svg/svg.go +++ b/internal/svg/svg.go @@ -80,7 +80,8 @@ func (d *Decoder) Draw(width, height int) (*image.NRGBA, error) { imgH = int(float32(width) / config.Aspect) } - d.icon.SetTarget(0, 0, float64(imgW), float64(imgH)) + x, y := svgOffset(d.icon, imgW, imgH) + d.icon.SetTarget(x, y, float64(imgW), float64(imgH)) img := image.NewNRGBA(image.Rect(0, 0, imgW, imgH)) scanner := rasterx.NewScannerGV(config.Width, config.Height, img, img.Bounds()) @@ -115,6 +116,14 @@ func IsResourceSVG(res fyne.Resource) bool { return false } +func svgOffset(icon *oksvg.SvgIcon, _, height int) (x, y float64) { + if icon.ViewBox.Y < 0 { // adjust so our positive offset calculations work + y = icon.ViewBox.Y + (-icon.ViewBox.Y/icon.ViewBox.H)*float64(height) + } + + return 0, y +} + // svg holds the unmarshaled XML from a Scalable Vector Graphic type svg struct { XMLName xml.Name `xml:"svg"` diff --git a/internal/svg/svg_test.go b/internal/svg/svg_test.go index b7d9387b43..2d244b7b12 100644 --- a/internal/svg/svg_test.go +++ b/internal/svg/svg_test.go @@ -44,6 +44,11 @@ func TestColorize(t *testing.T) { color: color.NRGBA{R: 100, G: 100, B: 100, A: 200}, wantImage: "colorized/rects.png", }, + "negative rects": { + svgFile: "rects-negative.svg", + color: color.NRGBA{R: 100, G: 100, B: 100, A: 200}, + wantImage: "colorized/rects.png", + }, "group of paths": { svgFile: "check_GroupPaths.svg", color: color.NRGBA{R: 100, G: 100, A: 100}, @@ -195,7 +200,8 @@ func helperDrawSVG(t *testing.T, data []byte) image.Image { width := int(icon.ViewBox.W) * 2 height := int(icon.ViewBox.H) * 2 - icon.SetTarget(0, 0, float64(width), float64(height)) + x, y := svgOffset(icon, width, height) + icon.SetTarget(x, y, float64(width), float64(height)) img := image.NewNRGBA(image.Rect(0, 0, width, height)) scanner := rasterx.NewScannerGV(width, height, img, img.Bounds()) raster := rasterx.NewDasher(width, height, scanner) diff --git a/internal/svg/testdata/rects-negative.svg b/internal/svg/testdata/rects-negative.svg new file mode 100644 index 0000000000..b759e71d57 --- /dev/null +++ b/internal/svg/testdata/rects-negative.svg @@ -0,0 +1,4 @@ + + + +