@tobyt/expo-pdf-markup 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.md +85 -4
  3. package/build/ExpoPdfMarkup.types.d.ts +1 -1
  4. package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
  5. package/build/ExpoPdfMarkup.types.js.map +1 -1
  6. package/build/ExpoPdfMarkupView.web.d.ts +3 -1
  7. package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
  8. package/build/ExpoPdfMarkupView.web.js +410 -4
  9. package/build/ExpoPdfMarkupView.web.js.map +1 -1
  10. package/build/index.d.ts +1 -0
  11. package/build/index.d.ts.map +1 -1
  12. package/build/index.js +1 -0
  13. package/build/index.js.map +1 -1
  14. package/build/pdfjs.d.ts +2 -0
  15. package/build/pdfjs.d.ts.map +1 -0
  16. package/build/pdfjs.js +6 -0
  17. package/build/pdfjs.js.map +1 -0
  18. package/build/pdfjs.web.d.ts +2 -0
  19. package/build/pdfjs.web.d.ts.map +1 -0
  20. package/build/pdfjs.web.js +2 -0
  21. package/build/pdfjs.web.js.map +1 -0
  22. package/build/web/annotationUtils.d.ts +37 -0
  23. package/build/web/annotationUtils.d.ts.map +1 -0
  24. package/build/web/annotationUtils.js +213 -0
  25. package/build/web/annotationUtils.js.map +1 -0
  26. package/build/web/types.d.ts +13 -0
  27. package/build/web/types.d.ts.map +1 -0
  28. package/build/web/types.js +2 -0
  29. package/build/web/types.js.map +1 -0
  30. package/ios/ExpoPdfMarkup.podspec +1 -1
  31. package/metro.d.ts +5 -0
  32. package/metro.js +90 -0
  33. package/package.json +8 -1
  34. package/src/ExpoPdfMarkup.types.ts +1 -1
  35. package/src/ExpoPdfMarkupView.web.tsx +512 -5
  36. package/src/index.ts +1 -0
  37. package/src/pdfjs.ts +5 -0
  38. package/src/pdfjs.web.ts +1 -0
  39. package/src/web/annotationUtils.ts +257 -0
  40. package/src/web/types.ts +12 -0
