annotate samples/freetype.tp @ 328:c1fad3d93861

Add getKerning to freetype module and use it in sample
author Michael Pavone <pavone@retrodev.com>
date Wed, 25 Mar 2015 00:16:37 -0700
parents 50760ba52b11
children e70f9d3f19f8
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 <- #[]
328
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
31 foreach: (face charmap) :char glyphIdx {
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
32 face loadGlyph: glyphIdx flags: (render or linearDesign)
324
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
328
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
61 glyphIndex <- glyphIdx
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
62
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
63 atlasX <- -1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
64 atlasY <- -1
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
65 atlasRect <- {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
66 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
67 }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
68 destRect <- :x y {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
69 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
70 }
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
71
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
72 <= <- :other {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
73 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
74 true
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
75 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
76 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
77 false
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
78 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
79 width >= (other width)
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 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
85 glyphs sort
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
86 maxDim <- 2048
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
87 aWidth <- 128
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
88 minSize <- maxDim * maxDim
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
89 minSizeWidth <- -1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
90 aHeight <- maxDim
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
91
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
92 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
93 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
94 curX <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
95 curY <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
96 minHeight <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
97 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
98 val | acc
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
99 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
100 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
101 curGlyph <- avail value
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
102 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
103 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
104 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
105 avail <- avail tail
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
106 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
107 minHeight <- curGlyph height
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
108 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
109 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
110 curY <- maxDim + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
111 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
112 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
113 skinny <- option none
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
114 if: aWidth > curX {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
115 availPixels <- aWidth - curX
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
116
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
117 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
118 (val width) <= availPixels
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
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
122 skinny value: :curGlyph {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
123 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
124 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
125 minHeight <- curGlyph height
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
126 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
127 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
128 (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
129 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
130 } none: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
131 curY <- curY + minHeight
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
132 minHeight <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
133 curX <- 0
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 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
137 if: (avail empty?) {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
138 aHeight <- curY + minHeight
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
139 p2Height <- 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
140 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
141 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
142 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
143
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
144 size <- aWidth * p2Height
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
145 if: size < minSize {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
146 minSize <- size
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
147 minSizeWidth <- aWidth
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 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
150 aWidth <- aWidth * 2
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
151 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
152 if: minSizeWidth > -1 {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
153 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
154 aWidth <- minSizeWidth
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
155 aHeight <- minSize / minSizeWidth
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
156
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
157 (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
158 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
159 //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
160 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
161 n <- aHeight * pitch
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
162 i <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
163 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
164 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
165 i <- i + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
166 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
167 curX <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
168 curY <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
169 minHeight <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
170 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
171 val | acc
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
172 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
173 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
174 curGlyph <- avail value
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
175 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
176 curGlyph atlasX!: curX
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
177 curGlyph atlasY!: curY
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
178 y <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
179 dstY <- curY
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
180 idx <- curGlyph pixelOffset
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
181 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
182 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
183 x <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
184 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
185 //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
186 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
187 dstIdx <- dstIdx + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
188 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
189 dstIdx <- dstIdx + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
190 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
191 dstIdx <- dstIdx + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
192 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
193 dstIdx <- dstIdx + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
194
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
195 idx <- idx + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
196 x <- x + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
197 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
198 y <- y + 1
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
199 dstY <- dstY + 1
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
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
202 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
203 avail <- avail tail
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
204 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
205 minHeight <- curGlyph height
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
206 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
207 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
208 skinny <- option none
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
209 if: aWidth > curX {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
210 availPixels <- aWidth - curX
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
211 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
212 (val width) <= availPixels
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
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
216 skinny value: :curGlyph {
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
217 curGlyph atlasX!: curX
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
218 curGlyph atlasY!: curY
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
219 y <- 0
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
220 dstY <- curY
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
221 idx <- curGlyph pixelOffset
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
222 while: { y < (curGlyph height) } do: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
223 dstIdx <- dstY * pitch + curX * 4
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
224 x <- 0
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
225 while: { x < (curGlyph width) } do: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
226 //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
227 bytearr set: dstIdx (pixels get: idx)
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
228 dstIdx <- dstIdx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
229 bytearr set: dstIdx (color r)
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
230 dstIdx <- dstIdx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
231 bytearr set: dstIdx (color g)
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
232 dstIdx <- dstIdx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
233 bytearr set: dstIdx (color b)
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
234 dstIdx <- dstIdx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
235
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
236 idx <- idx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
237 x <- x + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
238 }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
239 y <- y + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
240 dstY <- dstY + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
241 }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
242
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
243 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
244 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
245 minHeight <- curGlyph height
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
246 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
247 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
248 (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
249 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
250 } none: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
251 curY <- curY + minHeight
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
252 minHeight <- 0
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
253 curX <- 0
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 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
258 glyphDict <- dict hash
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
259 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
260 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
261 }
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
262 _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
263 option value: #{
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
264 texture <- drawTex
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
265 width <- aWidth
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
266 height <- aHeight
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
267 glyphs <- glyphDict
328
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
268 drawString:at:useKerning? <- :str :xPos yPos :kern? {
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
269 //pixels to font units
328
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
270 designPosition <- (xPos f64) * _pixelFactor
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
271 charIdx <- 0
328
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
272 last <- 0u32
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
273 useKerning? <- kern? && (face hasKerning?)
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
274
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
275 while: { charIdx < (str byte_length) } do: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
276 //TODO: UTF-8
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
277 char <- (str byte: charIdx) uint32
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
278 glyph <- glyphs get: char else: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
279 glyphs get: 0u32 else: { false }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
280 }
328
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
281 texture copyRect: (glyph atlasRect) To: (glyph destRect: xPos yPos)
326
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 designPosition <- designPosition + ((glyph hAdvance) f64)
328
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
284 if: charIdx > 0 && useKerning? {
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
285 (face getKerning: last (glyph glyphIndex) mode: ((freetype kerning) unscaled)) value: :kern {
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
286 designPosition <- designPosition + ((kern x) f64)
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
287 } none: {}
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
288 }
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
289 xPos <- (designPosition / _pixelFactor + 0.5) truncInt32
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
290
328
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
291 last <- glyph glyphIndex
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
292 charIdx <- charIdx + 1
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
293 }
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
294 }
324
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 } none: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
297 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
298 option none
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
299 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
300 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
301 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
302 option none
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
303 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
304 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
305
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
306 hex2 <- :num {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
307 val <- hex: num
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
308 if: (val length) < 2 {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
309 val <- "0" . val
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
310 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
311 val
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
312 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
313
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
314 main <- :args {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
315 retcode <- 0
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
316 dpi <- 96
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
317 arg <- 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
318 expectVal <- false
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
319 optName <- ""
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
320 windowWidth <- 512
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
321 windowHeight <- 512
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
322 posArgs <- #[]
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
323 while: { arg < (args length) } do: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
324 curArg <- args get: arg
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
325 if: expectVal {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
326 if: optName = "--dpi" || optName = "-d" {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
327 dpi <- curArg int32
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
328 } else: {
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
329 if: optName = "--height" {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
330 windowHeight <- curArg int32
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
331 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
332 if: optName = "--width" {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
333 windowWidth <- curArg int32
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
334 } else: {
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
335 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
336 }
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
337 }
321
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 expectVal <- false
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
340 } else: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
341 if: (curArg startsWith?: "-") {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
342 expectVal <- true
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
343 optName <- curArg
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
344 } else: {
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
345 posArgs append: curArg
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
346 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
347 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
348 arg <- arg + 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
349 }
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
350 path <- posArgs get: 0
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
351 str <- if: (posArgs length) > 1 { posArgs get: 1 } else: { "" }
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
352 ft <- freetype init
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
353 maybeFace <- ft faceFromPath: path index: 0
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
354 charCodes <- #[]
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
355 maybeFace value: :face {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
356 charMap <- face charmap
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
357 foreach: charMap :char glyph {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
358 print: "Char: " . char . ", glyph index: " . glyph . "\n"
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
359 charCodes append: char
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
360 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
361
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
362 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
363 (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
364 (window createRenderer: -1 flags: ((window renderOpts) accelerated)) value: :renderer {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
365 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
366
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
367 (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
368 continue? <- true
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
369 while: { continue? } do: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
370 renderer clear
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
371 if: (str length) > 0 {
328
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
372 atlas drawString: str at: 0 windowHeight / 3 useKerning?: true
c1fad3d93861 Add getKerning to freetype module and use it in sample
Michael Pavone <pavone@retrodev.com>
parents: 326
diff changeset
373 atlas drawString: str at: 0 windowHeight * 2 / 3 useKerning?: false
326
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
374 } else: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
375 y <- 0
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
376 x <- 0
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
377 while: { y < (atlas height) } do: {
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
378 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
379 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
380 (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
381 y <- y + windowHeight
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
382 x <- x + copyWidth
50760ba52b11 Added basic rendering of strings to freetype demo
Michael Pavone <pavone@retrodev.com>
parents: 324
diff changeset
383 }
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
384 }
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
385 renderer present
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
386 event <- option none
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
387 while: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
388 event <- sdl pollEvent
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
389 event value?
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
390 } do: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
391 event value: :ev {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
392 if: (ev type) = quit {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
393 continue? <- false
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 } none: {}
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
396 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
397 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
398 } none: {
324
615f23450f8f Freetype sample: Build texture of glyphs in a more intelligent way
Michael Pavone <pavone@retrodev.com>
parents: 321
diff changeset
399 retcode <- -1
321
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
400 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
401 } none: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
402 print: "Failed to create renderer\n"
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
403 retcode <- 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
404 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
405 window destroy
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
406 } none: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
407 print: "Failed to create window\n"
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
408 retcode <- 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
409 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
410 } else: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
411 print: "Failed to initialize SDL\n"
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
412 retcode <- 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
413 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
414 } none: {
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
415 retcode <- 1
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
416 print: "Failed to load font face from " . path . "\n"
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
417 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
418
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
419 ft destroy
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
420 retcode
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
421 }
3edd0169311a Add basic binding to Freetype2
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
422 }