Skip to content

Commit

Permalink
Updated to restore compatibility with webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica committed Sep 6, 2024
1 parent 61c24f9 commit 54e06b4
Showing 1 changed file with 36 additions and 28 deletions.
64 changes: 36 additions & 28 deletions scribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

/**
Expand All @@ -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 {
Expand Down

0 comments on commit 54e06b4

Please sign in to comment.