All files / src/geometry LineStore.ts

91.32% Statements 179/196
84.12% Branches 53/63
91.66% Functions 11/12
91.32% Lines 179/196

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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 2461x 1x   1x 1x     1x 25x 25x   25x 217x 217x 120x 157x 97x 97x 217x 217x   25x           25x 6x   6x   6x 11x 11x 11x 11x 11x   6x 6x   25x 9x   9x 14x 14x   14x 14x   14x 14x 14x 14x   9x 1x 1x   1x 1x             9x   9x 14x 14x   9x 9x   25x 15x 15x 15x   25x 19x   19x   19x 19x 119x 16x 119x 11x 103x 92x 92x 92x 92x 119x   19x 16x 16x 16x   19x 19x   25x 19x   19x   19x 19x 96x 16x 96x 38x 42x 42x 42x 42x 42x 96x   19x 16x 16x 16x   19x 19x   25x 15x 15x   15x 112x 46x 46x 112x   15x 131x 45x 45x 131x   15x 15x 15x   25x 15x 10x 1x 1x 1x 10x 14x 15x   25x 108x 108x   108x 108x   108x 108x   108x 119x 11x     11x 7x 7x 119x 108x     108x 108x 108x 119x   108x   108x       108x 108x 108x   108x 108x   25x 58x 58x   58x 58x   58x 58x   58x 96x 21x     21x 17x 17x 96x 75x 17x 17x 75x 75x 75x 96x   58x   58x 7x 7x 7x 58x 51x 51x   58x 58x 25x  
import { Line, LineDirection } from './Line.js';
import { Point } from './Point.js';
import type { Rectangle } from './Rectangle.js';
import { Shape } from './Shape.js';
import { Table } from './Table.js';
import type { TableData } from './TableData.js';
 
export class LineStore {
	public hLines: Array<Line> = [];
	public vLines: Array<Line> = [];
 
	public add(line: Line): void {
		if (line.valid) {
			if (line.direction === LineDirection.Horizontal) {
				this.hLines.push(line);
			} else if (line.direction === LineDirection.Vertical) {
				this.vLines.push(line);
			}
		}
	}
 
	public addRectangle(rect: Rectangle): void {
		for (const line of rect.getLines()) {
			this.add(line);
		}
	}
 
	public getTableData(): Array<TableData> {
		const result: Array<TableData> = [];
 
		const tables = this.getTables();
 
		for (const table of tables) {
			const data = table.toData();
			if (data) {
				result.push(data);
			}
		}
 
		return result;
	}
 
	public getTables(): Array<Table> {
		const result: Array<Table> = [];
 
		while (this.hLines.length !== 0) {
			const hLine = this.hLines.shift();
			if (!hLine) continue;
 
			const filled = this.tryFill(result, hLine);
			if (filled) continue;
 
			const table = new Table(hLine);
			this.fillTable(table);
			result.push(table);
		}
 
		while (this.vLines.length !== 0) {
			const vLine = this.vLines.shift();
			if (!vLine) continue;
 
			const filled = this.tryFill(result, vLine);
			if (filled) continue;
 
			const table = new Table(vLine);
			this.fillTable(table);
			result.push(table);
		}
 
		const validTables = result.filter((t) => t.isValid);
 
		for (const table of validTables) {
			table.normalize();
		}
 
		return validTables;
	}
 
	public normalize(): void {
		this.normalizeHorizontal();
		this.normalizeVertical();
	}
 
	public normalizeHorizontal() {
		this.hLines.sort((l1, l2) => l1.from.y - l2.from.y);
 
		const newLines: Array<Line> = [];
 
		let sameY: Array<Line> = [];
		for (const line of this.hLines) {
			if (sameY.length === 0) {
				sameY.push(line);
			} else if (Math.abs(sameY[0]?.from.y - line.from.y) < Shape.tolerance) {
				sameY.push(line);
			} else {
				const merged = this.margeHorizontalLines(sameY);
				newLines.push(...merged);
				sameY = [line];
			}
		}
 
		if (sameY.length > 0) {
			const merged = this.margeHorizontalLines(sameY);
			newLines.push(...merged);
		}
 
		this.hLines = newLines;
	}
 
