1-- Copyright 2009-2010 Corey O'Connor
2{-# LANGUAGE BangPatterns #-}
3{-# LANGUAGE NamedFieldPuns #-}
4{-# LANGUAGE DisambiguateRecordFields #-}
5{-# OPTIONS_GHC -funbox-strict-fields #-}
6module Graphics.Vty.Image ( Image(..)
22 -- | The possible display attributes used in constructing an `Image`.
23 , module Graphics.Vty.Attributes
27import Graphics.Vty.Attributes
29import Codec.Binary.UTF8.Width
31import Codec.Binary.UTF8.String ( decode )
33import qualified Data.ByteString as BS
35import qualified Data.Sequence as Seq
36import qualified Data.String.UTF8 as UTF8
42type StringSeq = Seq.Seq Char
44-- | An image in VTY defines:
46-- * properties required to display the image. These are properties that effect the output image
47-- but are independent of position
49-- * A set of position-dependent text and attribute regions. The possible regions are:
53-- * a horizontal line of characters with a single attribute. (string, utf8_string,
56-- * a fill of a single character. (char_fill)
58-- * a fill of the picture's background. (background_fill)
60-- todo: increase the number of encoded bytestring formats supported.
62 -- A horizontal text span is always >= 1 column and has a row height of 1.
65 -- All character data is stored as Char sequences with the ISO-10646 encoding.
68 , output_width :: !Word -- >= 0
69 , char_width :: !Word -- >= 1
71 -- A horizontal join can be constructed between any two images. However a HorizJoin instance is
72 -- required to be between two images of equal height. The horiz_join constructor adds background
73 -- filles to the provided images that assure this is true for the HorizJoin value produced.
77 , output_width :: !Word -- >= 1
78 , output_height :: !Word -- >= 1
80 -- A veritical join can be constructed between any two images. However a VertJoin instance is
81 -- required to be between two images of equal width. The vert_join constructor adds background
82 -- fills to the provides images that assure this is true for the VertJoin value produced.
85 , part_bottom :: Image
86 , output_width :: !Word -- >= 1
87 , output_height :: !Word -- >= 1
89 -- A background fill will be filled with the background pattern. The background pattern is
90 -- defined as a property of the Picture this Image is used to form.
92 { output_width :: !Word -- >= 1
93 , output_height :: !Word -- >= 1
95 -- The combining operators identity constant.
96 -- EmptyImage <|> a = a
97 -- EmptyImage <-> a = a
99 -- Any image of zero size equals the empty image.
101 | Translation (Int, Int) Image
104instance Show Image where
105 show ( HorizText { output_width = ow, text = txt } )
106 = "HorizText [" ++ show ow ++ "] (" ++ show txt ++ ")"
107 show ( BGFill { output_width = c, output_height = r } )
108 = "BGFill (" ++ show c ++ "," ++ show r ++ ")"
109 show ( HorizJoin { part_left = l, part_right = r, output_width = c } )
110 = "HorizJoin " ++ show c ++ " ( " ++ show l ++ " <|> " ++ show r ++ " )"
111 show ( VertJoin { part_top = t, part_bottom = b, output_width = c, output_height = r } )
112 = "VertJoin (" ++ show c ++ ", " ++ show r ++ ") ( " ++ show t ++ " ) <-> ( " ++ show b ++ " )"
113 show ( Translation offset i )
114 = "Translation ( " ++ show offset ++ ", " ++ show i ++ " )"
115 show ( EmptyImage ) = "EmptyImage"
117-- | Currently append in the Monoid instance is equivalent to <->. Future versions will just stack
119instance Monoid Image where
123-- A horizontal text image of 0 characters in width simplifies to the EmptyImage
124horiz_text :: Attr -> StringSeq -> Word -> Image
126 | ow == 0 = EmptyImage
127 | otherwise = HorizText a txt ow (toEnum $ Seq.length txt)
129horiz_join :: Image -> Image -> Word -> Word -> Image
130horiz_join i_0 i_1 w h
131 -- A horiz join of two 0 width images simplifies to the EmptyImage
132 | w == 0 = EmptyImage
133 -- A horizontal join where either part is 0 columns in width simplifies to the other part.
134 -- This covers the case where one part is the EmptyImage.
135 | image_width i_0 == 0 = i_1
136 | image_width i_1 == 0 = i_0
137 -- If the images are of the same height then no BG padding is required
138 | image_height i_0 == image_height i_1 = HorizJoin i_0 i_1 w h
139 -- otherwise one of the imagess needs to be padded to the right size.
140 | image_height i_0 < image_height i_1 -- Pad i_0
141 = let pad_amount = image_height i_1 - image_height i_0
142 in horiz_join ( vert_join i_0
143 ( BGFill ( image_width i_0 ) pad_amount )
149 | image_height i_0 > image_height i_1 -- Pad i_1
150 = let pad_amount = image_height i_0 - image_height i_1
153 ( BGFill ( image_width i_1 ) pad_amount )
158horiz_join _ _ _ _ = error "horiz_join applied to undefined values."
160vert_join :: Image -> Image -> Word -> Word -> Image
162 -- A vertical join of two 0 height images simplifies to the EmptyImage
163 | h == 0 = EmptyImage
164 -- A vertical join where either part is 0 rows in height simplifies to the other part.
165 -- This covers the case where one part is the EmptyImage
166 | image_height i_0 == 0 = i_1
167 | image_height i_1 == 0 = i_0
168 -- If the images are of the same height then no background padding is required
169 | image_width i_0 == image_width i_1 = VertJoin i_0 i_1 w h
170 -- Otherwise one of the images needs to be padded to the size of the other image.
171 | image_width i_0 < image_width i_1
172 = let pad_amount = image_width i_1 - image_width i_0
173 in vert_join ( horiz_join i_0
174 ( BGFill pad_amount ( image_height i_0 ) )
180 | image_width i_0 > image_width i_1
181 = let pad_amount = image_width i_0 - image_width i_1
184 ( BGFill pad_amount ( image_height i_1 ) )
189vert_join _ _ _ _ = error "vert_join applied to undefined values."
191-- | An area of the picture's bacground (See Background) of w columns and h rows.
192background_fill :: Word -> Word -> Image
194 | w == 0 = EmptyImage
195 | h == 0 = EmptyImage
196 | otherwise = BGFill w h
198-- | The width of an Image. This is the number display columns the image will occupy.
199image_width :: Image -> Word
200image_width HorizText { output_width = w } = w
201image_width HorizJoin { output_width = w } = w
202image_width VertJoin { output_width = w } = w
203image_width BGFill { output_width = w } = w
204image_width EmptyImage = 0
205image_width ( Translation _v i ) = image_width i
207-- | The height of an Image. This is the number of display rows the image will occupy.
208image_height :: Image -> Word
209image_height HorizText {} = 1
210image_height HorizJoin { output_height = r } = r
211image_height VertJoin { output_height = r } = r
212image_height BGFill { output_height = r } = r
213image_height EmptyImage = 0
214image_height ( Translation _v i ) = image_height i
216-- | Combines two images side by side.
218-- The result image will have a width equal to the sum of the two images width. And the height will
219-- equal the largest height of the two images. The area not defined in one image due to a height
220-- missmatch will be filled with the background pattern.
221(<|>) :: Image -> Image -> Image
223-- Two horizontal text spans with the same attributes can be merged.
224h0@(HorizText attr_0 text_0 ow_0 _) <|> h1@(HorizText attr_1 text_1 ow_1 _)
225 | attr_0 == attr_1 = horiz_text attr_0 (text_0 Seq.>< text_1) (ow_0 + ow_1)
226 | otherwise = horiz_join h0 h1 (ow_0 + ow_1) 1
228-- Anything placed to the right of a join wil be joined to the right sub image.
229-- The total columns for the join is the sum of the two arguments columns
230h0@( HorizJoin {} ) <|> h1
231 = horiz_join ( part_left h0 )
232 ( part_right h0 <|> h1 )
233 ( image_width h0 + image_width h1 )
234 ( max (image_height h0) (image_height h1) )
236-- Anything but a join placed to the left of a join wil be joined to the left sub image.
237-- The total columns for the join is the sum of the two arguments columns
238h0 <|> h1@( HorizJoin {} )
239 = horiz_join ( h0 <|> part_left h1 )
241 ( image_width h0 + image_width h1 )
242 ( max (image_height h0) (image_height h1) )
247 ( image_width h0 + image_width h1 )
248 ( max (image_height h0) (image_height h1) )
250-- | Combines two images vertically.
251-- The result image will have a height equal to the sum of the heights of both images.
252-- The width will equal the largest width of the two images.
253-- The area not defined in one image due to a width missmatch will be filled with the background
255(<->) :: Image -> Image -> Image
259 ( max (image_width im_t) (image_width im_b) )
260 ( image_height im_t + image_height im_b )
262-- | Compose any number of images horizontally.
263horiz_cat :: [Image] -> Image
264horiz_cat = foldr (<|>) EmptyImage
266-- | Compose any number of images vertically.
267vert_cat :: [Image] -> Image
268vert_cat = foldr (<->) EmptyImage
270-- | an image of a single character. This is a standard Haskell 31-bit character assumed to be in
271-- the ISO-10646 encoding.
272char :: Attr -> Char -> Image
273char !a !c = HorizText a (Seq.singleton c) (safe_wcwidth c) 1
275-- | A string of characters layed out on a single row with the same display attribute. The string is
276-- assumed to be a sequence of ISO-10646 characters.
278-- Note: depending on how the Haskell compiler represents string literals a string literal in a
279-- UTF-8 encoded source file, for example, may be represented as a ISO-10646 string.
280-- That is, I think, the case with GHC 6.10. This means, for the most part, you don't need to worry
281-- about the encoding format when outputting string literals. Just provide the string literal
282-- directly to iso_10646_string or string.
284iso_10646_string :: Attr -> String -> Image
285iso_10646_string !a !str = horiz_text a (Seq.fromList str) (safe_wcswidth str)
287-- | Alias for iso_10646_string. Since the usual case is that a literal string like "foo" is
288-- represented internally as a list of ISO 10646 31 bit characters.
290-- Note: Keep in mind that GHC will compile source encoded as UTF-8 but the literal strings, while
291-- UTF-8 encoded in the source, will be transcoded to a ISO 10646 31 bit characters runtime
293string :: Attr -> String -> Image
294string = iso_10646_string
296-- | A string of characters layed out on a single row. The string is assumed to be a sequence of
298utf8_string :: Attr -> [Word8] -> Image
299utf8_string !a !str = string a ( decode str )
301safe_wcwidth :: Char -> Word
302safe_wcwidth c = case wcwidth c of
303 i | i < 0 -> 0 -- error "negative wcwidth"
304 | otherwise -> toEnum i
306safe_wcswidth :: String -> Word
307safe_wcswidth str = case wcswidth str of
308 i | i < 0 -> 0 -- error "negative wcswidth"
309 | otherwise -> toEnum i
311-- | Renders a UTF-8 encoded bytestring.
312utf8_bytestring :: Attr -> BS.ByteString -> Image
313utf8_bytestring !a !bs = string a (UTF8.toString $ UTF8.fromRep bs)
315-- | creates a fill of the specified character. The dimensions are in number of characters wide and
316-- number of rows high.
318-- Unlike the Background fill character this character can have double column display width.
319char_fill :: Enum d => Attr -> Char -> d -> d -> Image
321 vert_cat $ replicate (fromEnum h) $ horiz_cat $ replicate (fromEnum w) $ char a c
323-- | The empty image. Useful for fold combinators. These occupy no space nor define any display
326empty_image = EmptyImage
328translate :: (Int, Int) -> Image -> Image
329translate v i = Translation v 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