@tobyt/expo-pdf-markup 1.0.1 → 1.1.1

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 (31) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationGeometry.kt +2 -2
  3. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationHitTester.kt +1 -1
  4. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +2 -1
  5. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +36 -0
  6. package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +3 -1
  7. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +72 -4
  8. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +8 -0
  9. package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +9 -0
  10. package/android/src/main/java/dev/terhoeven/expopdfmarkup/TouchInterceptPolicy.kt +20 -0
  11. package/build/ExpoPdfMarkup.types.d.ts +27 -2
  12. package/build/ExpoPdfMarkup.types.d.ts.map +1 -1
  13. package/build/ExpoPdfMarkup.types.js.map +1 -1
  14. package/build/ExpoPdfMarkupView.web.d.ts.map +1 -1
  15. package/build/ExpoPdfMarkupView.web.js +34 -5
  16. package/build/ExpoPdfMarkupView.web.js.map +1 -1
  17. package/build/web/annotationUtils.d.ts.map +1 -1
  18. package/build/web/annotationUtils.js +14 -0
  19. package/build/web/annotationUtils.js.map +1 -1
  20. package/ios/AnnotationModels.swift +1 -0
  21. package/ios/AnnotationSerializer.swift +20 -0
  22. package/ios/ExpoPdfMarkupModule.swift +8 -0
  23. package/ios/ExpoPdfMarkupView+Move.swift +1 -1
  24. package/ios/ExpoPdfMarkupView.swift +6 -0
  25. package/ios/StampPDFAnnotation.swift +100 -0
  26. package/ios/Tests/AnnotationSerializerTests.swift +63 -0
  27. package/ios/Tests/StampPDFAnnotationTests.swift +128 -0
  28. package/package.json +3 -3
  29. package/src/ExpoPdfMarkup.types.ts +34 -1
  30. package/src/ExpoPdfMarkupView.web.tsx +40 -2
  31. package/src/web/annotationUtils.ts +13 -0
@@ -91,6 +91,49 @@ final class AnnotationSerializerTests: XCTestCase {
91
91
  XCTAssertEqual(try XCTUnwrap(annotation).bounds.maxY, originalBounds.cgRect.maxY, accuracy: 0.01)
92
92
  }
