* Create a new ink annotation dictionary.
(options: InkAnnotationOptions)
| 25 | * Create a new ink annotation dictionary. |
| 26 | */ |
| 27 | static create(options: InkAnnotationOptions): PdfDict { |
| 28 | const { paths } = options; |
| 29 | const color = options.color ?? rgb(0, 0, 0); |
| 30 | const colorComponents = colorToArray(color); |
| 31 | |
| 32 | // Calculate bounding rect from all paths |
| 33 | let minX = Number.POSITIVE_INFINITY; |
| 34 | let minY = Number.POSITIVE_INFINITY; |
| 35 | let maxX = Number.NEGATIVE_INFINITY; |
| 36 | let maxY = Number.NEGATIVE_INFINITY; |
| 37 | |
| 38 | for (const path of paths) { |
| 39 | for (const point of path) { |
| 40 | minX = Math.min(minX, point.x); |
| 41 | minY = Math.min(minY, point.y); |
| 42 | maxX = Math.max(maxX, point.x); |
| 43 | maxY = Math.max(maxY, point.y); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Build InkList |
| 48 | const inkList = new PdfArray([]); |
| 49 | |
| 50 | for (const path of paths) { |
| 51 | const pathArr = new PdfArray([]); |
| 52 | |
| 53 | for (const point of path) { |
| 54 | pathArr.push(PdfNumber.of(point.x)); |
| 55 | pathArr.push(PdfNumber.of(point.y)); |
| 56 | } |
| 57 | |
| 58 | inkList.push(pathArr); |
| 59 | } |
| 60 | |
| 61 | const annotDict = PdfDict.of({ |
| 62 | Type: PdfName.of("Annot"), |
| 63 | Subtype: PdfName.of("Ink"), |
| 64 | Rect: new PdfArray([ |
| 65 | PdfNumber.of(minX), |
| 66 | PdfNumber.of(minY), |
| 67 | PdfNumber.of(maxX), |
| 68 | PdfNumber.of(maxY), |
| 69 | ]), |
| 70 | InkList: inkList, |
| 71 | C: new PdfArray(colorComponents.map(n => PdfNumber.of(n))), |
| 72 | F: PdfNumber.of(4), // Print flag |
| 73 | }); |
| 74 | |
| 75 | // Border style for stroke width |
| 76 | if (options.width !== undefined) { |
| 77 | const bs = new PdfDict(); |
| 78 | bs.set("W", PdfNumber.of(options.width)); |
| 79 | bs.set("S", PdfName.of("S")); |
| 80 | annotDict.set("BS", bs); |
| 81 | } |
| 82 | |
| 83 | if (options.contents) { |
| 84 | annotDict.set("Contents", PdfString.fromString(options.contents)); |
nothing calls this directly
no test coverage detected