1-- Copyright 2009-2010 Corey O'Connor
2{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE NamedFieldPuns #-}
4{-# LANGUAGE DisambiguateRecordFields #-}
5module Graphics.Vty.Image
49import Graphics.Vty.Attributes
50import Graphics.Vty.Image.Internal
51import Graphics.Text.Width
53import qualified Data.ByteString as B
54import qualified Data.ByteString.Lazy as BL
55import qualified Data.Text as T
56import qualified Data.Text.Encoding as T
57import qualified Data.Text.Lazy as TL
58import qualified Data.Text.Lazy.Encoding as TL
61-- | Named alias for a Int pair
62type DisplayRegion = (Int,Int)
64regionWidth :: DisplayRegion -> Int
67regionHeight :: DisplayRegion -> Int
73-- | An area of the picture's bacground (See Background) of w columns
75backgroundFill :: Int -> Int -> Image
79 | otherwise = BGFill w h
81-- | Combines two images horizontally. Alias for horizJoin
84(<|>) :: Image -> Image -> Image
87-- | Combines two images vertically. Alias for vertJoin
90(<->) :: Image -> Image -> Image
93-- | Compose any number of images horizontally.
94horizCat :: [Image] -> Image
95horizCat = foldr horizJoin EmptyImage
97-- | Compose any number of images vertically.
98vertCat :: [Image] -> Image
99vertCat = foldr vertJoin EmptyImage
101-- | A Data.Text.Lazy value
102text :: Attr -> TL.Text -> Image
103text a txt = let displayWidth = safeWcswidth (TL.unpack txt)
104 in HorizText a txt displayWidth (fromIntegral $! TL.length txt)
106-- | A Data.Text value
107text' :: Attr -> T.Text -> Image
108text' a txt = let displayWidth = safeWcswidth (T.unpack txt)
109 in HorizText a (TL.fromStrict txt) displayWidth (T.length txt)
111-- | An image of a single character. This is a standard Haskell 31-bit
112-- character assumed to be in the ISO-10646 encoding.
113char :: Attr -> Char -> Image
115 let displayWidth = safeWcwidth c
116 in HorizText a (TL.singleton c) displayWidth 1
118-- | A string of characters layed out on a single row with the same
119-- display attribute. The string is assumed to be a sequence of
120-- ISO-10646 characters.
122-- Note: depending on how the Haskell compiler represents string
123-- literals a string literal in a UTF-8 encoded source file, for
124-- example, may be represented as a ISO-10646 string. That is, I think,
125-- the case with GHC 6.10. This means, for the most part, you don't need
126-- to worry about the encoding format when outputting string literals.
127-- Just provide the string literal directly to iso10646String or string.
128iso10646String :: Attr -> String -> Image
129iso10646String a str =
130 let displayWidth = safeWcswidth str
131 in HorizText a (TL.pack str) displayWidth (length str)
133-- | Alias for iso10646String. Since the usual case is that a literal
134-- string like "foo" is represented internally as a list of ISO 10646 31
137-- Note: Keep in mind that GHC will compile source encoded as UTF-8
138-- but the literal strings, while UTF-8 encoded in the source, will be
139-- transcoded to a ISO 10646 31 bit characters runtime representation.
140string :: Attr -> String -> Image
141string = iso10646String
143-- | A string of characters layed out on a single row. The input is
144-- assumed to be the bytes for UTF-8 encoded text.
145utf8String :: Attr -> [Word8] -> Image
146utf8String a bytes = utf8Bytestring a (BL.pack bytes)
148-- | Renders a UTF-8 encoded lazy bytestring.
149utf8Bytestring :: Attr -> BL.ByteString -> Image
150utf8Bytestring a bs = text a (TL.decodeUtf8 bs)
152-- | Renders a UTF-8 encoded strict bytestring.
153utf8Bytestring' :: Attr -> B.ByteString -> Image
154utf8Bytestring' a bs = text' a (T.decodeUtf8 bs)
156-- | Creates a fill of the specified character. The dimensions are in
157-- number of characters wide and number of rows high.
158charFill :: Integral d => Attr -> Char -> d -> d -> Image
159charFill _a _c 0 _h = EmptyImage
160charFill _a _c _w 0 = EmptyImage
162 vertCat $ replicate (fromIntegral h) $ HorizText a txt displayWidth charWidth
164 txt = TL.replicate (fromIntegral w) (TL.singleton c)
165 displayWidth = safeWcwidth c * (fromIntegral w)
166 charWidth = fromIntegral w
168-- | The empty image. Useful for fold combinators. These occupy no space
169-- nor define any display attributes.
171emptyImage = EmptyImage
173-- | Pad the given image. This adds background character fills to the
174-- left, top, right, bottom. The pad values are how many display columns
176pad :: Int -> Int -> Int -> Int -> Image -> Image
178pad inL inT inR inB inImage
179 | inL < 0 || inT < 0 || inR < 0 || inB < 0 = error "cannot pad by negative amount"
180 | otherwise = go inL inT inR inB inImage
183 go 0 0 0 b i = VertJoin i (BGFill w b) w h
184 where w = imageWidth i
185 h = imageHeight i + b
186 go 0 0 r b i = go 0 0 0 b $ HorizJoin i (BGFill r h) w h
187 where w = imageWidth i + r
189 go 0 t r b i = go 0 0 r b $ VertJoin (BGFill w t) i w h
190 where w = imageWidth i
191 h = imageHeight i + t
192 go l t r b i = go 0 t r b $ HorizJoin (BGFill l h) i w h
193 where w = imageWidth i + l
196-- | Translates an image by padding or cropping the left and top.
197-- First param is amount to translate left. Second param is amount to
200-- This can have an unexpected effect: Translating an image to less than
201-- (0,0) then to greater than (0,0) will crop the image.
202translate :: Int -> Int -> Image -> Image
203translate x y i = translateX x (translateY y i)
205-- | translates an image by padding or cropping the left
206translateX :: Int -> Image -> Image
208 | x < 0 = let s = abs x in CropLeft i s (imageWidth i - s) (imageHeight i)
210 | otherwise = let h = imageHeight i in HorizJoin (BGFill x h) i (imageWidth i + x) h
212-- | translates an image by padding or cropping the top
213translateY :: Int -> Image -> Image
215 | y < 0 = let s = abs y in CropTop i s (imageWidth i) (imageHeight i - s)
217 | otherwise = let w = imageWidth i in VertJoin (BGFill w y) i w (imageHeight i + y)
219-- | Ensure an image is no larger than the provided size. If the image
220-- is larger then crop the right or bottom.
222-- This is transformed to a vertical crop from the bottom followed by
223-- horizontal crop from the right.
224crop :: Int -> Int -> Image -> Image
225crop 0 _ _ = EmptyImage
226crop _ 0 _ = EmptyImage
227crop w h i = cropBottom h (cropRight w i)
229-- | crop the display height. If the image is less than or equal in
230-- height then this operation has no effect. Otherwise the image is
231-- cropped from the bottom.
232cropBottom :: Int -> Image -> Image
233cropBottom 0 _ = EmptyImage
235 | h < 0 = error "cannot crop height to less than zero"
238 go EmptyImage = EmptyImage
239 go i@(CropBottom {croppedImage, outputWidth, outputHeight})
240 | outputHeight <= h = i
241 | otherwise = CropBottom croppedImage outputWidth h
243 | h >= imageHeight i = i
244 | otherwise = CropBottom i (imageWidth i) h
246-- | ensure the image is no wider than the given width. If the image is
247-- wider then crop the right side.
248cropRight :: Int -> Image -> Image
249cropRight 0 _ = EmptyImage
251 | w < 0 = error "cannot crop width to less than zero"
254 go EmptyImage = EmptyImage
255 go i@(CropRight {croppedImage, outputWidth, outputHeight})
256 | outputWidth <= w = i
257 | otherwise = CropRight croppedImage w outputHeight
259 | w >= imageWidth i = i
260 | otherwise = CropRight i w (imageHeight i)
262-- | ensure the image is no wider than the given width. If the image is
263-- wider then crop the left side.
264cropLeft :: Int -> Image -> Image
265cropLeft 0 _ = EmptyImage
267 | w < 0 = error "cannot crop the width to less than zero"
270 go EmptyImage = EmptyImage
271 go i@(CropLeft {croppedImage, leftSkip, outputWidth, outputHeight})
272 | outputWidth <= w = i
274 let leftSkip' = leftSkip + outputWidth - w
275 in CropLeft croppedImage leftSkip' w outputHeight
277 | imageWidth i <= w = i
278 | otherwise = CropLeft i (imageWidth i - w) w (imageHeight i)
280-- | crop the display height. If the image is less than or equal in
281-- height then this operation has no effect. Otherwise the image is
282-- cropped from the top.
283cropTop :: Int -> Image -> Image
284cropTop 0 _ = EmptyImage
286 | h < 0 = error "cannot crop the height to less than zero"
289 go EmptyImage = EmptyImage
290 go i@(CropTop {croppedImage, topSkip, outputWidth, outputHeight})
291 | outputHeight <= h = i
293 let topSkip' = topSkip + outputHeight - h
294 in CropTop croppedImage topSkip' outputWidth h
296 | imageHeight i <= h = i
297 | otherwise = CropTop i (imageHeight i - h) (imageWidth i) h
299-- | Generic resize. Pads and crops as required to assure the given
300-- display width and height. This is biased to pad/crop the right and
302resize :: Int -> Int -> Image -> Image
303resize w h i = resizeHeight h (resizeWidth w i)
305-- | Resize the width. Pads and crops as required to assure the given
306-- display width. This is biased to pad/crop the right.
307resizeWidth :: Int -> Image -> Image
308resizeWidth w i = case w `compare` imageWidth i of
311 GT -> i <|> BGFill (w - imageWidth i) (imageHeight i)
313-- | Resize the height. Pads and crops as required to assure the given
314-- display height. This is biased to pad/crop the bottom.
315resizeHeight :: Int -> Image -> Image
316resizeHeight h i = case h `compare` imageHeight i of
319 GT -> i <-> BGFill (imageWidth i) (h - imageHeight i)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list