import type { Annotation } from '@/ui/types' import React from 'react' export function parseAnnotations(raw: unknown): Annotation[] { if (!Array.isArray(raw)) return [] const valid: Annotation[] = [] for (const item of raw) { if (!item || typeof item !== 'object') continue const rec = item as Record const type = rec.type const color = rec.color const points = rec.points if ( (type === 'box' || type === 'line' || type === 'arrow' || type === 'freehand') && typeof color === 'string' && Array.isArray(points) && points.length >= 2 && points.every( (pt) => Array.isArray(pt) && pt.length >= 2 && typeof pt[0] === 'number' && typeof pt[1] === 'number', ) ) { valid.push({ type, color, points: points as [number, number][], } as Annotation) } } return valid } interface AnnotationOverlaySvgProps { annotations: Annotation[] width: number height: number className?: string } function buildFreehandPath(points: [number, number][], w: number, h: number): string { if (points.length === 0) return '' const scaled = points.map(([nx, ny]) => [nx * w, ny * h] as const) if (scaled.length === 2) { return `M ${scaled[0][0]} ${scaled[0][1]} L ${scaled[1][0]} ${scaled[1][1]}` } // Smooth quadratic path matching Konva's tension ~0.5 appearance let d = `M ${scaled[0][0]} ${scaled[0][1]}` for (let i = 1; i < scaled.length - 1; i++) { const midX = (scaled[i][0] + scaled[i + 1][0]) / 2 const midY = (scaled[i][1] + scaled[i + 1][1]) / 2 d += ` Q ${scaled[i][0]} ${scaled[i][1]} ${midX} ${midY}` } const last = scaled[scaled.length - 1] d += ` L ${last[0]} ${last[1]}` return d } /** * Vector SVG overlay for normalized [0..1] comment annotations. * Using inline SVG instead of rasterized canvas ensures sharp markups when * printing or exporting to PDF and avoids browser canvas limits on long lists. */ export const AnnotationOverlaySvg: React.FC = ({ annotations, width, height, className, }) => { if (annotations.length === 0 || width <= 0 || height <= 0) return null // Keep stroke visually consistent with DrawingCanvas regardless of image dimensions const baseDim = Math.max(width, height) const strokeWidth = Math.max(1.5, baseDim * 0.014) const headLength = strokeWidth * 3.8 const headHalfWidth = strokeWidth * 2.1 return ( ) }