diff --git a/admin-lehrer/components/ocr-overlay/usePixelWordPositions.ts b/admin-lehrer/components/ocr-overlay/usePixelWordPositions.ts index 1141181..b7b1b27 100644 --- a/admin-lehrer/components/ocr-overlay/usePixelWordPositions.ts +++ b/admin-lehrer/components/ocr-overlay/usePixelWordPositions.ts @@ -126,10 +126,20 @@ export function usePixelWordPositions( const wordPos: WordPosition[] = [] - if (groups.length > 1 && clusters.length >= groups.length) { - // Multiple text groups with enough clusters: map each group to a cluster + // When more clusters than groups, pick the N widest clusters + // (text clusters are wider than box-border clusters) + let matchClusters = clusters + if (groups.length > 1 && clusters.length > groups.length) { + matchClusters = [...clusters] + .sort((a, b) => (b.end - b.start) - (a.end - a.start)) + .slice(0, groups.length) + .sort((a, b) => a.start - b.start) + } + + if (groups.length > 1 && matchClusters.length >= groups.length) { + // Multiple text groups with matching clusters for (let i = 0; i < groups.length; i++) { - const cl = clusters[i] + const cl = matchClusters[i] const clusterW = cl.end - cl.start + 1 const measured = ctx.measureText(groups[i]) const autoFontPx = refFontSize * (clusterW / measured.width)