All files / src TableUtil.ts

84.61% Statements 143/169
78.57% Branches 33/42
81.25% Functions 13/16
84.61% Lines 143/169

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218    1x 1x 1x 1x 1x 1x     1x 13x 13x 13x 13x 13x 13x 13x 13x   13x 13x 13x 13x 13x 13x 13x 13x 13x 13x   13x       13x 3x 3x   3x   3x 2x       2x 2x 2x 1x 3x   13x 6x   6x 6x   6x 3x 3x   2x 2x 3x   3x 3x   6x 3x 3x   2x 2x 3x     6x 13x       1x 4x 4x 4x 4x 4x   4x 4x 4x   4x   4x 2x       2x 2x 2x   2x 2x   4x       4x       4x 1x 1x                 1x   1x 1x   4x 4x 2x 2x 2x 2x 2x   2x 1x 1x 1x   1x 1x 1x 1x 1x 1x 1x 1x   2x       2x       2x 2x 4x   4x 1x 1x 2x 2x 3x 3x 2x 2x 1x 1x   4x 1x 1x   1x       1x 1x   1x       1x 1x 1x 1x 1x   4x 2x 2x   2x 2x   4x 1x   1x 2x 2x 2x   1x 1x 4x  
import type { TextItem } from 'pdfjs-dist/types/src/display/api.js';
 
enum RelativeDirections {
	None = 0,
	Left,
	Right,
	Top,
	Bottom,
}
 
export class Rectangle {
	public id: number;
	public x: number;
	public y: number;
	public width: number;
	public height: number;
	public x2: number;
	public y2: number;
	public text: string;
 
	constructor(id: number, x: number, y: number, width: number, height: number) {
		this.id = id;
		this.x = x;
		this.y = y;
		this.width = width;
		this.height = height;
		this.x2 = x + width;
		this.y2 = y + height;
		this.text = '';
	}
 
	public toString(): string {
		return `${this.id} ${this.text}`;
	}
 
	public tryAddText(item: TextItem) {
		const x = item.transform[4];
		const y = item.transform[5];
 
		const isInside = x >= this.x && y >= this.y && x <= this.x2 && y <= this.y2;
 
		if (isInside) {
			if (item.str?.length === 0 && this.text.length === 0) {
				return true;
			}
 
			this.text += `${item.str}${item.hasEOL ? '\n' : ''}`;
			return true;
		}
		return false;
	}
 
	public isNeighbour(rect: Rectangle, distance: number = 1): RelativeDirections {
		const result: RelativeDirections = RelativeDirections.None;
 
		const heightOk = Math.abs(this.height - rect.height) < distance;
		const yOk = Math.abs(this.y - rect.y) < distance;
 
		if (heightOk && yOk) {
			const isLeft = Math.abs(this.x - rect.x2) < distance;
			if (isLeft) return RelativeDirections.Left;
 
			const isRight = Math.abs(this.x2 - rect.x) < distance;
			if (isRight) return RelativeDirections.Right;
		}
 
		const widthOk = Math.abs(this.width - rect.width) < distance;
		const xOk = Math.abs(this.x - rect.x) < distance;
 
		if (widthOk && xOk) {
			const isTop = Math.abs(this.y - rect.y2) < distance;
			if (isTop) return RelativeDirections.Top;
 
			const isBottom = Math.abs(this.y2 - rect.y) < distance;
			if (isBottom) return RelativeDirections.Bottom;
		}
 
		return result;
	}
}
 
type TableRow = Array<Rectangle>;
 
export class Table {
	public grid: Array<TableRow>;
	private minTableX1: number = Number.MAX_VALUE;
	private minTableY1: number = Number.MAX_VALUE;
	private maxTableX2: number = Number.MIN_VALUE;
	private maxTableY2: number = Number.MIN_VALUE;
 
	constructor(rect: Rectangle) {
		this.grid = [[rect]];
	}
 
	private _cellCount: number = -1;
 
	public get cellCount(): number {
		if (this._cellCount > -1) {
			return this._cellCount;
		}
 
		for (const row of this.grid) {
			this._cellCount += row.length;
		}
 
		return this._cellCount;
	}
 
	public get width(): number {
		return this.maxTableX2 - this.minTableX1;
	}
 
	public get height(): number {
		return this.maxTableY2 - this.minTableY1;
	}
 
	public static tryAddText(pageTables: Array<Table>, item: TextItem): boolean {
		for (const table of pageTables) {
			if (table.cellCount < 3) continue;
			if (table.isInside(item)) {
				for (const row of table.grid) {
					for (const rectangle of row) {
						const res = rectangle.tryAddText(item);
						if (res) return true;
					}
				}
			}
		}
 
		return false;
	}
 
	public static addRectangle(pageTables: Array<Table>, rect: Rectangle): boolean {
		for (const table of pageTables) {
			for (let rowIndex = 0; rowIndex < table.grid.length; rowIndex++) {
				const row = table.grid[rowIndex];
				for (let colIndex = 0; row && colIndex < row.length; colIndex++) {
					const currentRect = row[colIndex];
					const dir = currentRect?.isNeighbour(rect);
 
					if (dir === RelativeDirections.Right) {
						row.push(rect);
						return true;
					}
 
					if (dir === RelativeDirections.Bottom) {
						const bottomRow = table.grid[rowIndex + 1];
						if (bottomRow === undefined) {
							const newRow: TableRow = [rect];
							table.grid.push(newRow);
							return true;
						}
					}
 
					if (dir === RelativeDirections.Left || dir === RelativeDirections.Top) {
						// TODO remove
						// debugger;
					}
				}
			}
		}
 
		pageTables.push(new Table(rect));
		return true;
	}
 
	public getTableArray(): Array<Array<string>> {
		const result: Array<Array<string>> = [];
		for (const row of this.grid) {
			const rowStr: Array<string> = [];
			for (const rect of row) {
				rowStr.push(rect.text.trim());
			}
			result.push(rowStr);
		}
		return result;
	}
 
	public initMinMax(): void {
		const firstRow = this.grid[0];
		const lastRow = this.grid[this.grid.length - 1];
 
		if (firstRow === undefined || lastRow === undefined) {
			throw new Error('malformed table');
		}
 
		const firstRect = firstRow[0];
		const lastRect = lastRow[lastRow.length - 1];
 
		if (firstRect === undefined || lastRect === undefined) {
			throw new Error('malformed table');
		}
 
		this.minTableX1 = firstRect.x;
		this.minTableY1 = firstRect.y;
		this.maxTableX2 = lastRect.x2;
		this.maxTableY2 = lastRect.y2;
	}
 
	public isInside(item: TextItem) {
		const x = item.transform[4];
		const y = item.transform[5];
 
		return x >= this.minTableX1 && y >= this.minTableY1 && x <= this.maxTableX2 && y <= this.maxTableY2;
	}
 
	public toString(): string {
		const result: Array<string> = [];
 
		for (const row of this.grid) {
			const rowStr = row.map((i) => i.text).join('\t');
			result.push(rowStr);
		}
 
		return result.join('\n');
	}
}