[Haskell-cafe] Drawing an existing image into a cairo surface?

Rafal Kolanski xs at xaph.net
Sun Sep 21 09:01:00 EDT 2008

Austin Seipp wrote:
> Excerpts from Rafal Kolanski's message of Sun Sep 21 07:28:37 -0500 2008:
>> The best I can find is withImageSurfaceFromPNG, but I can't
>> make it work.
> Why not? Seems to me all you need to do is:
>  withImageSurfaceFromPNG "blah.png" $ \surface -> do
>     ...
> Lots of code is written this way (create a resource, pass it to a
> function and then release it after the function is over.)

Well, but withImageSurfaceFromPNG only lets you work within that scope
and wipes out the surface straight afterwards ...

>> I tried playing around with unsafePerformIO but that just gets me into:
> Without further context as to what you are doing, I really see no
> reason why you would have to use unsafePerformIO at all.

The context is, I want to render a multi-page PDF with a variety of
things on it, so I have something like (very simplified):

main :: IO ()
main = do
     withPDFSurface outputFile pdfWidth pdfHeight renderer

renderer s = renderWith s renderPresentation

renderPresentation = do

myDraw1 = do
     setSourceRGB 1 0 0
     setLineWidth 1
     moveTo 0 0
     lineTo 100 100

so renderPresentation will have type Render ()

Now lets say I want to draw an image on page 2, that will only appear on
page 2 ... with one image, your technique makes sense to me, but with
multiple images on multiple pages, I don't understand how to make it scale.

Perhaps this code is very naive, but I'm kinda new to the pure world of


Rafal Kolanski.

More information about the Haskell-Cafe mailing list