module Render.CG.Minus where
import qualified Codec.Binary.UTF8.String as U
import Data.CG.Minus
import Data.CG.Minus.Colour
import Data.Colour
import qualified Graphics.Rendering.Cairo as C
import System.FilePath
nil :: C.Render ()
nil = return ()
line :: Ls R -> C.Render ()
line l =
case l of
[] -> nil
Pt x0 y0 : pp ->
do C.moveTo x0 y0
let f (Pt x y) = C.lineTo x y
mapM_ f pp
polygon :: Ls R -> C.Render ()
polygon l =
case l of
[] -> nil
_ -> line l >> C.closePath
points :: R -> Ls R -> C.Render ()
points n l = do
let f (Pt x y) = C.rectangle x y n n >> C.fill
mapM_ f l
circle :: Pt R -> R -> C.Render ()
circle (Pt x y) z = C.arc x y z 0 (2 * pi)
grey :: R -> C.Render ()
grey x = C.setSourceRGBA x x x 1
colour :: Ca -> C.Render ()
colour c =
let (r,g,b,a) = unCa c
in C.setSourceRGBA r g b a
pen :: R -> Ca -> C.Render ()
pen lw c = C.setLineWidth lw >> colour c
area :: R -> Ca -> Ca -> Ls R -> C.Render ()
area lw sc fc a = do
polygon a
colour fc
C.fill
pen lw sc
C.stroke
area' :: Ca -> Ls R -> C.Render ()
area' = area 0.005 (opaque (mk_grey 0.15))
outline :: R -> Ca -> Ls R -> C.Render ()
outline lw c l = polygon l >> pen lw c >> C.stroke
rect :: Ca -> Pt R -> (R,R) -> C.Render ()
rect c (Pt x y) (w,h) = do
C.rectangle x y w h
pen 0.05 c
C.stroke
text :: Ca -> Pt R -> R -> String -> C.Render ()
text c (Pt x y) sz txt = do
let (r,g,b,_) = unCa c
C.save
C.selectFontFace "Times" C.FontSlantNormal C.FontWeightNormal
C.setFontSize sz
C.setSourceRGBA r g b 1
C.moveTo x y
C.showText (U.utf8Encode txt)
C.restore
data File_Type = F_PDF | F_SVG
render_to_file :: File_Type -> (R,R) -> FilePath -> C.Render () -> IO ()
render_to_file ty (w,h) nm f = do
let r_fn = case ty of
F_PDF -> C.withPDFSurface (nm <.> "pdf")
F_SVG -> C.withSVGSurface (nm <.> "svg")
r_fn w h (`C.renderWith` f)