on 30fd552 help sign in
vty/src/Graphics/Vty/Image.hs
371 lines 16 kB history raw
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(..)
7 , image_width
8 , image_height
9 , (<|>)
10 , (<->)
11 , horiz_cat
12 , vert_cat
13 , background_fill
14 , char
15 , string
16 , iso_10646_string
17 , utf8_string
18 , utf8_bytestring
19 , char_fill
20 , empty_image
21 , translate
22 , safe_wcwidth
23 , safe_wcswidth
24 , wcwidth
25 , wcswidth
26 , crop
27 , pad
28 -- | The possible display attributes used in constructing an `Image`.
29 , module Graphics.Vty.Attributes
30 )
31 where
33import Graphics.Vty.Attributes
35import Codec.Binary.UTF8.Width
37import Codec.Binary.UTF8.String ( decode )
39import qualified Data.ByteString as BS
40import Data.Monoid
41import qualified Data.Sequence as Seq
42import qualified Data.String.UTF8 as UTF8
43import Data.Word
45infixr 5 <|>
46infixr 4 <->
48-- | We pair each character with it's display length. This way we only compute the length once per
49-- character.
50-- * Though currently the width of some strings is still compute multiple times.
51type DisplayString = Seq.Seq (Char, Word)
53-- | An image in VTY defines:
54--
55-- * properties required to display the image. These are properties that effect the output image
56-- but are independent of position
57--
58-- * A set of position-dependent text and attribute regions. The possible regions are:
59--
60-- * a point. ( char )
61--
62-- * a horizontal line of characters with a single attribute. (string, utf8_string,
63-- utf8_bytestring )
64--
65-- * a fill of a single character. (char_fill)
66--
67-- * a fill of the picture's background. (background_fill)
68--
69-- todo: increase the number of encoded bytestring formats supported.
70data Image =
71 -- A horizontal text span is always >= 1 column and has a row height of 1.
72 HorizText
73 { attr :: !Attr
74 -- All character data is stored as Char sequences with the ISO-10646 encoding.
75 , text :: DisplayString
76 , output_width :: !Word -- >= 0
77 , char_width :: !Word -- >= 1
78 }
79 -- A horizontal join can be constructed between any two images. However a HorizJoin instance is
80 -- required to be between two images of equal height. The horiz_join constructor adds background
81 -- filles to the provided images that assure this is true for the HorizJoin value produced.
82 | HorizJoin
83 { part_left :: Image
84 , part_right :: Image
85 , output_width :: !Word -- >= 1
86 , output_height :: !Word -- >= 1
87 }
88 -- A veritical join can be constructed between any two images. However a VertJoin instance is
89 -- required to be between two images of equal width. The vert_join constructor adds background
90 -- fills to the provides images that assure this is true for the VertJoin value produced.
91 | VertJoin
92 { part_top :: Image
93 , part_bottom :: Image
94 , output_width :: !Word -- >= 1
95 , output_height :: !Word -- >= 1
96 }
97 -- A background fill will be filled with the background pattern. The background pattern is
98 -- defined as a property of the Picture this Image is used to form.
99 | BGFill
100 { output_width :: !Word -- >= 1
101 , output_height :: !Word -- >= 1
102 }
103 -- The combining operators identity constant.
104 -- EmptyImage <|> a = a
105 -- EmptyImage <-> a = a
106 --
107 -- Any image of zero size equals the empty image.
108 | EmptyImage
109 | Translation (Int, Int) Image
110 -- Crop an image to a size
111 | ImageCrop (Word, Word) Image
112 -- Pad an image up to a size
113 | ImagePad (Word, Word) Image
114 deriving Eq
116instance Show Image where
117 show ( HorizText { output_width = ow, text = txt } )
118 = "HorizText [" ++ show ow ++ "] (" ++ show (fmap fst txt) ++ ")"
119 show ( BGFill { output_width = c, output_height = r } )
120 = "BGFill (" ++ show c ++ "," ++ show r ++ ")"
121 show ( HorizJoin { part_left = l, part_right = r, output_width = c } )
122 = "HorizJoin " ++ show c ++ " ( " ++ show l ++ " <|> " ++ show r ++ " )"
123 show ( VertJoin { part_top = t, part_bottom = b, output_width = c, output_height = r } )
124 = "VertJoin (" ++ show c ++ ", " ++ show r ++ ") ( " ++ show t ++ " ) <-> ( " ++ show b ++ " )"
125 show ( Translation offset i )
126 = "Translation " ++ show offset ++ " ( " ++ show i ++ " )"
127 show ( ImageCrop size i )
128 = "ImageCrop " ++ show size ++ " ( " ++ show i ++ " )"
129 show ( ImagePad size i )
130 = "ImagePad " ++ show size ++ " ( " ++ show i ++ " )"
131 show ( EmptyImage ) = "EmptyImage"
133-- | Currently append in the Monoid instance is equivalent to <->.
134instance Monoid Image where
135 mempty = empty_image
136 mappend = (<->)
138-- A horizontal text image of 0 characters in width simplifies to the EmptyImage
139horiz_text :: Attr -> DisplayString -> Word -> Image
140horiz_text a txt ow
141 | ow == 0 = EmptyImage
142 | otherwise = HorizText a txt ow (toEnum $ Seq.length txt)
144horiz_join :: Image -> Image -> Word -> Word -> Image
145horiz_join i_0 i_1 w h
146 -- A horiz join of two 0 width images simplifies to the EmptyImage
147 | w == 0 = EmptyImage
148 -- A horizontal join where either part is 0 columns in width simplifies to the other part.
149 -- This covers the case where one part is the EmptyImage.
150 | image_width i_0 == 0 = i_1
151 | image_width i_1 == 0 = i_0
152 -- If the images are of the same height then no BG padding is required
153 | image_height i_0 == image_height i_1 = HorizJoin i_0 i_1 w h
154 -- otherwise one of the imagess needs to be padded to the right size.
155 | image_height i_0 < image_height i_1 -- Pad i_0
156 = let pad_amount = image_height i_1 - image_height i_0
157 in horiz_join ( vert_join i_0
158 ( BGFill ( image_width i_0 ) pad_amount )
159 ( image_width i_0 )
160 ( image_height i_1 )
161 )
162 i_1
163 w h
164 | image_height i_0 > image_height i_1 -- Pad i_1
165 = let pad_amount = image_height i_0 - image_height i_1
166 in horiz_join i_0
167 ( vert_join i_1
168 ( BGFill ( image_width i_1 ) pad_amount )
169 ( image_width i_1 )
170 ( image_height i_0 )
171 )
172 w h
173horiz_join _ _ _ _ = error "horiz_join applied to undefined values."
175vert_join :: Image -> Image -> Word -> Word -> Image
176vert_join i_0 i_1 w h
177 -- A vertical join of two 0 height images simplifies to the EmptyImage
178 | h == 0 = EmptyImage
179 -- A vertical join where either part is 0 rows in height simplifies to the other part.
180 -- This covers the case where one part is the EmptyImage
181 | image_height i_0 == 0 = i_1
182 | image_height i_1 == 0 = i_0
183 -- If the images are of the same height then no background padding is required
184 | image_width i_0 == image_width i_1 = VertJoin i_0 i_1 w h
185 -- Otherwise one of the images needs to be padded to the size of the other image.
186 | image_width i_0 < image_width i_1
187 = let pad_amount = image_width i_1 - image_width i_0
188 in vert_join ( horiz_join i_0
189 ( BGFill pad_amount ( image_height i_0 ) )
190 ( image_width i_1 )
191 ( image_height i_0 )
192 )
193 i_1
194 w h
195 | image_width i_0 > image_width i_1
196 = let pad_amount = image_width i_0 - image_width i_1
197 in vert_join i_0
198 ( horiz_join i_1
199 ( BGFill pad_amount ( image_height i_1 ) )
200 ( image_width i_0 )
201 ( image_height i_1 )
202 )
203 w h
204vert_join _ _ _ _ = error "vert_join applied to undefined values."
206-- | An area of the picture's bacground (See Background) of w columns and h rows.
207background_fill :: Word -> Word -> Image
208background_fill w h
209 | w == 0 = EmptyImage
210 | h == 0 = EmptyImage
211 | otherwise = BGFill w h
213-- | The width of an Image. This is the number display columns the image will occupy.
214image_width :: Image -> Word
215image_width HorizText { output_width = w } = w
216image_width HorizJoin { output_width = w } = w
217image_width VertJoin { output_width = w } = w
218image_width BGFill { output_width = w } = w
219image_width EmptyImage = 0
220image_width ( Translation v i ) = toEnum $ max 0 $ (fst v +) $ fromEnum $ image_width i
221image_width ( ImageCrop v i ) = min (image_width i) $ fst v
222image_width ( ImagePad v i ) = max (image_width i) $ fst v
224-- | The height of an Image. This is the number of display rows the image will occupy.
225image_height :: Image -> Word
226image_height HorizText {} = 1
227image_height HorizJoin { output_height = r } = r
228image_height VertJoin { output_height = r } = r
229image_height BGFill { output_height = r } = r
230image_height EmptyImage = 0
231image_height ( Translation v i ) = toEnum $ max 0 $ (snd v +) $ fromEnum $ image_height i
232image_height ( ImageCrop v i ) = min (image_height i) $ snd v
233image_height ( ImagePad v i ) = max (image_height i) $ snd v
235-- | Combines two images side by side.
236--
237-- The result image will have a width equal to the sum of the two images width. And the height will
238-- equal the largest height of the two images. The area not defined in one image due to a height
239-- missmatch will be filled with the background pattern.
240(<|>) :: Image -> Image -> Image
242-- Two horizontal text spans with the same attributes can be merged.
243h0@(HorizText attr_0 text_0 ow_0 _) <|> h1@(HorizText attr_1 text_1 ow_1 _)
244 | attr_0 == attr_1 = horiz_text attr_0 (text_0 Seq.>< text_1) (ow_0 + ow_1)
245 | otherwise = horiz_join h0 h1 (ow_0 + ow_1) 1
247-- Anything placed to the right of a join wil be joined to the right sub image.
248-- The total columns for the join is the sum of the two arguments columns
249h0@( HorizJoin {} ) <|> h1
250 = horiz_join ( part_left h0 )
251 ( part_right h0 <|> h1 )
252 ( image_width h0 + image_width h1 )
253 ( max (image_height h0) (image_height h1) )
255-- Anything but a join placed to the left of a join wil be joined to the left sub image.
256-- The total columns for the join is the sum of the two arguments columns
257h0 <|> h1@( HorizJoin {} )
258 = horiz_join ( h0 <|> part_left h1 )
259 ( part_right h1 )
260 ( image_width h0 + image_width h1 )
261 ( max (image_height h0) (image_height h1) )
263h0 <|> h1
264 = horiz_join h0
265 h1
266 ( image_width h0 + image_width h1 )
267 ( max (image_height h0) (image_height h1) )
269-- | Combines two images vertically.
270-- The result image will have a height equal to the sum of the heights of both images.
271-- The width will equal the largest width of the two images.
272-- The area not defined in one image due to a width missmatch will be filled with the background
273-- pattern.
274(<->) :: Image -> Image -> Image
275im_t <-> im_b
276 = vert_join im_t
277 im_b
278 ( max (image_width im_t) (image_width im_b) )
279 ( image_height im_t + image_height im_b )
281-- | Compose any number of images horizontally.
282horiz_cat :: [Image] -> Image
283horiz_cat = foldr (<|>) EmptyImage
285-- | Compose any number of images vertically.
286vert_cat :: [Image] -> Image
287vert_cat = foldr (<->) EmptyImage
289-- | an image of a single character. This is a standard Haskell 31-bit character assumed to be in
290-- the ISO-10646 encoding.
291char :: Attr -> Char -> Image
292char !a !c =
293 let display_width = safe_wcwidth c
294 in HorizText a (Seq.singleton (c, display_width)) display_width 1
296-- | A string of characters layed out on a single row with the same display attribute. The string is
297-- assumed to be a sequence of ISO-10646 characters.
298--
299-- Note: depending on how the Haskell compiler represents string literals a string literal in a
300-- UTF-8 encoded source file, for example, may be represented as a ISO-10646 string.
301-- That is, I think, the case with GHC 6.10. This means, for the most part, you don't need to worry
302-- about the encoding format when outputting string literals. Just provide the string literal
303-- directly to iso_10646_string or string.
304--
305iso_10646_string :: Attr -> String -> Image
306iso_10646_string !a !str =
307 let display_text = Seq.fromList $ map (\c -> (c, safe_wcwidth c)) str
308 in horiz_text a display_text (safe_wcswidth str)
310-- | Alias for iso_10646_string. Since the usual case is that a literal string like "foo" is
311-- represented internally as a list of ISO 10646 31 bit characters.
312--
313-- Note: Keep in mind that GHC will compile source encoded as UTF-8 but the literal strings, while
314-- UTF-8 encoded in the source, will be transcoded to a ISO 10646 31 bit characters runtime
315-- representation.
316string :: Attr -> String -> Image
317string = iso_10646_string
319-- | A string of characters layed out on a single row. The string is assumed to be a sequence of
320-- UTF-8 characters.
321utf8_string :: Attr -> [Word8] -> Image
322utf8_string !a !str = string a ( decode str )
324-- | Returns the display width of a character. Assumes all characters with unknown widths are 0 width
325safe_wcwidth :: Char -> Word
326safe_wcwidth c = case wcwidth c of
327 i | i < 0 -> 0
328 | otherwise -> toEnum i
330-- | Returns the display width of a string. Assumes all characters with unknown widths are 0 width
331safe_wcswidth :: String -> Word
332safe_wcswidth str = case wcswidth str of
333 i | i < 0 -> 0
334 | otherwise -> toEnum i
336-- | Renders a UTF-8 encoded bytestring.
337utf8_bytestring :: Attr -> BS.ByteString -> Image
338utf8_bytestring !a !bs = string a (UTF8.toString $ UTF8.fromRep bs)
340-- | creates a fill of the specified character. The dimensions are in number of characters wide and
341-- number of rows high.
342--
343-- Unlike the Background fill character this character can have double column display width.
344char_fill :: Enum d => Attr -> Char -> d -> d -> Image
345char_fill !a !c w h =
346 vert_cat $ replicate (fromEnum h) $ horiz_cat $ replicate (fromEnum w) $ char a c
348-- | The empty image. Useful for fold combinators. These occupy no space nor define any display
349-- attributes.
350empty_image :: Image
351empty_image = EmptyImage
353-- | Apply the given offset to the image.
354translate :: (Int, Int) -> Image -> Image
355translate v i = Translation v i
357-- | Ensure an image is no larger than the provided size. If the image is larger then crop.
358crop :: (Word, Word) -> Image -> Image
359crop (0,_) _ = EmptyImage
360crop (_,0) _ = EmptyImage
361crop v (ImageCrop _size i) = ImagePad (min (fst v) (fst _size), min (snd v) (snd _size)) i
362crop v (ImagePad _size i) = ImagePad (min (fst v) (fst _size), min (snd v) (snd _size)) i
363crop v i = ImagePad v i
365-- | Ensure an image is at least the provided size. If the image is smaler then pad.
366pad :: (Word, Word) -> Image -> Image
367pad (0,_) _ = EmptyImage
368pad (_,0) _ = EmptyImage
369pad v (ImagePad _size i) = ImagePad (max (fst v) (fst _size), max (snd v) (snd _size)) i
370pad v i = ImagePad v i