module FRP.Helm.Graphics (
Element(..),
FontWeight(..),
FontStyle(..),
Text(..),
Form(..),
FormStyle(..),
FillStyle(..),
LineCap(..),
LineJoin(..),
LineStyle(..),
Path,
Shape(..),
image,
fittedImage,
croppedImage,
collage,
centeredCollage,
fixedCollage,
defaultLine,
solid,
dashed,
dotted,
filled,
textured,
gradient,
outlined,
traced,
sprite,
toForm,
blank,
group,
groupTransform,
rotate,
scale,
move,
moveX,
moveY,
path,
segment,
polygon,
rect,
square,
oval,
circle,
ngon
) where
import FRP.Helm.Color (Color, black, Gradient)
import Graphics.Rendering.Cairo.Matrix (Matrix)
data FontWeight = LightWeight |
NormalWeight |
BoldWeight deriving (Show, Eq, Ord, Enum, Read)
data FontStyle = NormalStyle |
ObliqueStyle |
ItalicStyle deriving (Show, Eq, Ord, Enum, Read)
data Text = Text {
textUTF8 :: String,
textColor :: Color,
textTypeface :: String,
textHeight :: Double,
textWeight :: FontWeight,
textStyle :: FontStyle
} deriving (Show, Eq)
data Element = CollageElement Int Int (Maybe (Double, Double)) [Form] |
ImageElement (Int, Int) Int Int FilePath Bool |
TextElement Text deriving (Show, Eq)
image :: Int -> Int -> FilePath -> Element
image w h src = ImageElement (0, 0) w h src True
fittedImage :: Int -> Int -> FilePath -> Element
fittedImage w h src = ImageElement (0, 0) w h src False
croppedImage :: (Int, Int) -> Int -> Int -> FilePath -> Element
croppedImage pos w h src = ImageElement pos w h src False
data Form = Form {
formTheta :: Double,
formScale :: Double,
formX :: Double,
formY :: Double,
formStyle :: FormStyle
} deriving (Show, Eq)
data FillStyle = Solid Color | Texture String | Gradient Gradient deriving (Show, Eq, Ord, Read)
data LineCap = FlatCap | RoundCap | PaddedCap deriving (Show, Eq, Enum, Ord, Read)
data LineJoin = SmoothJoin | SharpJoin Double | ClippedJoin deriving (Show, Eq, Ord, Read)
data LineStyle = LineStyle {
lineColor :: Color,
lineWidth :: Double,
lineCap :: LineCap,
lineJoin :: LineJoin,
lineDashing :: [Double],
lineDashOffset :: Double
} deriving (Show, Eq)
defaultLine :: LineStyle
defaultLine = LineStyle {
lineColor = black,
lineWidth = 1,
lineCap = FlatCap,
lineJoin = SharpJoin 10,
lineDashing = [],
lineDashOffset = 0
}
solid :: Color -> LineStyle
solid color = defaultLine { lineColor = color }
dashed :: Color -> LineStyle
dashed color = defaultLine { lineColor = color, lineDashing = [8, 4] }
dotted :: Color -> LineStyle
dotted color = defaultLine { lineColor = color, lineDashing = [3, 3] }
data FormStyle = PathForm LineStyle Path |
ShapeForm (Either LineStyle FillStyle) Shape |
ElementForm Element |
GroupForm (Maybe Matrix) [Form] deriving (Show, Eq)
form :: FormStyle -> Form
form style = Form { formTheta = 0, formScale = 1, formX = 0, formY = 0, formStyle = style }
fill :: FillStyle -> Shape -> Form
fill style shape = form (ShapeForm (Right style) shape)
filled :: Color -> Shape -> Form
filled color = fill (Solid color)
textured :: String -> Shape -> Form
textured src = fill (Texture src)
gradient :: Gradient -> Shape -> Form
gradient grad = fill (Gradient grad)
outlined :: LineStyle -> Shape -> Form
outlined style shape = form (ShapeForm (Left style) shape)
traced :: LineStyle -> Path -> Form
traced style p = form (PathForm style p)
sprite :: Int -> Int -> (Int, Int) -> FilePath -> Form
sprite w h pos src = form (ElementForm (ImageElement pos w h src False))
toForm :: Element -> Form
toForm element = form (ElementForm element)
blank :: Form
blank = group []
group :: [Form] -> Form
group forms = form (GroupForm Nothing forms)
groupTransform :: Matrix -> [Form] -> Form
groupTransform matrix forms = form (GroupForm (Just matrix) forms)
rotate :: Double -> Form -> Form
rotate t f = f { formTheta = t + formTheta f }
scale :: Double -> Form -> Form
scale n f = f { formScale = n * formScale f }
move :: (Double, Double) -> Form -> Form
move (rx, ry) f = f { formX = rx + formX f, formY = ry + formY f }
moveX :: Double -> Form -> Form
moveX x = move (x, 0)
moveY :: Double -> Form -> Form
moveY y = move (0, y)
collage :: Int -> Int -> [Form] -> Element
collage w h = CollageElement w h Nothing
centeredCollage :: Int -> Int -> [Form] -> Element
centeredCollage w h = CollageElement w h (Just (realToFrac w / 2, realToFrac h / 2))
fixedCollage :: Int -> Int -> (Double, Double) -> [Form] -> Element
fixedCollage w h (x, y) = CollageElement w h (Just (realToFrac w / 2 x, realToFrac h / 2 y))
type Path = [(Double, Double)]
path :: [(Double, Double)] -> Path
path points = points
segment :: (Double, Double) -> (Double, Double) -> Path
segment p1 p2 = [p1, p2]
data Shape = PolygonShape Path |
RectangleShape (Double, Double) |
ArcShape (Double, Double) Double Double Double (Double, Double) deriving (Show, Eq, Ord, Read)
polygon :: Path -> Shape
polygon = PolygonShape
rect :: Double -> Double -> Shape
rect w h = RectangleShape (w, h)
square :: Double -> Shape
square n = rect n n
oval :: Double -> Double -> Shape
oval w h = ArcShape (0, 0) 0 (2 * pi) 1 (w / 2, h / 2)
circle :: Double -> Shape
circle r = ArcShape (0, 0) 0 (2 * pi) r (1, 1)
ngon :: Int -> Double -> Shape
ngon n r = PolygonShape (map (\i -> (r * cos (t * i), r * sin (t * i))) [0 .. fromIntegral (n 1)])
where
m = fromIntegral n
t = 2 * pi / m