-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
156 lines (147 loc) · 5.15 KB
/
index.coffee
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
fontSep = 25
fullCharHeight = 172.35469
## Q extends below baseline
window.font.folded.Q.depth = window.font.folded.Q.height - fullCharHeight
window.font.folded.g.depth = 42.333
window.font.folded.j.depth = 42.333 # compromise
window.font.folded.q.depth = 42.333
#window.font.folded.y.depth = 63.5 # actual depth
window.font.folded.y.depth = 42.333 # compromise to avoid overlap
window.font.folded.A.lead = 69.504
window.font.folded.H.lead = 27.171
window.font.folded.N.lead = 27.171
window.font.folded.U.lead = 26.897
window.font.folded.W.lead = 27.171
window.font.folded.Y.lead = 27.568
window.font.folded['4'].lead = 63.500
window.font.folded['6'].lead = 21.167
window.font.folded.d.lead = 27.146
window.font.folded.g.lead = 37.764
window.font.folded.h.lead = 21.167
window.font.folded.k.lead = 21.167
window.font.folded.m.lead = 21.167
window.font.folded.n.lead = 10.583
window.font.folded.p.lead = 63.5
window.font.folded.q.lead = 21.167
window.font.folded.u.lead = 31.75
window.font.folded.w.lead = 63.5
window.font.folded.y.lead = 63.5
window.font.folded.z.lead = 21.167
letterURL = (letter) ->
#"font.svg##{letter.id}"
"##{letter.id}"
drawLetter = (char, container, state) ->
group = container.group()
width = height = 0
letters =
for subfont in ['unfolded', 'folded'] \
when state.font in [subfont, 'both'] and char of window.font[subfont]
window.font[subfont][char]
for letter, i in letters
height += fontSep if i > 0
use = group.use().attr 'href', letterURL letter
.y height - (letter.height - fullCharHeight) + (letter.depth ? 0)
lead = letter.lead ? 0
letterWidth = letter.width - lead
if letters.length == 2 and letterWidth < letters[1-i].width - (letters[1-i].lead ? 0)
use.x (letters[1-i].width - letterWidth)/2 - lead
else
use.x -lead
width = Math.max width, letterWidth
#height += letter.height - (letter.depth ? 0)
height += fullCharHeight
element: group
#x: 0
#y: 0
width: width
height: height
## Origami Simulator
simulator = null
ready = false
onReady = null
checkReady = ->
if ready
onReady?()
onReady = null
window.addEventListener 'message', (e) ->
if e.data and e.data.from == 'OrigamiSimulator' and e.data.status == 'ready'
ready = true
checkReady()
simulate = (svg) ->
if simulator? and not simulator.closed
simulator.focus()
else
ready = false
#simulator = window.open 'OrigamiSimulator/?model=', 'simulator'
simulator = window.open 'https://origamisimulator.org/?model=', 'simulator'
onReady = -> simulator.postMessage
op: 'importSVG'
svg: svg
vertTol: 0.1
filename: 'strip-simulate.svg'
, '*'
checkReady()
svgPrefixId = (svg, prefix = 'N') ->
svg.replace /\b(id\s*=\s*")([^"]*")/gi, "$1#{prefix}$2"
.replace /\b(xlink:href\s*=\s*"#)([^"]*")/gi, "$1#{prefix}$2"
cleanupSVG = (svg) -> svg
simulateSVG = (svg) ->
explicit = SVG().addTo '#output'
try
explicit.svg svgPrefixId svg.svg(), ''
## Expand <use> into duplicate copies with translation
explicit.find 'use'
.each ->
replacement = document.getElementById @attr('xlink:href').replace /^#/, ''
replacement = null if replacement?.id.startsWith 'f' # remove folded
unless replacement? # reference to non-existing object
return @remove()
replacement = SVG replacement
viewbox = replacement.attr('viewBox') ? ''
viewbox = viewbox.split /\s+/
viewbox = (parseFloat n for n in viewbox)
replacement = svgPrefixId replacement.svg()
replacement = replacement.replace /<symbol\b/, '<g'
replacement = explicit.group().svg replacement
## First transform according to `transform`, then translate by `x`, `y`
replacement.translate \
(@attr('x') or 0) - (viewbox[0] or 0),
(@attr('y') or 0) - (viewbox[1] or 0)
#replacement.translate (@attr('x') or 0), (@attr('y') or 0)
replacement.attr 'viewBox', null
replacement.attr 'id', null
#console.log 'replaced', @attr('xlink:href'), 'with', replacement.svg()
@replace replacement
## Delete now-useless <symbol>s
explicit.find 'symbol'
.each ->
@clear()
explicit.svg()
## Remove surrounding <svg>...</svg> from explicit SVG container
.replace /^<svg[^<>]*>/, ''
.replace /<\/svg>$/, ''
finally
explicit.remove()
window?.onload = ->
app = new FontWebappSVG
root: '#output'
rootSVG: '#svg'
margin: 70
charKern: 25
lineKern: 50
spaceWidth: 85
renderChar: (char, state, group) ->
unless state.lowercase
char = char.toUpperCase()
return unless char of window.font.folded or char of window.font.unfolded
drawLetter char, group, state
document.getElementById('links').innerHTML = (
for char, letter of font.unfolded
"""<a href="#{letter.filename}">#{char}</a>"""
).join ', '
document.getElementById('downloadSVG')?.addEventListener 'click', ->
app.downloadSVG 'impossible.svg', cleanupSVG app.svg.svg()
document.getElementById('downloadSim')?.addEventListener 'click', ->
app.downloadSVG 'impossible-simulate.svg', simulateSVG app.svg
document.getElementById('simulate')?.addEventListener 'click', ->
simulate simulateSVG app.svg