(data: Uint8Array)
| 40 | * Parse an EMF file and extract its embedded content. |
| 41 | */ |
| 42 | export function parseEmfContent(data: Uint8Array): EmfContent { |
| 43 | if (data.length < 44) return { type: 'unsupported' } |
| 44 | |
| 45 | const view = new DataView(data.buffer, data.byteOffset, data.byteLength) |
| 46 | |
| 47 | // Validate EMF signature at offset 40 |
| 48 | if (view.getUint32(40, true) !== EMF_SIGNATURE) { |
| 49 | return { type: 'unsupported' } |
| 50 | } |
| 51 | |
| 52 | let offset = 0 |
| 53 | let recordCount = 0 |
| 54 | |
| 55 | while (offset + 8 <= data.length) { |
| 56 | const recordType = view.getUint32(offset, true) |
| 57 | const recordSize = view.getUint32(offset + 4, true) |
| 58 | |
| 59 | // Sanity check record size |
| 60 | if (recordSize < 8 || offset + recordSize > data.length) break |
| 61 | |
| 62 | recordCount++ |
| 63 | |
| 64 | if (recordType === EMR_EOF) break |
| 65 | |
| 66 | // Check GDI Comment records for embedded PDF |
| 67 | if (recordType === EMR_COMMENT && recordSize > 16) { |
| 68 | const result = parseGdiComment(data, view, offset, recordSize) |
| 69 | if (result) return result |
| 70 | } |
| 71 | |
| 72 | // Check STRETCHDIBITS for embedded bitmaps |
| 73 | if (recordType === EMR_STRETCHDIBITS && recordSize > 80) { |
| 74 | const result = parseStretchDibits(data, view, offset, recordSize) |
| 75 | if (result) return result |
| 76 | } |
| 77 | |
| 78 | offset += recordSize |
| 79 | } |
| 80 | |
| 81 | // Only HEADER + EOF → empty |
| 82 | if (recordCount <= 2) { |
| 83 | return { type: 'empty' } |
| 84 | } |
| 85 | |
| 86 | return { type: 'unsupported' } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Parse a GDI Comment record looking for embedded PDF data. |
no test coverage detected