@tobyt/expo-pdf-markup 1.0.0 → 1.1.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 +21 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationGeometry.kt +2 -2
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationHitTester.kt +1 -1
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +2 -1
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +36 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +3 -1
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +34 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +8 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +8 -0
- package/build/ExpoPdfMarkup.types.d.ts +27 -2
- package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
- package/build/ExpoPdfMarkup.types.js.map +1 -1
- package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
- package/build/ExpoPdfMarkupView.web.js +34 -5
- package/build/ExpoPdfMarkupView.web.js.map +1 -1
- package/build/web/annotationUtils.d.ts.map +1 -1
- package/build/web/annotationUtils.js +14 -0
- package/build/web/annotationUtils.js.map +1 -1
- package/ios/AnnotationModels.swift +1 -0
- package/ios/AnnotationSerializer.swift +20 -0
- package/ios/ExpoPdfMarkupModule.swift +8 -0
- package/ios/ExpoPdfMarkupView+Ink.swift +183 -0
- package/ios/ExpoPdfMarkupView+Move.swift +266 -0
- package/ios/ExpoPdfMarkupView.swift +23 -401
- package/ios/StampPDFAnnotation.swift +100 -0
- package/ios/Tests/AnnotationSerializerTests.swift +69 -6
- package/ios/Tests/ExpoPdfMarkupViewTests.swift +40 -0
- package/ios/Tests/StampPDFAnnotationTests.swift +128 -0
- package/package.json +3 -3
- package/src/ExpoPdfMarkup.types.ts +34 -1
- package/src/ExpoPdfMarkupView.web.tsx +40 -2
- package/src/web/annotationUtils.ts +13 -0
|
@@ -11,7 +11,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
11
11
|
|
|
12
12
|
private var currentSource: String?
|
|
13
13
|
private var currentAnnotationsJSON: String?
|
|
14
|
-
private var currentMode: String = "none"
|
|
14
|
+
private(set) var currentMode: String = "none"
|
|
15
15
|
var annotationColor: String = "#FF0000" {
|
|
16
16
|
didSet {
|
|
17
17
|
inkShapeLayer?.strokeColor = (AnnotationSerializer.colorFromHex(annotationColor) ?? .red).cgColor
|
|
@@ -21,27 +21,35 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
21
21
|
var annotationLineWidth: CGFloat = 2.0
|
|
22
22
|
var annotationFontFamily: String?
|
|
23
23
|
var useJsTextDialog: Bool = false
|
|
24
|
+
var stampText: String?
|
|
25
|
+
var stampSize: CGFloat = 48.0
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
27
|
+
var inkOverlayView: InkOverlayView?
|
|
28
|
+
var disabledPdfGestures: [UIGestureRecognizer] = []
|
|
29
|
+
var inkShapeLayer: CAShapeLayer?
|
|
30
|
+
var inkPoints: [CGPoint] = []
|
|
28
31
|
private var eraserTapGesture: UITapGestureRecognizer?
|
|
29
|
-
|
|
30
|
-
|
|
32
|
+
var movePanGesture: UIPanGestureRecognizer?
|
|
33
|
+
var editingOverlayLayer: CAShapeLayer?
|
|
31
34
|
var textTapGesture: UITapGestureRecognizer?
|
|
35
|
+
var stampTapGesture: UITapGestureRecognizer?
|
|
32
36
|
private var selectionObserver: NSObjectProtocol?
|
|
33
37
|
private var selectionDebounceTimer: Timer?
|
|
34
38
|
private var currentMarkupType: String?
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
39
|
+
var movingPage: PDFPage?
|
|
40
|
+
var movingOriginalModel: AnnotationModel?
|
|
41
|
+
var movingPreviewModel: AnnotationModel?
|
|
42
|
+
var movingPreviewAnnotation: PDFAnnotation?
|
|
43
|
+
var moveStartPoint: CGPoint?
|
|
40
44
|
|
|
41
45
|
var pendingTextPage: PDFPage?
|
|
42
46
|
var pendingTextPoint: CGPoint?
|
|
43
47
|
var pendingTextAnnotation: PDFAnnotation?
|
|
44
48
|
|
|
49
|
+
var pdfScrollView: UIScrollView? {
|
|
50
|
+
pdfView.subviews.first(where: { $0 is UIScrollView }) as? UIScrollView
|
|
51
|
+
}
|
|
52
|
+
|
|
45
53
|
required init(appContext: AppContext? = nil) {
|
|
46
54
|
super.init(appContext: appContext)
|
|
47
55
|
clipsToBounds = true
|
|
@@ -204,6 +212,7 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
204
212
|
removeEraserGesture()
|
|
205
213
|
removeMoveMode()
|
|
206
214
|
removeTextGesture()
|
|
215
|
+
removeStampGesture()
|
|
207
216
|
removeSelectionObserver()
|
|
208
217
|
}
|
|
209
218
|
|
|
@@ -218,6 +227,8 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
218
227
|
setupSelectionObserver(markupType: mode)
|
|
219
228
|
case "text":
|
|
220
229
|
setupTextGesture()
|
|
230
|
+
case "stamp":
|
|
231
|
+
setupStampGesture()
|
|
221
232
|
default:
|
|
222
233
|
break
|
|
223
234
|
}
|
|
@@ -225,130 +236,6 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
225
236
|
updateEditingOverlay()
|
|
226
237
|
}
|
|
227
238
|
|
|
228
|
-
// MARK: - Ink drawing
|
|
229
|
-
|
|
230
|
-
private var pdfScrollView: UIScrollView? {
|
|
231
|
-
pdfView.subviews.first(where: { $0 is UIScrollView }) as? UIScrollView
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
private func setupInkMode() {
|
|
235
|
-
guard inkPanGesture == nil else { return }
|
|
236
|
-
|
|
237
|
-
// Disable scrolling so pan gestures go to our recognizer, not the scroll view
|
|
238
|
-
pdfScrollView?.isScrollEnabled = false
|
|
239
|
-
|
|
240
|
-
// Shape layer for live stroke feedback (added to self so it's above pdfView)
|
|
241
|
-
let shape = CAShapeLayer()
|
|
242
|
-
shape.fillColor = nil
|
|
243
|
-
shape.lineCap = .round
|
|
244
|
-
shape.lineJoin = .round
|
|
245
|
-
shape.strokeColor = (AnnotationSerializer.colorFromHex(annotationColor) ?? .red).cgColor
|
|
246
|
-
shape.lineWidth = annotationLineWidth
|
|
247
|
-
shape.frame = bounds
|
|
248
|
-
layer.addSublayer(shape)
|
|
249
|
-
inkShapeLayer = shape
|
|
250
|
-
|
|
251
|
-
// Pan gesture on pdfView — points come in pdfView's coordinate space
|
|
252
|
-
let pan = UIPanGestureRecognizer(target: self, action: #selector(handleInkPan(_:)))
|
|
253
|
-
pan.maximumNumberOfTouches = 1
|
|
254
|
-
pdfView.addGestureRecognizer(pan)
|
|
255
|
-
inkPanGesture = pan
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
private func removeInkMode() {
|
|
259
|
-
if let gesture = inkPanGesture {
|
|
260
|
-
pdfView.removeGestureRecognizer(gesture)
|
|
261
|
-
inkPanGesture = nil
|
|
262
|
-
}
|
|
263
|
-
inkShapeLayer?.removeFromSuperlayer()
|
|
264
|
-
inkShapeLayer = nil
|
|
265
|
-
inkPoints = []
|
|
266
|
-
pdfScrollView?.isScrollEnabled = true
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
@objc private func handleInkPan(_ gesture: UIPanGestureRecognizer) {
|
|
270
|
-
// Get point in self's coordinate space for visual feedback
|
|
271
|
-
let displayPoint = gesture.location(in: self)
|
|
272
|
-
|
|
273
|
-
switch gesture.state {
|
|
274
|
-
case .began:
|
|
275
|
-
inkPoints = [displayPoint]
|
|
276
|
-
updateInkShapeLayer()
|
|
277
|
-
case .changed:
|
|
278
|
-
inkPoints.append(displayPoint)
|
|
279
|
-
updateInkShapeLayer()
|
|
280
|
-
case .ended:
|
|
281
|
-
inkPoints.append(displayPoint)
|
|
282
|
-
updateInkShapeLayer()
|
|
283
|
-
finishInkStroke()
|
|
284
|
-
case .cancelled, .failed:
|
|
285
|
-
clearInkStroke()
|
|
286
|
-
default:
|
|
287
|
-
break
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
private func updateInkShapeLayer() {
|
|
292
|
-
let path = UIBezierPath()
|
|
293
|
-
for (index, point) in inkPoints.enumerated() {
|
|
294
|
-
if index == 0 {
|
|
295
|
-
path.move(to: point)
|
|
296
|
-
} else {
|
|
297
|
-
path.addLine(to: point)
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
inkShapeLayer?.path = path.cgPath
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
private func finishInkStroke() {
|
|
304
|
-
defer { clearInkStroke() }
|
|
305
|
-
guard inkPoints.count >= 2 else { return }
|
|
306
|
-
|
|
307
|
-
// Determine which page the stroke is on using the midpoint
|
|
308
|
-
let midPoint = inkPoints[inkPoints.count / 2]
|
|
309
|
-
// Convert display point (in self's coordinate space) to pdfView coordinate space
|
|
310
|
-
let pdfViewMidPoint = pdfView.convert(midPoint, from: self)
|
|
311
|
-
guard let page = pdfView.page(for: pdfViewMidPoint, nearest: true) else { return }
|
|
312
|
-
|
|
313
|
-
// Convert all display points to PDF page coordinates
|
|
314
|
-
let pdfPoints = inkPoints.map { displayPoint -> CGPoint in
|
|
315
|
-
let pdfViewPoint = pdfView.convert(displayPoint, from: self)
|
|
316
|
-
return pdfView.convert(pdfViewPoint, to: page)
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
// Use the full page bounds — PDFKit clips ink rendering to annotation bounds,
|
|
320
|
-
// and tight bounds calculations are fragile with coordinate transforms.
|
|
321
|
-
let annotationBounds = page.bounds(for: pdfView.displayBox)
|
|
322
|
-
|
|
323
|
-
let annotation = PDFAnnotation(bounds: annotationBounds, forType: .ink, withProperties: nil)
|
|
324
|
-
annotation.color = AnnotationSerializer.colorFromHex(annotationColor) ?? .red
|
|
325
|
-
|
|
326
|
-
let border = PDFBorder()
|
|
327
|
-
border.lineWidth = annotationLineWidth
|
|
328
|
-
annotation.border = border
|
|
329
|
-
|
|
330
|
-
let bezierPath = UIBezierPath()
|
|
331
|
-
for (index, point) in pdfPoints.enumerated() {
|
|
332
|
-
if index == 0 {
|
|
333
|
-
bezierPath.move(to: point)
|
|
334
|
-
} else {
|
|
335
|
-
bezierPath.addLine(to: point)
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
annotation.add(bezierPath)
|
|
339
|
-
|
|
340
|
-
let id = UUID().uuidString
|
|
341
|
-
AnnotationSerializer.tagAsModuleManaged(annotation, id: id, createdAt: Date().timeIntervalSince1970)
|
|
342
|
-
page.addAnnotation(annotation)
|
|
343
|
-
|
|
344
|
-
emitAnnotationsChanged()
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
private func clearInkStroke() {
|
|
348
|
-
inkPoints = []
|
|
349
|
-
inkShapeLayer?.path = nil
|
|
350
|
-
}
|
|
351
|
-
|
|
352
239
|
// MARK: - Eraser
|
|
353
240
|
|
|
354
241
|
private func setupEraserGesture() {
|
|
@@ -381,11 +268,10 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
381
268
|
}
|
|
382
269
|
}
|
|
383
270
|
|
|
384
|
-
|
|
271
|
+
func hitTestAnnotation(at point: CGPoint, on page: PDFPage) -> PDFAnnotation? {
|
|
385
272
|
for annotation in page.annotations.reversed() {
|
|
386
273
|
guard AnnotationSerializer.isModuleManaged(annotation) else { continue }
|
|
387
274
|
|
|
388
|
-
// For ink annotations, test against the actual paths
|
|
389
275
|
if let paths = annotation.paths {
|
|
390
276
|
let tolerance = max(annotation.border?.lineWidth ?? 2.0, 10.0)
|
|
391
277
|
for path in paths {
|
|
@@ -400,7 +286,6 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
400
286
|
}
|
|
401
287
|
}
|
|
402
288
|
} else if annotation.bounds.contains(point) {
|
|
403
|
-
// For non-ink annotations (highlight, underline, text), bounds check is fine
|
|
404
289
|
return annotation
|
|
405
290
|
}
|
|
406
291
|
}
|
|
@@ -500,266 +385,3 @@ class ExpoPdfMarkupView: ExpoView, UIGestureRecognizerDelegate {
|
|
|
500
385
|
updateEditingOverlay()
|
|
501
386
|
}
|
|
502
387
|
}
|
|
503
|
-
|
|
504
|
-
extension ExpoPdfMarkupView {
|
|
505
|
-
// MARK: - Move
|
|
506
|
-
|
|
507
|
-
func setupMoveMode() {
|
|
508
|
-
guard movePanGesture == nil else { return }
|
|
509
|
-
pdfScrollView?.isScrollEnabled = false
|
|
510
|
-
let pan = UIPanGestureRecognizer(target: self, action: #selector(handleMovePan(_:)))
|
|
511
|
-
pan.maximumNumberOfTouches = 1
|
|
512
|
-
pan.delegate = self
|
|
513
|
-
pdfView.addGestureRecognizer(pan)
|
|
514
|
-
movePanGesture = pan
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
func removeMoveMode() {
|
|
518
|
-
if let gesture = movePanGesture {
|
|
519
|
-
pdfView.removeGestureRecognizer(gesture)
|
|
520
|
-
movePanGesture = nil
|
|
521
|
-
}
|
|
522
|
-
finishMove(commit: false)
|
|
523
|
-
pdfScrollView?.isScrollEnabled = true
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
@objc func handleMovePan(_ gesture: UIPanGestureRecognizer) {
|
|
527
|
-
switch gesture.state {
|
|
528
|
-
case .began:
|
|
529
|
-
beginMove(at: gesture.location(in: pdfView))
|
|
530
|
-
case .changed:
|
|
531
|
-
updateMove(at: gesture.location(in: pdfView))
|
|
532
|
-
case .ended:
|
|
533
|
-
finishMove(commit: true)
|
|
534
|
-
case .cancelled, .failed:
|
|
535
|
-
finishMove(commit: false)
|
|
536
|
-
default:
|
|
537
|
-
break
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
private func beginMove(at location: CGPoint) {
|
|
542
|
-
guard let document = pdfView.document,
|
|
543
|
-
let page = pdfView.page(for: location, nearest: true) else { return }
|
|
544
|
-
|
|
545
|
-
let pdfPoint = pdfView.convert(location, to: page)
|
|
546
|
-
guard let annotation = hitTestAnnotation(at: pdfPoint, on: page) else { return }
|
|
547
|
-
|
|
548
|
-
let pageIndex = document.index(for: page)
|
|
549
|
-
guard let model = AnnotationSerializer.toModel(from: annotation, page: pageIndex),
|
|
550
|
-
let preview = annotationForModel(model, on: page) else { return }
|
|
551
|
-
|
|
552
|
-
movingPage = page
|
|
553
|
-
movingOriginalModel = model
|
|
554
|
-
movingPreviewModel = model
|
|
555
|
-
moveStartPoint = pdfPoint
|
|
556
|
-
|
|
557
|
-
page.removeAnnotation(annotation)
|
|
558
|
-
page.addAnnotation(preview)
|
|
559
|
-
movingPreviewAnnotation = preview
|
|
560
|
-
updateEditingOverlay()
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
private func updateMove(at location: CGPoint) {
|
|
564
|
-
guard let page = movingPage,
|
|
565
|
-
let originalModel = movingOriginalModel,
|
|
566
|
-
let startPoint = moveStartPoint else { return }
|
|
567
|
-
|
|
568
|
-
let pdfPoint = pdfView.convert(location, to: page)
|
|
569
|
-
let rawDeltaX = pdfPoint.x - startPoint.x
|
|
570
|
-
let rawDeltaY = pdfPoint.y - startPoint.y
|
|
571
|
-
let clampedDelta = clampedTranslation(for: originalModel, dx: rawDeltaX, dy: rawDeltaY, on: page)
|
|
572
|
-
let translatedModel = translatedModel(originalModel, dx: clampedDelta.x, dy: clampedDelta.y)
|
|
573
|
-
guard translatedModel != movingPreviewModel,
|
|
574
|
-
let replacement = annotationForModel(translatedModel, on: page) else { return }
|
|
575
|
-
|
|
576
|
-
if let currentPreview = movingPreviewAnnotation {
|
|
577
|
-
page.removeAnnotation(currentPreview)
|
|
578
|
-
}
|
|
579
|
-
page.addAnnotation(replacement)
|
|
580
|
-
movingPreviewAnnotation = replacement
|
|
581
|
-
movingPreviewModel = translatedModel
|
|
582
|
-
updateEditingOverlay()
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
private func finishMove(commit: Bool) {
|
|
586
|
-
guard let page = movingPage,
|
|
587
|
-
let originalModel = movingOriginalModel else {
|
|
588
|
-
clearMoveState()
|
|
589
|
-
updateEditingOverlay()
|
|
590
|
-
return
|
|
591
|
-
}
|
|
592
|
-
|
|
593
|
-
if let preview = movingPreviewAnnotation {
|
|
594
|
-
page.removeAnnotation(preview)
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
let finalModel = commit ? (movingPreviewModel ?? originalModel) : originalModel
|
|
598
|
-
if let finalAnnotation = annotationForModel(finalModel, on: page) {
|
|
599
|
-
page.addAnnotation(finalAnnotation)
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
let didChange = commit && finalModel != originalModel
|
|
603
|
-
clearMoveState()
|
|
604
|
-
updateEditingOverlay()
|
|
605
|
-
|
|
606
|
-
if didChange {
|
|
607
|
-
emitAnnotationsChanged()
|
|
608
|
-
}
|
|
609
|
-
}
|
|
610
|
-
|
|
611
|
-
private func clearMoveState() {
|
|
612
|
-
movingPage = nil
|
|
613
|
-
movingOriginalModel = nil
|
|
614
|
-
movingPreviewModel = nil
|
|
615
|
-
movingPreviewAnnotation = nil
|
|
616
|
-
moveStartPoint = nil
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
// MARK: - Editing overlay
|
|
620
|
-
|
|
621
|
-
func updateEditingOverlay() {
|
|
622
|
-
guard currentMode == "move" || currentMode == "eraser" else {
|
|
623
|
-
editingOverlayLayer?.removeFromSuperlayer()
|
|
624
|
-
editingOverlayLayer = nil
|
|
625
|
-
return
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
guard let document = pdfView.document,
|
|
629
|
-
let documentView = pdfView.documentView else { return }
|
|
630
|
-
|
|
631
|
-
let overlay = editingOverlayLayer ?? {
|
|
632
|
-
let shape = CAShapeLayer()
|
|
633
|
-
shape.fillColor = UIColor.clear.cgColor
|
|
634
|
-
shape.strokeColor = UIColor.systemBlue.cgColor
|
|
635
|
-
shape.lineWidth = 1.5
|
|
636
|
-
shape.lineDashPattern = [6, 4]
|
|
637
|
-
documentView.layer.addSublayer(shape)
|
|
638
|
-
editingOverlayLayer = shape
|
|
639
|
-
return shape
|
|
640
|
-
}()
|
|
641
|
-
|
|
642
|
-
if overlay.superlayer !== documentView.layer {
|
|
643
|
-
overlay.removeFromSuperlayer()
|
|
644
|
-
documentView.layer.addSublayer(overlay)
|
|
645
|
-
}
|
|
646
|
-
|
|
647
|
-
overlay.frame = documentView.bounds
|
|
648
|
-
let path = UIBezierPath()
|
|
649
|
-
|
|
650
|
-
for pageIndex in 0 ..< document.pageCount {
|
|
651
|
-
guard let page = document.page(at: pageIndex) else { continue }
|
|
652
|
-
for annotation in page.annotations where AnnotationSerializer.isModuleManaged(annotation) {
|
|
653
|
-
guard let outlineRect = annotationOutlineRect(for: annotation, on: page, in: documentView) else {
|
|
654
|
-
continue
|
|
655
|
-
}
|
|
656
|
-
path.append(UIBezierPath(rect: outlineRect))
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
overlay.path = path.cgPath
|
|
661
|
-
}
|
|
662
|
-
|
|
663
|
-
private func annotationOutlineRect(
|
|
664
|
-
for annotation: PDFAnnotation,
|
|
665
|
-
on page: PDFPage,
|
|
666
|
-
in documentView: UIView
|
|
667
|
-
) -> CGRect? {
|
|
668
|
-
let pdfRect: CGRect
|
|
669
|
-
if let paths = annotation.paths, !paths.isEmpty {
|
|
670
|
-
var combinedBounds = CGRect.null
|
|
671
|
-
for path in paths {
|
|
672
|
-
combinedBounds = combinedBounds.union(path.cgPath.boundingBoxOfPath)
|
|
673
|
-
}
|
|
674
|
-
guard !combinedBounds.isNull else { return nil }
|
|
675
|
-
let padding = max(annotation.border?.lineWidth ?? 2.0, 10.0)
|
|
676
|
-
pdfRect = combinedBounds.insetBy(dx: -padding, dy: -padding)
|
|
677
|
-
} else {
|
|
678
|
-
pdfRect = annotation.bounds
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
let pdfViewRect = pdfView.convert(pdfRect, from: page)
|
|
682
|
-
return documentView.convert(pdfViewRect, from: pdfView)
|
|
683
|
-
}
|
|
684
|
-
|
|
685
|
-
private func annotationForModel(_ model: AnnotationModel, on page: PDFPage) -> PDFAnnotation? {
|
|
686
|
-
AnnotationSerializer.toPDFAnnotation(model, pageBounds: page.bounds(for: pdfView.displayBox))
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
private func translatedModel(_ model: AnnotationModel, dx: CGFloat, dy: CGFloat) -> AnnotationModel {
|
|
690
|
-
guard dx != 0 || dy != 0 else { return model }
|
|
691
|
-
|
|
692
|
-
var updated = model
|
|
693
|
-
switch model.type {
|
|
694
|
-
case "ink":
|
|
695
|
-
let translatedPaths = model.decodedPaths.map { stroke in
|
|
696
|
-
stroke.map { point in
|
|
697
|
-
AnnotationPoint(x: point.x + dx, y: point.y + dy)
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
updated.paths = updated.encodedPaths(from: translatedPaths)
|
|
701
|
-
case "highlight", "underline", "text", "freeText":
|
|
702
|
-
if let bounds = model.bounds {
|
|
703
|
-
updated.bounds = AnnotationBounds(
|
|
704
|
-
x: bounds.x + dx,
|
|
705
|
-
y: bounds.y + dy,
|
|
706
|
-
width: bounds.width,
|
|
707
|
-
height: bounds.height
|
|
708
|
-
)
|
|
709
|
-
}
|
|
710
|
-
default:
|
|
711
|
-
break
|
|
712
|
-
}
|
|
713
|
-
return updated
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
private func clampedTranslation(
|
|
717
|
-
for model: AnnotationModel,
|
|
718
|
-
dx: CGFloat,
|
|
719
|
-
dy: CGFloat,
|
|
720
|
-
on page: PDFPage
|
|
721
|
-
) -> CGPoint {
|
|
722
|
-
guard let outlineBounds = annotationOutlineBounds(for: model) else {
|
|
723
|
-
return CGPoint(x: dx, y: dy)
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
let pageBounds = page.bounds(for: pdfView.displayBox)
|
|
727
|
-
let minDx = pageBounds.minX - outlineBounds.x
|
|
728
|
-
let maxDx = pageBounds.maxX - (outlineBounds.x + outlineBounds.width)
|
|
729
|
-
let minDy = pageBounds.minY - outlineBounds.y
|
|
730
|
-
let maxDy = pageBounds.maxY - (outlineBounds.y + outlineBounds.height)
|
|
731
|
-
|
|
732
|
-
return CGPoint(
|
|
733
|
-
x: min(max(dx, minDx), maxDx),
|
|
734
|
-
y: min(max(dy, minDy), maxDy)
|
|
735
|
-
)
|
|
736
|
-
}
|
|
737
|
-
|
|
738
|
-
private func annotationOutlineBounds(for model: AnnotationModel) -> AnnotationBounds? {
|
|
739
|
-
guard model.type == "ink" else { return model.bounds }
|
|
740
|
-
let paths = model.decodedPaths
|
|
741
|
-
var minX = CGFloat.greatestFiniteMagnitude
|
|
742
|
-
var minY = CGFloat.greatestFiniteMagnitude
|
|
743
|
-
var maxX = -CGFloat.greatestFiniteMagnitude
|
|
744
|
-
var maxY = -CGFloat.greatestFiniteMagnitude
|
|
745
|
-
|
|
746
|
-
for stroke in paths {
|
|
747
|
-
for point in stroke {
|
|
748
|
-
minX = min(minX, point.x)
|
|
749
|
-
minY = min(minY, point.y)
|
|
750
|
-
maxX = max(maxX, point.x)
|
|
751
|
-
maxY = max(maxY, point.y)
|
|
752
|
-
}
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
guard minX.isFinite, minY.isFinite, maxX.isFinite, maxY.isFinite else { return nil }
|
|
756
|
-
|
|
757
|
-
let padding = max(model.lineWidth ?? 2.0, 10.0)
|
|
758
|
-
return AnnotationBounds(
|
|
759
|
-
x: minX - padding,
|
|
760
|
-
y: minY - padding,
|
|
761
|
-
width: maxX - minX + padding * 2,
|
|
762
|
-
height: maxY - minY + padding * 2
|
|
763
|
-
)
|
|
764
|
-
}
|
|
765
|
-
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import PDFKit
|
|
2
|
+
import UIKit
|
|
3
|
+
|
|
4
|
+
/// Renders a text glyph (e.g. an emoji, or a plain character like `"f"`) as a PDFKit annotation.
|
|
5
|
+
/// Data (`text`) round-trips via `setValue(forAnnotationKey:)`, matching the `_id`/`_color`
|
|
6
|
+
/// convention in `AnnotationSerializer`, so this subclass stays a pure rendering concern. The
|
|
7
|
+
/// `_color` key (already tagged by `tagAsModuleManaged`) is reused here as the glyph's fill color.
|
|
8
|
+
final class StampPDFAnnotation: PDFAnnotation {
|
|
9
|
+
private static let textKey = PDFAnnotationKey(rawValue: "_stampText")
|
|
10
|
+
private static let colorKey = PDFAnnotationKey(rawValue: "_color")
|
|
11
|
+
|
|
12
|
+
private static var textImageCache: [String: CGImage] = [:]
|
|
13
|
+
|
|
14
|
+
required init?(coder: NSCoder) {
|
|
15
|
+
super.init(coder: coder)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
override init(bounds: CGRect, forType type: PDFAnnotationSubtype, withProperties properties: [AnyHashable: Any]?) {
|
|
19
|
+
super.init(bounds: bounds, forType: type, withProperties: properties)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
func setStampText(_ text: String) {
|
|
23
|
+
setValue(text, forAnnotationKey: Self.textKey)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var stampText: String? {
|
|
27
|
+
value(forAnnotationKey: Self.textKey) as? String
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
override func draw(with _: PDFDisplayBox, in context: CGContext) {
|
|
31
|
+
guard let text = stampText else { return }
|
|
32
|
+
let colorHex = (value(forAnnotationKey: Self.colorKey) as? String) ?? "#000000"
|
|
33
|
+
guard let cgImage = Self.image(forText: text, colorHex: colorHex) else { return }
|
|
34
|
+
|
|
35
|
+
context.draw(cgImage, in: bounds)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
private static func image(forText text: String, colorHex: String) -> CGImage? {
|
|
39
|
+
let cacheKey = "\(text)|\(colorHex)"
|
|
40
|
+
if let cached = textImageCache[cacheKey] {
|
|
41
|
+
return cached
|
|
42
|
+
}
|
|
43
|
+
let color = AnnotationSerializer.colorFromHex(colorHex) ?? .black
|
|
44
|
+
let size = CGSize(width: 256, height: 256)
|
|
45
|
+
let renderer = UIGraphicsImageRenderer(size: size)
|
|
46
|
+
let rendered = renderer.image { _ in
|
|
47
|
+
let font = UIFont.systemFont(ofSize: size.height * 0.8)
|
|
48
|
+
let attrs: [NSAttributedString.Key: Any] = [.font: font, .foregroundColor: color]
|
|
49
|
+
let textSize = (text as NSString).size(withAttributes: attrs)
|
|
50
|
+
let origin = CGPoint(x: (size.width - textSize.width) / 2, y: (size.height - textSize.height) / 2)
|
|
51
|
+
(text as NSString).draw(at: origin, withAttributes: attrs)
|
|
52
|
+
}
|
|
53
|
+
guard let cgImage = rendered.cgImage else { return nil }
|
|
54
|
+
textImageCache[cacheKey] = cgImage
|
|
55
|
+
return cgImage
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
extension ExpoPdfMarkupView {
|
|
60
|
+
// MARK: - Stamp
|
|
61
|
+
|
|
62
|
+
func setupStampGesture() {
|
|
63
|
+
guard stampTapGesture == nil else { return }
|
|
64
|
+
let tap = UITapGestureRecognizer(target: self, action: #selector(handleStampTap(_:)))
|
|
65
|
+
tap.delegate = self
|
|
66
|
+
pdfView.addGestureRecognizer(tap)
|
|
67
|
+
stampTapGesture = tap
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
func removeStampGesture() {
|
|
71
|
+
if let gesture = stampTapGesture {
|
|
72
|
+
pdfView.removeGestureRecognizer(gesture)
|
|
73
|
+
stampTapGesture = nil
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
@objc func handleStampTap(_ gesture: UITapGestureRecognizer) {
|
|
78
|
+
guard let text = stampText else { return }
|
|
79
|
+
let location = gesture.location(in: pdfView)
|
|
80
|
+
guard let page = pdfView.page(for: location, nearest: true) else { return }
|
|
81
|
+
let pdfPoint = pdfView.convert(location, to: page)
|
|
82
|
+
|
|
83
|
+
let half = stampSize / 2
|
|
84
|
+
var model = AnnotationModel(
|
|
85
|
+
id: UUID().uuidString,
|
|
86
|
+
type: "stamp",
|
|
87
|
+
page: 0,
|
|
88
|
+
color: annotationColor,
|
|
89
|
+
createdAt: Date().timeIntervalSince1970
|
|
90
|
+
)
|
|
91
|
+
model.bounds = AnnotationBounds(
|
|
92
|
+
CGRect(x: pdfPoint.x - half, y: pdfPoint.y - half, width: stampSize, height: stampSize)
|
|
93
|
+
)
|
|
94
|
+
model.text = text
|
|
95
|
+
|
|
96
|
+
guard let annotation = AnnotationSerializer.toPDFAnnotation(model) else { return }
|
|
97
|
+
page.addAnnotation(annotation)
|
|
98
|
+
emitAnnotationsChanged()
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -73,7 +73,7 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
73
73
|
XCTAssertEqual(annotation?.color, .clear)
|
|
74
74
|
}
|
|
75
75
|
|
|
76
|
-
func testCreateFreeTextAnnotationExpandsNarrowBoundsToFitText() {
|
|
76
|
+
func testCreateFreeTextAnnotationExpandsNarrowBoundsToFitText() throws {
|
|
77
77
|
let originalBounds = AnnotationBounds(x: 100, y: 200, width: 40, height: 24)
|
|
78
78
|
let model = AnnotationModel(
|
|
79
79
|
id: "txt-narrow-1",
|
|
@@ -88,7 +88,50 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
88
88
|
let annotation = AnnotationSerializer.toPDFAnnotation(model)
|
|
89
89
|
XCTAssertNotNil(annotation)
|
|
90
90
|
XCTAssertGreaterThan(annotation?.bounds.width ?? 0, originalBounds.width)
|
|
91
|
-
XCTAssertEqual(annotation
|
|
91
|
+
XCTAssertEqual(try XCTUnwrap(annotation).bounds.maxY, originalBounds.cgRect.maxY, accuracy: 0.01)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
func testCreateStampAnnotationWithText() throws {
|
|
95
|
+
let model = AnnotationModel(
|
|
96
|
+
id: "st-1",
|
|
97
|
+
type: "stamp",
|
|
98
|
+
page: 0,
|
|
99
|
+
color: "#000000",
|
|
100
|
+
bounds: AnnotationBounds(x: 10, y: 20, width: 48, height: 48),
|
|
101
|
+
text: "⭐"
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
let annotation = AnnotationSerializer.toPDFAnnotation(model)
|
|
105
|
+
XCTAssertNotNil(annotation)
|
|
106
|
+
XCTAssertEqual(annotation?.type, "Stamp")
|
|
107
|
+
XCTAssertTrue(annotation is StampPDFAnnotation)
|
|
108
|
+
XCTAssertTrue(try AnnotationSerializer.isModuleManaged(XCTUnwrap(annotation)))
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
func testStampMissingBoundsReturnsNil() {
|
|
112
|
+
let model = AnnotationModel(
|
|
113
|
+
id: "st-3",
|
|
114
|
+
type: "stamp",
|
|
115
|
+
page: 0,
|
|
116
|
+
color: "#000000",
|
|
117
|
+
text: "⭐"
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
let annotation = AnnotationSerializer.toPDFAnnotation(model)
|
|
121
|
+
XCTAssertNil(annotation)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
func testStampMissingTextReturnsNil() {
|
|
125
|
+
let model = AnnotationModel(
|
|
126
|
+
id: "st-4",
|
|
127
|
+
type: "stamp",
|
|
128
|
+
page: 0,
|
|
129
|
+
color: "#000000",
|
|
130
|
+
bounds: AnnotationBounds(x: 10, y: 20, width: 48, height: 48)
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
let annotation = AnnotationSerializer.toPDFAnnotation(model)
|
|
134
|
+
XCTAssertNil(annotation)
|
|
92
135
|
}
|
|
93
136
|
|
|
94
137
|
func testUnknownTypeReturnsNil() {
|
|
@@ -105,7 +148,7 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
105
148
|
|
|
106
149
|
// MARK: - Model extraction
|
|
107
150
|
|
|
108
|
-
func testExtractModelFromModuleManagedAnnotation() {
|
|
151
|
+
func testExtractModelFromModuleManagedAnnotation() throws {
|
|
109
152
|
let model = AnnotationModel(
|
|
110
153
|
id: "ext-1",
|
|
111
154
|
type: "ink",
|
|
@@ -127,7 +170,7 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
127
170
|
XCTAssertEqual(result.color, "#FF0000")
|
|
128
171
|
}
|
|
129
172
|
|
|
130
|
-
func testFreeTextColorRoundTrip() {
|
|
173
|
+
func testFreeTextColorRoundTrip() throws {
|
|
131
174
|
let model = AnnotationModel(
|
|
132
175
|
id: "ft-color-1",
|
|
133
176
|
type: "freeText",
|
|
@@ -147,6 +190,26 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
147
190
|
XCTAssertEqual(result.color, "#FF0000", "freeText color must survive a serialisation round-trip")
|
|
148
191
|
}
|
|
149
192
|
|
|
193
|
+
func testExtractStampModelRoundTrip() throws {
|
|
194
|
+
let model = AnnotationModel(
|
|
195
|
+
id: "st-rt-1",
|
|
196
|
+
type: "stamp",
|
|
197
|
+
page: 0,
|
|
198
|
+
color: "#123456",
|
|
199
|
+
bounds: AnnotationBounds(x: 10, y: 20, width: 48, height: 48),
|
|
200
|
+
text: "⭐"
|
|
201
|
+
)
|
|
202
|
+
|
|
203
|
+
let pdfAnnotation = try XCTUnwrap(AnnotationSerializer.toPDFAnnotation(model))
|
|
204
|
+
let extracted = AnnotationSerializer.toModel(from: pdfAnnotation, page: 0)
|
|
205
|
+
|
|
206
|
+
let result = try XCTUnwrap(extracted)
|
|
207
|
+
XCTAssertEqual(result.type, "stamp")
|
|
208
|
+
XCTAssertEqual(result.text, "⭐")
|
|
209
|
+
XCTAssertEqual(result.color, "#123456")
|
|
210
|
+
XCTAssertEqual(result.bounds?.cgRect, CGRect(x: 10, y: 20, width: 48, height: 48))
|
|
211
|
+
}
|
|
212
|
+
|
|
150
213
|
func testExtractModelFromNonManagedAnnotationReturnsNil() {
|
|
151
214
|
let annotation = PDFAnnotation(
|
|
152
215
|
bounds: CGRect(x: 0, y: 0, width: 100, height: 10),
|
|
@@ -179,7 +242,7 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
179
242
|
|
|
180
243
|
// MARK: - Color helpers
|
|
181
244
|
|
|
182
|
-
func testColorFromHex() {
|
|
245
|
+
func testColorFromHex() throws {
|
|
183
246
|
let red = try XCTUnwrap(AnnotationSerializer.colorFromHex("#FF0000"))
|
|
184
247
|
|
|
185
248
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|
|
@@ -189,7 +252,7 @@ final class AnnotationSerializerTests: XCTestCase {
|
|
|
189
252
|
XCTAssertEqual(b, 0.0, accuracy: 0.01)
|
|
190
253
|
}
|
|
191
254
|
|
|
192
|
-
func testColorFromHexWithoutHash() {
|
|
255
|
+
func testColorFromHexWithoutHash() throws {
|
|
193
256
|
let blue = try XCTUnwrap(AnnotationSerializer.colorFromHex("0000FF"))
|
|
194
257
|
|
|
195
258
|
var r: CGFloat = 0, g: CGFloat = 0, b: CGFloat = 0, a: CGFloat = 0
|