93
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)
135
+ }
136
+
94
137
  func testUnknownTypeReturnsNil() {
95
138
  let model = AnnotationModel(
96
139
  id: "bad-1",
@@ -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),
@@ -0,0 +1,128 @@
1
+ @testable internal import ExpoPdfMarkup
2
+ import PDFKit
3
+ import XCTest
4
+
5
+ final class StampPDFAnnotationTests: XCTestCase {
6
+ func testDrawWithTextContentDoesNotThrowAndPaintsPixels() throws {
7
+ let bounds = CGRect(x: 0, y: 0, width: 48, height: 48)
8
+ let annotation = StampPDFAnnotation(bounds: bounds, forType: .stamp, withProperties: nil)
9
+ annotation.setStampText("⭐")
10
+
11
+ let context = try XCTUnwrap(makeContext(size: bounds.size))
12
+ annotation.draw(with: .mediaBox, in: context)
13
+
14
+ XCTAssertTrue(hasNonTransparentPixel(context: context, size: bounds.size))
15
+ }
16
+
17
+ func testDrawAppliesAnnotationColor() throws {
18
+ let bounds = CGRect(x: 0, y: 0, width: 48, height: 48)
19
+ let annotation = StampPDFAnnotation(bounds: bounds, forType: .stamp, withProperties: nil)
20
+ annotation.setStampText("█")
21
+ annotation.setValue("#FF0000", forAnnotationKey: PDFAnnotationKey(rawValue: "_color"))
22
+
23
+ let context = try XCTUnwrap(makeContext(size: bounds.size))
24
+ annotation.draw(with: .mediaBox, in: context)
25
+
26
+ let pixel = try XCTUnwrap(centerPixel(context: context, size: bounds.size))
27
+ XCTAssertGreaterThan(pixel.r, 200)
28
+ XCTAssertLessThan(pixel.g, 50)
29
+ XCTAssertLessThan(pixel.b, 50)
30
+ }
31
+
32
+ func testDrawWithoutTextDoesNothing() throws {
33
+ let bounds = CGRect(x: 0, y: 0, width: 48, height: 48)
34
+ let annotation = StampPDFAnnotation(bounds: bounds, forType: .stamp, withProperties: nil)
35
+
36
+ let context = try XCTUnwrap(makeContext(size: bounds.size))
37
+ annotation.draw(with: .mediaBox, in: context)
38
+
39
+ XCTAssertFalse(hasNonTransparentPixel(context: context, size: bounds.size))
40
+ }
41
+
42
+ /// "L" is asymmetric top-to-bottom: a wide horizontal foot at the bottom of the glyph and
43
+ /// a narrow vertical stroke at the top. A bitmap context backing draw(with:in:) stores rows
44
+ /// top-down while PDF space is bottom-up, so an unflipped (or wrongly-flipped) draw flips the
45
+ /// glyph vertically. Assert the foot lands near the low-y (bottom) edge of bounds, matching
46
+ /// normal reading orientation when the page is viewed right-side up.
47
+ func testDrawRendersTextInUprightOrientation() throws {
48
+ let bounds = CGRect(x: 0, y: 0, width: 64, height: 64)
49
+ let width = Int(bounds.width)
50
+ let height = Int(bounds.height)
51
+ let context = try XCTUnwrap(makeContext(size: bounds.size))
52
+
53
+ let annotation = StampPDFAnnotation(bounds: bounds, forType: .stamp, withProperties: nil)
54
+ annotation.setStampText("L")
55
+ annotation.setValue("#000000", forAnnotationKey: PDFAnnotationKey(rawValue: "_color"))
56
+ annotation.draw(with: .mediaBox, in: context)
57
+
58
+ let data = try XCTUnwrap(context.data)
59
+ let buffer = data.bindMemory(to: UInt8.self, capacity: width * height * 4)
60
+
61
+ func inkSpan(row: Int) -> Int {
62
+ var minX = -1
63
+ var maxX = -1
64
+ for x in 0 ..< width {
65
+ let idx = (row * width + x) * 4
66
+ if buffer[idx + 3] > 10 {
67
+ if minX == -1 { minX = x }
68
+ maxX = x
69
+ }
70
+ }
71
+ return minX == -1 ? 0 : (maxX - minX)
72
+ }
73
+
74
+ var minInkRow = -1
75
+ var maxInkRow = -1
76
+ for row in 0 ..< height where inkSpan(row: row) > 0 {
77
+ if minInkRow == -1 { minInkRow = row }
78
+ maxInkRow = row
79
+ }
80
+ let minInkRowUnwrapped = try XCTUnwrap(minInkRow == -1 ? nil : minInkRow)
81
+ let maxInkRowUnwrapped = try XCTUnwrap(maxInkRow == -1 ? nil : maxInkRow)
82
+
83
+ // Buffer row 0 is the top of the bitmap (PDF-space maxY); the last row is the bottom
84
+ // (PDF-space minY, where the page's "down" is).
85
+ let spanNearBufferTop = inkSpan(row: minInkRowUnwrapped + 2)
86
+ let spanNearBufferBottom = inkSpan(row: maxInkRowUnwrapped - 2)
87
+ XCTAssertGreaterThan(
88
+ spanNearBufferBottom, spanNearBufferTop,
89
+ "expected the wide foot of \"L\" near the bottom of the page (low PDF y), not the top"
90
+ )
91
+ }
92
+
93
+ // MARK: - Helpers
94
+
95
+ private func makeContext(size: CGSize) -> CGContext? {
96
+ let colorSpace = CGColorSpaceCreateDeviceRGB()
97
+ return CGContext(
98
+ data: nil,
99
+ width: Int(size.width),
100
+ height: Int(size.height),
101
+ bitsPerComponent: 8,
102
+ bytesPerRow: 0,
103
+ space: colorSpace,
104
+ bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
105
+ )
106
+ }
107
+
108
+ private func hasNonTransparentPixel(context: CGContext, size: CGSize) -> Bool {
109
+ guard let data = context.data else { return false }
110
+ let byteCount = Int(size.width) * Int(size.height) * 4
111
+ let buffer = data.bindMemory(to: UInt8.self, capacity: byteCount)
112
+ for pixelIndex in stride(from: 0, to: byteCount, by: 4) {
113
+ let alpha = buffer[pixelIndex + 3]
114
+ if alpha > 0 { return true }
115
+ }
116
+ return false
117
+ }
118
+
119
+ private func centerPixel(context: CGContext, size: CGSize) -> (r: UInt8, g: UInt8, b: UInt8)? {
120
+ guard let data = context.data else { return nil }
121
+ let width = Int(size.width)
122
+ let height = Int(size.height)
123
+ let byteCount = width * height * 4
124
+ let buffer = data.bindMemory(to: UInt8.self, capacity: byteCount)
125
+ let pixelIndex = ((height / 2) * width + width / 2) * 4
126
+ return (r: buffer[pixelIndex], g: buffer[pixelIndex + 1], b: buffer[pixelIndex + 2])
127
+ }
128
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tobyt/expo-pdf-markup",
3
- "version": "1.0.1",
3
+ "version": "1.1.1",
4
4
  "description": "An Expo module for displaying and annotating PDFs",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -55,10 +55,10 @@
55
55
  "@changesets/cli": "^2.30.0",
56
56
  "@types/react": "~19.2.10",
57
57
  "@types/react-test-renderer": "^19.1.0",
58
- "expo": "^55.0.5",
58
+ "expo": "~55.0.26",
59
59
  "expo-module-scripts": "^55.0.2",
60
60
  "pdfjs-dist": "^4.10.38",
61
- "react-native": "0.83.2",
61
+ "react-native": "0.83.6",
62
62
  "react-native-web": "^0.21.2",
63
63
  "typedoc": "^0.28.17"
64
64
  },
@@ -51,7 +51,24 @@ export type TextAnnotation = {
51
51
  createdAt?: number;
52
52
  };
53
53
 
54
- export type Annotation = InkAnnotation | HighlightAnnotation | UnderlineAnnotation | TextAnnotation;
54
+ export type StampAnnotation = {
55
+ id: string;
56
+ type: 'stamp';
57
+ page: number;
58
+ /** Colour applied to the text glyph, as a CSS colour string (e.g. `'#FF0000'`). Has no visible effect on full-colour emoji. */
59
+ color: string;
60
+ bounds: AnnotationBounds;
61
+ /** A glyph or short string to render (e.g. an emoji, or a plain character like `'f'`). */
62
+ text: string;
63
+ createdAt?: number;
64
+ };
65
+
66
+ export type Annotation =
67
+ | InkAnnotation
68
+ | HighlightAnnotation
69
+ | UnderlineAnnotation
70
+ | TextAnnotation
71
+ | StampAnnotation;
55
72
 
56
73
  export type AnnotationsData = {
57
74
  version: 1;
@@ -64,9 +81,21 @@ export type AnnotationMode =
64
81
  | 'highlight'
65
82
  | 'underline'
66
83
  | 'text'
84
+ | 'stamp'
67
85
  | 'eraser'
68
86
  | 'move';
69
87
 
88
+ /**
89
+ * Recommended shape for describing a consumer-defined stamp set (e.g. domain-specific glyphs).
90
+ * Purely a convenience type for organizing your own stamp picker UI — never sent to native;
91
+ * extract `text` to set as the `stampText` prop before placement.
92
+ */
93
+ export type StampDefinition = {
94
+ id: string;
95
+ label: string;
96
+ text: string;
97
+ };
98
+
70
99
  export type TextInputRequest = {
71
100
  mode: 'create' | 'edit';
72
101
  page: number;
@@ -96,6 +125,10 @@ export type ExpoPdfMarkupViewProps = {
96
125
  * - Web: CSS font family (`"Georgia, serif"`) or `undefined` for `sans-serif`
97
126
  */
98
127
  annotationFontFamily?: string;
128
+ /** Glyph or short string for the next stamp to place. Required for the `'stamp'` tool to place anything on tap. */
129
+ stampText?: string;
130
+ /** Side length, in PDF points, of the square bounding box for newly placed stamps. Defaults to 48. */
131
+ stampSize?: number;
99
132
  /** Fired when the visible page changes. */
100
133
  onPageChanged?: (event: {
101
134
  nativeEvent: { page: number; pageCount: number; pageWidth: number; pageHeight: number };
@@ -55,6 +55,8 @@ type PageViewProps = {
55
55
  annotationMode: AnnotationMode;
56
56
  annotationColor: string;
57
57
  annotationLineWidth: number;
58
+ stampText?: string;
59
+ stampSize: number;
58
60
  zoomLevel: number;
59
61
  onAnnotationAdded: (annotation: Annotation) => void;
60
62
  onAnnotationRemoved: (id: string) => void;
@@ -74,6 +76,8 @@ function PageView({
74
76
  annotationMode,
75
77
  annotationColor,
76
78
  annotationLineWidth,
79
+ stampText,
80
+ stampSize,
77
81
  zoomLevel,
78
82
  onAnnotationAdded,
79
83
  onAnnotationRemoved,
@@ -263,7 +267,13 @@ function PageView({
263
267
  }
264
268
 
265
269
  function handlePointerUp(e: React.PointerEvent<HTMLCanvasElement>) {
266
- if (!pointerDownRef.current && annotationMode !== 'text' && annotationMode !== 'eraser') return;
270
+ if (
271
+ !pointerDownRef.current &&
272
+ annotationMode !== 'text' &&
273
+ annotationMode !== 'eraser' &&
274
+ annotationMode !== 'stamp'
275
+ )
276
+ return;
267
277
  pointerDownRef.current = false;
268
278
  const pt = getCanvasPoint(e);
269
279
  const { scale, pdfHeight } = meta;
@@ -342,12 +352,36 @@ function PageView({
342
352
  const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
343
353
  const hit = hitTestAnnotation(pdfPoint, annotations, pageIndex);
344
354
  if (hit) onAnnotationRemoved(hit.id);
355
+ } else if (annotationMode === 'stamp') {
356
+ if (!stampText) return;
357
+ const pdfPoint = canvasToPdf(pt.x, pt.y, scale, pdfHeight);
358
+ const half = stampSize / 2;
359
+ onAnnotationAdded({
360
+ id: newAnnotationId(),
361
+ type: 'stamp',
362
+ page: pageIndex,
363
+ color: annotationColor,
364
+ bounds: {
365
+ x: pdfPoint.x - half,
366
+ y: pdfPoint.y - half,
367
+ width: stampSize,
368
+ height: stampSize,
369
+ },
370
+ text: stampText,
371
+ createdAt: Date.now(),
372
+ });
345
373
  }
346
374
  }
347
375
 
348
376
  const pointerEvents = annotationMode === 'none' ? 'none' : 'auto';
349
377
  const cursor =
350
- annotationMode === 'move' ? 'grab' : annotationMode === 'eraser' ? 'not-allowed' : 'crosshair';
378
+ annotationMode === 'move'
379
+ ? 'grab'
380
+ : annotationMode === 'eraser'
381
+ ? 'not-allowed'
382
+ : annotationMode === 'stamp'
383
+ ? 'copy'
384
+ : 'crosshair';
351
385
 
352
386
  return (
353
387
  <div
@@ -398,6 +432,8 @@ export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
398
432
  annotationColor = '#FF0000',
399
433
  annotationLineWidth = 2,
400
434
  annotationFontFamily,
435
+ stampText,
436
+ stampSize = 48,
401
437
  onPageChanged,
402
438
  onLoadComplete,
403
439
  onError,
@@ -726,6 +762,8 @@ export default function ExpoPdfMarkupView(props: ExpoPdfMarkupViewProps) {
726
762
  annotationMode={annotationMode}
727
763
  annotationColor={annotationColor}
728
764
  annotationLineWidth={annotationLineWidth}
765
+ stampText={stampText}
766
+ stampSize={stampSize}
729
767
  zoomLevel={zoomLevel}
730
768
  onAnnotationAdded={handleAnnotationAdded}
731
769
  onAnnotationRemoved={handleAnnotationRemoved}
@@ -322,6 +322,19 @@ export function drawAnnotationsOnCanvas(
322
322
  ctx.restore();
323
323
  break;
324
324
  }
325
+ case 'stamp': {
326
+ const bounds = annotation.bounds;
327
+ if (!bounds || !annotation.text) break;
328
+ const rect = pdfBoundsToCanvas(bounds, scale, pdfHeight);
329
+ ctx.save();
330
+ ctx.fillStyle = annotation.color;
331
+ ctx.font = `${rect.height * 0.7}px sans-serif`;
332
+ ctx.textAlign = 'center';
333
+ ctx.textBaseline = 'middle';
334
+ ctx.fillText(annotation.text, rect.x + rect.width / 2, rect.y + rect.height / 2);
335
+ ctx.restore();
336
+ break;
337
+ }
325
338
  }
326
339
  }
327
340