@internetarchive/bookreader 5.0.0-96 → 5.0.0-98
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/BookReader/474.js +2 -0
- package/BookReader/474.js.map +1 -0
- package/BookReader/BookReader.css +39 -34
- package/BookReader/BookReader.js +1 -1
- package/BookReader/BookReader.js.map +1 -1
- package/BookReader/bergamot-translator-worker.js +2966 -0
- package/BookReader/bergamot-translator-worker.wasm +0 -0
- package/BookReader/ia-bookreader-bundle.js +1 -1
- package/BookReader/ia-bookreader-bundle.js.map +1 -1
- package/BookReader/images/icon_experiment.svg +1 -0
- package/BookReader/images/translate.svg +1 -0
- package/BookReader/plugins/plugin.experiments.js +1 -1
- package/BookReader/plugins/plugin.experiments.js.map +1 -1
- package/BookReader/plugins/plugin.text_selection.js +1 -1
- package/BookReader/plugins/plugin.text_selection.js.map +1 -1
- package/BookReader/plugins/plugin.translate.js +3 -0
- package/BookReader/plugins/plugin.translate.js.LICENSE.txt +1 -0
- package/BookReader/plugins/plugin.translate.js.map +1 -0
- package/BookReader/plugins/plugin.tts.js +1 -1
- package/BookReader/plugins/plugin.tts.js.map +1 -1
- package/BookReader/plugins/translator-worker.js +2 -0
- package/BookReader/plugins/translator-worker.js.map +1 -0
- package/BookReader/silence.mp3 +0 -0
- package/BookReader/translator-worker.js +475 -0
- package/package.json +6 -3
- package/src/BookNavigator/book-navigator.js +1 -0
- package/src/BookReader/Mode1UpLit.js +6 -1
- package/src/BookReader/Mode2UpLit.js +11 -1
- package/src/BookReader/Navbar/Navbar.js +61 -0
- package/src/BookReader/options.js +12 -8
- package/src/BookReader.js +67 -140
- package/src/assets/images/icon_experiment.svg +1 -0
- package/src/assets/images/translate.svg +1 -0
- package/src/assets/silence.mp3 +0 -0
- package/src/css/_BRnav.scss +0 -24
- package/src/css/_BRsearch.scss +1 -5
- package/src/css/_TextSelection.scss +38 -9
- package/src/plugins/plugin.experiments.js +34 -9
- package/src/plugins/plugin.text_selection.js +17 -20
- package/src/plugins/translate/TranslationManager.js +170 -0
- package/src/plugins/translate/plugin.translate.js +489 -0
- package/src/plugins/tts/AbstractTTSEngine.js +3 -4
- package/src/plugins/tts/PageChunk.js +28 -9
- package/src/plugins/tts/WebTTSEngine.js +5 -7
- package/src/plugins/tts/plugin.tts.js +40 -4
- package/src/plugins/tts/utils.js +21 -22
- package/src/util/cache.js +20 -0
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
!function(){"use strict";var e={};"undefined"==typeof self&&(global.Module=e,global.self=new class{#e;constructor(){const{parentPort:e}=require("node:worker_threads");this.#e=e}addEventListener(e,t){this.#e.on(e,(e=>t({data:e})))}postMessage(e){this.#e.postMessage(e)}importScripts(...e){const{readFileSync:t}=require("node:fs"),{join:n}=require("node:path");for(let r of e){const e=t(n(__dirname,r),{encoding:"utf-8"});eval.call(global,e)}}async fetch(e,t){if("file:"===e.protocol){const{readFile:t}=require("node:fs/promises"),n=await t(e.pathname),r=new Blob([n]);return new Response(r,{status:200,statusText:"OK",headers:{"Content-Type":"application/wasm","Content-Length":r.size.toString()}})}return await fetch(e,t)}get location(){return new URL(`file://${__filename}`)}});class t{static parse(e){const t={};return e.split("\n").reduce(((e,n,r)=>{let s;if(s=n.match(/^\s*-\s+(.+?)$/))Array.isArray(t[e])||(t[e]=t[e].trim()?[t[e]]:[]),t[e].push(s[1].trim());else if(s=n.match(/^\s*([A-Za-z0-9_][A-Za-z0-9_-]*):\s*(.*)$/))e=s[1],t[e]=s[2].trim();else if(n.trim())throw Error(`Could not parse line ${r+1}: "${n}"`);return e}),null),t}static stringify(e){return Object.entries(e).reduce(((e,[t,n])=>{let r="";return r=Array.isArray(n)?n.map((e=>`\n - ${e}`)).join(""):("number"==typeof n||"boolean"==typeof n||n.match(/^\d*(\.\d+)?$/),`${n}`),`${e}${t}: ${r}\n`}),"")}}class n{static GEMM_TO_FALLBACK_FUNCTIONS_MAP={int8_prepare_a:"int8PrepareAFallback",int8_prepare_b:"int8PrepareBFallback",int8_prepare_b_from_transposed:"int8PrepareBFromTransposedFallback",int8_prepare_b_from_quantized_transposed:"int8PrepareBFromQuantizedTransposedFallback",int8_prepare_bias:"int8PrepareBiasFallback",int8_multiply_and_add_bias:"int8MultiplyAndAddBiasFallback",int8_select_columns_of_b:"int8SelectColumnsOfBFallback"};static NATIVE_INT_GEMM="mozIntGemm";constructor(e){}async initialize(e){this.options=e||{},this.models=new Map,this.module=await this.loadModule(),this.service=await this.loadTranslationService()}linkNativeIntGemm(e){if(!WebAssembly.mozIntGemm)return console.warn("Native gemm requested but not available, falling back to embedded gemm"),this.linkFallbackIntGemm(e);const t=new WebAssembly.Instance(WebAssembly.mozIntGemm(),{"":{memory:e.env.memory}});return Array.from(Object.keys(n.GEMM_TO_FALLBACK_FUNCTIONS_MAP)).every((e=>t.exports[e]))?t.exports:(console.warn("Native gemm is missing expected functions, falling back to embedded gemm"),this.linkFallbackIntGemm(e))}linkFallbackIntGemm(t){const r=Object.entries(n.GEMM_TO_FALLBACK_FUNCTIONS_MAP).map((([t,n])=>[t,(...t)=>e.asm[n](...t)]));return Object.fromEntries(r)}loadModule(){return new Promise((async(t,n)=>{try{const r=await self.fetch(new URL("./bergamot-translator-worker.wasm",self.location));Object.assign(e,{instantiateWasm:(e,t)=>{try{WebAssembly.instantiateStreaming(r,{...e,wasm_gemm:this.options.useNativeIntGemm?this.linkNativeIntGemm(e):this.linkFallbackIntGemm(e)}).then((({instance:e})=>t(e))).catch(n)}catch(e){n(e)}return{}},onRuntimeInitialized:()=>{t(e)}}),self.Module=e,self.importScripts("bergamot-translator-worker.js")}catch(e){n(e)}}))}loadTranslationService(){return new this.module.BlockingService({cacheSize:Math.max(this.options.cacheSize||0,0)})}hasTranslationModel({from:e,to:t}){const n=JSON.stringify({from:e,to:t});return this.models.has(n)}loadTranslationModel({from:e,to:n},r){const s=r.vocabs.filter(((e,t,n)=>!n.slice(0,t).includes(e))),[a,i,o,...l]=[this.prepareAlignedMemoryFromBuffer(r.model,256),this.prepareAlignedMemoryFromBuffer(r.shortlist,64),r.qualityModel?this.prepareAlignedMemoryFromBuffer(r.qualityModel,64):null,...s.map((e=>this.prepareAlignedMemoryFromBuffer(e,64)))],m=new this.module.AlignedMemoryList;l.forEach((e=>m.push_back(e)));let c=t.parse("\n beam-size: 1\n normalize: 1.0\n word-penalty: 0\n cpu-threads: 0\n gemm-precision: int8shiftAlphaAll\n skip-cost: true\n ");r.config&&Object.assign(c,r.config),"int8"===c["gemm-precision"]&&(c["gemm-precision"]="int8shiftAll"),Object.assign(c,t.parse("\n alignment: soft\n quiet: true\n quiet-translation: true\n max-length-break: 128\n mini-batch-words: 1024\n workspace: 128\n max-length-factor: 2.0\n "));const d=JSON.stringify({from:e,to:n});this.models.set(d,new this.module.TranslationModel(t.stringify(c),a,i,m,o))}freeTranslationModel({from:e,to:t}){const n=JSON.stringify({from:e,to:t});if(!this.models.has(n))return;const r=this.models.get(n);this.models.delete(n),r.delete()}prepareAlignedMemoryFromBuffer(e,t){const n=new Int8Array(e),r=new this.module.AlignedMemory(n.byteLength,t);return r.getByteArrayView().set(n),r}translate({models:e,texts:t}){let n=new this.module.VectorString;t.forEach((({text:e})=>n.push_back(e)));let r=new this.module.VectorResponseOptions;t.forEach((({html:e,qualityScores:t})=>r.push_back({alignment:!1,html:e,qualityScores:t})));const s=e.map((({from:e,to:t})=>{const n=JSON.stringify({from:e,to:t});return this.models.get(n)})),a=e.length>1?this.service.translateViaPivoting(...s,n,r):this.service.translate(...s,n,r);n.delete(),r.delete();const i=t.map(((e,t)=>({target:{text:a.get(t).getTranslatedText()}})));return a.delete(),i}}function r(e){return{name:e.name,message:e.message,stack:e.stack}}const s=new n;self.addEventListener("message",(async function({data:{id:e,name:t,args:n}}){e||console.error("Received message without id",arguments[0]);try{if("function"!=typeof s[t])throw TypeError(`worker[${t}] is not a function`);const r=await Promise.resolve(Reflect.apply(s[t],s,n));self.postMessage({id:e,result:r})}catch(t){self.postMessage({id:e,error:r(t)})}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bergamot-translator/worker/translator-worker.js"],"sourcesContent":["/**\n * Wrapper around the dirty bits of Bergamot's WASM bindings.\n */\n\n// Global because importScripts is global.\nvar Module = {};\n\n/**\n * node.js compatibility: Fake GlobalWorkerScope that emulates being inside a\n * WebWorker\n */\nif (typeof self === 'undefined') {\n global.Module = Module;\n\n global.self = new class GlobalWorkerScope {\n /** @type {import(\"node:worker_threads\").MessagePort} */\n #port;\n\n constructor() {\n const {parentPort} = require(/* webpackIgnore: true */ 'node:worker_threads');\n this.#port = parentPort;\n }\n\n /**\n * Add event listener to listen for messages posted to the worker.\n * @param {string} eventName\n * @param {(object)} callback\n */\n addEventListener(eventName, callback) {\n this.#port.on(eventName, (data) => callback({data}));\n }\n\n /**\n * Post message outside, to the owner of the Worker.\n * @param {any} message\n */\n postMessage(message) {\n this.#port.postMessage(message);\n }\n\n /**\n * @param {...string} scripts - Paths to scripts to import in that order\n */\n importScripts(...scripts) {\n const {readFileSync} = require(/* webpackIgnore: true */ 'node:fs');\n const {join} = require(/* webpackIgnore: true */ 'node:path');\n for (let pathname of scripts) {\n const script = readFileSync(join(__dirname, pathname), {encoding: 'utf-8'});\n eval.call(global, script);\n }\n }\n\n /**\n * Adds support for local file urls. Assumes anything that doesn't start\n * with \"http\" to be a local path.\n * @param {string} url - path or url\n * @param {object?} options - See `fetch()` options\n * @return {Promise<Response>}\n */\n async fetch(url, options) {\n if (url.protocol === 'file:') {\n const {readFile} = require(/* webpackIgnore: true */ 'node:fs/promises');\n const buffer = await readFile(url.pathname);\n const blob = new Blob([buffer]);\n return new Response(blob, {\n status: 200,\n statusText: 'OK',\n headers: {\n 'Content-Type': 'application/wasm',\n 'Content-Length': blob.size.toString()\n }\n });\n }\n\n return await fetch(url, options);\n }\n\n get location() {\n return new URL(`file://${__filename}`);\n }\n }\n}\n\nclass YAML {\n /**\n * Parses YAML into dictionary. Does not interpret types, all values are a\n * string or a list of strings. No support for objects other than the top\n * level.\n * @param {string} yaml\n * @return {{[string]: string | string[]}}\n */\n static parse(yaml) {\n const out = {};\n\n yaml.split('\\n').reduce((key, line, i) => {\n let match;\n if (match = line.match(/^\\s*-\\s+(.+?)$/)) {\n if (!Array.isArray(out[key]))\n out[key] = out[key].trim() ? [out[key]] : [];\n out[key].push(match[1].trim());\n }\n else if (match = line.match(/^\\s*([A-Za-z0-9_][A-Za-z0-9_-]*):\\s*(.*)$/)) {\n key = match[1];\n out[key] = match[2].trim();\n }\n else if (!line.trim()) {\n // whitespace, ignore\n }\n else {\n throw Error(`Could not parse line ${i+1}: \"${line}\"`);\n }\n return key;\n }, null);\n\n return out;\n }\n\n /**\n * Turns an object into a YAML string. No support for objects, only simple\n * types and lists of simple types.\n * @param {{[string]: string | number | boolean | string[]}} data\n * @return {string}\n */\n static stringify(data) {\n return Object.entries(data).reduce((str, [key, value]) => {\n let valstr = '';\n if (Array.isArray(value))\n valstr = value.map(val => `\\n - ${val}`).join('');\n else if (typeof value === 'number' || typeof value === 'boolean' || value.match(/^\\d*(\\.\\d+)?$/))\n valstr = `${value}`;\n else\n valstr = `${value}`; // Quote?\n\n return `${str}${key}: ${valstr}\\n`;\n }, '');\n }\n}\n\n/**\n * Wrapper around the bergamot-translator exported module that hides the need\n * of working with C++ style data structures and does model management.\n */\nclass BergamotTranslatorWorker {\n /**\n * Map of expected symbol -> name of fallback symbol for functions that can\n * be swizzled for a faster implementation. Firefox Nightly makes use of\n * this.\n */\n static GEMM_TO_FALLBACK_FUNCTIONS_MAP = {\n 'int8_prepare_a': 'int8PrepareAFallback',\n 'int8_prepare_b': 'int8PrepareBFallback',\n 'int8_prepare_b_from_transposed': 'int8PrepareBFromTransposedFallback',\n 'int8_prepare_b_from_quantized_transposed': 'int8PrepareBFromQuantizedTransposedFallback',\n 'int8_prepare_bias': 'int8PrepareBiasFallback',\n 'int8_multiply_and_add_bias': 'int8MultiplyAndAddBiasFallback',\n 'int8_select_columns_of_b': 'int8SelectColumnsOfBFallback'\n };\n\n /**\n * Name of module exported by Firefox Nightly that exports an optimised\n * implementation of the symbols mentioned above.\n */\n static NATIVE_INT_GEMM = 'mozIntGemm';\n\n /**\n * Empty because we can't do async constructors yet. It is the\n * responsibility of whoever owns this WebWorker to call `initialize()`.\n */\n constructor(options) {}\n\n /**\n * Instantiates a new translation worker with optional options object.\n * If this call succeeds, the WASM runtime is loaded and ready.\n * \n * Available options are:\n * useNativeIntGemm: {true | false} defaults to false. If true, it will\n * attempt to link to the intgemm module available in\n * Firefox Nightly which makes translations much faster.\n * cacheSize: {Number} defaults to 0 which disables translation\n * cache entirely. Note that this is a theoretical\n * upper bound. In practice it will use about 1/3th of\n * the cache specified here. 2^14 is not a bad starting\n * value.\n * @param {{useNativeIntGemm: boolean, cacheSize: number}} options\n */\n async initialize(options) {\n this.options = options || {};\n this.models = new Map(); // Map<str,Promise<TranslationModel>>\n this.module = await this.loadModule();\n this.service = await this.loadTranslationService();\n }\n\n /**\n * Tries to load native IntGEMM module for bergamot-translator. If that\n * fails because it or any of the expected functions is not available, it\n * falls back to using the naive implementations that come with the wasm\n * binary itself through `linkFallbackIntGemm()`.\n * @param {{env: {memory: WebAssembly.Memory}}} info\n * @return {{[method:string]: (...any) => any}}\n */\n linkNativeIntGemm(info) {\n if (!WebAssembly['mozIntGemm']) {\n console.warn('Native gemm requested but not available, falling back to embedded gemm');\n return this.linkFallbackIntGemm(info);\n }\n\n const instance = new WebAssembly.Instance(WebAssembly['mozIntGemm'](), {\n '': {memory: info['env']['memory']}\n });\n\n if (!Array.from(Object.keys(BergamotTranslatorWorker.GEMM_TO_FALLBACK_FUNCTIONS_MAP)).every(fun => instance.exports[fun])) {\n console.warn('Native gemm is missing expected functions, falling back to embedded gemm');\n return this.linkFallbackIntGemm(info);\n }\n\n return instance.exports;\n }\n\n /**\n * Links intgemm functions that are already available in the wasm binary,\n * but just exports them under the name that is expected by\n * bergamot-translator.\n * @param {{env: {memory: WebAssembly.Memory}}} info\n * @return {{[method:string]: (...any) => any}}\n */\n linkFallbackIntGemm(info) {\n const mapping = Object.entries(BergamotTranslatorWorker.GEMM_TO_FALLBACK_FUNCTIONS_MAP).map(([key, name]) => {\n return [key, (...args) => Module['asm'][name](...args)]\n });\n\n return Object.fromEntries(mapping);\n }\n\n /**\n * Internal method. Reads and instantiates the WASM binary. Returns a\n * promise for the exported Module object that contains all the classes\n * and functions exported by bergamot-translator.\n * @return {Promise<BergamotTranslator>}\n */\n loadModule() {\n return new Promise(async (resolve, reject) => {\n try {\n const response = await self.fetch(new URL('./bergamot-translator-worker.wasm', self.location));\n\n Object.assign(Module, {\n instantiateWasm: (info, accept) => {\n try {\n WebAssembly.instantiateStreaming(response, {\n ...info,\n 'wasm_gemm': this.options.useNativeIntGemm\n ? this.linkNativeIntGemm(info)\n : this.linkFallbackIntGemm(info)\n }).then(({instance}) => accept(instance)).catch(reject);\n } catch (err) {\n reject(err);\n }\n return {};\n },\n onRuntimeInitialized: () => {\n resolve(Module);\n }\n });\n\n // Emscripten glue code. Webpack et al. should not mangle the `Module` property name!\n self.Module = Module;\n self.importScripts('bergamot-translator-worker.js');\n } catch (err) {\n reject(err);\n }\n });\n }\n\n /**\n * Internal method. Instantiates a BlockingService()\n * @return {BergamotTranslator.BlockingService}\n */\n loadTranslationService() {\n return new this.module.BlockingService({\n cacheSize: Math.max(this.options.cacheSize || 0, 0)\n });\n }\n\n /**\n * Returns whether a model has already been loaded in this worker. Marked\n * async because the message passing interface we use expects async methods.\n * @param {{from:string, to:string}}\n * @return boolean\n */ \n hasTranslationModel({from,to}) {\n const key = JSON.stringify({from,to});\n return this.models.has(key);\n }\n\n /**\n * Loads a translation model from a set of file buffers. After this, the\n * model is available to translate with and `hasTranslationModel()` will\n * return true for this pair.\n * @param {{from:string, to:string}}\n * @param {{\n * model: ArrayBuffer,\n * shortlist: ArrayBuffer,\n * vocabs: ArrayBuffer[],\n * qualityModel: ArrayBuffer?,\n * config?: {\n * [key:string]: string\n * }\n * }} buffers\n */ \n loadTranslationModel({from, to}, buffers) {\n // This because service_bindings.cpp:prepareVocabsSmartMemories :(\n const uniqueVocabs = buffers.vocabs.filter((vocab, index, vocabs) => {\n return !vocabs.slice(0, index).includes(vocab);\n });\n\n const [modelMemory, shortlistMemory, qualityModel, ...vocabMemory] = [\n this.prepareAlignedMemoryFromBuffer(buffers.model, 256),\n this.prepareAlignedMemoryFromBuffer(buffers.shortlist, 64),\n buffers.qualityModel // optional quality model\n ? this.prepareAlignedMemoryFromBuffer(buffers.qualityModel, 64)\n : null,\n ...uniqueVocabs.map(vocab => this.prepareAlignedMemoryFromBuffer(vocab, 64))\n ];\n\n const vocabs = new this.module.AlignedMemoryList();\n vocabMemory.forEach(vocab => vocabs.push_back(vocab));\n\n // Defaults\n let modelConfig = YAML.parse(`\n beam-size: 1\n normalize: 1.0\n word-penalty: 0\n cpu-threads: 0\n gemm-precision: int8shiftAlphaAll\n skip-cost: true\n `);\n\n if (buffers.config)\n Object.assign(modelConfig, buffers.config);\n\n // WASM marian is only compiled with support for shiftedAll.\n if (modelConfig['gemm-precision'] === 'int8')\n modelConfig['gemm-precision'] = 'int8shiftAll';\n\n // Override these\n Object.assign(modelConfig, YAML.parse(`\n alignment: soft\n quiet: true\n quiet-translation: true\n max-length-break: 128\n mini-batch-words: 1024\n workspace: 128\n max-length-factor: 2.0\n `));\n\n const key = JSON.stringify({from,to});\n this.models.set(key, new this.module.TranslationModel(YAML.stringify(modelConfig), modelMemory, shortlistMemory, vocabs, qualityModel));\n }\n\n /**\n * Frees up memory used by old translation model. Does nothing if model is\n * already deleted.\n * @param {{from:string, to:string}}\n */\n freeTranslationModel({from, to}) {\n const key = JSON.stringify({from,to});\n \n if (!this.models.has(key))\n return;\n \n const model = this.models.get(key);\n this.models.delete(key);\n\n model.delete();\n }\n\n /**\n * Internal function. Copies the data from an ArrayBuffer into memory that\n * can be used inside the WASM vm by Marian.\n * @param {{ArrayBuffer}} buffer\n * @param {number} alignmentSize\n * @return {BergamotTranslator.AlignedMemory}\n */\n prepareAlignedMemoryFromBuffer(buffer, alignmentSize) {\n const bytes = new Int8Array(buffer);\n const memory = new this.module.AlignedMemory(bytes.byteLength, alignmentSize);\n memory.getByteArrayView().set(bytes);\n return memory;\n }\n\n /**\n * Public. Does actual translation work. You have to make sure that the\n * models necessary for translating text are already loaded before calling\n * this method. Returns a promise with translation responses.\n * @param {{models: {from:string, to:string}[], texts: {text: string, html: boolean}[]}}\n * @return {Promise<{target: {text: string}}[]>}\n */\n translate({models, texts}) {\n // Convert texts array into a std::vector<std::string>.\n let input = new this.module.VectorString();\n texts.forEach(({text}) => input.push_back(text));\n\n // Extracts the texts[].html options into ResponseOption objects\n let options = new this.module.VectorResponseOptions();\n texts.forEach(({html, qualityScores}) => options.push_back({alignment: false, html, qualityScores}));\n\n // Turn our model names into a list of TranslationModel pointers\n const translationModels = models.map(({from,to}) => {\n const key = JSON.stringify({from,to});\n return this.models.get(key);\n });\n\n // translate the input, which is a vector<String>; the result is a vector<Response>\n const responses = models.length > 1\n ? this.service.translateViaPivoting(...translationModels, input, options)\n : this.service.translate(...translationModels, input, options);\n \n input.delete();\n options.delete();\n\n // Convert the Response WASM wrappers into native JavaScript types we\n // can send over the 'wire' (message passing) in the same format as we\n // use in bergamot-translator.\n const translations = texts.map((_, i) => ({\n target: {\n text: responses.get(i).getTranslatedText()\n }\n }));\n\n responses.delete();\n\n return translations;\n }\n}\n\n/**\n * Because you can't put an Error object in a message. But you can post a\n * generic object!\n * @param {Error} error\n * @return {{\n * name: string?,\n * message: string?,\n * stack: string?\n * }}\n */\nfunction cloneError(error) {\n return {\n name: error.name,\n message: error.message,\n stack: error.stack\n };\n}\n\n// (Constructor doesn't really do anything, we need to call `initialize()`\n// first before using it. That happens from outside the worker.)\nconst worker = new BergamotTranslatorWorker();\n\nself.addEventListener('message', async function({data: {id, name, args}}) {\n if (!id)\n console.error('Received message without id', arguments[0]);\n\n try {\n if (typeof worker[name] !== 'function')\n throw TypeError(`worker[${name}] is not a function`);\n\n // Using `Promise.resolve` to await any promises that worker[name]\n // possibly returns.\n const result = await Promise.resolve(Reflect.apply(worker[name], worker, args));\n self.postMessage({id, result});\n } catch (error) {\n self.postMessage({\n id,\n error: cloneError(error)\n })\n }\n});\n"],"names":["Module","self","global","constructor","parentPort","require","this","addEventListener","eventName","callback","on","data","postMessage","message","importScripts","scripts","readFileSync","join","pathname","script","__dirname","encoding","eval","call","fetch","url","options","protocol","readFile","buffer","blob","Blob","Response","status","statusText","headers","size","toString","location","URL","__filename","YAML","parse","yaml","out","split","reduce","key","line","i","match","Array","isArray","trim","push","Error","stringify","Object","entries","str","value","valstr","map","val","BergamotTranslatorWorker","static","initialize","models","Map","module","loadModule","service","loadTranslationService","linkNativeIntGemm","info","WebAssembly","console","warn","linkFallbackIntGemm","instance","Instance","memory","from","keys","GEMM_TO_FALLBACK_FUNCTIONS_MAP","every","fun","exports","mapping","name","args","fromEntries","Promise","async","resolve","reject","response","assign","instantiateWasm","accept","instantiateStreaming","useNativeIntGemm","then","catch","err","onRuntimeInitialized","BlockingService","cacheSize","Math","max","hasTranslationModel","to","JSON","has","loadTranslationModel","buffers","uniqueVocabs","vocabs","filter","vocab","index","slice","includes","modelMemory","shortlistMemory","qualityModel","vocabMemory","prepareAlignedMemoryFromBuffer","model","shortlist","AlignedMemoryList","forEach","push_back","modelConfig","config","set","TranslationModel","freeTranslationModel","get","delete","alignmentSize","bytes","Int8Array","AlignedMemory","byteLength","getByteArrayView","translate","texts","input","VectorString","text","VectorResponseOptions","html","qualityScores","alignment","translationModels","responses","length","translateViaPivoting","translations","_","target","getTranslatedText","cloneError","error","stack","worker","id","arguments","TypeError","result","Reflect","apply"],"sourceRoot":""}
|
|
@@ -1130,13 +1130,6 @@ i.BRicon {
|
|
|
1130
1130
|
display: block;
|
|
1131
1131
|
border: 3px solid rgba(255, 255, 255, 0.3);
|
|
1132
1132
|
}
|
|
1133
|
-
.BRReadAloudToolbar .BRnavCntl {
|
|
1134
|
-
z-index: 6;
|
|
1135
|
-
background-color: #4D4D4D;
|
|
1136
|
-
}
|
|
1137
|
-
.BRReadAloudToolbar .BRnavCntlBtm:hover {
|
|
1138
|
-
background-color: #0074D1;
|
|
1139
|
-
}
|
|
1140
1133
|
.BRReadAloudToolbar--controls {
|
|
1141
1134
|
display: flex;
|
|
1142
1135
|
}
|
|
@@ -1245,13 +1238,6 @@ i.BRicon {
|
|
|
1245
1238
|
display: block;
|
|
1246
1239
|
border: 3px solid rgba(255, 255, 255, 0.3);
|
|
1247
1240
|
}
|
|
1248
|
-
.BRnav .BRnavCntl {
|
|
1249
|
-
z-index: 6;
|
|
1250
|
-
background-color: #4D4D4D;
|
|
1251
|
-
}
|
|
1252
|
-
.BRnav .BRnavCntlBtm:hover {
|
|
1253
|
-
background-color: #0074D1;
|
|
1254
|
-
}
|
|
1255
1241
|
@media (max-width: 800px) {
|
|
1256
1242
|
.BRnav {
|
|
1257
1243
|
height: 34px;
|
|
@@ -1359,20 +1345,12 @@ i.BRicon {
|
|
|
1359
1345
|
vertical-align: top;
|
|
1360
1346
|
}
|
|
1361
1347
|
|
|
1362
|
-
.BRup {
|
|
1363
|
-
background-image: url("images/nav_control-up.png");
|
|
1364
|
-
background-repeat: no-repeat;
|
|
1365
|
-
}
|
|
1366
|
-
|
|
1367
1348
|
/* Mobile Only */
|
|
1368
1349
|
@media (max-width: 800px) {
|
|
1369
1350
|
/* hide navline chapters and search in mobile */
|
|
1370
1351
|
.BRnavline .BRchapter {
|
|
1371
1352
|
display: none;
|
|
1372
1353
|
}
|
|
1373
|
-
.BRnavCntlBtm {
|
|
1374
|
-
bottom: 34px;
|
|
1375
|
-
}
|
|
1376
1354
|
.BRpager.ui-slider {
|
|
1377
1355
|
height: 10px;
|
|
1378
1356
|
top: 12px;
|
|
@@ -1512,6 +1490,7 @@ ia-bookreader .BRtoolbar {
|
|
|
1512
1490
|
|
|
1513
1491
|
.BookReader .searchHiliteLayer, .BookReader .ttsHiliteLayer, .BRfloat .searchHiliteLayer, .BRfloat .ttsHiliteLayer {
|
|
1514
1492
|
pointer-events: none;
|
|
1493
|
+
z-index: 4;
|
|
1515
1494
|
}
|
|
1516
1495
|
.BookReader .searchHiliteLayer rect, .BookReader .ttsHiliteLayer rect, .BRfloat .searchHiliteLayer rect, .BRfloat .ttsHiliteLayer rect {
|
|
1517
1496
|
fill: rgba(0, 0, 255, 0.2);
|
|
@@ -1715,9 +1694,6 @@ ia-bookreader .BRtoolbar {
|
|
|
1715
1694
|
width: 18px;
|
|
1716
1695
|
height: 18px;
|
|
1717
1696
|
}
|
|
1718
|
-
.BookReader .BRsearch-navigation + .BRnav .BRnavCntl, .BRfloat .BRsearch-navigation + .BRnav .BRnavCntl {
|
|
1719
|
-
display: none;
|
|
1720
|
-
}
|
|
1721
1697
|
|
|
1722
1698
|
@keyframes highlightFocus {
|
|
1723
1699
|
to {
|
|
@@ -2115,7 +2091,7 @@ ia-bookreader .BRtoolbar {
|
|
|
2115
2091
|
background-color: #ccc;
|
|
2116
2092
|
}
|
|
2117
2093
|
|
|
2118
|
-
.BRtextLayer {
|
|
2094
|
+
.BRtextLayer, .BRtranslateLayer {
|
|
2119
2095
|
z-index: 2;
|
|
2120
2096
|
position: absolute;
|
|
2121
2097
|
top: 0;
|
|
@@ -2124,9 +2100,16 @@ ia-bookreader .BRtoolbar {
|
|
|
2124
2100
|
transform-origin: 0 0;
|
|
2125
2101
|
pointer-events: none;
|
|
2126
2102
|
cursor: text;
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2105
|
+
.BRtextLayer {
|
|
2127
2106
|
mix-blend-mode: multiply;
|
|
2128
2107
|
}
|
|
2129
2108
|
|
|
2109
|
+
.BRtranslateLayer {
|
|
2110
|
+
hyphens: auto;
|
|
2111
|
+
}
|
|
2112
|
+
|
|
2130
2113
|
.BRparagraphElement {
|
|
2131
2114
|
margin: 0;
|
|
2132
2115
|
cursor: text;
|
|
@@ -2176,19 +2159,18 @@ ia-bookreader .BRtoolbar {
|
|
|
2176
2159
|
user-select: none;
|
|
2177
2160
|
}
|
|
2178
2161
|
|
|
2162
|
+
.BRtextLayer:has([_istranslated="1"], msreadoutspan) {
|
|
2163
|
+
mix-blend-mode: normal;
|
|
2164
|
+
}
|
|
2165
|
+
|
|
2179
2166
|
.BRwordElement[_istranslated="1"], .BRwordElement[_msttexthash], .BRspace[_istranslated="1"], .BRspace[_msttexthash] {
|
|
2180
|
-
background-color: #e4dccd;
|
|
2181
2167
|
color: black;
|
|
2182
2168
|
letter-spacing: unset !important;
|
|
2183
|
-
background: #ccbfa7;
|
|
2184
|
-
}
|
|
2185
|
-
|
|
2186
|
-
.BRlineElement font[_mstmutation="1"] {
|
|
2187
|
-
background: #ccbfa7;
|
|
2188
2169
|
}
|
|
2189
2170
|
|
|
2190
2171
|
.BRlineElement:has([_istranslated="1"], [_msttexthash]) {
|
|
2191
|
-
background
|
|
2172
|
+
background: rgba(248, 237, 192, 0.8);
|
|
2173
|
+
backdrop-filter: blur(8px);
|
|
2192
2174
|
color: black;
|
|
2193
2175
|
text-align: justify;
|
|
2194
2176
|
width: inherit;
|
|
@@ -2198,10 +2180,33 @@ ia-bookreader .BRtoolbar {
|
|
|
2198
2180
|
}
|
|
2199
2181
|
|
|
2200
2182
|
.BRlineElement[_msttexthash] {
|
|
2201
|
-
background: #ccbfa7;
|
|
2202
2183
|
word-spacing: unset !important;
|
|
2203
2184
|
}
|
|
2204
2185
|
|
|
2186
|
+
.BRtranslateLayer .BRparagraphElement {
|
|
2187
|
+
pointer-events: auto;
|
|
2188
|
+
overflow-y: auto;
|
|
2189
|
+
background: rgba(248, 237, 192, 0.8);
|
|
2190
|
+
backdrop-filter: blur(8px);
|
|
2191
|
+
color: black;
|
|
2192
|
+
line-height: 1em;
|
|
2193
|
+
text-align: justify;
|
|
2194
|
+
}
|
|
2195
|
+
|
|
2196
|
+
.BRtranslateLayer .BRparagraphElement .BRlineElement {
|
|
2197
|
+
white-space: break-spaces;
|
|
2198
|
+
display: inline-block;
|
|
2199
|
+
}
|
|
2200
|
+
|
|
2201
|
+
.BRtextLayer.showingTranslation {
|
|
2202
|
+
visibility: hidden;
|
|
2203
|
+
pointer-events: none;
|
|
2204
|
+
}
|
|
2205
|
+
|
|
2206
|
+
.BRtranslateLayer .BRparagraphElement.BRtranslateHidden {
|
|
2207
|
+
display: none;
|
|
2208
|
+
}
|
|
2209
|
+
|
|
2205
2210
|
/**
|
|
2206
2211
|
* Hide modal-manager that loads item-navigator-modal
|
|
2207
2212
|
* loading bookmarks related alert messages
|