0 | {- Tian Z (ecburx@burx.vip) -}
  1 |
  2 | module IdrisGL.SDL.SDL_ttf
  3 |
  4 | import IdrisGL.DataType
  5 | import IdrisGL.Color
  6 |
  7 | textStyle2Code : TextStyle -> Int
  8 | textStyle2Code TS_NORMAL        = 0
  9 | textStyle2Code TS_BOLD          = 1
 10 | textStyle2Code TS_ITALIC        = 2
 11 | textStyle2Code TS_UNDERLINE     = 3
 12 | textStyle2Code TS_STRIKETHROUGH = 4
 13 |
 14 | textHinting2Code : TextHinting -> Int
 15 | textHinting2Code TH_NORMAL      = 0
 16 | textHinting2Code TH_LIGHT       = 1
 17 | textHinting2Code TH_MONO        = 2
 18 | textHinting2Code TH_NONE        = 3
 19 |
 20 | {- 
 21 |     FFI 
 22 | -}
 23 |
 24 | frgn : String -> String
 25 | frgn func = "C:" ++ func ++ ",sdl_ttf"
 26 |
 27 | --
 28 |
 29 | %foreign frgn "drawText"
 30 | prim_drawText : AnyPtr 
 31 |               -> String -> Int -> String
 32 |               -> Int -> Int
 33 |               -> Int -> Int -> Int -> Int
 34 |               -> PrimIO ()
 35 |
 36 | ||| Draw blended text to a renderer with default settings.
 37 | ||| @ text Text.
 38 | ||| @ size Font size.
 39 | ||| @ font Path of font file.
 40 | export
 41 | drawText : HasIO io => Renderer
 42 |                     -> (text : String) -> (size : Int) -> (font : String)
 43 |                     -> Coordinate -> Color
 44 |                     -> io ()
 45 | drawText (MkRenderer ren) text size font (MkCoor x y) color
 46 |     = let (r,g,b,a) = rgbaOfColor color in
 47 |       primIO $ prim_drawText ren text size font x y r g b a
 48 |
 49 | --
 50 |
 51 | %foreign frgn "drawSolidText"
 52 | prim_drawSolidText : AnyPtr
 53 |                    -> String -> Int -> String
 54 |                    -> Int -> Int -> Int
 55 |                    -> Int -> Int
 56 |                    -> Int -> Int -> Int -> Int
 57 |                    -> PrimIO ()
 58 |
 59 | ||| Draw solid text to a renderer.
 60 | ||| @ text    Text.
 61 | ||| @ size    Font size.
 62 | ||| @ font    Path of font file.
 63 | ||| @ kerning Set freetype kerning setting.
 64 | export
 65 | drawSolidText : HasIO io => Renderer
 66 |                          -> (text : String) -> (size : Int) -> (font : String)
 67 |                          -> TextStyle -> (kerning : Int) -> TextHinting
 68 |                          -> Coordinate -> Color -> io ()
 69 | drawSolidText (MkRenderer ren) text size font style kerning hinting (MkCoor x y) color
 70 |     = let (r,g,b,a) = rgbaOfColor      color
 71 |           s         = textStyle2Code   style
 72 |           h         = textHinting2Code hinting in 
 73 |       primIO $ prim_drawSolidText ren text size font s kerning h x y r g b a
 74 |
 75 | --
 76 |
 77 | %foreign frgn "drawBlendedText"
 78 | prim_drawBlendedText : AnyPtr
 79 |                     -> String -> Int -> String
 80 |                     -> Int -> Int -> Int
 81 |                     -> Int -> Int
 82 |                     -> Int -> Int -> Int -> Int
 83 |                     -> PrimIO ()
 84 |
 85 | ||| Draw blended text to a renderer.
 86 | ||| @ text    Text.
 87 | ||| @ size    Font size.
 88 | ||| @ font    Path of font file.
 89 | ||| @ kerning Set freetype kerning setting.
 90 | export
 91 | drawBlendedText : HasIO io => Renderer 
 92 |                            -> (text : String) -> (size : Int) -> (font : String)
 93 |                            -> TextStyle -> (kerning : Int) -> TextHinting
 94 |                            -> Coordinate -> Color -> io ()
 95 | drawBlendedText (MkRenderer ren) text size font style kerning hinting (MkCoor x y) color
 96 |     = let (r,g,b,a) = rgbaOfColor      color 
 97 |           s         = textStyle2Code   style
 98 |           h         = textHinting2Code hinting in 
 99 |       primIO $ prim_drawBlendedText ren text size font s kerning h x y r g b a
100 |
101 | --
102 |
103 | %foreign frgn "drawShadedText"
104 | prim_drawShadedText : AnyPtr
105 |                     -> String -> Int -> String
106 |                     -> Int -> Int -> Int
107 |                     -> Int -> Int
108 |                     -> Int -> Int -> Int -> Int
109 |                     -> Int -> Int -> Int -> Int
110 |                     -> PrimIO ()
111 |
112 | ||| Draw shaded text to a renderer.
113 | ||| @ text    Text.
114 | ||| @ size    Font size.
115 | ||| @ font    Path of font file.
116 | ||| @ kerning Set freetype kerning setting.
117 | ||| @ color1  The color to render the text in.
118 | ||| @ color2  The color to render the background box in.
119 | export
120 | drawShadedText : HasIO io => Renderer 
121 |                           -> (text : String) -> (size : Int) -> (font : String)
122 |                           -> TextStyle -> (kerning : Int) -> TextHinting
123 |                           -> Coordinate -> (color1 : Color) -> (color2 : Color) 
124 |                           -> io ()
125 | drawShadedText (MkRenderer ren) 
126 |     text size font 
127 |     style kerning hinting 
128 |     (MkCoor x y) color1 color2
129 |     = let (r1,g1,b1,a1) = rgbaOfColor color1
130 |           (r2,g2,b2,a2) = rgbaOfColor color2
131 |           s             = textStyle2Code style
132 |           h             = textHinting2Code hinting in
133 |       primIO $ prim_drawShadedText ren text size font s kerning h x y r1 g1 b1 a1 r2 g2 b2 a2