This repository has been archived by the owner on Aug 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
/
compressor.html
431 lines (413 loc) · 19 KB
/
compressor.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>Hyphenator.js – pattern compressor</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
body {
font: 11px/1.8em Verdana;
margin-left: 20%;
margin-right:20%;
background-color:#EEEEEE;
width:60em;
}
fieldset {
background-color:#FFFFFF;
border:1px solid #AAAAAA;
margin:1em 0em;
padding: 0em 1em 2em 1em;
}
h1 {
background-color:#FFFFFF;
border:1px solid #AAAAAA;
margin:1em 0em;
color: #404041;
text-align:right;
}
img {
vertical-align:middle;
}
.info_btn {
vertical-align:text-bottom;
cursor:pointer;
}
.info_box {
display:none;
border:1px dashed #AAAAAA;
padding:0.2em 1em;
font-style:italic;
}
.multicol {
text-align:justify;
width: 28em;
float:left;
margin:0em 1em;
}
ul.multicol {
width:21em;
padding:0;
margin:0;
}
li {
list-style:none;
}
form {
clear: both;
padding-top:2em;
}
legend {
border:1px solid #AAAAAA;
background-color:#FFFFFF;
padding:0em 1em;
font-style: italic;
font-weight: 200;
}
input {
margin-right:1em;
}
textarea {
width:100%;
}
.grayedout {
border: 1px solid grey;
color: grey;
}
#fields {
float: left;
margin-right: 10px;
}
#desc {
font-size: 12px;
}
</style>
<script type="text/javascript" src="Hyphenator.js"></script>
<script type="text/javascript">
var newpatterns, specialChars;
function lengthSort(a, b) {
'use strict';
return a.length - b.length;
}
function output2String(meta) {
'use strict';
var tmp, len;
tmp = 'Hyphenator.languages["' + document.getElementsByName('language')[0].value + '"] = {\n';
tmp += '\tleftmin : ' + document.getElementsByName('leftmin')[0].value + ',\n';
tmp += '\trightmin : ' + document.getElementsByName('rightmin')[0].value + ',\n';
tmp += '\tspecialChars : "' + specialChars + '",\n';
tmp += '\tpatterns : {\n';
for (len in newpatterns) {
if (newpatterns.hasOwnProperty(len)) {
tmp += '\t\t' + len + ' : "' + newpatterns[len] + '",\n';
}
}
tmp = tmp.slice(0, -2);
tmp += '\n\t},\n';
tmp += '\tpatternChars : "' + meta.patternChars + '",\n';
tmp += '\tpatternArrayLength : ' + meta.patternArrayLength + ',\n';
tmp += '\tvalueStoreLength : ' + meta.valueStoreLength + '\n};';
document.getElementsByName('newpattern')[0].value = tmp;
}
function createAndMeasure() {
'use strict';
var valueStore = (function () {
var ValueStore = function () {
this.keys = [];
this.startIndex = 1;
this.actualIndex = 2;
this.lastValueIndex = 2;
this.add = function (p) {
this.keys[this.actualIndex] = p;
if (p !== 0) {
this.lastValueIndex = this.actualIndex;
}
this.actualIndex += 1;
};
this.finalize = function () {
var start = this.startIndex;
this.keys[start] = this.lastValueIndex - start;
this.startIndex = this.lastValueIndex + 1;
this.actualIndex = this.startIndex + 1;
return start;
};
};
return new ValueStore();
}()),
indexedTrie = [],
charMap = (function () {
var CharMap = function () {
this.keys = [];
this.values = {};
this.add = function (newValue) {
if (!this.values[newValue]) {
this.keys.push(newValue);
this.values[newValue] = this.keys.length - 1;
}
};
};
return new CharMap();
}()),
collected = {},
collectPatternChars = function (patterns) {
var chr = "",
i;
for (i = 0; i < patterns.length; i += 1) {
chr = patterns.charAt(i);
if (!chr.match(/\d/) && chr !== " " && !collected.hasOwnProperty(chr)) {
collected[chr] = true;
}
}
},
patternChars,
charMapValues,
steps,
indexedTrieMaxRow,
extract = function (patternSizeInt, patterns) {
var charPos = 0,
charCode = 0,
prevMappedCharCode = 0,
mappedCharCode = 0,
row = 0,
nextRow = 0,
prevWasPosition = false;
for (charPos = 0; charPos < patterns.length; charPos += 1) {
charCode = patterns.charCodeAt(charPos);
if ((charPos + 1) % patternSizeInt !== 0) {
//more to come…
if (charCode >= 49 && charCode <= 57) {
valueStore.add(charCode - 48);
prevWasPosition = true;
} else {
if (!prevWasPosition) {
valueStore.add(0);
}
prevWasPosition = false;
mappedCharCode = charMapValues[charCode];
if (nextRow === -1) {
nextRow = indexedTrieMaxRow + steps;
indexedTrieMaxRow = nextRow;
indexedTrie[row + prevMappedCharCode * 2] = nextRow;
}
row = nextRow;
if (indexedTrie[row + mappedCharCode * 2] !== undefined && indexedTrie[row + mappedCharCode * 2] !== 0) {
nextRow = indexedTrie[row + mappedCharCode * 2];
} else {
indexedTrie[row + mappedCharCode * 2] = -1;
nextRow = -1;
}
prevMappedCharCode = mappedCharCode;
}
} else {
//last part of pattern
if (charCode >= 49 && charCode <= 57) {
valueStore.add(charCode - 48);
indexedTrie[row + mappedCharCode * 2 + 1] = valueStore.finalize();
} else {
if (!prevWasPosition) {
valueStore.add(0);
}
valueStore.add(0);
mappedCharCode = charMapValues[charCode];
if (nextRow === -1) {
nextRow = indexedTrieMaxRow + steps;
indexedTrieMaxRow = nextRow;
indexedTrie[row + prevMappedCharCode * 2] = nextRow;
}
row = nextRow;
if (indexedTrie[row + mappedCharCode * 2] !== undefined && indexedTrie[row + mappedCharCode * 2] !== 0) {
indexedTrie[row + mappedCharCode * 2 + 1] = valueStore.finalize();
} else {
indexedTrie[row + mappedCharCode * 2] = -1;
indexedTrie[row + mappedCharCode * 2 + 1] = valueStore.finalize();
}
}
row = 0;
nextRow = 0;
prevMappedCharCode = 0;
prevWasPosition = false;
}
}
},
i;
for (i in newpatterns) {
if (newpatterns.hasOwnProperty(i)) {
collectPatternChars(newpatterns[i]);
}
}
patternChars = [];
for (i in collected) {
if (collected.hasOwnProperty(i)) {
patternChars.push(i);
}
}
patternChars.sort();
for (i = 0; i < patternChars.length; i += 1) {
charMap.add(patternChars[i].charCodeAt(0));
}
patternChars = patternChars.join("");
charMapValues = charMap.values;
steps = charMap.keys.length * 2;
indexedTrieMaxRow = 0;
for (i in newpatterns) {
if (newpatterns.hasOwnProperty(i)) {
extract(parseInt(i, 10), newpatterns[i]);
}
}
return {
"patternChars": patternChars,
"patternArrayLength": indexedTrie.length / 2,
"valueStoreLength": valueStore.lastValueIndex + 1
};
}
function arr2uniqueString(arr) {
'use strict';
var s = {},
i,
special = "";
for (i = 0; i < arr.length; i += 1) {
s[arr[i]] = true;
}
for (i in s) {
if (s.hasOwnProperty(i)) {
special += i;
}
}
return special;
}
function startConversion() {
'use strict';
var len, i, curlen, key, special, meta,
oldpatternString = document.getElementsByName('oldpattern')[0].value,
oldpatterns;
newpatterns = {};
special = oldpatternString.match(/[^a-z0-9\. ]/gi);
if (special) {
specialChars = arr2uniqueString(special);
} else {
specialChars = '';
}
oldpatternString = oldpatternString.replace(/ $/, ''); //trim
oldpatternString = oldpatternString.replace(/\./g, '_'); // . -> _
oldpatterns = oldpatternString.split(' ');
oldpatterns.sort(lengthSort);
len = oldpatterns.length;
//Sort patterns -> better compression
for (i = 0; i < len; i += 1) {
curlen = oldpatterns[i].length;
if (newpatterns.hasOwnProperty(curlen)) {
newpatterns[curlen].push(oldpatterns[i]);
} else {
newpatterns[curlen] = [oldpatterns[i]];
}
}
for (key in newpatterns) {
if (newpatterns.hasOwnProperty(key)) {
newpatterns[key].sort();
newpatterns[key] = newpatterns[key].join("");
}
}
meta = createAndMeasure();
output2String(meta);
}
Hyphenator.run();
window.onload = function () {
'use strict';
var desc = document.getElementById('desc');
document.getElementsByName('language')[0].onfocus = function () {
if (this.value === 'language') {
this.value = '';
}
desc.innerHTML = "<em>Define language:</em><br>Use the <a href='http://www.loc.gov/standards/iso639-2/php/code_list.php'>ISO 639-1</a> two-letter language code.<br>(e.g. 'en' for english.)";
};
document.getElementsByName('language')[0].onblur = function () {
if (this.value === '') {
this.value = 'language';
}
desc.innerHTML = "";
};
document.getElementsByName('leftmin')[0].onfocus = function () {
if (this.value === 'leftmin') {
this.value = '';
}
desc.innerHTML = "<em>Define leftmin:</em><br>Define how many letters at minimum should stay on the old line upon hyphenation.<br>(e.g.: setting this to 3 would prevent 'Hy-phenation')";
};
document.getElementsByName('leftmin')[0].onblur = function () {
if (this.value === '') {
this.value = 'leftmin';
}
desc.innerHTML = "";
};
document.getElementsByName('rightmin')[0].onfocus = function () {
if (this.value === 'rightmin') {
this.value = '';
}
desc.innerHTML = "<em>Define rightmin:</em><br>Define how many letters at minimum should stay on the new line upon hyphenation.<br>(e.g.: setting this to 3 would prevent 'divid-ed')";
};
document.getElementsByName('rightmin')[0].onblur = function () {
if (this.value === '') {
this.value = 'rightmin';
}
desc.innerHTML = "";
};
document.getElementsByName('oldpattern')[0].onfocus = function () {
if (this.value === '.a5 .rt4 .d4gt2 asdf2 9kas3') {
this.value = '';
}
desc.innerHTML = "<em>Define oldpattern:</em><br>Paste the patterns you like to convert. They have to be separated by spaces, not newlines! Points (.) will be automatically replaced by underscores (_).<br>(e.g.: .ach4 .ad4der .af1t .al3t .am5at .an5c .ang4 .ani5m .ant4)";
};
document.getElementsByName('oldpattern')[0].onblur = function () {
if (this.value === '') {
this.value = '.a5 .rt4 .d4gt2 asdf2 9kas3';
}
desc.innerHTML = "";
};
document.getElementById('myform').onsubmit = function (e) {
if (e && e.preventDefault) {
e.preventDefault();
} else if (window.event) {
window.event.returnValue = false;
}
startConversion();
};
};
</script>
</head>
<body>
<h1>Compressor for
<svg height="60" width="150" version="1.1" style="vertical-align: middle">
<g font-family="Consolas" font-weight="bold" font-size="36px" transform="translate(-90,-191)">
<text y="230" x="139" fill="#bbb">&shy;</text>
<text line-height="70%" fill="#444">
<tspan y="218" x="103">Hyphen-</tspan>
<tspan y="243" x="103">ator.js</tspan>
</text>
<text font-weight="normal" line-height="125%" font-size="32px" y="228" x="89" fill="#000">{</text>
</g>
</svg>
</h1>
<p class="multicol hyphenate">
Compressor converts standard TeX hyphenation patterns to a special format used by hyphenator.js and adds some meta-information automatically. Just fill in the language, leftmin, rightmin and the patterns (overwrite the examples!) and click 'Convert!'.</p>
<p class="multicol hyphenate">
Copy the output and paste it in the appropriate pattern file ('patterns/lang.js').<br>
The script loads the version of Hyphenator.js that resides in the same directory as this page.
</p>
<form action="#outanchor" method="post" id="myform">
<fieldset>
<legend>Paste TeX-styled patterns:</legend>
<p id="fields">
<input type="text" name="language" value="language"><br>
<input type="text" name="leftmin" value="leftmin"><br>
<input type="text" name="rightmin" value="rightmin">
</p>
<p id="desc"></p>
<textarea name="oldpattern" cols="80" rows="20">hy3ph he2n hena4 hen5at 1na n2at 1tio 2io o2n</textarea>
<input type="submit" value="Convert!"><input type="reset" value="Reset">
</fieldset>
<fieldset>
<legend>Copy Hyphenator.js pattern files:</legend>
<p>Copy the output and paste it in the appropriate pattern file ('patterns/<lang>.js').</p>
<textarea name="newpattern" cols="80" rows="20"></textarea>
</fieldset>
</form>
</body>
</html>