-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
63 lines (51 loc) · 2.88 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
-- -*- mode: Haskell; fill-column; 79; default-justification: full; -*-
{-# LANGUAGE UnicodeSyntax #-}
module Main ( main
) where
import Control.Monad (forM_, void, when)
import Data.Word (Word8)
import Foreign.Ptr (Ptr, plusPtr)
import Foreign.Marshal.Array (copyArray)
import qualified Graphics.Rendering.Cairo as Cairo
import qualified Graphics.Rendering.Cairo.Internal as CairoInternal
import System.Environment (getArgs)
import System.Framebuffer
-------------------------------------------------------------------------------
main ∷ IO ()
main = do
args ← getArgs
case args of
[path] → withFramebuffer $ \vinfo finfo fbp → do
Cairo.withImageSurface Cairo.FormatARGB32
(fromIntegral $ fixLineLength finfo)
(fromIntegral $ varResolutionY vinfo) $ \surface → do
Cairo.withImageSurfaceFromPNG path $ \imgSurface → do
imgW ← Cairo.imageSurfaceGetWidth imgSurface
imgH ← Cairo.imageSurfaceGetHeight imgSurface
let x = if imgW < (fromIntegral $ varResolutionX vinfo)
then ((fromIntegral $ varResolutionX vinfo) - fromIntegral imgW) / 2.0
else 0.0
y = if imgH < (fromIntegral $ varResolutionY vinfo)
then ((fromIntegral $ varResolutionY vinfo) - fromIntegral imgH) / 2.0
else 0.0
Cairo.renderWith surface $ do
Cairo.setSourceRGBA 1.0 1.0 1.0 1.0
Cairo.setSourceSurface imgSurface x y
Cairo.rectangle x y (fromIntegral $ imgW) (fromIntegral $ imgH)
Cairo.fill
when (imgW < (fromIntegral $ varResolutionX vinfo) &&
imgH < (fromIntegral $ varResolutionY vinfo)) $ do
Cairo.selectFontFace "sans-serif" Cairo.FontSlantNormal Cairo.FontWeightNormal
Cairo.setSourceRGBA 0.8 0.8 0.8 1.0
extents ← Cairo.textExtents path
Cairo.moveTo (((fromIntegral $ varResolutionX vinfo) - Cairo.textExtentsWidth extents) / 2.0)
(y + fromIntegral imgH + Cairo.textExtentsHeight extents)
Cairo.showText path
stride ← Cairo.imageSurfaceGetStride surface
sPtr ← CairoInternal.imageSurfaceGetData surface
forM_ [0 .. varResolutionY vinfo - 1] $ \row →
let dst = fbp `plusPtr` fromIntegral ((row + varOffsetY vinfo) * fixLineLength finfo)
src = sPtr `plusPtr` (stride * fromIntegral row)
in copyArray (dst ∷ Ptr Word8) (src ∷ Ptr Word8) (fromIntegral (varResolutionX vinfo * 4))
void $ getChar
_ → putStrLn "usage: fb-test <png-file>"