From 887a8a1e1008c9d1cec22c88d010f89badc2523c Mon Sep 17 00:00:00 2001 From: Jonathan Daugherty Date: Sat, 20 May 2017 08:13:47 -0700 Subject: [PATCH] translateX/Y: fix negative translations This change makes it so that translating an image negatively will return an empty image if the negative translation completely hides the input image. Previously we'd just return an image with a negative height, which not only violates the requirement that all images have height no less than zero, but it caused downstream bugs in mutable vector accesses due to negative vector indices. --- src/Graphics/Vty/Image.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Graphics/Vty/Image.hs b/src/Graphics/Vty/Image.hs index 2e0f206..1fb90aa 100644 --- a/src/Graphics/Vty/Image.hs +++ b/src/Graphics/Vty/Image.hs @@ -242,6 +242,7 @@ translate x y i = translateX x (translateY y i) -- | Translates an image by padding or cropping its left side. translateX :: Int -> Image -> Image translateX x i + | x < 0 && (abs x > imageWidth i) = emptyImage | x < 0 = let s = abs x in CropLeft i s (imageWidth i - s) (imageHeight i) | x == 0 = i | otherwise = let h = imageHeight i in HorizJoin (BGFill x h) i (imageWidth i + x) h @@ -249,6 +250,7 @@ translateX x i -- | Translates an image by padding or cropping its top. translateY :: Int -> Image -> Image translateY y i + | y < 0 && (abs y > imageHeight i) = emptyImage | y < 0 = let s = abs y in CropTop i s (imageWidth i) (imageHeight i - s) | y == 0 = i | otherwise = let w = imageWidth i in VertJoin (BGFill w y) i w (imageHeight i + y)