|
| 1 | +import { Matrix, Rotation, Rect, Size } from '@embedpdf/models'; |
1 | 2 | import { PdfiumRuntimeMethods, PdfiumModule } from '@embedpdf/pdfium'; |
2 | 3 |
|
3 | 4 | /** |
@@ -92,3 +93,65 @@ export function isValidCustomKey(key: string): boolean { |
92 | 93 | } |
93 | 94 | return true; |
94 | 95 | } |
| 96 | + |
| 97 | +interface FormDrawParams { |
| 98 | + startX: number; |
| 99 | + startY: number; |
| 100 | + formsWidth: number; |
| 101 | + formsHeight: number; |
| 102 | + scaleX: number; |
| 103 | + scaleY: number; |
| 104 | +} |
| 105 | + |
| 106 | +export function computeFormDrawParams( |
| 107 | + matrix: Matrix, |
| 108 | + rect: Rect, |
| 109 | + pageSize: Size, |
| 110 | + rotation: Rotation, |
| 111 | +): FormDrawParams { |
| 112 | + const rectLeft = rect.origin.x; |
| 113 | + const rectBottom = rect.origin.y; |
| 114 | + const rectRight = rectLeft + rect.size.width; |
| 115 | + const rectTop = rectBottom + rect.size.height; |
| 116 | + const pageWidth = pageSize.width; |
| 117 | + const pageHeight = pageSize.height; |
| 118 | + |
| 119 | + // Extract the per-axis scale that the render matrix applies. |
| 120 | + const scaleX = Math.hypot(matrix.a, matrix.b); |
| 121 | + const scaleY = Math.hypot(matrix.c, matrix.d); |
| 122 | + const swap = (rotation & 1) === 1; |
| 123 | + |
| 124 | + const formsWidth = swap |
| 125 | + ? Math.max(1, Math.round(pageHeight * scaleX)) |
| 126 | + : Math.max(1, Math.round(pageWidth * scaleX)); |
| 127 | + const formsHeight = swap |
| 128 | + ? Math.max(1, Math.round(pageWidth * scaleY)) |
| 129 | + : Math.max(1, Math.round(pageHeight * scaleY)); |
| 130 | + |
| 131 | + let startX: number; |
| 132 | + let startY: number; |
| 133 | + switch (rotation) { |
| 134 | + case Rotation.Degree0: |
| 135 | + startX = -Math.round(rectLeft * scaleX); |
| 136 | + startY = -Math.round(rectBottom * scaleY); |
| 137 | + break; |
| 138 | + case Rotation.Degree90: |
| 139 | + startX = Math.round((rectTop - pageHeight) * scaleX); |
| 140 | + startY = -Math.round(rectLeft * scaleY); |
| 141 | + break; |
| 142 | + case Rotation.Degree180: |
| 143 | + startX = Math.round((rectRight - pageWidth) * scaleX); |
| 144 | + startY = Math.round((rectTop - pageHeight) * scaleY); |
| 145 | + break; |
| 146 | + case Rotation.Degree270: |
| 147 | + startX = -Math.round(rectBottom * scaleX); |
| 148 | + startY = Math.round((rectRight - pageWidth) * scaleY); |
| 149 | + break; |
| 150 | + default: |
| 151 | + startX = -Math.round(rectLeft * scaleX); |
| 152 | + startY = -Math.round(rectBottom * scaleY); |
| 153 | + break; |
| 154 | + } |
| 155 | + |
| 156 | + return { startX, startY, formsWidth, formsHeight, scaleX, scaleY }; |
| 157 | +} |
0 commit comments