@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
|
@@ -124,4 +124,44 @@ final class ExpoPdfMarkupViewTests: XCTestCase {
|
|
|
124
124
|
|
|
125
125
|
XCTAssertEqual(view.pdfView.frame, view.bounds)
|
|
126
126
|
}
|
|
127
|
+
|
|
128
|
+
// MARK: - Ink mode
|
|
129
|
+
|
|
130
|
+
func testInkModeAddsOverlayAndDisablesPdfGestures() throws {
|
|
131
|
+
view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
|
|
132
|
+
|
|
133
|
+
// Add a known gesture so the disabled-gesture assertions are non-trivial.
|
|
134
|
+
let knownGesture = UITapGestureRecognizer()
|
|
135
|
+
view.pdfView.addGestureRecognizer(knownGesture)
|
|
136
|
+
|
|
137
|
+
view.setAnnotationMode("ink")
|
|
138
|
+
|
|
139
|
+
XCTAssertNotNil(view.inkOverlayView)
|
|
140
|
+
XCTAssertTrue(try view.subviews.contains(XCTUnwrap(view.inkOverlayView)))
|
|
141
|
+
XCTAssertTrue(view.disabledPdfGestures.contains(knownGesture))
|
|
142
|
+
XCTAssertFalse(knownGesture.isEnabled)
|
|
143
|
+
if let scrollView = view.pdfScrollView {
|
|
144
|
+
XCTAssertFalse(scrollView.isScrollEnabled)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
func testExitingInkModeRemovesOverlayAndRestoresGestures() {
|
|
149
|
+
view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
|
|
150
|
+
|
|
151
|
+
// Add a known gesture so we can verify it is actually re-enabled on exit.
|
|
152
|
+
let knownGesture = UITapGestureRecognizer()
|
|
153
|
+
view.pdfView.addGestureRecognizer(knownGesture)
|
|
154
|
+
|
|
155
|
+
view.setAnnotationMode("ink")
|
|
156
|
+
XCTAssertFalse(knownGesture.isEnabled)
|
|
157
|
+
|
|
158
|
+
view.setAnnotationMode("none")
|
|
159
|
+
|
|
160
|
+
XCTAssertNil(view.inkOverlayView)
|
|
161
|
+
XCTAssertTrue(view.disabledPdfGestures.isEmpty)
|
|
162
|
+
XCTAssertTrue(knownGesture.isEnabled)
|
|
163
|
+
if let scrollView = view.pdfScrollView {
|
|
164
|
+
XCTAssertTrue(scrollView.isScrollEnabled)
|
|
165
|
+
}
|
|
166
|
+
}
|
|
127
167
|
}
|
|
@@ -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.
|
|
3
|
+
"version": "1.1.0",
|
|
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": "
|
|
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.
|
|
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
|
|
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 (
|
|
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'
|
|
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
|
|