Skip to content

Commit

Permalink
Ran linter
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica committed Jan 18, 2025
1 parent 7b53bca commit 0159d92
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 120 deletions.
16 changes: 8 additions & 8 deletions js/containers/fontContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
6 changes: 3 additions & 3 deletions js/fontEval.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export async function evalPagesFont(font, pageArr, opt, n = 500) {
}

/**
* @param {Array<OcrPage>} pageArr
* @param {boolean} opt - Whether to use optimized fonts.
*/
* @param {Array<OcrPage>} 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);
Expand Down
14 changes: 7 additions & 7 deletions js/fontSupp.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ const calcSuppFontInfoForWords = async (words) => {
};

/**
*
* @param {Array<OcrPage>} 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<OcrPage>} 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'] });
Expand Down
32 changes: 16 additions & 16 deletions js/generalWorkerMain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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++;
Expand Down Expand Up @@ -187,14 +187,14 @@ export class gs {
static optimizeFont = async (args) => (await gs.schedulerInner.addJob('optimizeFont', args));

/**
* @template {Partial<Tesseract.OutputFormats>} TO
* @param {Object} args
* @param {Parameters<Tesseract.Worker['recognize']>[0]} args.image
* @param {Parameters<Tesseract.Worker['recognize']>[1]} args.options
* @param {TO} args.output
* @returns {Promise<Tesseract.Page<TO>>}
* Exported for type inference purposes, should not be imported anywhere.
*/
* @template {Partial<Tesseract.OutputFormats>} TO
* @param {Object} args
* @param {Parameters<Tesseract.Worker['recognize']>[0]} args.image
* @param {Parameters<Tesseract.Worker['recognize']>[1]} args.options
* @param {TO} args.output
* @returns {Promise<Tesseract.Page<TO>>}
* Exported for type inference purposes, should not be imported anywhere.
*/
static recognize = async (args) => (await gs.schedulerInner.addJob('recognize', args));

/**
Expand Down
12 changes: 6 additions & 6 deletions js/import/convertPageHocr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down Expand Up @@ -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}`;

Expand Down Expand Up @@ -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;
Expand Down
16 changes: 8 additions & 8 deletions js/import/nodeAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ 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;
this.fileData = fileData;
}

/**
* Returns an ArrayBuffer with the file's contents.
* @returns {Promise<ArrayBuffer>} A promise that resolves with the file's contents as an ArrayBuffer.
*/
* Returns an ArrayBuffer with the file's contents.
* @returns {Promise<ArrayBuffer>} 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);
}
Expand Down
18 changes: 9 additions & 9 deletions js/objects/imageObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions js/objects/ocrObjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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++) {
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions js/utils/fontUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>}
*/
*/
export function addLigatures(word) {
if (word.smallCaps || !opt.ligatures) return word.text.split('');
const fontI = FontCont.getWordFont(word);
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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;

Expand Down
19 changes: 9 additions & 10 deletions js/utils/imageUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable no-bitwise */


/**
* Loads an image from a given URL and sets it to a specified HTML element.
*
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions js/utils/reflowPars.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
58 changes: 29 additions & 29 deletions js/worker/compareOCRModule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} params.charArr
* @param {number} params.left
* @param {number} params.bottom
* @param {Array<number>} 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<string>} params.charArr
* @param {number} params.left
* @param {number} params.bottom
* @param {Array<number>} 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',
}) => {
Expand All @@ -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.');
Expand Down Expand Up @@ -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.');

Expand Down
Loading

0 comments on commit 0159d92

Please sign in to comment.