diff --git a/scribe.js b/scribe.js index 2ca4dce..c7e0af1 100644 --- a/scribe.js +++ b/scribe.js @@ -98,10 +98,14 @@ const extractText = async (files, langs = ['eng'], outputFormat = 'txt', options * @public */ async function writeDebugImages(ctx, compDebugArrArr, filePath) { - await drawDebugImages({ ctx, compDebugArrArr, context: 'node' }); - const buffer0 = ctx.canvas.toBuffer('image/png'); - const fs = await import('fs'); - fs.writeFileSync(filePath, buffer0); + if (typeof process === 'undefined') { + throw new Error('This function is only available in Node.js.'); + } else { + await drawDebugImages({ ctx, compDebugArrArr, context: 'node' }); + const buffer0 = ctx.canvas.toBuffer('image/png'); + const fs = await import('fs'); + fs.writeFileSync(filePath, buffer0); + } } /** @@ -111,39 +115,43 @@ async function writeDebugImages(ctx, compDebugArrArr, filePath) { * @returns */ async function dumpDebugImages(dir) { - if (typeof process === 'undefined') throw new Error('This function is only available in Node.js.'); - - if (!DebugData.debugImg.Combined || DebugData.debugImg.Combined.length === 0) { - console.log('No debug images to dump.'); - return; - } + if (typeof process === 'undefined') { + throw new Error('This function is only available in Node.js.'); + } else { + if (!DebugData.debugImg.Combined || DebugData.debugImg.Combined.length === 0) { + console.log('No debug images to dump.'); + return; + } - const { createCanvas } = await import('canvas'); - const canvasAlt = createCanvas(200, 200); - const ctxDebug = canvasAlt.getContext('2d'); + const { createCanvas } = await import('canvas'); + const canvasAlt = createCanvas(200, 200); + const ctxDebug = canvasAlt.getContext('2d'); - for (const [name, imgArr] of Object.entries(DebugData.debugImg)) { - if (!imgArr || imgArr.length === 0) continue; - for (let i = 0; i < imgArr.length; i++) { - const filePath = `${dir}/${name}_${i}.png`; - await writeDebugImages(ctxDebug, [imgArr[i]], filePath); + for (const [name, imgArr] of Object.entries(DebugData.debugImg)) { + if (!imgArr || imgArr.length === 0) continue; + for (let i = 0; i < imgArr.length; i++) { + const filePath = `${dir}/${name}_${i}.png`; + await writeDebugImages(ctxDebug, [imgArr[i]], filePath); + } } } } async function dumpHOCR(dir) { - if (typeof process === 'undefined') throw new Error('This function is only available in Node.js.'); - - const activeCurrent = ocrAll.active; + if (typeof process === 'undefined') { + throw new Error('This function is only available in Node.js.'); + } else { + const activeCurrent = ocrAll.active; + + const fs = await import('fs'); + for (const [name, pages] of Object.entries(ocrAll)) { + ocrAll.active = pages; + const hocrStr = await exportData('hocr'); + fs.writeFileSync(`${dir}/${name}.hocr`, hocrStr); + } - const fs = await import('fs'); - for (const [name, pages] of Object.entries(ocrAll)) { - ocrAll.active = pages; - const hocrStr = await exportData('hocr'); - fs.writeFileSync(`${dir}/${name}.hocr`, hocrStr); + ocrAll.active = activeCurrent; } - - ocrAll.active = activeCurrent; } class data {