From 2b141f8aa58c30c7c5eba36df992728dbb3a7001 Mon Sep 17 00:00:00 2001 From: Kevin Kwok Date: Fri, 14 Oct 2016 01:30:44 -0400 Subject: [PATCH] works on iOS Safari --- examples/file-input/demo.html | 3 +++ src/browser/index.js | 2 +- src/browser/lang.js | 20 ++++++++++++++++---- src/common/worker.js | 3 ++- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/examples/file-input/demo.html b/examples/file-input/demo.html index 4403e13..bbe01e2 100644 --- a/examples/file-input/demo.html +++ b/examples/file-input/demo.html @@ -122,7 +122,10 @@ function recognizeFile(file){ + + +
diff --git a/src/browser/index.js b/src/browser/index.js index 07ca34c..2df586a 100644 --- a/src/browser/index.js +++ b/src/browser/index.js @@ -6,7 +6,7 @@ var defaultOptions = { if (process.env.NODE_ENV === "development") { console.debug('Using Development Configuration') - defaultOptions.workerPath = location.protocol + '//' + location.host + '/dist/worker.dev.js' + defaultOptions.workerPath = location.protocol + '//' + location.host + '/dist/worker.dev.js?nocache=' + Math.random().toString(36).slice(3) }else{ var version = require('../../package.json').version; defaultOptions.workerPath = 'https://cdn.rawgit.com/naptha/tesseract.js/' + version + '/dist/worker.js' diff --git a/src/browser/lang.js b/src/browser/lang.js index 9fc638e..4093acd 100644 --- a/src/browser/lang.js +++ b/src/browser/lang.js @@ -1,11 +1,18 @@ const leveljs = require('level-js') -var db = typeof indexedDB === 'undefined' ? { open: (_, cb) => cb(true) } : leveljs('./tessdata2') + +// something about trying to store these language files in indexedDB +// causes iOS Safari to crash + +var iOS = /iPad|iPhone|iPod/.test(navigator.userAgent); +var noIDB = typeof indexedDB === 'undefined' || iOS; + +var db = noIDB ? { open: (_, cb) => cb(true) } : leveljs('./tessdata2') var langdata = require('../common/langdata.json') module.exports = function getLanguageData(req, res, cb){ var lang = req.options.lang; - + function saveDataFile(data){ db.put(lang, data, err => console.log('cached', lang, err)) cb(data) @@ -45,15 +52,20 @@ function fetchLanguageData(req, res, cb){ xhr.onload = e => { if (!(xhr.status == 200 || (xhr.status == 0 && xhr.response))) return res.reject('Error downloading language ' + url); - res.progress({ status: 'unzipping ' + langfile }) + res.progress({ status: 'unzipping ' + langfile, progress: 0 }) // in case the gzips are already ungzipped or extra gzipped var response = new Uint8Array(xhr.response) try { - while(response[0] == 0x1f && response[1] == 0x8b) response = ungzip(response); + var n = 2; + while(response[0] == 0x1f && response[1] == 0x8b){ + response = ungzip(response); + res.progress({ status: 'unzipping ' + langfile, progress: 1 - 1 / (n++) }) + } } catch (err) { return res.reject('Error unzipping language file ' + langfile + '\n' + err.message) } + res.progress({ status: 'unzipping ' + langfile, progress: 1 }) cb(response) } diff --git a/src/common/worker.js b/src/common/worker.js index 400fe60..1ff024a 100644 --- a/src/common/worker.js +++ b/src/common/worker.js @@ -85,9 +85,10 @@ function loadLanguage(req, res, cb){ if(lang in Module._loadedLanguages) return cb(); adapter.getLanguageData(req, res, function(data){ + res.progress({ status: 'loading ' + lang + '.traineddata', progress: 0 }) Module.FS_createDataFile('tessdata', lang + ".traineddata", data, true, false); - res.progress({ status: 'loading ' + lang + '.traineddata', progress: 1 }) Module._loadedLanguages[lang] = true; + res.progress({ status: 'loading ' + lang + '.traineddata', progress: 1 }) cb() }) }