@@ -0,0 +1,257 @@
1
+ import type {
2
+ Annotation,
3
+ AnnotationBounds,
4
+ AnnotationPoint,
5
+ AnnotationsData,
6
+ } from '../ExpoPdfMarkup.types';
7
+ import type { PdfPageMeta } from './types';
8
+
9
+ // ---------------------------------------------------------------------------
10
+ // Coordinate transforms
11
+ // ---------------------------------------------------------------------------
12
+
13
+ /** Convert canvas pixel coords → PDF user-space coords (y-up) */
14
+ export function canvasToPdf(
15
+ canvasX: number,
16
+ canvasY: number,
17
+ scale: number,
18
+ pageHeight: number
19
+ ): AnnotationPoint {
20
+ return {
21
+ x: canvasX / scale,
22
+ y: pageHeight - canvasY / scale,
23
+ };
24
+ }
25
+
26
+ /** Convert PDF user-space coords (y-up) → canvas pixel coords */
27
+ export function pdfToCanvas(
28
+ pdfX: number,
29
+ pdfY: number,
30
+ scale: number,
31
+ pageHeight: number
32
+ ): { x: number; y: number } {
33
+ return {
34
+ x: pdfX * scale,
35
+ y: (pageHeight - pdfY) * scale,
36
+ };
37
+ }
38
+
39
+ /**
40
+ * Convert a PDF bounds rect (y-up, y = bottom edge) → canvas rect (y-down, y = top edge).
41
+ * All values are in CSS pixels (not physical pixels).
42
+ */
43
+ export function pdfBoundsToCanvas(
44
+ bounds: AnnotationBounds,
45
+ scale: number,
46
+ pageHeight: number
47
+ ): { x: number; y: number; width: number; height: number } {
48
+ return {
49
+ x: bounds.x * scale,
50
+ y: (pageHeight - bounds.y - bounds.height) * scale,
51
+ width: bounds.width * scale,
52
+ height: bounds.height * scale,
53
+ };
54
+ }
55
+
56
+ /**
57
+ * Convert a canvas drag rect (minX/Y, maxX/Y in CSS pixels) → PDF bounds (y-up).
58
+ * `bounds.y` is the bottom edge in PDF space, consistent with iOS/Android.
59
+ */
60
+ export function canvasRectToPdfBounds(
61
+ minX: number,
62
+ minY: number,
63
+ maxX: number,
64
+ maxY: number,
65
+ scale: number,
66
+ pageHeight: number
67
+ ): AnnotationBounds {
68
+ return {
69
+ x: minX / scale,
70
+ y: pageHeight - maxY / scale,
71
+ width: (maxX - minX) / scale,
72
+ height: (maxY - minY) / scale,
73
+ };
74
+ }
75
+
76
+ // ---------------------------------------------------------------------------
77
+ // Hit testing (ported from AnnotationHitTester.kt)
78
+ // ---------------------------------------------------------------------------
79
+
80
+ export function distanceToSegment(
81
+ point: AnnotationPoint,
82
+ a: AnnotationPoint,
83
+ b: AnnotationPoint
84
+ ): number {
85
+ const dx = b.x - a.x;
86
+ const dy = b.y - a.y;
87
+ const lenSq = dx * dx + dy * dy;
88
+ if (lenSq === 0) {
89
+ const px = point.x - a.x;
90
+ const py = point.y - a.y;
91
+ return Math.sqrt(px * px + py * py);
92
+ }
93
+ let t = ((point.x - a.x) * dx + (point.y - a.y) * dy) / lenSq;
94
+ t = Math.max(0, Math.min(1, t));
95
+ const projX = a.x + t * dx;
96
+ const projY = a.y + t * dy;
97
+ const ex = point.x - projX;
98
+ const ey = point.y - projY;
99
+ return Math.sqrt(ex * ex + ey * ey);
100
+ }
101
+
102
+ function boundsContains(bounds: AnnotationBounds, point: AnnotationPoint): boolean {
103
+ return (
104
+ point.x >= bounds.x &&
105
+ point.x <= bounds.x + bounds.width &&
106
+ point.y >= bounds.y &&
107
+ point.y <= bounds.y + bounds.height
108
+ );
109
+ }
110
+
111
+ /** Return the topmost annotation hit at pdfPoint on pageIndex, or null. */
112
+ export function hitTestAnnotation(
113
+ pdfPoint: AnnotationPoint,
114
+ annotations: Annotation[],
115
+ pageIndex: number
116
+ ): Annotation | null {
117
+ const onPage = annotations.filter((a) => a.page === pageIndex);
118
+ for (let i = onPage.length - 1; i >= 0; i--) {
119
+ const annotation = onPage[i];
120
+ if (annotation.type === 'ink') {
121
+ const tolerance = Math.max(annotation.lineWidth ?? 2, 10);
122
+ const paths = annotation.paths ?? [];
123
+ for (const stroke of paths) {
124
+ if (stroke.length === 1) {
125
+ const dx = pdfPoint.x - stroke[0].x;
126
+ const dy = pdfPoint.y - stroke[0].y;
127
+ if (Math.sqrt(dx * dx + dy * dy) <= tolerance) return annotation;
128
+ }
129
+ for (let j = 0; j < stroke.length - 1; j++) {
130
+ if (distanceToSegment(pdfPoint, stroke[j], stroke[j + 1]) <= tolerance) {
131
+ return annotation;
132
+ }
133
+ }
134
+ }
135
+ } else {
136
+ const bounds = annotation.bounds;
137
+ if (bounds && boundsContains(bounds, pdfPoint)) return annotation;
138
+ }
139
+ }
140
+ return null;
141
+ }
142
+
143
+ // ---------------------------------------------------------------------------
144
+ // Canvas drawing (mirrors AnnotationRenderer.kt)
145
+ // ---------------------------------------------------------------------------
146
+
147
+ /**
148
+ * Draw all annotations for pageIndex onto ctx.
149
+ * meta.scale is CSS-pixels-per-PDF-point; canvas backing = CSS * devicePixelRatio,
150
+ * but the caller should have called ctx.scale(dpr, dpr) so we work in CSS pixels here.
151
+ */
152
+ export function drawAnnotationsOnCanvas(
153
+ ctx: CanvasRenderingContext2D,
154
+ annotations: Annotation[],
155
+ pageIndex: number,
156
+ meta: PdfPageMeta
157
+ ): void {
158
+ const { scale, pdfHeight } = meta;
159
+ for (const annotation of annotations) {
160
+ if (annotation.page !== pageIndex) continue;
161
+ switch (annotation.type) {
162
+ case 'ink': {
163
+ const paths = annotation.paths ?? [];
164
+ ctx.save();
165
+ ctx.strokeStyle = annotation.color;
166
+ ctx.lineWidth = (annotation.lineWidth ?? 2) * scale;
167
+ ctx.lineCap = 'round';
168
+ ctx.lineJoin = 'round';
169
+ for (const stroke of paths) {
170
+ if (stroke.length === 0) continue;
171
+ ctx.beginPath();
172
+ for (let i = 0; i < stroke.length; i++) {
173
+ const { x, y } = pdfToCanvas(stroke[i].x, stroke[i].y, scale, pdfHeight);
174
+ if (i === 0) ctx.moveTo(x, y);
175
+ else ctx.lineTo(x, y);
176
+ }
177
+ ctx.stroke();
178
+ }
179
+ ctx.restore();
180
+ break;
181
+ }
182
+ case 'highlight': {
183
+ const bounds = annotation.bounds;
184
+ if (!bounds) break;
185
+ const rect = pdfBoundsToCanvas(bounds, scale, pdfHeight);
186
+ ctx.save();
187
+ ctx.globalAlpha = annotation.alpha ?? 0.5;
188
+ ctx.fillStyle = annotation.color;
189
+ ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
190
+ ctx.restore();
191
+ break;
192
+ }
193
+ case 'underline': {
194
+ const bounds = annotation.bounds;
195
+ if (!bounds) break;
196
+ // Underline sits at the bottom of bounds (PDF y = bounds.y = bottom edge)
197
+ const left = bounds.x * scale;
198
+ const bottom = (pdfHeight - bounds.y) * scale;
199
+ const right = left + bounds.width * scale;
200
+ ctx.save();
201
+ ctx.strokeStyle = annotation.color;
202
+ ctx.lineWidth = scale;
203
+ ctx.beginPath();
204
+ ctx.moveTo(left, bottom);
205
+ ctx.lineTo(right, bottom);
206
+ ctx.stroke();
207
+ ctx.restore();
208
+ break;
209
+ }
210
+ case 'text':
211
+ case 'freeText': {
212
+ const bounds = annotation.bounds;
213
+ const text = annotation.contents;
214
+ if (!bounds || !text) break;
215
+ const fontSize = (annotation.fontSize ?? 16) * scale;
216
+ ctx.save();
217
+ ctx.fillStyle = annotation.color;
218
+ ctx.font = `${fontSize}px sans-serif`;
219
+ const canvasTop = (pdfHeight - bounds.y - bounds.height) * scale;
220
+ ctx.fillText(text, bounds.x * scale, canvasTop + fontSize);
221
+ ctx.restore();
222
+ break;
223
+ }
224
+ }
225
+ }
226
+ }
227
+
228
+ // ---------------------------------------------------------------------------
229
+ // Serialization
230
+ // ---------------------------------------------------------------------------
231
+
232
+ export function parseAnnotations(json?: string): AnnotationsData {
233
+ if (!json) return { version: 1, annotations: [] };
234
+ try {
235
+ const data = JSON.parse(json) as AnnotationsData;
236
+ if (!Array.isArray(data?.annotations)) return { version: 1, annotations: [] };
237
+ // Deduplicate by id — last wins
238
+ const seen = new Map<string, Annotation>();
239
+ for (const a of data.annotations) {
240
+ seen.set(a.id, a);
241
+ }
242
+ return { version: 1, annotations: Array.from(seen.values()) };
243
+ } catch {
244
+ return { version: 1, annotations: [] };
245
+ }
246
+ }
247
+
248
+ export function serializeAnnotations(data: AnnotationsData): string {
249
+ return JSON.stringify(data);
250
+ }
251
+
252
+ export function newAnnotationId(): string {
253
+ if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {
254
+ return crypto.randomUUID();
255
+ }
256
+ return Math.random().toString(36).slice(2) + Date.now().toString(36);
257
+ }
@@ -0,0 +1,12 @@
1
+ export type PdfPageMeta = {
2
+ /** PDF page width in PDF user-space points */
3
+ pdfWidth: number;
4
+ /** PDF page height in PDF user-space points */
5
+ pdfHeight: number;
6
+ /** Pixels per PDF point (containerWidth / pdfWidth) */
7
+ scale: number;
8
+ /** Canvas backing width in physical pixels (CSS width × devicePixelRatio) */
9
+ canvasWidth: number;
10
+ /** Canvas backing height in physical pixels (CSS height × devicePixelRatio) */
11
+ canvasHeight: number;
12
+ };