improve positioning

This commit is contained in:
Celogeek 2023-04-22 10:28:53 +02:00
parent 4a43760535
commit b27a826cd2
Signed by: celogeek
SSH Key Fingerprint: SHA256:njNJLzoLQdbV9PC6ehcruRb0QnEgxABoCYZ+0+aUIYc

View File

@ -24,20 +24,32 @@ type positionFilter struct {
} }
func (p *positionFilter) Bounds(srcBounds image.Rectangle) image.Rectangle { func (p *positionFilter) Bounds(srcBounds image.Rectangle) image.Rectangle {
return image.Rect(0, 0, p.viewWidth, p.viewHeight) w, h := p.viewWidth, p.viewHeight
srcw, srch := srcBounds.Dx(), srcBounds.Dy()
if w <= 0 || h <= 0 || srcw <= 0 || srch <= 0 {
return image.Rect(0, 0, 0, 0)
}
return image.Rect(0, 0, w, h)
} }
func (p *positionFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) { func (p *positionFilter) Draw(dst draw.Image, src image.Image, options *gift.Options) {
if dst.Bounds().Dx() == 0 || dst.Bounds().Dy() == 0 {
return
}
draw.Draw(dst, dst.Bounds(), image.White, dst.Bounds().Min, draw.Over) draw.Draw(dst, dst.Bounds(), image.White, dst.Bounds().Min, draw.Over)
srcBounds := src.Bounds() srcBounds := src.Bounds()
left, top := (p.viewWidth-srcBounds.Dx())/2, (p.viewHeight-srcBounds.Dy())/2 left, top := 0, (dst.Bounds().Dy()-srcBounds.Dy())/2
if p.align == PositionLeft {
left = 0 if p.align == PositionCenter {
left = (dst.Bounds().Dx() - srcBounds.Dx()) / 2
} }
if p.align == PositionRight { if p.align == PositionRight {
left = p.viewWidth - srcBounds.Dx() left = dst.Bounds().Dx() - srcBounds.Dx()
} }
draw.Draw( draw.Draw(
@ -45,8 +57,8 @@ func (p *positionFilter) Draw(dst draw.Image, src image.Image, options *gift.Opt
image.Rect( image.Rect(
left, left,
top, top,
p.viewWidth, dst.Bounds().Dx(),
p.viewHeight, dst.Bounds().Dy(),
), ),
src, src,
srcBounds.Min, srcBounds.Min,