@tobyt/expo-pdf-markup 0.5.0 → 0.6.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.
- package/CHANGELOG.md +20 -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 +83 -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 +63 -33
- package/ios/ExpoPdfMarkupModule.swift +4 -0
- package/ios/ExpoPdfMarkupView.swift +279 -86
- package/ios/Tests/AnnotationSerializerTests.swift +18 -0
- 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
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @tobyt/expo-pdf-markup
|
|
2
2
|
|
|
3
|
+
## 0.6.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- e1a942b: Fix Android custom font rendering
|
|
8
|
+
|
|
9
|
+
## 0.6.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- fc961ab: Add support for a custom font for text markups
|
|
14
|
+
- 26bdae1: When in Move or Eraser mode, annotations are now surrounded by an outline to indicate the scope of the move/erasure
|
|
15
|
+
- 26bdae1: A Move tool was added for drag and drop repositioning of annotations
|
|
16
|
+
- 76f9b23: Text annotations can now be edited (breaking change, see example/docs)
|
|
17
|
+
|
|
18
|
+
### Patch Changes
|
|
19
|
+
|
|
20
|
+
- dae52fb: Reset currentSource in close
|
|
21
|
+
- 8146a57: Add padding to Android freeText annotation bounds to prevent text from being clipped when rendered
|
|
22
|
+
|
|
3
23
|
## 0.5.0
|
|
4
24
|
|
|
5
25
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -124,6 +124,13 @@ export default function App() {
|
|
|
124
124
|
}
|
|
125
125
|
```
|
|
126
126
|
|
|
127
|
+
### Custom text input UI
|
|
128
|
+
|
|
129
|
+
If you provide `onTextInputRequested`, the built-in native prompt is skipped and your callback is
|
|
130
|
+
used instead. The callback receives a request object with `mode`, `page`, `point`, and
|
|
131
|
+
`currentText` for edit flows, so you can prefill your own UI when the user taps an existing text
|
|
132
|
+
annotation while the text tool is active.
|
|
133
|
+
|
|
127
134
|
## Development
|
|
128
135
|
|
|
129
136
|
This module uses the [Expo Modules API](https://docs.expo.dev/modules/overview/). The `example` directory contains a test app.
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
package dev.terhoeven.expopdfmarkup
|
|
2
|
+
|
|
3
|
+
object AnnotationGeometry {
|
|
4
|
+
|
|
5
|
+
fun outlineBounds(annotation: AnnotationModel): AnnotationBounds? = when (annotation.type) {
|
|
6
|
+
"ink" -> outlineBoundsForInk(annotation)
|
|
7
|
+
"highlight", "underline", "freeText", "text" -> annotation.bounds
|
|
8
|
+
else -> null
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
fun clampTranslation(
|
|
12
|
+
annotation: AnnotationModel,
|
|
13
|
+
deltaX: Float,
|
|
14
|
+
deltaY: Float,
|
|
15
|
+
pageWidth: Float,
|
|
16
|
+
pageHeight: Float
|
|
17
|
+
): AnnotationPoint {
|
|
18
|
+
val bounds = outlineBounds(annotation) ?: return AnnotationPoint(deltaX, deltaY)
|
|
19
|
+
val minDx = -bounds.x
|
|
20
|
+
val maxDx = pageWidth - (bounds.x + bounds.width)
|
|
21
|
+
val minDy = -bounds.y
|
|
22
|
+
val maxDy = pageHeight - (bounds.y + bounds.height)
|
|
23
|
+
return AnnotationPoint(
|
|
24
|
+
x = deltaX.coerceIn(minDx, maxDx),
|
|
25
|
+
y = deltaY.coerceIn(minDy, maxDy)
|
|
26
|
+
)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
fun translate(annotation: AnnotationModel, deltaX: Float, deltaY: Float): AnnotationModel {
|
|
30
|
+
if (deltaX == 0f && deltaY == 0f) return annotation
|
|
31
|
+
return when (annotation.type) {
|
|
32
|
+
"ink" ->
|
|
33
|
+
annotation.copy(
|
|
34
|
+
paths =
|
|
35
|
+
annotation.paths?.map { stroke ->
|
|
36
|
+
stroke.map { point ->
|
|
37
|
+
AnnotationPoint(point.x + deltaX, point.y + deltaY)
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
"highlight", "underline", "freeText", "text" -> {
|
|
43
|
+
val bounds = annotation.bounds ?: return annotation
|
|
44
|
+
annotation.copy(
|
|
45
|
+
bounds =
|
|
46
|
+
AnnotationBounds(
|
|
47
|
+
x = bounds.x + deltaX,
|
|
48
|
+
y = bounds.y + deltaY,
|
|
49
|
+
width = bounds.width,
|
|
50
|
+
height = bounds.height
|
|
51
|
+
)
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
else -> annotation
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private fun outlineBoundsForInk(annotation: AnnotationModel): AnnotationBounds? {
|
|
60
|
+
val paths = annotation.paths ?: return null
|
|
61
|
+
var minX = Float.POSITIVE_INFINITY
|
|
62
|
+
var minY = Float.POSITIVE_INFINITY
|
|
63
|
+
var maxX = Float.NEGATIVE_INFINITY
|
|
64
|
+
var maxY = Float.NEGATIVE_INFINITY
|
|
65
|
+
|
|
66
|
+
for (stroke in paths) {
|
|
67
|
+
for (point in stroke) {
|
|
68
|
+
minX = minOf(minX, point.x)
|
|
69
|
+
minY = minOf(minY, point.y)
|
|
70
|
+
maxX = maxOf(maxX, point.x)
|
|
71
|
+
maxY = maxOf(maxY, point.y)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!minX.isFinite() || !minY.isFinite()) return null
|
|
76
|
+
|
|
77
|
+
val padding = maxOf(annotation.lineWidth ?: 2f, 10f)
|
|
78
|
+
return AnnotationBounds(
|
|
79
|
+
x = minX - padding,
|
|
80
|
+
y = minY - padding,
|
|
81
|
+
width = maxX - minX + padding * 2,
|
|
82
|
+
height = maxY - minY + padding * 2
|
|
83
|
+
)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
package dev.terhoeven.expopdfmarkup
|
|
2
2
|
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.content.res.AssetManager
|
|
3
5
|
import android.graphics.Canvas
|
|
6
|
+
import android.graphics.DashPathEffect
|
|
4
7
|
import android.graphics.Paint
|
|
5
8
|
import android.graphics.Path
|
|
9
|
+
import android.graphics.Typeface
|
|
6
10
|
|
|
7
11
|
object AnnotationRenderer {
|
|
8
12
|
|
|
@@ -12,7 +16,8 @@ object AnnotationRenderer {
|
|
|
12
16
|
pageIndex: Int,
|
|
13
17
|
pageYOffset: Float,
|
|
14
18
|
renderScale: Float,
|
|
15
|
-
pageHeight: Int
|
|
19
|
+
pageHeight: Int,
|
|
20
|
+
context: Context
|
|
16
21
|
) {
|
|
17
22
|
for (annotation in annotations) {
|
|
18
23
|
if (annotation.page != pageIndex) continue
|
|
@@ -40,12 +45,41 @@ object AnnotationRenderer {
|
|
|
40
45
|
annotation,
|
|
41
46
|
pageYOffset,
|
|
42
47
|
renderScale,
|
|
43
|
-
pageHeight
|
|
48
|
+
pageHeight,
|
|
49
|
+
context
|
|
44
50
|
)
|
|
45
51
|
}
|
|
46
52
|
}
|
|
47
53
|
}
|
|
48
54
|
|
|
55
|
+
fun drawEditingAffordances(
|
|
56
|
+
canvas: Canvas,
|
|
57
|
+
annotations: List<AnnotationModel>,
|
|
58
|
+
pageIndex: Int,
|
|
59
|
+
pageYOffset: Float,
|
|
60
|
+
renderScale: Float,
|
|
61
|
+
pageHeight: Int,
|
|
62
|
+
currentScale: Float
|
|
63
|
+
) {
|
|
64
|
+
val paint = Paint().apply {
|
|
65
|
+
color = 0xFF0A84FF.toInt()
|
|
66
|
+
strokeWidth = 2f / currentScale
|
|
67
|
+
style = Paint.Style.STROKE
|
|
68
|
+
isAntiAlias = true
|
|
69
|
+
pathEffect = DashPathEffect(floatArrayOf(10f / currentScale, 6f / currentScale), 0f)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
for (annotation in annotations) {
|
|
73
|
+
if (annotation.page != pageIndex) continue
|
|
74
|
+
val bounds = AnnotationGeometry.outlineBounds(annotation) ?: continue
|
|
75
|
+
val left = bounds.x * renderScale
|
|
76
|
+
val top = (pageHeight - bounds.y - bounds.height) * renderScale + pageYOffset
|
|
77
|
+
val right = left + bounds.width * renderScale
|
|
78
|
+
val bottom = top + bounds.height * renderScale
|
|
79
|
+
canvas.drawRect(left, top, right, bottom, paint)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
49
83
|
private fun drawInk(
|
|
50
84
|
canvas: Canvas,
|
|
51
85
|
annotation: AnnotationModel,
|
|
@@ -122,17 +156,63 @@ object AnnotationRenderer {
|
|
|
122
156
|
annotation: AnnotationModel,
|
|
123
157
|
pageYOffset: Float,
|
|
124
158
|
renderScale: Float,
|
|
125
|
-
pageHeight: Int
|
|
159
|
+
pageHeight: Int,
|
|
160
|
+
context: Context
|
|
126
161
|
) {
|
|
127
162
|
val bounds = annotation.bounds ?: return
|
|
128
163
|
val text = annotation.contents ?: return
|
|
129
164
|
val paint = Paint().apply {
|
|
130
165
|
color = AnnotationSerializer.colorFromHex(annotation.color)
|
|
131
166
|
textSize = (annotation.fontSize ?: 16f) * renderScale
|
|
167
|
+
typeface = resolveTypeface(context, annotation.fontFamily)
|
|
132
168
|
isAntiAlias = true
|
|
133
169
|
}
|
|
134
170
|
val left = bounds.x * renderScale
|
|
135
171
|
val top = (pageHeight - bounds.y - bounds.height) * renderScale + pageYOffset
|
|
136
172
|
canvas.drawText(text, left, top + paint.textSize, paint)
|
|
137
173
|
}
|
|
174
|
+
|
|
175
|
+
/** Resolve a typeface the same way React Native text does when available, so
|
|
176
|
+
* expo-font loaded families are measured and rendered consistently with the
|
|
177
|
+
* rest of the app. */
|
|
178
|
+
fun resolveTypeface(context: Context, fontFamily: String?): Typeface {
|
|
179
|
+
if (fontFamily == null) return Typeface.DEFAULT
|
|
180
|
+
return resolveReactTypeface(fontFamily, context.assets)
|
|
181
|
+
?: try {
|
|
182
|
+
Typeface.createFromAsset(context.assets, "fonts/$fontFamily.ttf")
|
|
183
|
+
} catch (_: Exception) {
|
|
184
|
+
Typeface.create(fontFamily, Typeface.NORMAL)
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private fun resolveReactTypeface(fontFamily: String, assetManager: AssetManager): Typeface? {
|
|
189
|
+
val managerClasses = listOf(
|
|
190
|
+
"com.facebook.react.common.assets.ReactFontManager",
|
|
191
|
+
"com.facebook.react.views.text.ReactFontManager"
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
for (className in managerClasses) {
|
|
195
|
+
try {
|
|
196
|
+
val managerClass = Class.forName(className)
|
|
197
|
+
val getInstance = managerClass.getMethod("getInstance")
|
|
198
|
+
val manager = getInstance.invoke(null)
|
|
199
|
+
val getTypeface = managerClass.getMethod(
|
|
200
|
+
"getTypeface",
|
|
201
|
+
String::class.java,
|
|
202
|
+
Int::class.javaPrimitiveType,
|
|
203
|
+
AssetManager::class.java
|
|
204
|
+
)
|
|
205
|
+
return getTypeface.invoke(
|
|
206
|
+
manager,
|
|
207
|
+
fontFamily,
|
|
208
|
+
Typeface.NORMAL,
|
|
209
|
+
assetManager
|
|
210
|
+
) as? Typeface
|
|
211
|
+
} catch (_: Exception) {
|
|
212
|
+
continue
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
return null
|
|
217
|
+
}
|
|
138
218
|
}
|
|
@@ -76,6 +76,7 @@ object AnnotationSerializer {
|
|
|
76
76
|
bounds = parseBounds(obj.optJSONObject("bounds")),
|
|
77
77
|
contents = if (obj.has("contents")) obj.getString("contents") else null,
|
|
78
78
|
fontSize = optFloat(obj, "fontSize"),
|
|
79
|
+
fontFamily = if (obj.has("fontFamily")) obj.getString("fontFamily") else null,
|
|
79
80
|
createdAt = if (obj.has("createdAt")) obj.optDouble("createdAt") else null
|
|
80
81
|
)
|
|
81
82
|
}
|
|
@@ -122,6 +123,7 @@ object AnnotationSerializer {
|
|
|
122
123
|
model.bounds?.let { obj.put("bounds", boundsToJson(it)) }
|
|
123
124
|
model.contents?.let { obj.put("contents", it) }
|
|
124
125
|
model.fontSize?.let { obj.put("fontSize", it.toDouble()) }
|
|
126
|
+
model.fontFamily?.let { obj.put("fontFamily", it) }
|
|
125
127
|
model.createdAt?.let { obj.put("createdAt", it) }
|
|
126
128
|
return obj
|
|
127
129
|
}
|
|
@@ -49,8 +49,27 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
49
49
|
invalidate()
|
|
50
50
|
}
|
|
51
51
|
var annotationMode: String = "none"
|
|
52
|
+
set(value) {
|
|
53
|
+
if (field == value) return
|
|
54
|
+
field = value
|
|
55
|
+
if (value != "ink") {
|
|
56
|
+
isDrawingInk = false
|
|
57
|
+
currentInkPoints.clear()
|
|
58
|
+
inkScreenPath.reset()
|
|
59
|
+
}
|
|
60
|
+
if (value != "highlight" && value != "underline") {
|
|
61
|
+
isDragging = false
|
|
62
|
+
dragStartPoint = null
|
|
63
|
+
dragCurrentPoint = null
|
|
64
|
+
}
|
|
65
|
+
if (value != "move") {
|
|
66
|
+
clearMovePreview()
|
|
67
|
+
}
|
|
68
|
+
invalidate()
|
|
69
|
+
}
|
|
52
70
|
var annotationColor: String = "#000000"
|
|
53
71
|
var annotationLineWidth: Float = 2f
|
|
72
|
+
var annotationFontFamily: String? = null
|
|
54
73
|
var onAnnotationsChangedListener: (() -> Unit)? = null
|
|
55
74
|
|
|
56
75
|
// Ink drawing state
|
|
@@ -63,8 +82,15 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
63
82
|
private var dragCurrentPoint: AnnotationPoint? = null
|
|
64
83
|
private var isDragging = false
|
|
65
84
|
|
|
85
|
+
// Move mode state
|
|
86
|
+
private var movingAnnotation: AnnotationModel? = null
|
|
87
|
+
private var movingPreviewAnnotation: AnnotationModel? = null
|
|
88
|
+
private var moveStartPdfPoint: AnnotationPoint? = null
|
|
89
|
+
|
|
66
90
|
// Text mode callback
|
|
67
|
-
var onTextInputRequested: (
|
|
91
|
+
var onTextInputRequested: (
|
|
92
|
+
(page: Int, point: AnnotationPoint, annotation: AnnotationModel?) -> Unit
|
|
93
|
+
)? = null
|
|
68
94
|
|
|
69
95
|
fun loadPages(renderer: PdfRenderer, viewWidth: Int) {
|
|
70
96
|
recycle()
|
|
@@ -113,6 +139,21 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
113
139
|
super.onDraw(canvas)
|
|
114
140
|
if (pageBitmaps.isEmpty()) return
|
|
115
141
|
|
|
142
|
+
val renderedAnnotations =
|
|
143
|
+
if (movingAnnotation != null && movingPreviewAnnotation != null) {
|
|
144
|
+
annotations.map { annotation ->
|
|
145
|
+
if (annotation.id ==
|
|
146
|
+
movingAnnotation!!.id
|
|
147
|
+
) {
|
|
148
|
+
movingPreviewAnnotation!!
|
|
149
|
+
} else {
|
|
150
|
+
annotation
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
annotations
|
|
155
|
+
}
|
|
156
|
+
|
|
116
157
|
canvas.save()
|
|
117
158
|
canvas.translate(-panX, -panY)
|
|
118
159
|
canvas.scale(currentScale, currentScale)
|
|
@@ -128,12 +169,24 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
128
169
|
canvas.drawBitmap(pageBitmaps[i], 0f, pageYOffsets[i], null)
|
|
129
170
|
AnnotationRenderer.drawAnnotations(
|
|
130
171
|
canvas,
|
|
131
|
-
|
|
172
|
+
renderedAnnotations,
|
|
132
173
|
i,
|
|
133
174
|
pageYOffsets[i],
|
|
134
175
|
renderScales[i],
|
|
135
|
-
pageHeights[i]
|
|
176
|
+
pageHeights[i],
|
|
177
|
+
context
|
|
136
178
|
)
|
|
179
|
+
if (annotationMode == "move" || annotationMode == "eraser") {
|
|
180
|
+
AnnotationRenderer.drawEditingAffordances(
|
|
181
|
+
canvas,
|
|
182
|
+
renderedAnnotations,
|
|
183
|
+
i,
|
|
184
|
+
pageYOffsets[i],
|
|
185
|
+
renderScales[i],
|
|
186
|
+
pageHeights[i],
|
|
187
|
+
currentScale
|
|
188
|
+
)
|
|
189
|
+
}
|
|
137
190
|
}
|
|
138
191
|
}
|
|
139
192
|
|
|
@@ -205,6 +258,13 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
205
258
|
return true
|
|
206
259
|
}
|
|
207
260
|
|
|
261
|
+
"move" -> {
|
|
262
|
+
if (event.pointerCount == 1) {
|
|
263
|
+
handleMoveTouch(event)
|
|
264
|
+
return true
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
|
|
208
268
|
"text" -> {
|
|
209
269
|
if (event.action == MotionEvent.ACTION_UP && event.pointerCount == 1) {
|
|
210
270
|
handleTextTap(event)
|
|
@@ -369,57 +429,132 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
369
429
|
// --- Eraser mode ---
|
|
370
430
|
|
|
371
431
|
private fun handleEraserTap(event: MotionEvent) {
|
|
372
|
-
val
|
|
373
|
-
val
|
|
374
|
-
val pageIndex = pageIndexAtContentY(contentY)
|
|
375
|
-
if (pageIndex < 0) return
|
|
376
|
-
|
|
377
|
-
val localY = contentY - pageYOffsets[pageIndex]
|
|
378
|
-
val pdfX = contentX / renderScales[pageIndex]
|
|
379
|
-
val pdfY = pageHeights[pageIndex] - (localY / renderScales[pageIndex])
|
|
380
|
-
|
|
381
|
-
val hit = AnnotationHitTester.hitTest(
|
|
382
|
-
AnnotationPoint(pdfX, pdfY),
|
|
383
|
-
annotations,
|
|
384
|
-
pageIndex
|
|
385
|
-
)
|
|
432
|
+
val pdfTouch = screenToPdfPoint(event.x, event.y) ?: return
|
|
433
|
+
val hit = AnnotationHitTester.hitTest(pdfTouch.point, annotations, pdfTouch.pageIndex)
|
|
386
434
|
if (hit != null) {
|
|
387
435
|
annotations = annotations.filter { it.id != hit.id }
|
|
388
436
|
onAnnotationsChangedListener?.invoke()
|
|
389
437
|
}
|
|
390
438
|
}
|
|
391
439
|
|
|
440
|
+
private fun handleMoveTouch(event: MotionEvent) {
|
|
441
|
+
when (event.action) {
|
|
442
|
+
MotionEvent.ACTION_DOWN -> {
|
|
443
|
+
val pdfTouch = screenToPdfPoint(event.x, event.y) ?: return
|
|
444
|
+
val hit = AnnotationHitTester.hitTest(
|
|
445
|
+
pdfTouch.point,
|
|
446
|
+
annotations,
|
|
447
|
+
pdfTouch.pageIndex
|
|
448
|
+
)
|
|
449
|
+
if (hit != null) {
|
|
450
|
+
movingAnnotation = hit
|
|
451
|
+
movingPreviewAnnotation = hit
|
|
452
|
+
moveStartPdfPoint = pdfTouch.point
|
|
453
|
+
parent?.requestDisallowInterceptTouchEvent(true)
|
|
454
|
+
invalidate()
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
MotionEvent.ACTION_MOVE -> {
|
|
459
|
+
val annotation = movingAnnotation ?: return
|
|
460
|
+
val startPoint = moveStartPdfPoint ?: return
|
|
461
|
+
val pdfTouch = screenToPdfPoint(event.x, event.y, annotation.page) ?: return
|
|
462
|
+
val delta = AnnotationGeometry.clampTranslation(
|
|
463
|
+
annotation,
|
|
464
|
+
pdfTouch.point.x - startPoint.x,
|
|
465
|
+
pdfTouch.point.y - startPoint.y,
|
|
466
|
+
pageWidths[annotation.page].toFloat(),
|
|
467
|
+
pageHeights[annotation.page].toFloat()
|
|
468
|
+
)
|
|
469
|
+
movingPreviewAnnotation = AnnotationGeometry.translate(annotation, delta.x, delta.y)
|
|
470
|
+
invalidate()
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
|
474
|
+
val annotation = movingAnnotation
|
|
475
|
+
val preview = movingPreviewAnnotation
|
|
476
|
+
if (annotation != null && preview != null && preview != annotation) {
|
|
477
|
+
annotations = annotations.map { existing ->
|
|
478
|
+
if (existing.id == annotation.id) preview else existing
|
|
479
|
+
}
|
|
480
|
+
onAnnotationsChangedListener?.invoke()
|
|
481
|
+
}
|
|
482
|
+
clearMovePreview()
|
|
483
|
+
invalidate()
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
392
488
|
// --- Text mode ---
|
|
393
489
|
|
|
394
490
|
private fun handleTextTap(event: MotionEvent) {
|
|
395
|
-
val
|
|
396
|
-
val
|
|
397
|
-
val
|
|
398
|
-
if (
|
|
491
|
+
val pdfTouch = screenToPdfPoint(event.x, event.y) ?: return
|
|
492
|
+
val point = pdfTouch.point
|
|
493
|
+
val hit = AnnotationHitTester.hitTest(point, annotations, pdfTouch.pageIndex)
|
|
494
|
+
val annotation = if (hit?.type == "text" || hit?.type == "freeText") hit else null
|
|
495
|
+
onTextInputRequested?.invoke(pdfTouch.pageIndex, point, annotation)
|
|
496
|
+
}
|
|
399
497
|
|
|
400
|
-
|
|
401
|
-
val
|
|
402
|
-
|
|
498
|
+
fun addTextAnnotation(page: Int, point: AnnotationPoint, text: String) {
|
|
499
|
+
val annotation = buildTextAnnotation(text = text, page = page, point = point)
|
|
500
|
+
annotations = annotations + annotation
|
|
501
|
+
onAnnotationsChangedListener?.invoke()
|
|
502
|
+
}
|
|
403
503
|
|
|
404
|
-
|
|
504
|
+
fun updateTextAnnotation(annotationId: String, text: String) {
|
|
505
|
+
val existing = annotations.find { it.id == annotationId } ?: return
|
|
506
|
+
val updated = buildTextAnnotation(text = text, page = existing.page, existing = existing)
|
|
507
|
+
annotations = annotations.map { annotation ->
|
|
508
|
+
if (annotation.id == annotationId) updated else annotation
|
|
509
|
+
}
|
|
510
|
+
onAnnotationsChangedListener?.invoke()
|
|
405
511
|
}
|
|
406
512
|
|
|
407
|
-
fun
|
|
408
|
-
|
|
409
|
-
|
|
513
|
+
private fun buildTextAnnotation(
|
|
514
|
+
text: String,
|
|
515
|
+
page: Int,
|
|
516
|
+
point: AnnotationPoint? = null,
|
|
517
|
+
existing: AnnotationModel? = null
|
|
518
|
+
): AnnotationModel {
|
|
519
|
+
val fontSize = existing?.fontSize ?: 16f
|
|
520
|
+
val padding = 4f
|
|
521
|
+
val paint = Paint().apply {
|
|
522
|
+
textSize = fontSize
|
|
523
|
+
typeface = AnnotationRenderer.resolveTypeface(
|
|
524
|
+
context,
|
|
525
|
+
existing?.fontFamily ?: annotationFontFamily
|
|
526
|
+
)
|
|
527
|
+
}
|
|
410
528
|
val textWidth = paint.measureText(text)
|
|
411
|
-
val
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
529
|
+
val height = fontSize * 1.2f + padding * 2
|
|
530
|
+
val bounds = if (existing != null && existing.bounds != null) {
|
|
531
|
+
AnnotationBounds(
|
|
532
|
+
existing.bounds.x,
|
|
533
|
+
existing.bounds.y + existing.bounds.height - height,
|
|
534
|
+
textWidth + padding * 2,
|
|
535
|
+
height
|
|
536
|
+
)
|
|
537
|
+
} else {
|
|
538
|
+
val anchorPoint = requireNotNull(point)
|
|
539
|
+
AnnotationBounds(
|
|
540
|
+
anchorPoint.x,
|
|
541
|
+
anchorPoint.y - fontSize - padding,
|
|
542
|
+
textWidth + padding * 2,
|
|
543
|
+
height
|
|
544
|
+
)
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
return AnnotationModel(
|
|
548
|
+
id = existing?.id ?: UUID.randomUUID().toString(),
|
|
549
|
+
type = existing?.type ?: "freeText",
|
|
550
|
+
page = existing?.page ?: page,
|
|
551
|
+
color = existing?.color ?: annotationColor,
|
|
552
|
+
bounds = bounds,
|
|
417
553
|
contents = text,
|
|
418
554
|
fontSize = fontSize,
|
|
419
|
-
|
|
555
|
+
fontFamily = existing?.fontFamily ?: annotationFontFamily,
|
|
556
|
+
createdAt = existing?.createdAt ?: System.currentTimeMillis() / 1000.0
|
|
420
557
|
)
|
|
421
|
-
annotations = annotations + annotation
|
|
422
|
-
onAnnotationsChangedListener?.invoke()
|
|
423
558
|
}
|
|
424
559
|
|
|
425
560
|
// --- Helpers ---
|
|
@@ -431,6 +566,30 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
431
566
|
return 0
|
|
432
567
|
}
|
|
433
568
|
|
|
569
|
+
private data class PdfTouchPoint(val pageIndex: Int, val point: AnnotationPoint)
|
|
570
|
+
|
|
571
|
+
private fun clearMovePreview() {
|
|
572
|
+
movingAnnotation = null
|
|
573
|
+
movingPreviewAnnotation = null
|
|
574
|
+
moveStartPdfPoint = null
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
private fun screenToPdfPoint(
|
|
578
|
+
screenX: Float,
|
|
579
|
+
screenY: Float,
|
|
580
|
+
forcedPageIndex: Int? = null
|
|
581
|
+
): PdfTouchPoint? {
|
|
582
|
+
val contentX = (screenX + panX) / currentScale
|
|
583
|
+
val contentY = (screenY + panY) / currentScale
|
|
584
|
+
val pageIndex = forcedPageIndex ?: pageIndexAtContentY(contentY)
|
|
585
|
+
if (pageIndex < 0 || pageIndex >= pageYOffsets.size) return null
|
|
586
|
+
|
|
587
|
+
val localY = contentY - pageYOffsets[pageIndex]
|
|
588
|
+
val pdfX = contentX / renderScales[pageIndex]
|
|
589
|
+
val pdfY = pageHeights[pageIndex] - (localY / renderScales[pageIndex])
|
|
590
|
+
return PdfTouchPoint(pageIndex, AnnotationPoint(pdfX, pdfY))
|
|
591
|
+
}
|
|
592
|
+
|
|
434
593
|
override fun computeScroll() {
|
|
435
594
|
if (scroller.computeScrollOffset()) {
|
|
436
595
|
panX = scroller.currX.toFloat()
|
|
@@ -481,6 +640,13 @@ class ContinuousPdfView(context: Context) : View(context) {
|
|
|
481
640
|
pageWidths.clear()
|
|
482
641
|
pageHeights.clear()
|
|
483
642
|
renderScales.clear()
|
|
643
|
+
currentInkPoints.clear()
|
|
644
|
+
inkScreenPath.reset()
|
|
645
|
+
dragStartPoint = null
|
|
646
|
+
dragCurrentPoint = null
|
|
647
|
+
isDragging = false
|
|
648
|
+
isDrawingInk = false
|
|
649
|
+
clearMovePreview()
|
|
484
650
|
totalHeight = 0f
|
|
485
651
|
lastNotifiedPage = -1
|
|
486
652
|
}
|
|
@@ -80,6 +80,10 @@ class ExpoPdfMarkupModule : Module() {
|
|
|
80
80
|
view.setAnnotationLineWidth(width)
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
Prop("annotationFontFamily") { view: ExpoPdfMarkupView, font: String? ->
|
|
84
|
+
view.setAnnotationFontFamily(font)
|
|
85
|
+
}
|
|
86
|
+
|
|
83
87
|
Prop("useJsTextDialog") { view: ExpoPdfMarkupView, use: Boolean? ->
|
|
84
88
|
view.useJsTextDialog = use ?: false
|
|
85
89
|
}
|