1import Graphics.Rendering.Cairo
2import qualified Graphics.Rendering.Cairo.Matrix as M
3
4boxText :: String -> Double -> Double -> Render ()
5boxText text x y = do
6  save
7
8  lineWidth <- getLineWidth
9
10  (TextExtents xb yb w h _ _) <- textExtents text
11
12  rectangle (x + xb - lineWidth)
13            (y + yb - lineWidth)
14            (w + 2 * lineWidth)
15            (h + 2 * lineWidth)
16  stroke
17  moveTo x y
18  textPath text
19  fillPreserve
20  setSourceRGBA 0 0 1 0.5
21  setLineWidth 3.0
22  stroke
23
24  restore
25
26transpSurface :: Double -> Double -> Render ()
27transpSurface w h = do
28  save
29  rectangle 0 0 w h
30  setSourceRGBA 0 0 0 0
31  setOperator OperatorSource
32  fill
33  restore
34
35sWidth = 400
36sHeight = 300
37
38main :: IO ()
39main = withImageSurface FormatARGB32 sWidth sHeight $ \surface -> do
40  renderWith surface $ do
41    setSourceRGB 0.0 0.0 0.0
42    setLineWidth 2.0
43
44    transpSurface (fromIntegral sWidth) (fromIntegral sHeight)
45
46    selectFontFace "sans" FontSlantNormal FontWeightNormal
47    setFontSize 40
48
49    extents <- fontExtents
50    let fontHeight = fontExtentsHeight extents
51
52    boxText "Howdy, world!" 10 fontHeight
53
54    translate 0 fontHeight
55
56    save
57    translate 10 fontHeight
58    rotate (10.0 * pi / 180.0)
59    boxText "Yay for Haskell!" 0 0
60    restore
61
62    translate 0 (3 * fontHeight)
63
64    save
65    setFontMatrix $ M.rotate ((-10.0) * pi / 180.0) $ M.scale 40.0 40.0 M.identity
66    boxText "...and Cairo!" 10 fontHeight
67    restore
68
69  surfaceWriteToPNG surface "Text.png"
70
71  return ()
72