annotate samples/freetype.tp @ 326:50760ba52b11

Added basic rendering of strings to freetype demo
author Michael Pavone <pavone@retrodev.com>
date Tue, 24 Mar 2015 21:50:28 -0700
parents 615f23450f8f
children c1fad3d93861
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #{
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 import: [
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 video
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 timer
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 ] from: (sdl subsystems)
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 import: [
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 quit
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9 ] from: (sdl eventTypes)
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 import: [
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 streaming
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 ] from: (sdl textureAccess)
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 import: [
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 bgra8888
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 ] from: (sdl pixelFormats)
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
19 import: [
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
20 render
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
21 linearDesign
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
22 ] from: (freetype loadFlags)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
23
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
24 makeAtlas <- :renderer face fontSize dpi color {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
25 face setCharSize: fontSize res: dpi
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
26 slot <- face glyphSlot
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
27
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
28 glyphs <- #[]
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
29 //TODO: Use a bytearray once that has an append method
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
30 pixels <- #[]
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
31 foreach: (face charmap) :char glyphIndex {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
32 face loadGlyph: glyphIndex flags: (render or linearDesign)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
33 pixelStart <- pixels length
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
34 _width <- slot bitmapWidth
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
35 _height <- slot bitmapRows
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
36 pitch <- slot bitmapPitch
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
37 buffer <- slot bitmapData
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
38
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
39 y <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
40 while: { y < _height } do: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
41 x <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
42 idx <- y * pitch
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
43 while: { x < _width } do: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
44 pixels append: (buffer get: idx)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
45
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
46 x <- x + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
47 idx <- idx + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
48 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
49 y <- y + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
50 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
51
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
52 glyphs append: #{
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
53 width <- _width
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
54 height <- _height
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
55 pixelOffset <- pixelStart
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
56 hAdvance <- slot linearHoriAdvance
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
57 vAdvance <- slot linearVertAdvance
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
58 leftOffset <- (slot bitmapLeft)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
59 topOffset <- (slot bitmapTop)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
60 charCode <- char
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
61
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
62 atlasX <- -1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
63 atlasY <- -1
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
64 atlasRect <- {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
65 sdl rect: atlasX atlasY size: width height
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
66 }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
67 destRect <- :x y {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
68 sdl rect: x + leftOffset y - topOffset size: width height
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
69 }
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
70
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
71 <= <- :other {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
72 if: height > (other height) {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
73 true
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
74 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
75 if: height < (other height) {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
76 false
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
77 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
78 width >= (other width)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
79 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
80 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
81 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
82 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
83 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
84 glyphs sort
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
85 maxDim <- 2048
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
86 aWidth <- 128
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
87 minSize <- maxDim * maxDim
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
88 minSizeWidth <- -1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
89 aHeight <- maxDim
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
90
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
91 while: { aWidth <= maxDim } do: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
92 print: "Checking width: " . aWidth . "\n"
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
93 curX <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
94 curY <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
95 minHeight <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
96 avail <- glyphs foldr: [] with: :acc val {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
97 val | acc
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
98 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
99 while: { (not: (avail empty?)) && curY <= maxDim } do: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
100 curGlyph <- avail value
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
101 if: curX + (curGlyph width) < aWidth {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
102 if: curY + (curGlyph height) < maxDim {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
103 curX <- curX + (curGlyph width)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
104 avail <- avail tail
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
105 if: (curGlyph height) > minHeight {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
106 minHeight <- curGlyph height
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
107 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
108 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
109 curY <- maxDim + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
110 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
111 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
112 skinny <- option none
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
113 if: aWidth > curX {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
114 availPixels <- aWidth - curX
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
115
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
116 skinny <- avail find: :val {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
117 (val width) <= availPixels
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
118 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
119 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
120
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
121 skinny value: :curGlyph {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
122 curX <- curX + (curGlyph width)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
123 if: (curGlyph height) > minHeight {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
124 minHeight <- curGlyph height
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
125 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
126 avail <- avail filter: :glyph {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
127 (glyph charCode) != (curGlyph charCode)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
128 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
129 } none: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
130 curY <- curY + minHeight
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
131 minHeight <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
132 curX <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
133 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
134 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
135 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
136 if: (avail empty?) {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
137 aHeight <- curY + minHeight
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
138 p2Height <- 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
139 while: { p2Height < aHeight } do: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
140 p2Height <- lshift: p2Height by: 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
141 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
142
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
143 size <- aWidth * p2Height
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
144 if: size < minSize {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
145 minSize <- size
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
146 minSizeWidth <- aWidth
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
147 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
148 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
149 aWidth <- aWidth * 2
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
150 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
151 if: minSizeWidth > -1 {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
152 print: "Best width: " . minSizeWidth . ", height: " . (minSize / minSizeWidth) . "\n"
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
153 aWidth <- minSizeWidth
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
154 aHeight <- minSize / minSizeWidth
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
155
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
156 (renderer createTexture: bgra8888 access: streaming width: aWidth height: aHeight) value: :drawTex {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
157 drawTex blendMode!: ((sdl blendModes) blend)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
158 //TODO: Use "update" with a static texture
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
159 drawTex lockRect: (sdl rect: 0 0 size: aWidth aHeight) with: :bytearr pitch {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
160 n <- aHeight * pitch
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
161 i <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
162 while: { i < n } do: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
163 bytearr set: i 0u8
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
164 i <- i + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
165 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
166 curX <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
167 curY <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
168 minHeight <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
169 avail <- glyphs foldr: [] with: :acc val {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
170 val | acc
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
171 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
172 while: { not: (avail empty?) } do: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
173 curGlyph <- avail value
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
174 if: curX + (curGlyph width) < aWidth {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
175 curGlyph atlasX!: curX
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
176 curGlyph atlasY!: curY
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
177 y <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
178 dstY <- curY
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
179 idx <- curGlyph pixelOffset
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
180 while: { y < (curGlyph height) } do: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
181 dstIdx <- dstY * pitch + curX * 4
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
182 x <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
183 while: { x < (curGlyph width) } do: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
184 //FIXME: This will probably only work on little endian machines
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
185 bytearr set: dstIdx (pixels get: idx)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
186 dstIdx <- dstIdx + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
187 bytearr set: dstIdx (color r)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
188 dstIdx <- dstIdx + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
189 bytearr set: dstIdx (color g)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
190 dstIdx <- dstIdx + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
191 bytearr set: dstIdx (color b)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
192 dstIdx <- dstIdx + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
193
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
194 idx <- idx + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
195 x <- x + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
196 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
197 y <- y + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
198 dstY <- dstY + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
199 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
200
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
201 curX <- curX + (curGlyph width)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
202 avail <- avail tail
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
203 if: (curGlyph height) > minHeight {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
204 minHeight <- curGlyph height
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
205 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
206 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
207 skinny <- option none
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
208 if: aWidth > curX {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
209 availPixels <- aWidth - curX
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
210 skinny <- avail find: :val {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
211 (val width) <= availPixels
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
212 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
213 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
214
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
215 skinny value: :curGlyph {
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
216 curGlyph atlasX!: curX
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
217 curGlyph atlasY!: curY
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
218 y <- 0
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
219 dstY <- curY
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
220 idx <- curGlyph pixelOffset
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
221 while: { y < (curGlyph height) } do: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
222 dstIdx <- dstY * pitch + curX * 4
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
223 x <- 0
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
224 while: { x < (curGlyph width) } do: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
225 //FIXME: This will probably only work on little endian machines
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
226 bytearr set: dstIdx (pixels get: idx)
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
227 dstIdx <- dstIdx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
228 bytearr set: dstIdx (color r)
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
229 dstIdx <- dstIdx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
230 bytearr set: dstIdx (color g)
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
231 dstIdx <- dstIdx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
232 bytearr set: dstIdx (color b)
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
233 dstIdx <- dstIdx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
234
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
235 idx <- idx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
236 x <- x + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
237 }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
238 y <- y + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
239 dstY <- dstY + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
240 }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
241
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
242 curX <- curX + (curGlyph width)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
243 if: (curGlyph height) > minHeight {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
244 minHeight <- curGlyph height
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
245 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
246 avail <- avail filter: :glyph {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
247 (glyph charCode) != (curGlyph charCode)
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
248 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
249 } none: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
250 curY <- curY + minHeight
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
251 minHeight <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
252 curX <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
253 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
254 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
255 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
256 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
257 glyphDict <- dict hash
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
258 foreach: glyphs :idx glyph {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
259 glyphDict set: (glyph charCode) glyph
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
260 }
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
261 _pixelFactor <- ((face unitsPerEm) f64) * 72.0 / (fontSize * (dpi f64))
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
262 option value: #{
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
263 texture <- drawTex
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
264 width <- aWidth
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
265 height <- aHeight
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
266 glyphs <- glyphDict
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
267 drawString:at <- :str :x y {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
268 //pixels to font units
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
269 designPosition <- (x f64) * _pixelFactor
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
270 charIdx <- 0
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
271
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
272 while: { charIdx < (str byte_length) } do: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
273 //TODO: UTF-8
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
274 char <- (str byte: charIdx) uint32
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
275 glyph <- glyphs get: char else: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
276 glyphs get: 0u32 else: { false }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
277 }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
278 texture copyRect: (glyph atlasRect) To: (glyph destRect: x y)
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
279
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
280 designPosition <- designPosition + ((glyph hAdvance) f64)
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
281 x <- (designPosition / _pixelFactor + 0.5) truncInt32
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
282
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
283 charIdx <- charIdx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
284 }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
285 }
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
286 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
287 } none: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
288 print: "Failed to create texture for atlas"
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
289 option none
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
290 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
291 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
292 print: "Font is too big for a 2048x2048 texture!"
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
293 option none
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
294 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
295 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
296
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
297 hex2 <- :num {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
298 val <- hex: num
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
299 if: (val length) < 2 {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
300 val <- "0" . val
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
301 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
302 val
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
303 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
304
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
305 main <- :args {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
306 retcode <- 0
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
307 dpi <- 96
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
308 arg <- 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
309 expectVal <- false
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
310 optName <- ""
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
311 windowWidth <- 512
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
312 windowHeight <- 512
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
313 posArgs <- #[]
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
314 while: { arg < (args length) } do: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
315 curArg <- args get: arg
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
316 if: expectVal {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
317 if: optName = "--dpi" || optName = "-d" {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
318 dpi <- curArg int32
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
319 } else: {
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
320 if: optName = "--height" {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
321 windowHeight <- curArg int32
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
322 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
323 if: optName = "--width" {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
324 windowWidth <- curArg int32
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
325 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
326 print: "Unrecognized option: " . optName . "\n"
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
327 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
328 }
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
329 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
330 expectVal <- false
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
331 } else: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
332 if: (curArg startsWith?: "-") {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
333 expectVal <- true
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
334 optName <- curArg
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
335 } else: {
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
336 posArgs append: curArg
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
337 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
338 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
339 arg <- arg + 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
340 }
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
341 path <- posArgs get: 0
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
342 str <- if: (posArgs length) > 1 { posArgs get: 1 } else: { "" }
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
343 ft <- freetype init
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
344 maybeFace <- ft faceFromPath: path index: 0
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
345 charCodes <- #[]
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
346 maybeFace value: :face {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
347 charMap <- face charmap
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
348 foreach: charMap :char glyph {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
349 print: "Char: " . char . ", glyph index: " . glyph . "\n"
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
350 charCodes append: char
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
351 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
352
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
353 if: (sdl init: (video or timer)) = 0 {
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
354 (sdl createWindow: "Freetype Test" pos: 0 0 size: windowWidth windowHeight flags: 0u32) value: :window {
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
355 (window createRenderer: -1 flags: ((window renderOpts) accelerated)) value: :renderer {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
356 renderer drawColor!: (sdl r: 255u8 g: 255u8 b: 255u8)
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
357
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
358 (makeAtlas: renderer face 12.0 dpi (sdl r: 0u8 g: 0u8 b: 0u8)) value: :atlas {
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
359 continue? <- true
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
360 while: { continue? } do: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
361 renderer clear
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
362 if: (str length) > 0 {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
363 atlas drawString: str at: 0 windowHeight / 2
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
364 } else: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
365 y <- 0
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
366 x <- 0
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
367 while: { y < (atlas height) } do: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
368 copyWidth <- if: (atlas width) < windowWidth { atlas width } else: { windowWidth }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
369 copyHeight <- if: (atlas height) < windowHeight { atlas height } else: { windowHeight }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
370 (atlas texture) copyRect: (sdl rect: 0 y size: copyWidth copyHeight) To: (sdl rect: x 0 size: copyWidth copyHeight)
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
371 y <- y + windowHeight
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
372 x <- x + copyWidth
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
373 }
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
374 }
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
375 renderer present
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
376 event <- option none
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
377 while: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
378 event <- sdl pollEvent
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
379 event value?
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
380 } do: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
381 event value: :ev {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
382 if: (ev type) = quit {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
383 continue? <- false
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
384 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
385 } none: {}
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
386 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
387 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
388 } none: {
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
389 retcode <- -1
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
390 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
391 } none: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
392 print: "Failed to create renderer\n"
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
393 retcode <- 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
394 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
395 window destroy
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
396 } none: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
397 print: "Failed to create window\n"
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
398 retcode <- 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
399 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
400 } else: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
401 print: "Failed to initialize SDL\n"
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
402 retcode <- 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
403 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
404 } none: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
405 retcode <- 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
406 print: "Failed to load font face from " . path . "\n"
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
407 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
408
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
409 ft destroy
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
410 retcode
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
411 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
412 }