|
| 1 | +import { type DetectedField, type FieldType } from "../workers/inference.worker"; |
| 2 | + |
| 3 | +export interface FieldColors { |
| 4 | + background: string; |
| 5 | + label: string; |
| 6 | +} |
| 7 | + |
| 8 | +export const FIELD_COLORS: Record<FieldType, FieldColors> = { |
| 9 | + ChoiceButton: { |
| 10 | + background: "#a4dcf891", |
| 11 | + label: "#10B981", |
| 12 | + }, |
| 13 | + Signature: { |
| 14 | + background: "#a4dcf891", |
| 15 | + label: "#F59E0B", |
| 16 | + }, |
| 17 | + TextBox: { |
| 18 | + background: "#a4dcf891", |
| 19 | + label: "#3B82F6", |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +const LABEL_BAR_HEIGHT = 12.5; |
| 24 | +const LABEL_FONT = "10px Arial"; |
| 25 | +const LABEL_TEXT_COLOR = "white"; |
| 26 | +const LABEL_PADDING_X = 3; |
| 27 | +const LABEL_PADDING_Y = 3; |
| 28 | + |
| 29 | +const drawWidgets = (canvas: HTMLCanvasElement, fields: DetectedField[]): void => { |
| 30 | + const context = canvas.getContext("2d"); |
| 31 | + if (!context) { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + fields.forEach((field) => { |
| 36 | + const [normalizedX, normalizedY, normalizedWidth, normalizedHeight] = field.bbox; |
| 37 | + const absoluteX = normalizedX * canvas.width; |
| 38 | + const absoluteY = normalizedY * canvas.height; |
| 39 | + const absoluteWidth = normalizedWidth * canvas.width; |
| 40 | + const absoluteHeight = normalizedHeight * canvas.height; |
| 41 | + |
| 42 | + const fieldColors = FIELD_COLORS[field.type]; |
| 43 | + |
| 44 | + context.fillStyle = fieldColors.background; |
| 45 | + context.fillRect(absoluteX, absoluteY, absoluteWidth, absoluteHeight); |
| 46 | + |
| 47 | + context.fillStyle = fieldColors.label; |
| 48 | + context.fillRect(absoluteX, absoluteY - LABEL_BAR_HEIGHT, absoluteWidth, LABEL_BAR_HEIGHT); |
| 49 | + |
| 50 | + context.fillStyle = LABEL_TEXT_COLOR; |
| 51 | + context.font = LABEL_FONT; |
| 52 | + const confidencePercentage = (field.confidence * 100).toFixed(0); |
| 53 | + context.fillText( |
| 54 | + `${field.type} (${confidencePercentage}%)`, |
| 55 | + absoluteX + LABEL_PADDING_X, |
| 56 | + absoluteY - LABEL_PADDING_Y |
| 57 | + ); |
| 58 | + }); |
| 59 | +}; |
| 60 | + |
| 61 | +interface PageDetectionDataInput { |
| 62 | + fields: DetectedField[]; |
| 63 | + imageData: ImageData; |
| 64 | + pdfMetadata: { |
| 65 | + originalWidth: number; |
| 66 | + originalHeight: number; |
| 67 | + canvasSize: number; |
| 68 | + offsetX: number; |
| 69 | + offsetY: number; |
| 70 | + }; |
| 71 | +} |
| 72 | + |
| 73 | +interface PageDetectionDataOutput { |
| 74 | + fields: DetectedField[]; |
| 75 | + imageData: string; |
| 76 | + pdfMetadata: { |
| 77 | + originalWidth: number; |
| 78 | + originalHeight: number; |
| 79 | + canvasSize: number; |
| 80 | + offsetX: number; |
| 81 | + offsetY: number; |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +interface DetectionDataInput { |
| 86 | + pages: PageDetectionDataInput[]; |
| 87 | + processingTime: number; |
| 88 | + modelInfo: string; |
| 89 | +} |
| 90 | + |
| 91 | +interface DetectionDataOutput { |
| 92 | + pages: PageDetectionDataOutput[]; |
| 93 | + processingTime: number; |
| 94 | + modelInfo: string; |
| 95 | +} |
| 96 | + |
| 97 | +export const drawDetections = (detectionData: DetectionDataInput): DetectionDataOutput => { |
| 98 | + const pagesWithDrawings = detectionData.pages.map((page) => { |
| 99 | + const canvas = document.createElement("canvas"); |
| 100 | + canvas.width = page.imageData.width; |
| 101 | + canvas.height = page.imageData.height; |
| 102 | + const ctx = canvas.getContext("2d")!; |
| 103 | + ctx.putImageData(page.imageData, 0, 0); |
| 104 | + |
| 105 | + drawWidgets(canvas, page.fields); |
| 106 | + |
| 107 | + return { |
| 108 | + ...page, |
| 109 | + imageData: canvas.toDataURL(), |
| 110 | + }; |
| 111 | + }); |
| 112 | + |
| 113 | + return { |
| 114 | + ...detectionData, |
| 115 | + pages: pagesWithDrawings, |
| 116 | + }; |
| 117 | +}; |
0 commit comments