From 0159d920a301d4ec6159d8f9ae0a9787452e8119 Mon Sep 17 00:00:00 2001 From: Balearica Date: Fri, 17 Jan 2025 20:04:24 -0800 Subject: [PATCH] Ran linter --- js/containers/fontContainer.js | 16 +++++----- js/fontEval.js | 6 ++-- js/fontSupp.js | 14 ++++---- js/generalWorkerMain.js | 32 +++++++++---------- js/import/convertPageHocr.js | 12 +++---- js/import/nodeAdapter.js | 16 +++++----- js/objects/imageObjects.js | 18 +++++------ js/objects/ocrObjects.js | 6 ++-- js/utils/fontUtils.js | 16 +++++----- js/utils/imageUtils.js | 19 ++++++----- js/utils/reflowPars.js | 10 +++--- js/worker/compareOCRModule.js | 58 +++++++++++++++++----------------- js/worker/generalWorker.js | 16 +++++----- 13 files changed, 119 insertions(+), 120 deletions(-) diff --git a/js/containers/fontContainer.js b/js/containers/fontContainer.js index 61e36a1..78fce63 100644 --- a/js/containers/fontContainer.js +++ b/js/containers/fontContainer.js @@ -365,14 +365,14 @@ export class FontCont { }; /** - * Gets a font object. Unlike accessing the font containers directly, - * this method allows for special values 'Default', 'SansDefault', and 'SerifDefault' to be used. - * - * @param {('Default'|'SansDefault'|'SerifDefault'|string)} family - Font family name. - * @param {('normal'|'italic'|'bold'|string)} [style='normal'] - * @param {string} [lang='eng'] - * @returns {FontContainerFont} - */ + * Gets a font object. Unlike accessing the font containers directly, + * this method allows for special values 'Default', 'SansDefault', and 'SerifDefault' to be used. + * + * @param {('Default'|'SansDefault'|'SerifDefault'|string)} family - Font family name. + * @param {('normal'|'italic'|'bold'|string)} [style='normal'] + * @param {string} [lang='eng'] + * @returns {FontContainerFont} + */ static getFont = (family, style = 'normal', lang = 'eng') => { if (FontCont.doc?.[family]?.[style] && !FontCont.doc?.[family]?.[style]?.disable) { return FontCont.doc[family][style]; diff --git a/js/fontEval.js b/js/fontEval.js index c70ad73..af6db06 100644 --- a/js/fontEval.js +++ b/js/fontEval.js @@ -40,9 +40,9 @@ export async function evalPagesFont(font, pageArr, opt, n = 500) { } /** -* @param {Array} pageArr -* @param {boolean} opt - Whether to use optimized fonts. -*/ + * @param {Array} pageArr + * @param {boolean} opt - Whether to use optimized fonts. + */ export async function evaluateFonts(pageArr, opt) { const evalCarlito = !!(opt ? FontCont.opt?.Carlito : FontCont.raw?.Carlito); const evalNimbusSans = !!(opt ? FontCont.opt?.NimbusSans : FontCont.raw?.NimbusSans); diff --git a/js/fontSupp.js b/js/fontSupp.js index 37e4681..0d8b36d 100644 --- a/js/fontSupp.js +++ b/js/fontSupp.js @@ -62,13 +62,13 @@ const calcSuppFontInfoForWords = async (words) => { }; /** - * - * @param {Array} ocrArr - * @returns - * This function runs recognition on certain fonts when we need more information about them. - * Fonts are included when either (1) we need to know if they are sans or serif or (2) if the text is extracted from a PDF, - * and we need to determine how large to render the text. - */ + * + * @param {Array} ocrArr + * @returns + * This function runs recognition on certain fonts when we need more information about them. + * Fonts are included when either (1) we need to know if they are sans or serif or (2) if the text is extracted from a PDF, + * and we need to determine how large to render the text. + */ export const calcSuppFontInfo = async (ocrArr) => { if (!ocrArr) return; await gs.initTesseract({ anyOk: true, langs: ['eng'] }); diff --git a/js/generalWorkerMain.js b/js/generalWorkerMain.js index 1112447..dda912d 100644 --- a/js/generalWorkerMain.js +++ b/js/generalWorkerMain.js @@ -57,10 +57,10 @@ export async function initGeneralWorker() { } /** - * Wraps a function to be called via worker messages. - * @param {string} func The function name to call. - * @returns {Function} A function that returns a promise resolving to the worker's response. - */ + * Wraps a function to be called via worker messages. + * @param {string} func The function name to call. + * @returns {Function} A function that returns a promise resolving to the worker's response. + */ function wrap(func) { return function (...args) { return new Promise((innerResolve, innerReject) => { @@ -72,10 +72,10 @@ export async function initGeneralWorker() { } /** - * Similar to wrap, but handles two promises. - * @param {string} func The function name to call. - * @returns {Array} Returns two promises in an array. - */ + * Similar to wrap, but handles two promises. + * @param {string} func The function name to call. + * @returns {Array} Returns two promises in an array. + */ function wrap2(func) { return function (...args) { const id = promiseId++; @@ -187,14 +187,14 @@ export class gs { static optimizeFont = async (args) => (await gs.schedulerInner.addJob('optimizeFont', args)); /** - * @template {Partial} TO - * @param {Object} args - * @param {Parameters[0]} args.image - * @param {Parameters[1]} args.options - * @param {TO} args.output - * @returns {Promise>} - * Exported for type inference purposes, should not be imported anywhere. - */ + * @template {Partial} TO + * @param {Object} args + * @param {Parameters[0]} args.image + * @param {Parameters[1]} args.options + * @param {TO} args.output + * @returns {Promise>} + * Exported for type inference purposes, should not be imported anywhere. + */ static recognize = async (args) => (await gs.schedulerInner.addJob('recognize', args)); /** diff --git a/js/import/convertPageHocr.js b/js/import/convertPageHocr.js index 46e7201..672d785 100644 --- a/js/import/convertPageHocr.js +++ b/js/import/convertPageHocr.js @@ -87,8 +87,8 @@ export async function convertPageHocr({ } /** - * @param {string} match - */ + * @param {string} match + */ function convertLine(match) { const titleStrLine = match.match(/title=['"]([^'"]+)/)?.[1]; if (!titleStrLine) return ''; @@ -258,8 +258,8 @@ export async function convertPageHocr({ } /** - * @param {string} match - */ + * @param {string} match + */ function convertWord(match) { const wordID = match.match(/id=['"]([^'"]*)['"]/i)?.[1] || `word_${n + 1}_${pageObj.lines.length + 1}_${lineObj.words.length + 1}`; @@ -357,8 +357,8 @@ export async function convertPageHocr({ } /** - * @param {string} match - */ + * @param {string} match + */ const convertPar = (match) => { const parLang = match.match(/^.+?lang=['"]([^'"]*)['"]/i)?.[1]; if (parLang) currentLang = parLang; diff --git a/js/import/nodeAdapter.js b/js/import/nodeAdapter.js index 22db36a..492f95a 100644 --- a/js/import/nodeAdapter.js +++ b/js/import/nodeAdapter.js @@ -9,11 +9,11 @@ import path from 'path'; */ export class FileNode { /** - * Creates an instance of the File class. - * @param {string} filePath - The path to the file. - * @param {string} name - The name of the file. - * @param {Buffer} fileData - The file's data. - */ + * Creates an instance of the File class. + * @param {string} filePath - The path to the file. + * @param {string} name - The name of the file. + * @param {Buffer} fileData - The file's data. + */ constructor(filePath, name, fileData) { this.filePath = filePath; this.name = name; @@ -21,9 +21,9 @@ export class FileNode { } /** - * Returns an ArrayBuffer with the file's contents. - * @returns {Promise} A promise that resolves with the file's contents as an ArrayBuffer. - */ + * Returns an ArrayBuffer with the file's contents. + * @returns {Promise} A promise that resolves with the file's contents as an ArrayBuffer. + */ async arrayBuffer() { return this.fileData.buffer.slice(this.fileData.byteOffset, this.fileData.byteOffset + this.fileData.byteLength); } diff --git a/js/objects/imageObjects.js b/js/objects/imageObjects.js index f2c3f91..4b782fc 100644 --- a/js/objects/imageObjects.js +++ b/js/objects/imageObjects.js @@ -2,15 +2,15 @@ import { getJpegDimensions, getPngDimensions } from '../utils/imageUtils.js'; export class ImageWrapper { /** - * @param {number} n - Page number - * @param {string} imageStr - Base-64 encoded image string. Should start with "data:image/png" or "data:image/jpeg". - * @param {string} colorMode - Color mode ("color", "gray", or "binary"). - * @param {boolean} rotated - Whether image has been rotated. - * @param {boolean} upscaled - Whether image has been upscaled. - * - * All properties of this object must be serializable, as ImageWrapper objects are sent between threads. - * This means that no promises can be used. - */ + * @param {number} n - Page number + * @param {string} imageStr - Base-64 encoded image string. Should start with "data:image/png" or "data:image/jpeg". + * @param {string} colorMode - Color mode ("color", "gray", or "binary"). + * @param {boolean} rotated - Whether image has been rotated. + * @param {boolean} upscaled - Whether image has been upscaled. + * + * All properties of this object must be serializable, as ImageWrapper objects are sent between threads. + * This means that no promises can be used. + */ constructor(n, imageStr, colorMode, rotated = false, upscaled = false) { this.n = n; this.src = imageStr; diff --git a/js/objects/ocrObjects.js b/js/objects/ocrObjects.js index 166b682..25903e9 100644 --- a/js/objects/ocrObjects.js +++ b/js/objects/ocrObjects.js @@ -134,7 +134,7 @@ export function OcrWord(line, text, bbox, id) { /** * @type {boolean} - If `true`, left/right coordinates represent the left/rightmost pixel. * If `false`, left/right coordinates represent the start/end of the font bounding box. - */ + */ this.visualCoords = true; } @@ -331,7 +331,7 @@ export const getLineText = (line) => { /** * @param {OcrPar} par -*/ + */ export const getParText = (par) => { let text = ''; for (let i = 0; i < par.lines.length; i++) { @@ -584,7 +584,7 @@ function rotateLine(line, angle, dims = null, useCharLevel = false) { /** * Clones page object. * @param {OcrPage} page - */ + */ function clonePage(page) { const pageNew = new OcrPage(page.n, { ...page.dims }); for (const line of page.lines) { diff --git a/js/utils/fontUtils.js b/js/utils/fontUtils.js index 5fc5dc8..444ea0b 100644 --- a/js/utils/fontUtils.js +++ b/js/utils/fontUtils.js @@ -99,7 +99,7 @@ function calcWordFontSizePrecise(wordArr, fontOpentype, nonLatin = false) { * Adds ligatures to text of `OcrWord` object. Returns an array of letters. * @param {OcrWord} word * @returns {Array} -*/ + */ export function addLigatures(word) { if (word.smallCaps || !opt.ligatures) return word.text.split(''); const fontI = FontCont.getWordFont(word); @@ -181,9 +181,9 @@ function calcWordCharMetrics(wordText, fontOpentype) { if (opt.kerning) { const glyphJ = fontOpentype.charToGlyph(charJ); const kerning = fontOpentype.getKerningValue(glyphI, glyphJ); - kerningArr.push(kerning); + kerningArr.push(kerning); } else { - kerningArr.push(0); + kerningArr.push(0); } } } @@ -322,11 +322,11 @@ export const calcWordFontSize = (word) => { // Therefore, the appropriate font size must be calculated using (1) the character stats from the input image and // (2) stats regarding the font being used. /** -* Get or calculate font size for line. -* This value will either be (1) a manually set value or (2) a value calculated using line metrics. -* @param {OcrLine} line -* @returns {number} -*/ + * Get or calculate font size for line. + * This value will either be (1) a manually set value or (2) a value calculated using line metrics. + * @param {OcrLine} line + * @returns {number} + */ export const calcLineFontSize = (line) => { if (line._size) return line._size; diff --git a/js/utils/imageUtils.js b/js/utils/imageUtils.js index 904d24a..931d00c 100644 --- a/js/utils/imageUtils.js +++ b/js/utils/imageUtils.js @@ -1,6 +1,5 @@ /* eslint-disable no-bitwise */ - /** * Loads an image from a given URL and sets it to a specified HTML element. * @@ -53,15 +52,15 @@ export function base64ToBytes(base64) { } /** - * Extracts the width and height from the IHDR chunk of a PNG image encoded in base64. - * - * This function decodes the base64 to bytes and parses the IHDR chunk to extract the image dimensions. - * It assumes the base64 string is a valid PNG image and directly starts parsing the binary data. - * Note: This is a basic implementation without extensive error handling or validation. - * - * @param {string} base64 - The base64 encoded string of the PNG image. - * @returns {dims} An object containing the width and height of the image. - */ + * Extracts the width and height from the IHDR chunk of a PNG image encoded in base64. + * + * This function decodes the base64 to bytes and parses the IHDR chunk to extract the image dimensions. + * It assumes the base64 string is a valid PNG image and directly starts parsing the binary data. + * Note: This is a basic implementation without extensive error handling or validation. + * + * @param {string} base64 - The base64 encoded string of the PNG image. + * @returns {dims} An object containing the width and height of the image. + */ export function getPngDimensions(base64) { // The number 96 is chosen to line up leanly with byte boundaries (97 would result in an error) // but is otherwise arbitrary, while being large enough to contain the IHDR chunk. diff --git a/js/utils/reflowPars.js b/js/utils/reflowPars.js index 6bd57b3..99e00c2 100644 --- a/js/utils/reflowPars.js +++ b/js/utils/reflowPars.js @@ -51,11 +51,11 @@ export function assignParagraphs(page, angle) { } /** - * Calculates expected line start and end positions based on surrounding lines. - * If this line varies from those values, it may be the first or last line of a paragraph. - * @param {number} lineIndex - Index of the line to calculate the expected values for. - * @returns - */ + * Calculates expected line start and end positions based on surrounding lines. + * If this line varies from those values, it may be the first or last line of a paragraph. + * @param {number} lineIndex - Index of the line to calculate the expected values for. + * @returns + */ const calcExpected = (lineIndex) => { // Ideally, we compare the current line to the next 5 lines. // When there are fewer than 5 lines after the current line, we add previous lines to the window. diff --git a/js/worker/compareOCRModule.js b/js/worker/compareOCRModule.js index a3bb2b6..c146deb 100644 --- a/js/worker/compareOCRModule.js +++ b/js/worker/compareOCRModule.js @@ -74,24 +74,24 @@ export async function drawWordActual(words, imageBinaryBit, angle) { } /** - * Function that draws a word on a canvas. - * This code was factored out to allow for drawing multiple times while only calculating metrics once. - * Therefore, only the drawing code should be in this function; the metrics should be calculated elsewhere - * and passed to this function, rather than calcualting from an `OcrWord` object. - * - * @param {Object} params - * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} params.ctx - * @param {Array} params.charArr - * @param {number} params.left - * @param {number} params.bottom - * @param {Array} params.advanceArr - Array of pixels to advance for each character. - * Unlike the "advance" property of a glyph, this is the actual distance to advance on the canvas, - * and should include kerning and character spacing. - * @param {FontContainerFont} params.font - * @param {number} params.size - * @param {boolean} params.smallCaps - * @param {string} [params.fillStyle='black'] - */ + * Function that draws a word on a canvas. + * This code was factored out to allow for drawing multiple times while only calculating metrics once. + * Therefore, only the drawing code should be in this function; the metrics should be calculated elsewhere + * and passed to this function, rather than calcualting from an `OcrWord` object. + * + * @param {Object} params + * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} params.ctx + * @param {Array} params.charArr + * @param {number} params.left + * @param {number} params.bottom + * @param {Array} params.advanceArr - Array of pixels to advance for each character. + * Unlike the "advance" property of a glyph, this is the actual distance to advance on the canvas, + * and should include kerning and character spacing. + * @param {FontContainerFont} params.font + * @param {number} params.size + * @param {boolean} params.smallCaps + * @param {string} [params.fillStyle='black'] + */ const printWordOnCanvas = async ({ ctx, charArr, left, bottom, advanceArr, font, size, smallCaps, fillStyle = 'black', }) => { @@ -118,15 +118,15 @@ const printWordOnCanvas = async ({ }; /** - * Print word on canvas. - * - * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} ctx - * @param {OcrWord} word - * @param {number} offsetX - * @param {number} cropY - * @param {?CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} ctxView - * @param {boolean} [imageRotated=false] - - */ + * Print word on canvas. + * + * @param {CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} ctx + * @param {OcrWord} word + * @param {number} offsetX + * @param {number} cropY + * @param {?CanvasRenderingContext2D|OffscreenCanvasRenderingContext2D} ctxView + * @param {boolean} [imageRotated=false] - + */ export const drawWordRender = async (ctx, word, offsetX = 0, cropY = 0, ctxView = null, imageRotated = false) => { if (!FontCont.raw) throw new Error('Fonts must be defined before running this function.'); if (!ctx) throw new Error('Canvases must be defined before running this function.'); @@ -1304,8 +1304,8 @@ export async function evalPageFont({ } /** - * @param {OcrLine} ocrLineJ - */ + * @param {OcrLine} ocrLineJ + */ const transformLineFont = (ocrLineJ) => { if (!FontCont.raw) throw new Error('Fonts must be defined before running this function.'); diff --git a/js/worker/generalWorker.js b/js/worker/generalWorker.js index 0fcce32..f1d94be 100644 --- a/js/worker/generalWorker.js +++ b/js/worker/generalWorker.js @@ -329,14 +329,14 @@ export const recognizeAndConvert2 = async ({ }; /** -* @template {Partial} TO -* @param {Object} args -* @param {Parameters[0]} args.image -* @param {Parameters[1]} args.options -* @param {TO} args.output -* @returns {Promise>} -* Exported for type inference purposes, should not be imported anywhere. -*/ + * @template {Partial} TO + * @param {Object} args + * @param {Parameters[0]} args.image + * @param {Parameters[1]} args.options + * @param {TO} args.output + * @returns {Promise>} + * Exported for type inference purposes, should not be imported anywhere. + */ export const recognize = async ({ image, options, output }) => { if (!worker) throw new Error('Worker not initialized'); const res1 = await worker.recognize(image, options, output);