-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbarrish.html
370 lines (321 loc) · 12.6 KB
/
barrish.html
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<!doctype html>
<meta charset="utf-8">
<title>Barrish Calculator</title>
<link rel="stylesheet" href="reset.css">
<link rel="stylesheet" href="typebase.css">
<link rel="stylesheet" href="animate.css">
<link rel="stylesheet" href="whatnot.css">
<script src="lib/coffee-script.js"></script>
<script src="lib/jquery-2.1.4.js"></script>
<script src="helpers.coffee" type="text/coffeescript"></script>
<label>
<h2>Input</h2>
<textarea id="input">! sek ; ! /ng/ʌl.et ; ! te̞l.fɐl.e̞r.et ; bʉnd.et e̞n.krɘ.vɐ mʉt ɘn.se̞p.et jor./ng/on.sɘx ɐk /ng/a.mɐlk.sjɘd , lɘd.ɘ xe̞.dɘn lot.p̪ɘxt se̞.lʌ ɐk ɐld o se̞les me̞.le̞s ; ja/ng/.et ɐk pe̞r.e̞n.se̞p.et s/ju/.ner.dɘ.lo/ng/ fʌr mɐs.jon ɐk op̪.e̞r.ʌ ɘn.dʌ s/ju/.fʉkt bɐ.sak , s/ju/or.sɐ.ke̞n le.nɐrt se.dʌn for.dɘ.bɘ.dɐt ; hɐr.set et.mɐ sʌm.de̞ fʌr /ng/ʌl.ve̞r.jɐn.mɐ.ja ɐk [p̪͡f]a.ve̞r.jɐn.et jed.mek , vol.vɘ.dɘt ɐv kɐ.vɐn.et bor.ɐ ; op̪.e̞r.ʌ.hɐrs.et ɐr.tʉs.ɘl fe̞r.mɐt ɐk fors.ɐ.le.for.hɐn.le̞n o fe̞r.mɐt ɐk fɘl.e̞s.kɐp.bɘst.e̞l ; sɘlv ne.het.e̞r re.sʌ ma.do.vɘn.de̞r.len.fɐt , les.ɐ</textarea>
</textarea>
</label>
<label>
<h2>Output</h2>
<div id="output-area">
<div id="output"></div>
</div>
</label>
<h2>Glyph Table</h2>
<div id="glyph-table" class="table-container"></div>
<script type="text/coffeescript">
alphabet = "Dg97bdeifhjklmnoprstvxAEuVwaJP"
alphabet_vowel_characters = "eioAEuVaJ"
punctuation = ",;…!“”"
font_image_path = "font/barrish.png"
font_image = new Image
font_image_load_callbacks = []
font_image.onload = (e)->
font_image.loaded = yes
fn() for fn in font_image_load_callbacks
when_font_image_is_loaded = (fn)->
if font_image.loaded
fn()
else
font_image_load_callbacks.push(fn)
font_image.src = font_image_path
size = 144
kerning_table = new Map
data_canvas = document.createElement("canvas")
data_ctx = data_canvas.getContext("2d")
get_image_data_for_glyph = (x, y)->
data_canvas.width = size
data_canvas.height = size
data_ctx.drawImage(font_image, 1 - x * size, 1 - y * size)
data_ctx.getImageData(0, 0, size, size)
minimum_space = 10
find_space_between = (image_data_1, image_data_2)->
relevant_rows = []
min_height = Math.min(image_data_1.height, image_data_2.height)
for y in [0..min_height]
present_in_1 = no
present_in_2 = no
for x in [0..image_data_1.width]
present_in_1 = yes if image_data_1.data[(y * image_data_1.width + x) * 4 + 3]
for x in [0..image_data_2.width]
present_in_2 = yes if image_data_2.data[(y * image_data_2.width + x) * 4 + 3]
if present_in_1 and present_in_2
relevant_rows.push(y)
smallest_gap = Infinity
for y in relevant_rows
for x in [image_data_1.width..0]
if image_data_1.data[(y * image_data_1.width + x) * 4 + 3]
rightmost_in_1 = x
break
for x in [0..image_data_2.width]
if image_data_2.data[(y * image_data_2.width + x) * 4 + 3]
leftmost_in_2 = x
break
gap = leftmost_in_2 - rightmost_in_1 + image_data_1.width
smallest_gap = Math.min(smallest_gap, gap)
smallest_gap - minimum_space
kern = (x1, y1, x2, y2)->
id = "(#{x1}, #{y1}) (#{x2}, #{y2})"
if kerning_table.has(id)
return kerning_table.get(id)
image_data_1 = get_image_data_for_glyph(x1, y1)
image_data_2 = get_image_data_for_glyph(x2, y2)
value = find_space_between(image_data_1, image_data_2)
kerning_table.set(id, value)
value
kern_glyph_elements = (left_glyph_el, right_glyph_el)->
{x: x1, y: y1} = left_glyph_el.coordinates
{x: x2, y: y2} = right_glyph_el.coordinates
value = kern(x1, y1, x2, y2)
left_glyph_el.style.marginRight = "#{-value}px"
normalize = (text)->
_text = ""
for c in text
_text += c unless c.charCodeAt(0) in [810, 865]
_text
.replace /p̪/g, "P" # pt
.replace /pj/g, "7"
.replace /\[p̪͡f\]|\[pf\]/g, "9" # pf
.replace /\/dzh\//g, "D" # dzh
.replace /\/ng\//g, "g" # ng
.replace /ʉ/g, "u" # oo
.replace /ɐ/g, "A"
.replace /ʌ/g, "V" # uh
.replace /\/ju\//g, "J" # ju
.replace /e̞/g, "i" # ih
.replace /ɘ/g, "E" # eh
.replace /\s\./g, ";"
.replace /!\s/g, "!"
.replace /\.|·/g, "" # dots can separate syllables
separate_gylphs = (text)->
syllables = text.split(/\.|·/g)
glyphs = []
for syllable in syllables
# cvc = c^c
# cac = cc
# cv = cv
# vc = vc
# cc = c^c
# cvcc = c^c + c
# cacc = cc + c
# ccvc = c^c + vc
# ccac = c^c + vc
# ccvcc = c^c + vc + c
# ccacc = c^c + vc + c
# cccacc = ...
# cvc
# cc
# cv
# vc
# v
# c
letters = normalize(syllable)
letter_types = (
for letter in letters
if alphabet_vowel_characters.indexOf(letter) >= 0
"v"
else
"c"
).join("")
i = 0
while i < letters.length
length_remaining = letters.length - i - 1
if length_remaining >= 2 and letter_types.substr(i, 3).match(/cvc/)
glyphs.push(letters.substr(i, 3))
i += 3
else if length_remaining >= 1 and letter_types.substr(i, 2).match(/cc|cv|vc/)
glyphs.push(letters.substr(i, 2))
i += 2
else if length_remaining >= 0
glyphs.push(letters[i])
i += 1
else
i += 1
glyphs
glyph_name_components = "Dzh Ᵹ P'f Pj B D Æ Ih F H J K L M N Oh P R S T V Xh Ah Eh Uu Uh W Ay Ju P'".split(" ")
vowel_glyph_name_components = "Æ Ih Oh Ah Eh Uu Uh Ay Ju".split(" ")
glyph_name_component_represents_vowel = (glyph_name_component)->
vowel_glyph_name_components.indexOf(glyph_name_component) >= 0
glyph_index_for_vowel_diacritic = (letter)->
alphabet_vowel_characters.indexOf(letter)
name_for_glyph_at = (x, y)->
if y is 31
switch x - alphabet_vowel_characters.length
when 0
"comma"
when 1
"full stop"
when 2
"trailing stop"
when 3
"exclamation mark"
when 4
"opening quotation mark"
when 5
"closing quotation mark"
else
"diacritic for vowel #{vowel_glyph_name_components[x]}"
else
"#{glyph_name_components[x]}" +
if y is 0
if glyph_name_component_represents_vowel(glyph_name_components[x])
""
else
"a"
else
if glyph_name_component_represents_vowel(glyph_name_components[y - 1])
"#{glyph_name_components[y - 1]?.toLowerCase()}"
else
"a#{glyph_name_components[y - 1]?.toLowerCase()}"
# name_for_glyph_at = (x, y, diacritic)->
# x_name = glyph_name_components[x]
# y_name = glyph_name_components[y - 1]
# name = x_name
#
# if y is 0
# if glyph_name_component_represents_vowel(x_name)
# ""
# else
# diacritic ? "a"
# else
# unless y_name?
# console.error x, y, diacritic, x_name, y_name
# unless glyph_name_component_represents_vowel(y_name)
# # if diacritic is "a" then "" else if diacritic? then diacritic else "a"
# # name += "a"
# name += diacritic ? "a"
# name += y_name?.toLowerCase()
# if diacritic is "a"
# name += "a"
# name
glyph_coordinates = (glyph, as_diacritic)->
if glyph.length >= 3
x: alphabet.indexOf(glyph[0])
y: alphabet.indexOf(glyph[2]) + 1
else if glyph.length >= 2
x: alphabet.indexOf(glyph[0])
y: alphabet.indexOf(glyph[1]) + 1
else if glyph in punctuation
x: alphabet_vowel_characters.length + punctuation.indexOf(glyph)
y: 31
else if as_diacritic
x: glyph_index_for_vowel_diacritic(glyph[0])
y: 31
else
x: alphabet.indexOf(glyph[0])
y: 0
render_glyph = (glyph, as_diacritic)->
glyph_el = document.createElement("span")
{x, y} = glyph_coordinates(glyph, as_diacritic)
if glyph.length is 3
diacritic = glyph[1]
glyph_el.style.backgroundImage = "url(#{font_image_path})"
glyph_el.style.backgroundPosition = "#{1 - x * size}px #{1 - y * size}px"
glyph_el.style.display = "inline-block"
glyph_el.style.width = "#{size}px"
glyph_el.style.height = "#{size}px"
glyph_el.title = name_for_glyph_at(x, y, diacritic)
# glyph_el.title = "#{name_for_glyph_at(x, y)} (#{glyph})"
if diacritic?
diacritic_el = render_glyph(diacritic, yes)
glyph_el.style.position = "relative"
diacritic_el.style.position = "absolute"
diacritic_el.style.top = "60%"
diacritic_el.style.left = "-9px" # FIXME: why shouldn't this be 0%? (also should probably do this in the backgroundPosition if anything?)
diacritic_el.style.clip = "rect(50px, auto, auto, auto)"
glyph_el.appendChild(diacritic_el)
glyph_el.glyph = glyph
glyph_el.coordinates = {x, y}
glyph_el
render_barrish = (text, container)->
container.innerHTML = ""
words = text.split(" ")
previous_glyph_el = null
for word in words
for glyph in separate_gylphs(word)
glyph_el = render_glyph(glyph)
container.appendChild(glyph_el)
glyph_el.setAttribute("data-glyph", glyph)
if previous_glyph_el?
do (glyph_el, previous_glyph_el)->
when_font_image_is_loaded ->
kern_glyph_elements(previous_glyph_el, glyph_el)
previous_glyph_el = glyph_el
unless word is "!"
space_el = document.createElement("span")
space_el.textContent = " "
container.appendChild(space_el)
previous_glyph_el = null
input = document.getElementById("input")
output = document.getElementById("output")
do input.onchange = input.oninput = input.onkeyup = ->
render_barrish(input.value, output)
$.get "lexicon.txt", (text)->
words =
for [word, definition] in colon_separated lines_of text
word
# TODO: enumerate occurences of "base glyphs", i.e. ignoring diacritics
glyph_occurrences = {}
for word in words
for glyph in separate_gylphs(word)
glyph_occurrences[glyph] ?= 0
glyph_occurrences[glyph] += 1
sortable_glyph_occurrences = []
for glyph, occurrences of glyph_occurrences
sortable_glyph_occurrences.push {glyph, occurrences}
sortable_glyph_occurrences.sort (a, b)-> a.occurrences - b.occurrences
# do make_glyph_occurrences_table = ->
# table = document.createElement "table"
# for {glyph, occurrences} in sortable_glyph_occurrences
# tr = document.createElement "tr"
# glyph_td = document.createElement "td"
# occurrences_td = document.createElement "td"
# glyph_td.textContent = glyph
# occurrences_td.textContent = occurrences
# tr.appendChild(glyph_td)
# tr.appendChild(occurrences_td)
# table.appendChild(tr)
# document.body.appendChild(table)
# heatmap_color_scale = ['#ffffe0','#fff2c7','#ffe4b1','#ffd69d','#ffc88e','#ffb981','#ffaa76','#ff9a6e','#fc8968','#f77a63','#f16b5f','#e95d5a','#e14f55','#d8404e','#cd3346','#c2263d','#b61832','#a80c25','#9b0316','#8b0000']
heatmap_color_scale = ['#ffffe0','#fffddd','#fffbd9','#fff9d5','#fff7d1','#fff5cd','#fff4ca','#fff1c6','#fff0c2','#ffedbf','#ffebbc','#ffeab8','#ffe8b5','#ffe5b2','#ffe3af','#ffe2ac','#ffe0a9','#ffdda7','#ffdba3','#ffd9a0','#ffd69d','#ffd59b','#ffd299','#ffd096','#ffce94','#ffcc92','#ffca8f','#ffc88d','#ffc68b','#ffc389','#ffc187','#ffbf85','#ffbd83','#ffbb82','#ffb87f','#ffb67e','#ffb37d','#ffb17b','#ffaf79','#ffac78','#ffaa76','#ffa875','#ffa674','#ffa273','#ffa072','#ff9d70','#ff9c6f','#ff996e','#ff976d','#ff946c','#fe916b','#fe8e6a','#fd8d69','#fc8a69','#fc8968','#fb8767','#fa8567','#f98266','#f98065','#f87e64','#f77b64','#f67963','#f57662','#f57462','#f47261','#f37060','#f26e5f','#f16b5f','#f0695e','#ef675d','#ee655d','#ec635c','#eb615b','#ea5f5b','#e95d5a','#e85959','#e75858','#e55658','#e45357','#e35256','#e24f55','#e04d54','#df4b53','#dd4952','#dc4751','#db4550','#d9434f','#d8404e','#d63e4d','#d53c4c','#d43a4b','#d2384a','#d13649','#ce3447','#cd3246','#cc3045','#ca2e43','#c82c42','#c62a41','#c5283f','#c3263d','#c1243c','#bf223b','#be2039','#bb1e37','#b91c35','#b81a34','#b61832','#b41630','#b2152e','#b0132c','#ae112a','#ac0f28','#aa0d26','#a80b24','#a50922','#a40820','#a2061e','#9f051c','#9e041a','#9b0316','#980214','#970112','#95010f','#92010b','#8f0008','#8d0004','#8b0000']
do make_glyph_table = ->
table_container = document.getElementById("glyph-table")
table = document.createElement("div")
table.className = "table"
table.style.position = "relative"
table.style.width = "#{size * 30}px"
table.style.height = "#{size * 32}px"
for y in [0...32]
for x in [0...30]
glyph_el = document.createElement("div")
glyph_el.style.backgroundImage = "url(#{font_image_path})"
glyph_el.style.backgroundPosition = "#{1 - x * size}px #{1 - y * size}px"
glyph_el.style.position = "absolute"
glyph_el.style.left = "#{x * size}px"
glyph_el.style.top = "#{y * size}px"
glyph_el.style.width = "#{size}px"
glyph_el.style.height = "#{size}px"
# glyph_el.style.backgroundColor = heatmap_color_scale[...]
glyph_el.title = name_for_glyph_at(x, y)
table.appendChild(glyph_el)
table_container.appendChild(table)
document.body.appendChild(table_container)
</script>