<commit_before>-- Copyright 2009-2010 <NAME>
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}
module Graphics.Vty.Image ( Image(..)
                          , image_width
                          , image_height
                          , (<|>)
                          , (<->)
                          , horiz_cat
                          , vert_cat
                          , background_fill
                          , char
                          , string
                          , iso_10646_string
                          , utf8_string
                          , utf8_bytestring
                          , char_fill
                          , empty_image
                          , translate
                          , crop
                          -- | The possible display attributes used in constructing an `Image`.
                          , module Graphics.Vty.Attributes
                          )
    where

import Graphics.Vty.Attributes

import Codec.Binary.UTF8.Width

import Codec.Binary.UTF8.String ( decode )

import qualified Data.ByteString as BS
import Data.Monoid
import qualified Data.Sequence as Seq
import qualified Data.String.UTF8 as UTF8
import Data.Word

infixr 5 <|>
infixr 4 <->

type StringSeq = Seq.Seq Char

-- | An image in VTY defines:
--
--  * properties required to display the image. These are properties that effect the output image
--    but are independent of position 
--
--  * A set of position-dependent text and attribute regions. The possible regions are:
--
--      * a point. ( char )
--
--      * a horizontal line of characters with a single attribute. (string, utf8_string,
--      utf8_bytestring )
--
--      * a fill of a single character. (char_fill)
--
--      * a fill of the picture's background. (background_fill)
--
-- todo: increase the number of encoded bytestring formats supported.
data Image = 
    -- A horizontal text span is always >= 1 column and has a row height of 1.
      HorizText
      { attr :: !Attr
      -- All character data is stored as Char sequences with the ISO-10646 encoding.
      , text :: StringSeq
      -- in columns
      , output_width :: !Word -- >= 0
      , char_width :: !Word -- >= 1
      }
    -- A horizontal join can be constructed between any two images. However a HorizJoin instance is
    -- required to be between two images of equal height. The horiz_join constructor adds background
    -- filles to the provided images that assure this is true for the HorizJoin value produced.
    | HorizJoin
      { part_left :: Image 
      , part_right :: Image
      , output_width :: !Word -- >= 1
      , output_height :: !Word -- >= 1
      }
    -- A veritical join can be constructed between any two images. However a VertJoin instance is
    -- required to be between two images of equal width. The vert_join constructor adds background
    -- fills to the provides images that assure this is true for the VertJoin value produced.
    | VertJoin
      { part_top :: Image
      , part_bottom :: Image
      , output_width :: !Word -- >= 1
      , output_height :: !Word -- >= 1
      }
    -- A background fill will be filled with the background pattern. The background pattern is
    -- defined as a property of the Picture this Image is used to form. 
    | BGFill
      { output_width :: !Word -- >= 1
      , output_height :: !Word -- >= 1
      }
    -- The combining operators identity constant. 
    -- EmptyImage <|> a = a
    -- EmptyImage <-> a = a
    -- 
    -- Any image of zero size equals the empty image.
    | EmptyImage
    | Translation (Int, Int) Image
    -- Crop an image to a size
    | ImageCrop (Int, Int) Image
    deriving Eq

instance Show Image where
    show ( HorizText { output_width = ow, text = txt } ) 
        = "HorizText [" ++ show ow ++ "] (" ++ show txt ++ ")"
    show ( BGFill { output_width = c, output_height = r } ) 
        = "BGFill (" ++ show c ++ "," ++ show r ++ ")"
    show ( HorizJoin { part_left = l, part_right = r, output_width = c } ) 
        = "HorizJoin " ++ show c ++ " ( " ++ show l ++ " <|> " ++ show r ++ " )"
    show ( VertJoin { part_top = t, part_bottom = b, output_width = c, output_height = r } ) 
        = "VertJoin (" ++ show c ++ ", " ++ show r ++ ") ( " ++ show t ++ " ) <-> ( " ++ show b ++ " )"
    show ( Translation offset i )
        = "Translation ( " ++ show offset ++ ", " ++ show i ++ " )"
    show ( ImageCrop size i )
        = "ImageCrop ( " ++ show size ++ ", " ++ show i ++ " )"
    show ( EmptyImage ) = "EmptyImage"

-- | Currently append in the Monoid instance is equivalent to <->. Future versions will just stack
-- the images.
instance Monoid Image where
    mempty = empty_image
    mappend = (<->)
    
-- A horizontal text image of 0 characters in width simplifies to the EmptyImage
horiz_text :: Attr -> StringSeq -> Word -> Image
horiz_text a txt ow
    | ow == 0    = EmptyImage
    | otherwise = HorizText a txt ow (toEnum $ Seq.length txt)