	public normalizeVertical() {
		this.vLines.sort((l1, l2) => l1.from.x - l2.from.x);
 
		const newLines: Array<Line> = [];
 
		let sameX: Array<Line> = [];
		for (const line of this.vLines) {
			if (sameX.length === 0) {
				sameX.push(line);
			} else if (Math.abs(sameX[0]?.from.x - line.from.x) < Shape.tolerance) {
				sameX.push(line);
			} else {
				const merged = this.margeVerticalLines(sameX);
				newLines.push(...merged);
				sameX = [line];
			}
		}
 
		if (sameX.length > 0) {
			const merged = this.margeVerticalLines(sameX);
			newLines.push(...merged);
		}
 
		this.vLines = newLines;
	}
 
	private fillTable(table: Table): void {
		const newVLines: Array<Line> = [];
		const newHLines: Array<Line> = [];
 
		for (const vLine of this.vLines) {
			if (!table.add(vLine)) {
				newVLines.push(vLine);
			}
		}
 
		for (const hLine of this.hLines) {
			if (!table.add(hLine)) {
				newHLines.push(hLine);
			}
		}
 
		this.hLines = newHLines;
		this.vLines = newVLines;
	}
 
	private tryFill(tables: Array<Table>, line: Line): boolean {
		for (const table of tables) {
			if (table.add(line)) {
				this.fillTable(table);
				return true;
			}
		}
		return false;
	}
 
	private margeHorizontalLines(sameYLines: Array<Line>): Array<Line> {
		const result: Array<Line> = [];
		sameYLines.sort((l1, l2) => l1.from.x - l2.from.x);
 
		const sameY = sameYLines[0]?.from.y;
		if (sameY === undefined) return result;
 
		let minX: number = Number.MAX_SAFE_INTEGER;
		let maxX: number = Number.MIN_SAFE_INTEGER;
 
		for (const line of sameYLines) {
			if (line.from.x - maxX < Shape.tolerance) {
				if (line.from.x < minX) {
					minX = line.from.x;
				}
				if (line.to.x > maxX) {
					maxX = line.to.x;
				}
			} else {
				if (maxX > minX) {
					result.push(new Line(new Point(minX, sameY), new Point(maxX, sameY)));
				}
				minX = line.from.x;
				maxX = line.to.x;
			}
		}
 
		const last = result[result.length - 1];
 
		if (last) {
			if (last.from.x !== minX && last.to.x !== maxX) {
				result.push(new Line(new Point(minX, sameY), new Point(maxX, sameY)));
			}
		} else {
			result.push(new Line(new Point(minX, sameY), new Point(maxX, sameY)));
		}
 
		return result;
	}
 
	private margeVerticalLines(sameXLines: Array<Line>): Array<Line> {
		const result: Array<Line> = [];
		sameXLines.sort((l1, l2) => l1.from.y - l2.from.y);
 
		const sameX = sameXLines[0]?.from.x;
		if (sameX === undefined) return result;
 
		let minY: number = Number.MAX_SAFE_INTEGER;
		let maxY: number = Number.MIN_SAFE_INTEGER;
 
		for (const line of sameXLines) {
			if (line.from.y - maxY < Shape.tolerance) {
				if (line.from.y < minY) {
					minY = line.from.y;
				}
				if (line.to.y > maxY) {
					maxY = line.to.y;
				}
			} else {
				if (maxY > minY) {
					result.push(new Line(new Point(sameX, minY), new Point(sameX, maxY)));
				}
				minY = line.from.y;
				maxY = line.to.y;
			}
		}
 
		const last = result[result.length - 1];
 
		if (last) {
			if (last.from.y !== minY && last.to.y !== maxY) {
				result.push(new Line(new Point(sameX, minY), new Point(sameX, maxY)));
			}
		} else {
			result.push(new Line(new Point(sameX, minY), new Point(sameX, maxY)));
		}
 
		return result;
	}
}