@tobyt/expo-pdf-markup 0.5.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 +14 -0
- package/README.md +7 -0
- 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 +4 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +36 -8
- package/build/ExpoPdfMarkup.types.d.ts +25 -3
- package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
- package/build/ExpoPdfMarkup.types.js.map +1 -1
- package/build/ExpoPdfMarkupView.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.js +2 -2
- package/build/ExpoPdfMarkupView.js.map +1 -1
- package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.web.js +127 -43
- package/build/ExpoPdfMarkupView.web.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 +4 -0
- package/ios/ExpoPdfMarkupView.swift +279 -86
- package/package.json +1 -1
- package/src/ExpoPdfMarkup.types.ts +33 -3
- package/src/ExpoPdfMarkupView.tsx +12 -9
- package/src/ExpoPdfMarkupView.web.tsx +165 -41
- 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,6 +18,7 @@ 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
|
|
|
@@ -55,7 +58,11 @@ type PageViewProps = {
|
|
|
55
58
|
zoomLevel: number;
|
|
56
59
|
onAnnotationAdded: (annotation: Annotation) => void;
|
|
57
60
|
onAnnotationRemoved: (id: string) => void;
|
|
58
|
-
|
|
61
|
+
onAnnotationUpdated: (annotation: Annotation) => void;
|
|
62
|
+
onTextInputRequested: (
|
|
63
|
+
request: TextInputRequest,
|
|
64
|
+
annotation: TextAnnotation | null
|
|
65
|
+
) => void | Promise<void>;
|
|
59
66
|
containerRef: (el: HTMLDivElement | null) => void;
|
|
60
67
|
};
|
|
61
68
|
|
|
@@ -70,6 +77,7 @@ function PageView({
|
|
|
70
77
|
zoomLevel,
|
|
71
78
|
onAnnotationAdded,
|
|
72
79
|
onAnnotationRemoved,
|
|
80
|
+
onAnnotationUpdated,
|
|
73
81
|
onTextInputRequested,
|
|
74
82
|
containerRef,
|
|
75
83
|
}: PageViewProps) {
|
|
@@ -81,10 +89,14 @@ function PageView({
|
|
|
81
89
|
const inkPointsRef = React.useRef<{ x: number; y: number }[]>([]);
|
|
82
90
|
const dragStartRef = React.useRef<{ x: number; y: number } | null>(null);
|
|
83
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);
|
|
84
95
|
|
|
85
|
-
const dpr = typeof window
|
|
96
|
+
const dpr = typeof window === 'undefined' ? 1 : window.devicePixelRatio || 1;
|
|
86
97
|
const cssWidth = meta.canvasWidth / dpr;
|
|
87
98
|
const cssHeight = meta.canvasHeight / dpr;
|
|
99
|
+
const showEditingHighlights = annotationMode === 'move' || annotationMode === 'eraser';
|
|
88
100
|
|
|
89
101
|
// Render PDF page onto pdfCanvas
|
|
90
102
|
React.useEffect(() => {
|
|
@@ -131,7 +143,11 @@ function PageView({
|
|
|
131
143
|
const ctx = canvas.getContext('2d');
|
|
132
144
|
if (!ctx) return;
|
|
133
145
|
ctx.scale(dpr, dpr);
|
|
134
|
-
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
|
+
});
|
|
135
151
|
// Live ink preview
|
|
136
152
|
if (liveInkPoints && liveInkPoints.length > 1) {
|
|
137
153
|
ctx.save();
|
|
@@ -158,12 +174,32 @@ function PageView({
|
|
|
158
174
|
ctx.restore();
|
|
159
175
|
}
|
|
160
176
|
},
|
|
161
|
-
[annotations, pageIndex, meta, dpr, annotationColor, annotationLineWidth]
|
|
177
|
+
[annotations, pageIndex, meta, dpr, annotationColor, annotationLineWidth, showEditingHighlights]
|
|
162
178
|
);
|
|
163
179
|
|
|
164
180
|
React.useEffect(() => {
|
|
165
|
-
|
|
166
|
-
|
|
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]);
|
|
167
203
|
|
|
168
204
|
function getCanvasPoint(e: React.PointerEvent<HTMLCanvasElement>): { x: number; y: number } {
|
|
169
205
|
const rect = (e.currentTarget as HTMLCanvasElement).getBoundingClientRect();
|
|
@@ -172,14 +208,26 @@ function PageView({
|
|
|
172
208
|
|
|
173
209
|
function handlePointerDown(e: React.PointerEvent<HTMLCanvasElement>) {
|
|
174
210
|
if (annotationMode === 'none') return;
|
|
175
|
-
e.currentTarget.setPointerCapture(e.pointerId);
|
|
176
211
|
const pt = getCanvasPoint(e);
|
|
177
|
-
pointerDownRef.current = true;
|
|
178
212
|
if (annotationMode === 'ink') {
|
|
213
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
214
|
+
pointerDownRef.current = true;
|
|
179
215
|
inkPointsRef.current = [pt];
|
|
180
216
|
} else if (annotationMode === 'highlight' || annotationMode === 'underline') {
|
|
217
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
218
|
+
pointerDownRef.current = true;
|
|
181
219
|
dragStartRef.current = pt;
|
|
182
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();
|
|
183
231
|
}
|
|
184
232
|
}
|
|
185
233
|
|
|
@@ -197,6 +245,20 @@ function PageView({
|
|
|
197
245
|
const w = Math.abs(pt.x - start.x);
|
|
198
246
|
const h = Math.abs(pt.y - start.y);
|
|
199
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();
|
|
200
262
|
}
|
|
201
263
|
}
|
|
202
264
|
|
|
@@ -251,9 +313,31 @@ function PageView({
|
|
|
251
313
|
createdAt: Date.now(),
|
|
252
314
|
});
|
|
253
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
|
+
}
|
|
254
327
|
} else if (annotationMode === 'text') {
|
|
255
328
|
const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
|
|
256
|
-
|
|
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
|
+
);
|
|
257
341
|
} else if (annotationMode === 'eraser') {
|
|
258
342
|
const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
|
|
259
343
|
const hit = hitTestAnnotation(pdfPoint, annotations, pageIndex);
|
|
@@ -262,6 +346,8 @@ function PageView({
|
|
|
262
346
|
}
|
|
263
347
|
|
|
264
348
|
const pointerEvents = annotationMode === 'none' ? 'none' : 'auto';
|
|
349
|
+
const cursor =
|
|
350
|
+
annotationMode === 'move' ? 'grab' : annotationMode === 'eraser' ? 'not-allowed' : 'crosshair';
|
|
265
351
|
|
|
266
352
|
return (
|
|
267
353
|
<div
|
|
@@ -287,7 +373,7 @@ function PageView({
|
|
|
287
373
|
left: 0,
|
|
288
374
|
width: cssWidth,
|
|
289
375
|
height: cssHeight,
|
|
290
|
-
cursor
|
|
376
|
+
cursor,
|
|
291
377
|
pointerEvents,
|
|
292
378
|
}}
|
|
293
379
|
onPointerDown={handlePointerDown}
|
|
@@ -311,6 +397,7 @@ export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
|
|
|
311
397
|
annotationMode = 'none',
|
|
312
398
|
annotationColor = '#FF0000',
|
|
313
399
|
annotationLineWidth = 2,
|
|
400
|
+
annotationFontFamily,
|
|
314
401
|
onPageChanged,
|
|
315
402
|
onLoadComplete,
|
|
316
403
|
onError,
|
|
@@ -332,6 +419,19 @@ export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
|
|
|
332
419
|
const zoomRef = React.useRef(1);
|
|
333
420
|
|
|
334
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
|
+
|
|
335
435
|
React.useEffect(() => {
|
|
336
436
|
const el = scrollContainerRef.current;
|
|
337
437
|
if (!el) return;
|
|
@@ -526,56 +626,79 @@ export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
|
|
|
526
626
|
|
|
527
627
|
const handleAnnotationAdded = React.useCallback(
|
|
528
628
|
(annotation: Annotation) => {
|
|
529
|
-
|
|
530
|
-
const next = [...prev, annotation];
|
|
531
|
-
const json = serializeAnnotations({ version: 1, annotations: next });
|
|
532
|
-
lastAnnotationsJsonRef.current = json;
|
|
533
|
-
onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
|
|
534
|
-
return next;
|
|
535
|
-
});
|
|
629
|
+
applyAnnotations((prev) => [...prev, annotation]);
|
|
536
630
|
},
|
|
537
|
-
[
|
|
631
|
+
[applyAnnotations]
|
|
538
632
|
);
|
|
539
633
|
|
|
540
634
|
const handleAnnotationRemoved = React.useCallback(
|
|
541
635
|
(id: string) => {
|
|
542
|
-
|
|
543
|
-
const next = prev.filter((a) => a.id !== id);
|
|
544
|
-
const json = serializeAnnotations({ version: 1, annotations: next });
|
|
545
|
-
lastAnnotationsJsonRef.current = json;
|
|
546
|
-
onAnnotationsChanged?.({ nativeEvent: { annotations: json } });
|
|
547
|
-
return next;
|
|
548
|
-
});
|
|
636
|
+
applyAnnotations((prev) => prev.filter((a) => a.id !== id));
|
|
549
637
|
},
|
|
550
|
-
[
|
|
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]
|
|
551
648
|
);
|
|
552
649
|
|
|
553
650
|
const handleTextInputRequested = React.useCallback(
|
|
554
|
-
async (
|
|
651
|
+
async (request: TextInputRequest, annotation: TextAnnotation | null) => {
|
|
555
652
|
const text = onTextInputRequested
|
|
556
|
-
? await onTextInputRequested()
|
|
557
|
-
: window.prompt(
|
|
653
|
+
? await onTextInputRequested(request)
|
|
654
|
+
: window.prompt(
|
|
655
|
+
request.mode === 'edit' ? 'Edit annotation text:' : 'Enter annotation text:',
|
|
656
|
+
request.currentText ?? ''
|
|
657
|
+
);
|
|
558
658
|
if (!text) return;
|
|
559
|
-
const fontSize = 16;
|
|
560
|
-
const
|
|
561
|
-
const
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
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
|
+
|
|
567
678
|
handleAnnotationAdded({
|
|
568
679
|
id: newAnnotationId(),
|
|
569
680
|
type: 'text',
|
|
570
|
-
page:
|
|
681
|
+
page: request.page,
|
|
571
682
|
color: annotationColor,
|
|
572
|
-
bounds
|
|
683
|
+
bounds: {
|
|
684
|
+
x: request.point.x,
|
|
685
|
+
y: request.point.y,
|
|
686
|
+
width,
|
|
687
|
+
height,
|
|
688
|
+
},
|
|
573
689
|
contents: text,
|
|
574
690
|
fontSize,
|
|
691
|
+
fontFamily: annotationFontFamily,
|
|
575
692
|
createdAt: Date.now(),
|
|
576
693
|
});
|
|
577
694
|
},
|
|
578
|
-
[
|
|
695
|
+
[
|
|
696
|
+
annotationColor,
|
|
697
|
+
annotationFontFamily,
|
|
698
|
+
applyAnnotations,
|
|
699
|
+
handleAnnotationAdded,
|
|
700
|
+
onTextInputRequested,
|
|
701
|
+
]
|
|
579
702
|
);
|
|
580
703
|
|
|
581
704
|
const styleObj = style as React.CSSProperties | undefined;
|
|
@@ -606,6 +729,7 @@ export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
|
|
|
606
729
|
zoomLevel={zoomLevel}
|
|
607
730
|
onAnnotationAdded={handleAnnotationAdded}
|
|
608
731
|
onAnnotationRemoved={handleAnnotationRemoved}
|
|
732
|
+
onAnnotationUpdated={handleAnnotationUpdated}
|
|
609
733
|
onTextInputRequested={handleTextInputRequested}
|
|
610
734
|
containerRef={(el) => {
|
|
611
735
|
pageContainerRefs.current[i] = el;
|
|
@@ -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
|
// ---------------------------------------------------------------------------
|