Skip to content

Commit

Permalink
Merge pull request #5315 from andydotxyz/fix/5313
Browse files Browse the repository at this point in the history
Handle the negative offsets in viewport that Google started using
  • Loading branch information
andydotxyz authored Dec 18, 2024
2 parents c6d4727 + f5a64ef commit 0685580
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
11 changes: 10 additions & 1 deletion internal/svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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"`
Expand Down
8 changes: 7 additions & 1 deletion internal/svg/svg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions internal/svg/testdata/rects-negative.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0685580

Please sign in to comment.