module Render.CG.Minus where
import qualified Data.Colour as Colour
import qualified Graphics.Rendering.Cairo as Cairo
import System.FilePath
import Data.CG.Minus.Types
import qualified Data.CG.Minus.Colour as CG
import qualified Data.CG.Minus.Core as CG
nil :: Cairo.Render ()
nil = return ()
line2 :: Pt R -> Pt R -> Cairo.Render ()
line2 (Pt x1 y1) (Pt x2 y2) = Cairo.moveTo x1 y1 >> Cairo.lineTo x2 y2
line :: Ls R -> Cairo.Render ()
line (Ls l) =
case l of
[] -> nil
Pt x0 y0 : pp ->
do Cairo.moveTo x0 y0
let f (Pt x y) = Cairo.lineTo x y
mapM_ f pp
polygon :: Ls R -> Cairo.Render ()
polygon l = if CG.ls_null l then nil else line l >> Cairo.closePath
points :: R -> Ls R -> Cairo.Render ()
points n (Ls l) = do
let f (Pt x y) = Cairo.rectangle x y n n >> Cairo.fill
mapM_ f l
circle :: Pt R -> R -> Cairo.Render ()
circle (Pt x y) z = Cairo.arc x y z 0 (2 * pi)
grey :: R -> Cairo.Render ()
grey x = Cairo.setSourceRGBA x x x 1
colour :: Ca -> Cairo.Render ()
colour c =
let (r,g,b,a) = CG.ca_to_rgba c
in Cairo.setSourceRGBA r g b a
pen :: R -> Ca -> ([R],R) -> Cairo.Render ()
pen lw c (d,d0) = do
Cairo.setLineWidth lw
Cairo.setDash d d0
colour c
area :: R -> Ca -> Ca -> Ls R -> Cairo.Render ()
area lw sc fc a = do
polygon a
colour fc
Cairo.fill
pen lw sc ([],0)
Cairo.stroke
area_def :: Ca -> Ls R -> Cairo.Render ()
area_def = area 0.005 (Colour.opaque (CG.mk_grey 0.15))
outline :: R -> Ca -> Ls R -> Cairo.Render ()
outline lw c l = polygon l >> pen lw c ([],0) >> Cairo.stroke
rect :: R -> Ca -> Pt R -> (R,R) -> Cairo.Render ()
rect lw c (Pt x y) (w,h) = do
Cairo.rectangle x y w h
pen lw c ([],0)
Cairo.stroke
rect_fill :: Ca -> Pt R -> (R,R) -> Cairo.Render ()
rect_fill c (Pt x y) (w,h) = do
Cairo.rectangle x y w h
colour c
Cairo.fill
text :: Ca -> Pt R -> R -> String -> Cairo.Render ()
text c (Pt x y) sz txt = do
let (r,g,b,_) = CG.ca_to_rgba c
Cairo.save
Cairo.selectFontFace "Times" Cairo.FontSlantNormal Cairo.FontWeightNormal
Cairo.setFontSize sz
Cairo.setSourceRGBA r g b 1
Cairo.moveTo x y
Cairo.showText txt
Cairo.restore
data File_Type = F_PDF | F_SVG
maybe_add_extension :: String -> FilePath -> FilePath
maybe_add_extension ext nm = if takeExtension nm == ext then nm else nm <.> ext
render_to_file :: File_Type -> (R,R) -> FilePath -> Cairo.Render () -> IO ()
render_to_file ty (w,h) nm f = do
let r_fn = case ty of
F_PDF -> Cairo.withPDFSurface (maybe_add_extension ".pdf" nm)
F_SVG -> Cairo.withSVGSurface (maybe_add_extension ".svg" nm)
r_fn w h (`Cairo.renderWith` f)
render_to_pdf :: (R,R) -> FilePath -> Cairo.Render () -> IO ()
render_to_pdf = render_to_file F_PDF
render_to_svg :: (R,R) -> FilePath -> Cairo.Render () -> IO ()
render_to_svg = render_to_file F_SVG