-
Notifications
You must be signed in to change notification settings - Fork 4
/
easy-api.js
549 lines (450 loc) · 14.9 KB
/
easy-api.js
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
(function ( root, factory ) {
if ( typeof exports === 'object' ) {
// CommonJS
factory(exports);
} else if ( typeof define === 'function' && define.amd ) {
// AMD. Register as an anonymous module.
define(['exports'], factory);
} else {
// Browser globals
factory({browserGlobal: true}, root);
}
}(this, function(liblouis, ns) { "use strict";
var API_METHODS = ["version", "setLogLevel", "getTable", "checkTable", "free",
"charSize", "setDataPath", "getDataPath", "getFilesystem",
"registerLogCallback", "backTranslateString", "translateString",
"compileString", "loadTable", "enableOnDemandTableLoading",
"disableOnDemandTableLoading"];
var IS_BROWSER_GUI_THREAD = typeof window !== 'undefined';
var IS_WEB_WORKER = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
var IS_NODE = !IS_BROWSER_GUI_THREAD && !IS_WEB_WORKER;
function createWorker() {
var blob = new Blob([[
"var CMD = {",
"init: function(path) {",
// TODO: this is kind of dangerous...
'importScripts(path.capi);',
'importScripts(path.easyapi);',
'liblouis.registerLogCallback(function(logLevel, msg){',
'self.postMessage({isLog: true, level: liblouis.LOG[logLevel], msg: msg});',
'});',
"},",
"lou: function(data) {",
"if(LiblouisEasyApi.prototype.hasOwnProperty(data.fn) && typeof liblouis[data.fn] == 'function') {",
"return liblouis[data.fn].apply(liblouis, data.args);",
"}",
"return null",
"}",
"};",
"self.onmessage = function (ev) {",
"var msg = ev.data;",
"if(!msg.cmd || !CMD.hasOwnProperty(msg.cmd)) {",
"return;",
"}",
"var res = CMD[msg.cmd](msg.data);",
"self.postMessage({",
"callId: msg.callId,",
"data: res",
"});",
"};"
].join("\n")], { type: "text/javascript" });
return new Worker(window.URL.createObjectURL(blob));
}
function bindDynLoader(self) {
if(!self.capi.FS_ORIGINAL_LOOKUP) {
self.capi.FS_ORIGINAL_LOOKUP = self.capi.FS.lookup;
}
return function dynLoader(parent, name) {
self.capi.FS.lookup = self.capi.FS_ORIGINAL_LOOKUP;
var res;
if(/(\.cti|\.ctb|\.utb|\.dis|\.uti|\.tbl|\.dic)$/.test(name)) {
var url = self.capi.TABLE_URL + name;
res = self.capi.FS.createLazyFile(parent, name, url, true, true);
} else {
res = self.capi.FS.lookup.apply(this, [parent, name]);
}
self.capi.FS.lookup = dynLoader;
return res;
};
}
var MEM = {
// Memory Interface for 16bit Unicode Builds
"2": {
getBufferLength: function(buff) {
return (buff.length+1)*2;
},
transfer: function(capi, buff) {
var bufflen = MEM["2"].getBufferLength(buff);
var buff_ptr = capi._malloc(bufflen);
capi.stringToUTF16(buff, buff_ptr, bufflen);
return buff_ptr;
},
read: function(capi, buffptr, bufflenptr) {
if(!bufflenptr) { // null-terminated string
return capi.UTF16ToString(buffptr);
} else {
var start_index = buffptr >> 1;
var end_index = start_index + capi.getValue(bufflenptr, "i32");
var outstr_buff = capi.HEAPU16.slice(start_index, end_index);
return outstr_buff;
}
},
buffToString: function(buff) {
return String.fromCharCode.apply(null, buff);
}
},
// Memory Interface for 32bit Unicode Builds
"4": {
getBufferLength: function(buff) {
return (buff.length+1)*4;
},
transfer: function(capi, buff) {
var bufflen = MEM["4"].getBufferLength(buff);
var buff_ptr = capi._malloc(bufflen);
capi.stringToUTF32(buff, buff_ptr, bufflen);
return buff_ptr;
},
read: function(capi, buffptr, bufflenptr) {
if(!bufflenptr) { // null-terminated string
return capi.UTF32ToString(buffptr);
} else {
var start_index = buffptr >> 2;
var end_index = start_index + capi.getValue(bufflenptr, "i32");
var outstr_buff = capi.HEAPU32.slice(start_index, end_index);
return outstr_buff;
}
},
buffToString: function(buff) {
var str = "";
for(var i = 0; i < buff.length; ++i) {
var utf32 = buff[i];
// taken from emscripten, which ported it from
// http://unicode.org/faq/utf_bom.html#utf16-3
if (utf32 >= 0x10000) {
var ch = utf32 - 0x10000;
str += String.fromCharCode(0xD800 | (ch >> 10), 0xDC00 | (ch & 0x3FF));
} else {
str += String.fromCharCode(utf32);
}
}
return str;
}
}
};
var IMPL = {};
// Implementation for 16bit and 32bit unicode liblouis builds
IMPL.lou = {
version: function() { return this.capi.ccall('lou_version', 'string', [], []); },
setLogLevel: function(num) { return this.capi.ccall('lou_setLogLevel', 'void', ['number'], [num]); },
getTable: function(str) { return this.capi.ccall('lou_getTable', 'number', ['string'], [str]); },
checkTable: function(str) { return this.capi.ccall('lou_checkTable', 'number', ['string'], [str]); },
free: function() { return this.capi.ccall('lou_free', 'void', [], []); },
charSize: function() { return this.capi.ccall('lou_charSize', 'number', [], []); },
setDataPath: function(path) { this.capi.ccall('lou_setDataPath', 'number', ["string"], [path]); },
getDataPath: function() { return this.capi.ccall('lou_getDataPath', 'string', [], []); },
getFilesystem: function() { return this.capi.FS; },
compileString: function(table, opcodes) {
var success = this.capi.ccall('lou_compileString', 'number', ['string', 'string'], [table, opcodes]);
return !!success;
},
registerLogCallback: function(fn) {
if(this.capi._log_callback_fn_pointer) {
this.capi.Runtime.removeFunction(this.capi._log_callback_fn_pointer);
}
if(fn === null) {
fn = easyApiDefaultLogCallback;
}
var self = this;
this.capi._log_callback_fn_pointer = this.capi.Runtime.addFunction(function(logLvl, msg) {
fn(logLvl, self.capi.Pointer_stringify(msg));
});
this.capi.ccall('lou_registerLogCallback', 'void', ['pointer'],
[this.capi._log_callback_fn_pointer]);
},
backTranslateString: function(table, inbuf) {
return IMPL.lou.translateString.call(this, table, inbuf, true);
},
translateString: function(table, inbuf, backtranslate) {
if(typeof inbuf !== "string" || inbuf.length === 0) {
return "";
}
var mode = 0;
var inbuff_ptr = this.mem.transfer(this.capi, inbuf);
var bufflen = this.mem.getBufferLength(inbuf);
var outbuff_ptr = this.capi._malloc(bufflen);
var bufflen_ptr = this.capi._malloc(4);
var strlen_ptr = this.capi._malloc(4);
this.capi.setValue(bufflen_ptr, bufflen, "i32");
this.capi.setValue(strlen_ptr, bufflen, "i32");
var success = this.capi.ccall(backtranslate ?
'lou_backTranslateString' :
'lou_translateString', 'number', ['string',
'number', 'number', 'number', 'number',
'number', 'number'], [table, inbuff_ptr,
strlen_ptr, outbuff_ptr, bufflen_ptr, null,
null, mode]);
if(!success) {
return null;
}
var outbuff = this.mem.read(this.capi, outbuff_ptr, bufflen_ptr);
this.capi._free(outbuff_ptr);
this.capi._free(inbuff_ptr);
this.capi._free(bufflen_ptr);
this.capi._free(strlen_ptr);
return this.mem.buffToString(outbuff);
},
enableOnDemandTableLoading: function(tableurl) {
if(IS_BROWSER_GUI_THREAD) {
throw new Error("This feature is only available in web " +
"workers and nodeJS");
}
this.capi.TABLE_URL = tableurl;
if(!IS_NODE) {
this.capi.FS.lookup = bindDynLoader(this);
} else {
this.disableOnDemandTableLoading();
this.capi.FS.mkdir('/tables');
var tablefolder = tableurl;
if(!tablefolder) {
var path = require('path');
var buildpath = require.resolve('liblouis-build');
tablefolder = path.resolve(path.dirname(buildpath), "tables/");
}
try {
this.capi.FS.mount(this.capi.FS.filesystems.NODEFS, { root: tablefolder }, '/tables');
} catch(e) {
console.error(e);
throw new Error("mounting of table folder failed");
}
}
},
disableOnDemandTableLoading: function() {
if(!IS_NODE) {
this.capi.FS.lookup = this.capi.FS_ORIGINAL_LOOKUP;
} else if(node_dirExists(this.capi, '/tables')) {
try {
this.capi.FS.unmount('/tables');
} catch(e) { }
try {
this.capi.FS.rmdir('/tables');
} catch(e) {
throw new Error("removal of mounted table folder failed");
}
}
}
};
// Implementation for 16bit and 32bit unicode liblouis+liblouisutdml builds
IMPL.lbu = {
version: function() {
return capi.ccall('lbu_version', 'string', [], []);
},
free: function() {
return capi.ccall('lbu_free', 'string', [], []);
},
setLogLevel: IMPL.lou.setLogLevel,
getTable: IMPL.lou.getTable,
checkTable: IMPL.lou.checkTable,
charSize: IMPL.lou.charSize,
setDataPath: IMPL.lou.setDataPath,
getDataPath: IMPL.lou.getDataPath,
getFilesystem: IMPL.lou.getFilesystem,
registerLogCallback: IMPL.lou.registerLogCallback,
compileString: IMPL.lou.compileString,
};
// Implementation for async usage. By default they are generated to be
// identical to the normal implementations with an additional callback
// as last parameter. This object overwrites the default implementation.
IMPL.worker = {
registerLogCallback: function(cb) {
if(cb === null) {
this.logCallback = easyApiDefaultLogCallback;
} else {
this.logCallback = cb;
}
},
getFilesystem: function() { throw new Error("cannot get filesystem of async liblouis instance"); },
enableOnDemandTableLoading: function(url, fn) {
if(IS_BROWSER_GUI_THREAD) {
url = window.location.origin + "/" + url;
}
fn = fn || NOOP;
dispatch(this, "enableOnDemandTableLoading", [url, fn]);
}
};
function NOOP() {}
function hasLbu(capi) { // is utdml build
if(capi._hasLbu !== void 0) {
return capi._hasLbu;
}
return typeof capi._lbu_version == "function";
// above may change in emscripten as it is a not documented feature.
// Below works and is documented, but generates a warning in the
// console:
//
//try {
//capi.ccall('lbu_version', 'string', [], []);
//capi._hasLbu = true;
//} catch(e) {
//capi._hasLbu = false;
//}
}
function defaultLiblouisBuild() {
if(!IS_NODE) {
if(typeof liblouisBuild !== "undefined") {
return liblouisBuild;
}
if(typeof liblouis_emscripten !== "undefined") {
return liblouis_emscripten;
}
if(typeof Module !== "undefined") {
return Module;
}
} else {
return require("liblouis-build");
}
return null;
}
function LiblouisEasyApiAsync(opts) {
if(IS_NODE) {
throw new Error("Async API is only available in browser environments.");
}
this.registerLogCallback(null);
this.callId = 1;
this.cbs = { "0": function(){} };
this.worker = createWorker();
var self = this;
this.worker.addEventListener("message", function(e) {
self.resolveDispatch(e.data);
});
var prefix = "";
if(IS_BROWSER_GUI_THREAD) {
prefix = window.location.origin + "/";
}
this.worker.postMessage({
cmd: "init",
callId: 0,
data: {
capi: prefix + opts.capi,
easyapi: prefix + opts.easyapi
}
});
}
LiblouisEasyApiAsync.prototype.resolveDispatch = function(msg) {
if(msg && msg.isLog) {
this.logCallback(msg.level, msg.msg);
return;
}
if(!msg || typeof msg.callId !== "number" || typeof this.cbs[msg.callId] !== "function") {
console.debug("recieved bougus message from worker:", msg);
return;
}
this.cbs[msg.callId](msg.data);
this.cbs[msg.callId] = null;
delete this.cbs[msg.callId];
};
for(var i = 0; i < API_METHODS.length; ++i) {
LiblouisEasyApiAsync.prototype[API_METHODS[i]] = IMPL.worker[API_METHODS[i]] || genDispatch(API_METHODS[i]);
}
function genDispatch(fn) {
return function() {
dispatch(this, fn,
Array.prototype.slice.call(arguments));
};
}
function dispatch(self, fn, args) {
if(args.length < 1) { return; }
self.cbs[self.callId] = args[args.length-1];
self.worker.postMessage({
callId: self.callId,
cmd: "lou",
data: {
args: args.slice(0, -1),
fn: fn
}
});
self.callId++;
}
function LiblouisEasyApi(build) {
build = build || defaultLiblouisBuild();
if(typeof build === "string") {
var builds = LiblouisEasyApi.getAvailableBuilds();
if(!builds.hasOwnProperty(build)) {
throw new Error("no liblouis build for requested version loaded");
}
// there may be multiple builds, always grab the first one
build = builds[build][0];
}
if(typeof build === "function") {
build = build();
}
if(!build || !build._lou_version) {
throw new Error("argument cannot be resolved to a liblouis build.");
}
this.capi = build;
this.impl = hasLbu(build) ? IMPL.lbu : IMPL.lou;
this.mem = this.charSize() === 4 ? MEM["4"] : MEM["2"];
this.registerLogCallback(null);
}
for(i = 0; i < API_METHODS.length; ++i) {
LiblouisEasyApi.prototype[API_METHODS[i]] = genBridge(API_METHODS[i]);
}
function genBridge(fn) {
return function(arg1, arg2, arg3) {
if(!this.impl.hasOwnProperty(fn)) {
throw new Error("Your build does not support this method. Include a build containing liblouisutdml.");
}
return this.impl[fn].call(this, arg1, arg2, arg3);
};
}
LiblouisEasyApi.prototype.getAvailableBuilds = LiblouisEasyApi.getAvailableBuilds = function() {
return ns.liblouisBuilds || {};
};
LiblouisEasyApi.prototype.LOG = {};
LiblouisEasyApi.prototype.LOG[LiblouisEasyApi.prototype.LOG.ALL = 0] = "ALL";
LiblouisEasyApi.prototype.LOG[LiblouisEasyApi.prototype.LOG.DEBUG = 10000] = "DEBUG";
LiblouisEasyApi.prototype.LOG[LiblouisEasyApi.prototype.LOG.INFO = 20000] = "INFO";
LiblouisEasyApi.prototype.LOG[LiblouisEasyApi.prototype.LOG.WARN = 30000] = "WARN";
LiblouisEasyApi.prototype.LOG[LiblouisEasyApi.prototype.LOG.ERROR = 40000] = "ERROR";
LiblouisEasyApi.prototype.LOG[LiblouisEasyApi.prototype.LOG.FATAL = 50000] = "FATAL";
LiblouisEasyApi.prototype.LOG[LiblouisEasyApi.prototype.LOG.OFF = 60000] = "OFF";
LiblouisEasyApi.prototype._CONSOLE_MAPPING = {
ALL: "log",
DEBUG: "log",
INFO: "info",
WARN: "warn",
ERROR: "error",
FATAL: "error",
};
LiblouisEasyApi.prototype._defaultLogCallback = easyApiDefaultLogCallback;
function easyApiDefaultLogCallback(lvl_id, msg) {
var lvl_name = LiblouisEasyApi.prototype.LOG[lvl_id];
msg = "["+lvl_name+"] " + msg;
if(typeof console != "undefined") {
var fn = console[LiblouisEasyApi.prototype._CONSOLE_MAPPING[lvl_name]];
if(fn) {
fn(msg);
} else {
console.log(msg);
}
}
}
function node_dirExists(capi, path) {
try {
capi.FS.lookupPath(path);
return true;
} catch(e) {
return false;
}
}
// create a default instance in browser environments
if(liblouis.browserGlobal) {
if(defaultLiblouisBuild()) {
ns.liblouis = new LiblouisEasyApi();
}
ns.LiblouisEasyApi = LiblouisEasyApi;
ns.LiblouisEasyApiAsync = LiblouisEasyApiAsync;
} else {
liblouis.EasyApi = LiblouisEasyApi;
liblouis.EasyApiAsync = LiblouisEasyApiAsync;
}
}));