summaryrefslogtreecommitdiff
path: root/scripts/FontGlyphCreation.coffee
blob: d334b76bc1179298506dde4b1f07c99899168f6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# This work code was used to create the rendered font glyphs
  
game.vaquitas.splice(0)

@view.coffeecharniaConsole.style.setProperty("opacity", 0.5)

false then document.head.appendChild do(document)@>
    el = document.createElement "link"
    el.rel = "stylesheet"
    el.href = "Simonetta.css"
    el.type = "text/css"
    el

# document.head.lastChild.src
# window.canvaz = null

canvaz = window.canvaz ?= do(document)@>
    el = document.createElement "canvas"
    el.setAttribute "style", "bottom:0;position:absolute;background:red"
    document.body.appendChild el
    el

canvaz.setAttribute "style", "bottom:0;position:absolute;background:#09f"

retroScaling = (c)->
                c.imageSmoothingEnabled = false
                c.webkitImageSmoothingEnabled = false
                c.mozImageSmoothingEnabled = false


# throw canvaz

ctx = canvaz.getContext "2d"
ctx.clearRect 0, 0, 300, 150

retroScaling ctx

fontRenderer =
    ctx: ctx
    put: (t, x, y, c)@>
        @ctx.fillStyle = c
        @ctx.fillText t, x, y
        
    colors: [ "black", "white" ]

    put_b: (t, x, y)@>
        # [ m, n ] = [ "black", "white" ]
        [ m, n ] = @colors
    
        @put t, x-1, y+0, m
        @put t, x+0, y+1, m
        @put t, x+1, y+0, m
        @put t, x+0, y-1, m

        if true
            @ctx.globalAlpha = 0.3
            @put t, x-1, y-1, m
            @put t, x+1, y-1, m
            @put t, x-1, y+1, m
            @put t, x+1, y+1, m
            @ctx.globalAlpha = 1.0

        @put t, x, y, n
        @put t, x, y, n
        @ctx.globalAlpha = 0.3
        @put t, x, y, n
        @ctx.globalAlpha = 1.0

    put_m: (t, x, y)@>
        { width } = @ctx.measureText(t)
        # @ctx.fillStyle = "blue"
        # width++
        # @ctx.globalAlpha = 0.2
        # @ctx.fillRect x, 0, width, @lineHeight
        # @ctx.globalAlpha = 1
        @put_b t, x, y
        
    chars: " abcdefghijklmnopqrstuvwxyzñçABCDEFGHIJKLMNOPQRSTUVWXYZ!?,.@/:;0123456789áéíóúàèìòù"

    measure: (c)@> @ctx.measureText(c).width
    
    fontHeight: 8
    
    document: document
    
    drawingContext: (w,h)@>
        el = @document.createElement "canvas"
        el.width = w
        el.height = h
        el.getContext '2d'
        
    id2dc: (id)@>
        c = @drawingContext(id.width, id.height)
        c.putImageData(id, 0, 0)
        c

    renderChars: @>
        fontHeight = @fontHeight
        @lineHeight = lineHeight = fontHeight + (fontHeight / 2 | 0)

        ctx.font = font = "bold #{fontHeight}px Simonetta"

        false then for x in @chars
            @put_m x, 2, 1+fontHeight
            
        tw = 0
        @charMap = { }
        for x in @chars
            cw = @measure(x)
            @charMap[x] = { w: cw, o: tw }
            tw += cw + 4
        
        @glyphs = @drawingContext ((tw+15) >> 4) << 4, lineHeight
        
        @glyphs.font = font

        cc =
            ctx: @glyphs
            __proto__: @
            
        for ch in @chars
            p = @charMap[ch]
            cc.put_m ch, p.o + 2, 1+fontHeight
        
        @ctx.drawImage @glyphs.canvas, 0, 0, 150, lineHeight, 0, 0, 300, lineHeight * 2
        
        @glyphsConv 'toImageData'
        @glyphsConv 'toDrawingContext'
        @glyphsConv 'toDataUrl'
        @glyphsConv 'fromDataUrl'
        # @drawTextSpaced @chars, 0, lineHeight * 2 # For testing
        @
    test: @>
        { lineHeight } = @
        @drawText "Vilma, the Happy Vaquita!", 30, lineHeight * 2
        
    getFont: @>
        
    
    drawTextSpaced: (t, x, y)@>
        { charmap, lineHeight } = @
        scale = 2
        g = @glyphs.canvas
        for c in t
            p = @charMap[c]
            { w, o } = p
            # throw o
            @ctx.drawImage g, o, 0, w + 4, lineHeight, x, y * scale, (w + 4) * scale, lineHeight * scale
            x += (w + 4) * scale

    drawText: (t, x, y)@>
        { charmap, lineHeight } = @
        scale = 2
        g = @glyphs.canvas
        for c in t
            p = @charMap[c]
            { w, o } = p
            # throw o
            @ctx.drawImage g, o, 0, w + 4, lineHeight, x, y * scale, (w + 4) * scale, lineHeight * scale
            x += (w + 1) * scale
    
    glyphsConv: (format)@>
        @glyphs = ((f)-> f[format]())
            toImageData: =>
                g = @glyphs
                c = g.canvas
                g.getImageData(0, 0, c.width, c.height)
            toDrawingContext:  =>
                @id2dc(@glyphs)
            toDataUrl: => @glyphs.canvas.toDataURL()
            fromDataUrl: =>
                el = @document.createElement "img"
                el.src = @glyphs
                el

fontRenderer.renderChars() # finaly
    # ctx.drawImage canvaz, 0, 0, 50, 100, 50, 100, 100, 200
    # ctx.drawImage canvaz, 0, 0, 25, 50, 25, 50, 100, 200