import { useEffect, useState } from 'react' import type { GridCell } from '@/app/(admin)/ai/ocr-overlay/types' export interface WordPosition { xPct: number wPct: number text: string fontRatio: number } /** * "Slide from left" positioning algorithm. * * Takes ALL recognised words per cell and slides them left-to-right across * the row's dark-pixel projection until each word "locks" onto its ink. * * Font size: fontRatio = 1.0 for all tokens. The renderer computes the * actual font size as medianCellHeightPx * fontRatio * fontScale, which * matches the fallback rendering exactly. The user controls size via the * font-scale slider. * * Position: each token's x-position is found by sliding a cursor from left * to right and looking for dark-pixel coverage. Token width (wPct) is * computed from canvas measureText proportional to the median cell height, * giving visually correct character widths. * * Guarantees: no words dropped, no complex matching rules needed. */ export function useSlideWordPositions( imageUrl: string, cells: GridCell[], active: boolean, rotation: 0 | 180 = 0, ): Map { const [result, setResult] = useState>(new Map()) useEffect(() => { if (!active || cells.length === 0 || !imageUrl) return const img = new Image() img.crossOrigin = 'anonymous' img.onload = () => { const imgW = img.naturalWidth const imgH = img.naturalHeight const canvas = document.createElement('canvas') canvas.width = imgW canvas.height = imgH const ctx = canvas.getContext('2d') if (!ctx) return if (rotation === 180) { ctx.translate(imgW, imgH) ctx.rotate(Math.PI) ctx.drawImage(img, 0, 0) ctx.setTransform(1, 0, 0, 1, 0, 0) } else { ctx.drawImage(img, 0, 0) } // --- Compute median cell height in image pixels --- const cellHeights = cells .filter(c => c.bbox_pct && c.bbox_pct.h > 0) .map(c => Math.round(c.bbox_pct.h / 100 * imgH)) .sort((a, b) => a - b) const medianCh = cellHeights.length > 0 ? cellHeights[Math.floor(cellHeights.length / 2)] : 30 // The renderer computes: fontSize = medianCellHeightPx * fontRatio * fontScale // With fontRatio=1.0 and fontScale=0.7 (default), that's 70% of median cell height. // We need to know how wide each token is at THAT rendered font size, // expressed in image pixels. // // The rendered container is reconWidth px wide = imgW image pixels. // So 1 image pixel = reconWidth/imgW display pixels. // Rendered font size (display px) = medianCellHeightPx_display * 1.0 * fontScale // medianCellHeightPx_display = medianCh * (reconWidth / imgW) // So rendered font = medianCh * (reconWidth/imgW) * fontScale // In image-pixel units: medianCh * fontScale // // measureText at refFontSize=40 gives pixel widths. // Scale from refFontSize → actual image-pixel font size: const refFontSize = 40 const fontFam = "'Liberation Sans', Arial, sans-serif" ctx.font = `${refFontSize}px ${fontFam}` // Approximate rendered font size in image pixels. // fontScale default is 0.7 but we don't know it here. // Use 0.7 as approximation — the slide positions will still be correct // because we only use this for relative token widths (proportional). const approxFontScale = 0.7 const renderedFontImgPx = medianCh * approxFontScale const measureScale = renderedFontImgPx / refFontSize const positions = new Map() for (const cell of cells) { if (!cell.bbox_pct || !cell.text) continue // --- Cell rectangle in image pixels --- let cx: number, cy: number const cw = Math.round(cell.bbox_pct.w / 100 * imgW) const ch = Math.round(cell.bbox_pct.h / 100 * imgH) if (rotation === 180) { cx = Math.round((100 - cell.bbox_pct.x - cell.bbox_pct.w) / 100 * imgW) cy = Math.round((100 - cell.bbox_pct.y - cell.bbox_pct.h) / 100 * imgH) } else { cx = Math.round(cell.bbox_pct.x / 100 * imgW) cy = Math.round(cell.bbox_pct.y / 100 * imgH) } if (cw <= 0 || ch <= 0) continue if (cx < 0) cx = 0 if (cy < 0) cy = 0 if (cx + cw > imgW || cy + ch > imgH) continue // --- Dark-pixel projection --- const imageData = ctx.getImageData(cx, cy, cw, ch) const proj = new Float32Array(cw) for (let y = 0; y < ch; y++) { for (let x = 0; x < cw; x++) { const idx = (y * cw + x) * 4 const lum = 0.299 * imageData.data[idx] + 0.587 * imageData.data[idx + 1] + 0.114 * imageData.data[idx + 2] if (lum < 128) proj[x]++ } } const threshold = Math.max(1, ch * 0.03) // Binary ink mask const ink = new Uint8Array(cw) for (let x = 0; x < cw; x++) { ink[x] = proj[x] >= threshold ? 1 : 0 } if (rotation === 180) { ink.reverse() } // --- Tokens --- const tokens = cell.text.split(/\s+/).filter(Boolean) if (tokens.length === 0) continue // Token widths in image pixels at the approximate rendered font size const tokenWidthsPx = tokens.map(t => Math.max(4, Math.round(ctx.measureText(t).width * measureScale)) ) const spaceWidthPx = Math.max(2, Math.round(ctx.measureText(' ').width * measureScale)) // --- Slide each token left-to-right --- const wordPos: WordPosition[] = [] let cursor = 0 for (let ti = 0; ti < tokens.length; ti++) { const tokenW = tokenWidthsPx[ti] // Find first x from cursor where ≥20% of span has ink const coverageNeeded = Math.max(1, Math.round(tokenW * 0.20)) let bestX = cursor const searchLimit = Math.max(cursor, cw - tokenW) for (let x = cursor; x <= searchLimit; x++) { let inkCount = 0 const spanEnd = Math.min(x + tokenW, cw) for (let dx = 0; dx < spanEnd - x; dx++) { inkCount += ink[x + dx] } if (inkCount >= coverageNeeded) { bestX = x break } // Safety: don't scan more than 40% of cell width past cursor if (x > cursor + cw * 0.4 && ti > 0) { bestX = cursor break } } // Clamp to cell bounds if (bestX + tokenW > cw) { bestX = Math.max(0, cw - tokenW) } wordPos.push({ xPct: cell.bbox_pct.x + (bestX / cw) * cell.bbox_pct.w, wPct: (tokenW / cw) * cell.bbox_pct.w, text: tokens[ti], fontRatio: 1.0, }) // Advance cursor: past this token + space cursor = bestX + tokenW + spaceWidthPx } if (wordPos.length > 0) { positions.set(cell.cell_id, wordPos) } } setResult(positions) } img.src = imageUrl }, [active, cells, imageUrl, rotation]) return result }