horiz_join :: Image -> Image -> Word -> Word -> Image
horiz_join i_0 i_1 w h
    -- A horiz join of two 0 width images simplifies to the EmptyImage
    | w == 0 = EmptyImage
    -- A horizontal join where either part is 0 columns in width simplifies to the other part.
    -- This covers the case where one part is the EmptyImage.
    | image_width i_0 == 0 = i_1
    | image_width i_1 == 0 = i_0
    -- If the images are of the same height then no BG padding is required
    | image_height i_0 == image_height i_1 = HorizJoin i_0 i_1 w h
    -- otherwise one of the imagess needs to be padded to the right size.
    | image_height i_0 < image_height i_1  -- Pad i_0<commit_msg>Improve show instance for ImageCrop<commit_after>-- Copyright 2009-2010 <NAME>
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE DisambiguateRecordFields #-}
{-# OPTIONS_GHC -funbox-strict-fields #-}
module Graphics.Vty.Image ( Image(..)
                          , image_width
                          , image_height
                          , (<|>)
                          , (<->)
                          , horiz_cat
                          , vert_cat
                          , background_fill
                          , char
                          , string
                          , iso_10646_string
                          , utf8_string
                          , utf8_bytestring
                          , char_fill
                          , empty_image
                          , translate
                          , crop
                          -- | The possible display attributes used in constructing an `Image`.
                          , module Graphics.Vty.Attributes
                          )
    where

import Graphics.Vty.Attributes

import Codec.Binary.UTF8.Width

import Codec.Binary.UTF8.String ( decode )

import qualified Data.ByteString as BS
import Data.Monoid
import qualified Data.Sequence as Seq
import qualified Data.String.UTF8 as UTF8
import Data.Word

infixr 5 <|>
infixr 4 <->

type StringSeq = Seq.Seq Char

-- | An image in VTY defines:
--
--  * properties required to display the image. These are properties that effect the output image
--    but are independent of position 
--
--  * A set of position-dependent text and attribute regions. The possible regions are:
--
--      * a point. ( char )
--
--      * a horizontal line of characters with a single attribute. (string, utf8_string,
--      utf8_bytestring )
--
--      * a fill of a single character. (char_fill)
--
--      * a fill of the picture's background. (background_fill)
--
-- todo: increase the number of encoded bytestring formats supported.
data Image = 
    -- A horizontal text span is always >= 1 column and has a row height of 1.
      HorizText
      { attr :: !Attr
      -- All character data is stored as Char sequences with the ISO-10646 encoding.
      , text :: StringSeq
      -- in columns
      , output_width :: !Word -- >= 0
      , char_width :: !Word -- >= 1
      }
    -- A horizontal join can be constructed between any two images. However a HorizJoin instance is
    -- required to be between two images of equal height. The horiz_join constructor adds background
    -- filles to the provided images that assure this is true for the HorizJoin value produced.
    | HorizJoin
      { part_left :: Image 
      , part_right :: Image
      , output_width :: !Word -- >= 1
      , output_height :: !Word -- >= 1
      }
    -- A veritical join can be constructed between any two images. However a VertJoin instance is
    -- required to be between two images of equal width. The vert_join constructor adds background
    -- fills to the provides images that assure this is true for the VertJoin value produced.
    | VertJoin
      { part_top :: Image
      , part_bottom :: Image
      , output_width :: !Word -- >= 1
      , output_height :: !Word -- >= 1
      }
    -- A background fill will be filled with the background pattern. The background pattern is
    -- defined as a property of the Picture this Image is used to form. 
    | BGFill
      { output_width :: !Word -- >= 1
      , output_height :: !Word -- >= 1
      }
    -- The combining operators identity constant. 
    -- EmptyImage <|> a = a
    -- EmptyImage <-> a = a
    -- 
    -- Any image of zero size equals the empty image.
    | EmptyImage
    | Translation (Int, Int) Image
    -- Crop an image to a size
    | ImageCrop (Int, Int) Image
    deriving Eq

instance Show Image where
    show ( HorizText { output_width = ow, text = txt } ) 
        = "HorizText [" ++ show ow ++ "] (" ++ show txt ++ ")"
    show ( BGFill { output_width = c, output_height = r } ) 
        = "BGFill (" ++ show c ++ "," ++ show r ++ ")"
    show ( HorizJoin { part_left = l, part_right = r, output_width = c } ) 
        = "HorizJoin " ++ show c ++ " ( " ++ show l ++ " <|> " ++ show r ++ " )"
    show ( VertJoin { part_top = t, part_bottom = b, output_width = c, output_height = r } ) 
        = "VertJoin (" ++ show c ++ ", " ++ show r ++ ") ( " ++ show t ++ " ) <-> ( " ++ show b ++ " )"
    show ( Translation offset i )
        = "Translation ( " ++ show offset ++ ", " ++ show i ++ " )"
    show ( ImageCrop size i )
        = "ImageCrop " ++ show size ++ " ( " ++ show i ++ " )"
    show ( EmptyImage ) = "EmptyImage"

-- | Currently append in the Monoid instance is equivalent to <->. Future versions will just stack
-- the images.
instance Monoid Image where
    mempty = empty_image
    mappend = (<->)
    
-- A horizontal text image of 0 characters in width simplifies to the EmptyImage
horiz_text :: Attr -> StringSeq -> Word -> Image
horiz_text a txt ow
    | ow == 0    = EmptyImage
    | otherwise = HorizText a txt ow (toEnum $ Seq.length txt)

horiz_join :: Image -> Image -> Word -> Word -> Image
horiz_join i_0 i_1 w h
    -- A horiz join of two 0 width images simplifies to the EmptyImage
    | w == 0 = EmptyImage
    -- A horizontal join where either part is 0 columns in width simplifies to the other part.
    -- This covers the case where one part is the EmptyImage.
    | image_width i_0 == 0 = i_1
    | image_width i_1 == 0 = i_0
    -- If the images are of the same height then no BG padding is required
    | image_height i_0 == image_height i_1 = HorizJoin i_0 i_1 w h
    -- otherwise one of the imagess needs to be padded to the right size.
    | image_height i_0 < image_height i_1  -- Pad i_0
{'fraction_non_alphanumeric': 0.10087757407246503, 'fraction_numerical': 0.010079068555043879, 'mean_word_length': 1.8926866046745414, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '88205', 'n_tokens_mistral': 3331, 'n_tokens_neox': 3171, 'n_words': 1225}