comparison samples/freetype.tp @ 321:3edd0169311a

Add basic binding to Freetype2
author Michael Pavone <pavone@retrodev.com>
date Sun, 22 Mar 2015 19:10:32 -0700
parents
children 615f23450f8f
comparison
equal deleted inserted replaced
320:1debeb21dd47 321:3edd0169311a
1 #{
2 import: [
3 video
4 timer
5 ] from: (sdl subsystems)
6
7 import: [
8 quit
9 ] from: (sdl eventTypes)
10
11 import: [
12 streaming
13 ] from: (sdl textureAccess)
14
15 import: [
16 bgra8888
17 ] from: (sdl pixelFormats)
18
19 hex2 <- :num {
20 val <- hex: num
21 if: (val length) < 2 {
22 val <- "0" . val
23 }
24 val
25 }
26
27 main <- :args {
28 retcode <- 0
29 dpi <- 96
30 arg <- 1
31 expectVal <- false
32 optName <- ""
33 path <- ""
34 while: { arg < (args length) } do: {
35 curArg <- args get: arg
36 if: expectVal {
37 if: optName = "--dpi" || optName = "-d" {
38 dpi <- curArg int32
39 } else: {
40 print: "Unrecognized option: " . optName . "\n"
41 }
42 expectVal <- false
43 } else: {
44 if: (curArg startsWith?: "-") {
45 expectVal <- true
46 optName <- curArg
47 } else: {
48 path <- curArg
49 }
50 }
51 arg <- arg + 1
52 }
53 ft <- freetype init
54 maybeFace <- ft faceFromPath: path index: 0
55 charCodes <- #[]
56 maybeFace value: :face {
57 charMap <- face charmap
58 foreach: charMap :char glyph {
59 print: "Char: " . char . ", glyph index: " . glyph . "\n"
60 charCodes append: char
61 }
62
63 if: (sdl init: (video or timer)) = 0 {
64 (sdl createWindow: "Freetype Test" pos: 0 0 size: 512 512 flags: 0u32) value: :window {
65 (window createRenderer: -1 flags: ((window renderOpts) accelerated)) value: :renderer {
66 renderer drawColor!: (sdl r: 255u8 g: 255u8 b: 255u8)
67 (renderer createTexture: bgra8888 access: streaming width: 512 height: 512) value: :drawTex {
68 drawTex blendMode!: ((sdl blendModes) blend)
69 drawTex lockRect: (sdl rect: 0 0 size: 512 512) with: :bytearr pitch {
70 i <- 0
71 n <- charCodes length
72 maxHeight <- 0
73 startY <- 0
74 startX <- 0
75 slot <- face glyphSlot
76 face setCharSize: 12.0 res: dpi
77 while: { i < n && startY < 512 } do: {
78 charCode <- charCodes get: i
79 glyphIndex <- charMap get: charCode else: { 0 }
80 rescode <- face loadGlyph: glyphIndex flags: ((freetype loadFlags) render)
81 if: rescode = 0 {
82 height <- slot bitmapRows
83 width <- slot bitmapWidth
84 if: startX + width > 512 {
85 startY <- startY + maxHeight
86 startX <- 0
87 maxHeight <- 0
88 }
89
90 if: height > maxHeight {
91 maxHeight <- height
92 }
93
94 if: height + startY > 512 {
95 startY <- 512
96 } else: {
97 print: "Rendering glyph " . glyphIndex . " to " . startX . ", " . startY . " (" . (startY * pitch + 4 * startX) . ")\n"
98 print: "Width: " . width . ", Height: " . height . "\n"
99 destY <- startY
100 srcY <- 0
101 srcPitch <- slot bitmapPitch
102 srcBitmap <- slot bitmapData
103 while: { srcY < height } do: {
104 line <- ""
105 destIndex <- destY * pitch + startX * 4
106 srcIndex <- srcY * srcPitch
107 destX <- startX
108 srcX <- 0
109 while: { srcX < width } do: {
110 srcPixel <- srcBitmap get: srcIndex
111 bytearr set: destIndex srcPixel
112 destIndex <- destIndex + 1
113 bytearr set: destIndex 0u8
114 destIndex <- destIndex + 1
115 bytearr set: destIndex 0u8
116 destIndex <- destIndex + 1
117 bytearr set: destIndex 0u8
118 line <- line . " " . (hex2: srcPixel)
119
120 destX <- destX + 1
121 srcX <- srcX + 1
122 destIndex <- destIndex + 1
123 srcIndex <- srcIndex + 1
124 }
125 print: line . "\n"
126 destY <- destY + 1
127 srcY <- srcY + 1
128 }
129 }
130 startX <- startX + width
131
132 i <- i + 1
133 } else: {
134 print: "Got error " . rescode . " when loading glyph " . glyphIndex . "\n"
135 }
136 }
137 }
138 continue? <- true
139 while: { continue? } do: {
140 renderer clear
141 drawTex copy
142 renderer present
143 event <- option none
144 while: {
145 event <- sdl pollEvent
146 event value?
147 } do: {
148 event value: :ev {
149 if: (ev type) = quit {
150 continue? <- false
151 }
152 } none: {}
153 }
154 }
155 } none: {
156 print: "Failed to create texture\n"
157 retcode <- 1
158 }
159 } none: {
160 print: "Failed to create renderer\n"
161 retcode <- 1
162 }
163 window destroy
164 } none: {
165 print: "Failed to create window\n"
166 retcode <- 1
167 }
168 } else: {
169 print: "Failed to initialize SDL\n"
170 retcode <- 1
171 }
172 } none: {
173 retcode <- 1
174 print: "Failed to load font face from " . path . "\n"
175 }
176
177 ft destroy
178 retcode
179 }
180 }