@tobyt/expo-pdf-markup 0.4.0 → 0.6.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.
- package/CHANGELOG.md +22 -0
- package/README.md +12 -3
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationGeometry.kt +85 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +1 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +49 -3
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +2 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +203 -37
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +20 -1
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +52 -6
- package/build/ExpoPdfMarkup.types.d.ts +29 -1
- package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
- package/build/ExpoPdfMarkup.types.js.map +1 -1
- package/build/ExpoPdfMarkupView.d.ts +1 -1
- package/build/ExpoPdfMarkupView.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.js +15 -3
- package/build/ExpoPdfMarkupView.js.map +1 -1
- package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.web.js +131 -43
- package/build/ExpoPdfMarkupView.web.js.map +1 -1
- package/build/pdfjs.d.ts.map +1 -1
- package/build/pdfjs.js +0 -1
- package/build/pdfjs.js.map +1 -1
- package/build/web/annotationUtils.d.ts +9 -1
- package/build/web/annotationUtils.d.ts.map +1 -1
- package/build/web/annotationUtils.js +88 -5
- package/build/web/annotationUtils.js.map +1 -1
- package/ios/AnnotationModels.swift +185 -0
- package/ios/AnnotationSerializer.swift +39 -31
- package/ios/ExpoPdfMarkupModule.swift +16 -1
- package/ios/ExpoPdfMarkupView.swift +287 -74
- package/package.json +1 -1
- package/src/ExpoPdfMarkup.types.ts +37 -1
- package/src/ExpoPdfMarkupView.tsx +38 -5
- package/src/ExpoPdfMarkupView.web.tsx +170 -41
- package/src/pdfjs.ts +0 -1
- package/src/web/annotationUtils.ts +121 -4
|
@@ -5,10 +5,12 @@ import * as React from 'react';
|
|
|
5
5
|
import type {
|
|
6
6
|
Annotation,
|
|
7
7
|
AnnotationMode,
|
|
8
|
-
AnnotationPoint,
|
|
9
8
|
ExpoPdfMarkupViewProps,
|
|
9
|
+
TextAnnotation,
|
|
10
|
+
TextInputRequest,
|
|
10
11
|
} from './ExpoPdfMarkup.types';
|
|
11
12
|
import {
|
|
13
|
+
clampAnnotationTranslation,
|
|
12
14
|
canvasRectToPdfBounds,
|
|
13
15
|
canvasToPdf,
|
|
14
16
|
drawAnnotationsOnCanvas,
|
|
@@ -16,14 +18,17 @@ import {
|
|
|
16
18
|
newAnnotationId,
|
|
17
19
|
parseAnnotations,
|
|
18
20
|
serializeAnnotations,
|
|
21
|
+
translateAnnotation,
|
|
19
22
|
} from './web/annotationUtils';
|
|
20
23
|
import type { PdfPageMeta } from './web/types';
|
|
21
24
|
|
|
22
25
|
// Configure pdfjs worker once at module level (consumers can override before mounting)
|
|
23
26
|
if (!pdfjs.GlobalWorkerOptions.workerSrc) {
|
|
24
27
|
// Default: served from public/ by withPdfMarkup() in metro.js.
|
|
28
|
+
// Relative URL so the worker resolves correctly regardless of the sub-path
|
|
29
|
+
// the app is hosted at (e.g. /example/ on GitHub Pages).
|
|
25
30
|
// Override with setPdfJsWorkerSrc() if using a different bundler or CDN.
|
|
26
|
-
pdfjs.GlobalWorkerOptions.workerSrc = '
|
|
31
|
+
pdfjs.GlobalWorkerOptions.workerSrc = './pdf.worker.min.mjs';
|
|
27
32
|
}
|
|
28
33
|
|
|
29
34
|
/** Allow consumers to override the pdfjs worker URL before mounting the view. */
|
|
@@ -53,7 +58,11 @@ type PageViewProps = {
|
|
|
53
58
|
zoomLevel: number;
|
|
54
59
|
onAnnotationAdded: (annotation: Annotation) => void;
|
|
55
60
|
onAnnotationRemoved: (id: string) => void;
|
|
56
|
-
|
|
61
|
+
onAnnotationUpdated: (annotation: Annotation) => void;
|
|
62
|
+
onTextInputRequested: (
|
|
63
|
+
request: TextInputRequest,
|
|
64
|
+
annotation: TextAnnotation | null
|
|
65
|
+
) => void | Promise<void>;
|
|
57
66
|
containerRef: (el: HTMLDivElement | null) => void;
|
|
58
67
|
};
|
|
59
68
|
|
|
@@ -68,6 +77,7 @@ function PageView({
|
|
|
68
77
|
zoomLevel,
|
|
69
78
|
onAnnotationAdded,
|
|
70
79
|
onAnnotationRemoved,
|
|
80
|
+
onAnnotationUpdated,
|
|
71
81
|
onTextInputRequested,
|
|
72
82
|
containerRef,
|
|
73
83
|
}: PageViewProps) {
|
|
@@ -79,10 +89,14 @@ function PageView({
|
|
|
79
89
|
const inkPointsRef = React.useRef<{ x: number; y: number }[]>([]);
|
|
80
90
|
const dragStartRef = React.useRef<{ x: number; y: number } | null>(null);
|
|
81
91
|
const dragCurrentRef = React.useRef<{ x: number; y: number } | null>(null);
|
|
92
|
+
const moveTargetRef = React.useRef<Annotation | null>(null);
|
|
93
|
+
const moveStartPdfRef = React.useRef<{ x: number; y: number } | null>(null);
|
|
94
|
+
const movePreviewRef = React.useRef<Annotation | null>(null);
|
|
82
95
|
|
|
83
|
-
const dpr = typeof window
|
|
96
|
+
const dpr = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
|
|
84
97
|
const cssWidth = meta.canvasWidth / dpr;
|
|
85
98
|
const cssHeight = meta.canvasHeight / dpr;
|
|
99
|
+
const showEditingHighlights = annotationMode === 'move' || annotationMode === 'eraser';
|
|
86
100
|
|
|
87
101
|
// Render PDF page onto pdfCanvas
|
|
88
102
|
React.useEffect(() => {
|
|
@@ -129,7 +143,11 @@ function PageView({
|
|
|
129
143
|
const ctx = canvas.getContext('2d');
|
|
130
144
|
if (!ctx) return;
|
|
131
145
|
ctx.scale(dpr, dpr);
|
|
132
|
-
drawAnnotationsOnCanvas(ctx, annotations, pageIndex, meta
|
|
146
|
+
drawAnnotationsOnCanvas(ctx, annotations, pageIndex, meta, {
|
|
147
|
+
highlightAnnotations: showEditingHighlights,
|
|
148
|
+
previewAnnotationId: moveTargetRef.current?.id ?? null,
|
|
149
|
+
previewAnnotation: movePreviewRef.current,
|
|
150
|
+
});
|
|
133
151
|
// Live ink preview
|
|
134
152
|
if (liveInkPoints && liveInkPoints.length > 1) {
|
|
135
153
|
ctx.save();
|
|
@@ -156,12 +174,32 @@ function PageView({
|
|
|
156
174
|
ctx.restore();
|
|
157
175
|
}
|
|
158
176
|
},
|
|
159
|
-
[annotations, pageIndex, meta, dpr, annotationColor, annotationLineWidth]
|
|
177
|
+
[annotations, pageIndex, meta, dpr, annotationColor, annotationLineWidth, showEditingHighlights]
|
|
160
178
|
);
|
|
161
179
|
|
|
162
180
|
React.useEffect(() => {
|
|
163
|
-
|
|
164
|
-
|
|
181
|
+
// CSS @font-face fonts (e.g. loaded via expo-font on web) may not be available to canvas
|
|
182
|
+
// until explicitly requested via document.fonts.load(). Collect the unique font families
|
|
183
|
+
// used by text annotations on this page and load them before drawing.
|
|
184
|
+
const fontFamilies = [
|
|
185
|
+
...new Set(
|
|
186
|
+
annotations
|
|
187
|
+
.filter(
|
|
188
|
+
(a): a is TextAnnotation =>
|
|
189
|
+
a.page === pageIndex && (a.type === 'text' || a.type === 'freeText')
|
|
190
|
+
)
|
|
191
|
+
.map((a) => a.fontFamily)
|
|
192
|
+
.filter((f): f is string => Boolean(f))
|
|
193
|
+
),
|
|
194
|
+
];
|
|
195
|
+
if (fontFamilies.length > 0 && typeof document !== 'undefined') {
|
|
196
|
+
Promise.all(fontFamilies.map((f) => document.fonts.load(`16px ${f}`))).finally(() =>
|
|
197
|
+
redrawAnnotations()
|
|
198
|
+
);
|
|
199
|
+
} else {
|
|
200
|
+
redrawAnnotations();
|
|
201
|
+
}
|
|
202
|
+
}, [redrawAnnotations, annotations, pageIndex]);
|
|
165
203
|
|
|
166
204
|
function getCanvasPoint(e: React.PointerEvent<HTMLCanvasElement>): { x: number; y: number } {
|
|
167
205
|
const rect = (e.currentTarget as HTMLCanvasElement).getBoundingClientRect();
|
|
@@ -170,14 +208,26 @@ function PageView({
|
|
|
170
208
|
|
|
171
209
|
function handlePointerDown(e: React.PointerEvent<HTMLCanvasElement>) {
|
|
172
210
|
if (annotationMode === 'none') return;
|
|
173
|
-
e.currentTarget.setPointerCapture(e.pointerId);
|
|
174
211
|
const pt = getCanvasPoint(e);
|
|
175
|
-
pointerDownRef.current = true;
|
|
176
212
|
if (annotationMode === 'ink') {
|
|
213
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
214
|
+
pointerDownRef.current = true;
|
|
177
215
|
inkPointsRef.current = [pt];
|
|
178
216
|
} else if (annotationMode === 'highlight' || annotationMode === 'underline') {
|
|
217
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
218
|
+
pointerDownRef.current = true;
|
|
179
219
|
dragStartRef.current = pt;
|
|
180
220
|
dragCurrentRef.current = pt;
|
|
221
|
+
} else if (annotationMode === 'move') {
|
|
222
|
+
const pdfPoint = canvasToPdf(pt.x, pt.y, meta.scale, meta.pdfHeight);
|
|
223
|
+
const hit = hitTestAnnotation(pdfPoint, annotations, pageIndex);
|
|
224
|
+
if (!hit) return;
|
|
225
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
226
|
+
pointerDownRef.current = true;
|
|
227
|
+
moveTargetRef.current = hit;
|
|
228
|
+
moveStartPdfRef.current = pdfPoint;
|
|
229
|
+
movePreviewRef.current = hit;
|
|
230
|
+
redrawAnnotations();
|
|
181
231
|
}
|
|
182
232
|
}
|
|
183
233
|
|
|
@@ -195,6 +245,20 @@ function PageView({
|
|
|
195
245
|
const w = Math.abs(pt.x - start.x);
|
|
196
246
|
const h = Math.abs(pt.y - start.y);
|
|
197
247
|
redrawAnnotations(undefined, new DOMRect(minX, minY, w, h));
|
|
248
|
+
} else if (annotationMode === 'move') {
|
|
249
|
+
const target = moveTargetRef.current;
|
|
250
|
+
const startPdfPoint = moveStartPdfRef.current;
|
|
251
|
+
if (!target || !startPdfPoint) return;
|
|
252
|
+
const pdfPoint = canvasToPdf(pt.x, pt.y, meta.scale, meta.pdfHeight);
|
|
253
|
+
const delta = clampAnnotationTranslation(
|
|
254
|
+
target,
|
|
255
|
+
pdfPoint.x - startPdfPoint.x,
|
|
256
|
+
pdfPoint.y - startPdfPoint.y,
|
|
257
|
+
meta.pdfWidth,
|
|
258
|
+
meta.pdfHeight
|
|
259
|
+
);
|
|
260
|
+
movePreviewRef.current = translateAnnotation(target, delta.x, delta.y);
|
|
261
|
+
redrawAnnotations();
|
|
198
262
|
}
|
|
199
263
|
}
|
|
200
264
|
|
|
@@ -249,9 +313,31 @@ function PageView({
|
|
|
249
313
|
createdAt: Date.now(),
|
|
250
314
|
});
|
|
251
315
|
}
|
|
316
|
+
} else if (annotationMode === 'move') {
|
|
317
|
+
const target = moveTargetRef.current;
|
|
318
|
+
const preview = movePreviewRef.current;
|
|
319
|
+
moveTargetRef.current = null;
|
|
320
|
+
moveStartPdfRef.current = null;
|
|
321
|
+
movePreviewRef.current = null;
|
|
322
|
+
if (target && preview && preview !== target) {
|
|
323
|
+
onAnnotationUpdated(preview);
|
|
324
|
+
} else {
|
|
325
|
+
redrawAnnotations();
|
|
326
|
+
}
|
|
252
327
|
} else if (annotationMode === 'text') {
|
|
253
328
|
const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
|
|
254
|
-
|
|
329
|
+
const hit = hitTestAnnotation(pdfPoint, annotations, pageIndex);
|
|
330
|
+
const annotation =
|
|
331
|
+
hit?.type === 'text' || hit?.type === 'freeText' ? (hit as TextAnnotation) : null;
|
|
332
|
+
onTextInputRequested(
|
|
333
|
+
{
|
|
334
|
+
mode: annotation ? 'edit' : 'create',
|
|
335
|
+
page: pageIndex,
|
|
336
|
+
point: pdfPoint,
|
|
337
|
+
currentText: annotation?.contents,
|
|
338
|
+
},
|
|
339
|
+
annotation
|
|
340
|
+
);
|
|
255
341
|
} else if (annotationMode === 'eraser') {
|
|
256
342
|
const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
|
|
257
343
|
const hit = hitTestAnnotation(pdfPoint, annotations, pageIndex);
|
|
@@ -260,6 +346,8 @@ function PageView({
|
|
|
260
346
|
}
|
|
261
347
|
|
|
262
348
|
const pointerEvents = annotationMode === 'none' ? 'none' : 'auto';
|
|
349
|
+
const cursor =
|
|
350
|
+
annotationMode === 'move' ? 'grab' : annotationMode === 'eraser' ? 'not-allowed' : 'crosshair';
|
|
263
351
|
|
|
264
352
|
return (
|
|
265
353
|
<div
|
|
@@ -285,7 +373,7 @@ function PageView({
|
|
|
285
373
|
left: 0,
|
|
286
374
|
width: cssWidth,
|
|
287
375
|
height: cssHeight,
|
|
288
|
-
cursor
|
|
376
|
+
cursor,
|
|
289
377
|
pointerEvents,
|
|
290
378
|
}}
|
|
291
379
|
onPointerDown={handlePointerDown}
|
|
@@ -309,10 +397,12 @@ export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
|
|
|
309
397
|
annotationMode = 'none',
|
|
310
398
|
annotationColor = '#FF0000',
|
|
311
399
|
annotationLineWidth = 2,
|
|
400
|
+
annotationFontFamily,
|
|
312
401
|
onPageChanged,
|
|
313
402
|
onLoadComplete,
|
|
314
403
|
onError,
|
|
315
404
|
onAnnotationsChanged,
|
|
405
|
+
onTextInputRequested,
|
|
316
406
|
style,
|
|
317
407
|
} = props;
|
|
318
408
|
|
|
@@ -329,6 +419,19 @@ export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
|
|
|
329
419
|
const zoomRef = React.useRef(1);
|
|
330
420
|
|
|
331
421
|
// ResizeObserver → containerWidth
|
|
422
|
+
const applyAnnotations = React.useCallback(
|
|
423
|
+
(updater: (prev: Annotation[]) => Annotation[]) => {
|
|
424
|
+
setAnnotations((prev) => {
|
|
425
|
+
const next = updater(prev);
|
|
426
|
+
const json = serializeAnnotations({ version: 1, annotations: next });
|
|
427
|
+
lastAnnotationsJsonRef.current = json;
|
|
428
|
+
onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
|
|
429
|
+
return next;
|
|
430
|
+
});
|
|
431
|
+
},
|
|
432
|
+
[onAnnotationsChanged]
|
|
433
|
+
);
|
|
434
|
+
|
|
332
435
|
React.useEffect(() => {
|
|
333
436
|
const el = scrollContainerRef.current;
|
|
334
437
|
if (!el) return;
|
|
@@ -523,54 +626,79 @@ export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
|
|
|
523
626
|
|
|
524
627
|
const handleAnnotationAdded = React.useCallback(
|
|
525
628
|
(annotation: Annotation) => {
|
|
526
|
-
|
|
527
|
-
const next = [...prev, annotation];
|
|
528
|
-
const json = serializeAnnotations({ version: 1, annotations: next });
|
|
529
|
-
lastAnnotationsJsonRef.current = json;
|
|
530
|
-
onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
|
|
531
|
-
return next;
|
|
532
|
-
});
|
|
629
|
+
applyAnnotations((prev) => [...prev, annotation]);
|
|
533
630
|
},
|
|
534
|
-
[
|
|
631
|
+
[applyAnnotations]
|
|
535
632
|
);
|
|
536
633
|
|
|
537
634
|
const handleAnnotationRemoved = React.useCallback(
|
|
538
635
|
(id: string) => {
|
|
539
|
-
|
|
540
|
-
const next = prev.filter((a) => a.id !== id);
|
|
541
|
-
const json = serializeAnnotations({ version: 1, annotations: next });
|
|
542
|
-
lastAnnotationsJsonRef.current = json;
|
|
543
|
-
onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
|
|
544
|
-
return next;
|
|
545
|
-
});
|
|
636
|
+
applyAnnotations((prev) => prev.filter((a) => a.id !== id));
|
|
546
637
|
},
|
|
547
|
-
[
|
|
638
|
+
[applyAnnotations]
|
|
639
|
+
);
|
|
640
|
+
|
|
641
|
+
const handleAnnotationUpdated = React.useCallback(
|
|
642
|
+
(annotation: Annotation) => {
|
|
643
|
+
applyAnnotations((prev) =>
|
|
644
|
+
prev.map((existing) => (existing.id === annotation.id ? annotation : existing))
|
|
645
|
+
);
|
|
646
|
+
},
|
|
647
|
+
[applyAnnotations]
|
|
548
648
|
);
|
|
549
649
|
|
|
550
650
|
const handleTextInputRequested = React.useCallback(
|
|
551
|
-
(
|
|
552
|
-
const text =
|
|
651
|
+
async (request: TextInputRequest, annotation: TextAnnotation | null) => {
|
|
652
|
+
const text = onTextInputRequested
|
|
653
|
+
? await onTextInputRequested(request)
|
|
654
|
+
: window.prompt(
|
|
655
|
+
request.mode === 'edit' ? 'Edit annotation text:' : 'Enter annotation text:',
|
|
656
|
+
request.currentText ?? ''
|
|
657
|
+
);
|
|
553
658
|
if (!text) return;
|
|
554
|
-
const fontSize = 16;
|
|
555
|
-
const
|
|
556
|
-
const
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
659
|
+
const fontSize = annotation?.fontSize ?? 16;
|
|
660
|
+
const width = text.length * fontSize * 0.6;
|
|
661
|
+
const height = fontSize * 1.5;
|
|
662
|
+
|
|
663
|
+
if (annotation) {
|
|
664
|
+
const bounds = {
|
|
665
|
+
x: annotation.bounds.x,
|
|
666
|
+
y: annotation.bounds.y + annotation.bounds.height - height,
|
|
667
|
+
width,
|
|
668
|
+
height,
|
|
669
|
+
};
|
|
670
|
+
applyAnnotations((prev) =>
|
|
671
|
+
prev.map((existing) =>
|
|
672
|
+
existing.id === annotation.id ? { ...annotation, bounds, contents: text } : existing
|
|
673
|
+
)
|
|
674
|
+
);
|
|
675
|
+
return;
|
|
676
|
+
}
|
|
677
|
+
|
|
562
678
|
handleAnnotationAdded({
|
|
563
679
|
id: newAnnotationId(),
|
|
564
680
|
type: 'text',
|
|
565
|
-
page:
|
|
681
|
+
page: request.page,
|
|
566
682
|
color: annotationColor,
|
|
567
|
-
bounds
|
|
683
|
+
bounds: {
|
|
684
|
+
x: request.point.x,
|
|
685
|
+
y: request.point.y,
|
|
686
|
+
width,
|
|
687
|
+
height,
|
|
688
|
+
},
|
|
568
689
|
contents: text,
|
|
569
690
|
fontSize,
|
|
691
|
+
fontFamily: annotationFontFamily,
|
|
570
692
|
createdAt: Date.now(),
|
|
571
693
|
});
|
|
572
694
|
},
|
|
573
|
-
[
|
|
695
|
+
[
|
|
696
|
+
annotationColor,
|
|
697
|
+
annotationFontFamily,
|
|
698
|
+
applyAnnotations,
|
|
699
|
+
handleAnnotationAdded,
|
|
700
|
+
onTextInputRequested,
|
|
701
|
+
]
|
|
574
702
|
);
|
|
575
703
|
|
|
576
704
|
const styleObj = style as React.CSSProperties | undefined;
|
|
@@ -601,6 +729,7 @@ export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
|
|
|
601
729
|
zoomLevel={zoomLevel}
|
|
602
730
|
onAnnotationAdded={handleAnnotationAdded}
|
|
603
731
|
onAnnotationRemoved={handleAnnotationRemoved}
|
|
732
|
+
onAnnotationUpdated={handleAnnotationUpdated}
|
|
604
733
|
onTextInputRequested={handleTextInputRequested}
|
|
605
734
|
containerRef={(el) => {
|
|
606
735
|
pageContainerRefs.current[i] = el;
|
package/src/pdfjs.ts
CHANGED
|
@@ -108,6 +108,93 @@ function boundsContains(bounds: AnnotationBounds, point: AnnotationPoint): boole
|
|
|
108
108
|
);
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
export function getAnnotationOutlineBounds(annotation: Annotation): AnnotationBounds | null {
|
|
112
|
+
if (annotation.type !== 'ink') {
|
|
113
|
+
return annotation.bounds;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const paths = annotation.paths ?? [];
|
|
117
|
+
let minX = Number.POSITIVE_INFINITY;
|
|
118
|
+
let minY = Number.POSITIVE_INFINITY;
|
|
119
|
+
let maxX = Number.NEGATIVE_INFINITY;
|
|
120
|
+
let maxY = Number.NEGATIVE_INFINITY;
|
|
121
|
+
|
|
122
|
+
for (const stroke of paths) {
|
|
123
|
+
for (const point of stroke) {
|
|
124
|
+
minX = Math.min(minX, point.x);
|
|
125
|
+
minY = Math.min(minY, point.y);
|
|
126
|
+
maxX = Math.max(maxX, point.x);
|
|
127
|
+
maxY = Math.max(maxY, point.y);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (!Number.isFinite(minX) || !Number.isFinite(minY)) {
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const padding = Math.max(annotation.lineWidth ?? 2, 10);
|
|
136
|
+
return {
|
|
137
|
+
x: minX - padding,
|
|
138
|
+
y: minY - padding,
|
|
139
|
+
width: maxX - minX + padding * 2,
|
|
140
|
+
height: maxY - minY + padding * 2,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function clampAnnotationTranslation(
|
|
145
|
+
annotation: Annotation,
|
|
146
|
+
deltaX: number,
|
|
147
|
+
deltaY: number,
|
|
148
|
+
pageWidth: number,
|
|
149
|
+
pageHeight: number
|
|
150
|
+
): AnnotationPoint {
|
|
151
|
+
const bounds = getAnnotationOutlineBounds(annotation);
|
|
152
|
+
if (!bounds) {
|
|
153
|
+
return { x: deltaX, y: deltaY };
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const minDeltaX = -bounds.x;
|
|
157
|
+
const maxDeltaX = pageWidth - (bounds.x + bounds.width);
|
|
158
|
+
const minDeltaY = -bounds.y;
|
|
159
|
+
const maxDeltaY = pageHeight - (bounds.y + bounds.height);
|
|
160
|
+
|
|
161
|
+
return {
|
|
162
|
+
x: Math.min(Math.max(deltaX, minDeltaX), maxDeltaX),
|
|
163
|
+
y: Math.min(Math.max(deltaY, minDeltaY), maxDeltaY),
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function translateAnnotation(
|
|
168
|
+
annotation: Annotation,
|
|
169
|
+
deltaX: number,
|
|
170
|
+
deltaY: number
|
|
171
|
+
): Annotation {
|
|
172
|
+
if (deltaX === 0 && deltaY === 0) {
|
|
173
|
+
return annotation;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (annotation.type === 'ink') {
|
|
177
|
+
return {
|
|
178
|
+
...annotation,
|
|
179
|
+
paths: (annotation.paths ?? []).map((stroke) =>
|
|
180
|
+
stroke.map((point) => ({
|
|
181
|
+
x: point.x + deltaX,
|
|
182
|
+
y: point.y + deltaY,
|
|
183
|
+
}))
|
|
184
|
+
),
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
...annotation,
|
|
190
|
+
bounds: {
|
|
191
|
+
...annotation.bounds,
|
|
192
|
+
x: annotation.bounds.x + deltaX,
|
|
193
|
+
y: annotation.bounds.y + deltaY,
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
111
198
|
/** Return the topmost annotation hit at pdfPoint on pageIndex, or null. */
|
|
112
199
|
export function hitTestAnnotation(
|
|
113
200
|
pdfPoint: AnnotationPoint,
|
|
@@ -153,11 +240,25 @@ export function drawAnnotationsOnCanvas(
|
|
|
153
240
|
ctx: CanvasRenderingContext2D,
|
|
154
241
|
annotations: Annotation[],
|
|
155
242
|
pageIndex: number,
|
|
156
|
-
meta: PdfPageMeta
|
|
243
|
+
meta: PdfPageMeta,
|
|
244
|
+
options: {
|
|
245
|
+
highlightAnnotations?: boolean;
|
|
246
|
+
highlightColor?: string;
|
|
247
|
+
previewAnnotationId?: string | null;
|
|
248
|
+
previewAnnotation?: Annotation | null;
|
|
249
|
+
} = {}
|
|
157
250
|
): void {
|
|
158
251
|
const { scale, pdfHeight } = meta;
|
|
159
|
-
|
|
160
|
-
|
|
252
|
+
const pageAnnotations = annotations
|
|
253
|
+
.filter((annotation) => annotation.page === pageIndex)
|
|
254
|
+
.map((annotation) =>
|
|
255
|
+
options.previewAnnotationId === annotation.id && options.previewAnnotation
|
|
256
|
+
? options.previewAnnotation
|
|
257
|
+
: annotation
|
|
258
|
+
)
|
|
259
|
+
.filter((annotation) => annotation.page === pageIndex);
|
|
260
|
+
|
|
261
|
+
for (const annotation of pageAnnotations) {
|
|
161
262
|
switch (annotation.type) {
|
|
162
263
|
case 'ink': {
|
|
163
264
|
const paths = annotation.paths ?? [];
|
|
@@ -215,7 +316,7 @@ export function drawAnnotationsOnCanvas(
|
|
|
215
316
|
const fontSize = (annotation.fontSize ?? 16) * scale;
|
|
216
317
|
ctx.save();
|
|
217
318
|
ctx.fillStyle = annotation.color;
|
|
218
|
-
ctx.font = `${fontSize}px sans-serif`;
|
|
319
|
+
ctx.font = `${fontSize}px ${annotation.fontFamily ?? 'sans-serif'}`;
|
|
219
320
|
const canvasTop = (pdfHeight - bounds.y - bounds.height) * scale;
|
|
220
321
|
ctx.fillText(text, bounds.x * scale, canvasTop + fontSize);
|
|
221
322
|
ctx.restore();
|
|
@@ -223,6 +324,22 @@ export function drawAnnotationsOnCanvas(
|
|
|
223
324
|
}
|
|
224
325
|
}
|
|
225
326
|
}
|
|
327
|
+
|
|
328
|
+
if (!options.highlightAnnotations) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
for (const annotation of pageAnnotations) {
|
|
333
|
+
const bounds = getAnnotationOutlineBounds(annotation);
|
|
334
|
+
if (!bounds) continue;
|
|
335
|
+
const rect = pdfBoundsToCanvas(bounds, scale, pdfHeight);
|
|
336
|
+
ctx.save();
|
|
337
|
+
ctx.strokeStyle = options.highlightColor ?? '#0A84FF';
|
|
338
|
+
ctx.lineWidth = 1.5;
|
|
339
|
+
ctx.setLineDash([6, 4]);
|
|
340
|
+
ctx.strokeRect(rect.x, rect.y, rect.width, rect.height);
|
|
341
|
+
ctx.restore();
|
|
342
|
+
}
|
|
226
343
|
}
|
|
227
344
|
|
|
228
345
|
// ---------------------------------------------------------------------------
|