@tobyt/expo-pdf-markup 0.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/LICENSE +21 -0
- package/README.md +54 -0
- package/android/.editorconfig +2 -0
- package/android/build.gradle +30 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationHitTester.kt +76 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationModel.kt +27 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationRenderer.kt +138 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/AnnotationSerializer.kt +152 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ContinuousPdfView.kt +539 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupModule.kt +81 -0
- package/android/src/main/java/dev/terhoeven/expopdfmarkup/ExpoPdfMarkupView.kt +147 -0
- package/build/ExpoPdfMarkup.types.d.ts +96 -0
- package/build/ExpoPdfMarkup.types.d.ts.map +1 -0
- package/build/ExpoPdfMarkup.types.js +2 -0
- package/build/ExpoPdfMarkup.types.js.map +1 -0
- package/build/ExpoPdfMarkupModule.d.ts +2 -0
- package/build/ExpoPdfMarkupModule.d.ts.map +1 -0
- package/build/ExpoPdfMarkupModule.js +4 -0
- package/build/ExpoPdfMarkupModule.js.map +1 -0
- package/build/ExpoPdfMarkupModule.web.d.ts +2 -0
- package/build/ExpoPdfMarkupModule.web.d.ts.map +1 -0
- package/build/ExpoPdfMarkupModule.web.js +4 -0
- package/build/ExpoPdfMarkupModule.web.js.map +1 -0
- package/build/ExpoPdfMarkupView.d.ts +4 -0
- package/build/ExpoPdfMarkupView.d.ts.map +1 -0
- package/build/ExpoPdfMarkupView.js +7 -0
- package/build/ExpoPdfMarkupView.js.map +1 -0
- package/build/ExpoPdfMarkupView.web.d.ts +4 -0
- package/build/ExpoPdfMarkupView.web.d.ts.map +1 -0
- package/build/ExpoPdfMarkupView.web.js +8 -0
- package/build/ExpoPdfMarkupView.web.js.map +1 -0
- package/build/index.d.ts +3 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +3 -0
- package/build/index.js.map +1 -0
- package/expo-module.config.json +9 -0
- package/ios/AnnotationModels.swift +73 -0
- package/ios/AnnotationSerializer.swift +252 -0
- package/ios/ExpoPdfMarkup.podspec +28 -0
- package/ios/ExpoPdfMarkupModule.swift +39 -0
- package/ios/ExpoPdfMarkupView.swift +541 -0
- package/package.json +67 -0
- package/src/ExpoPdfMarkup.types.ts +80 -0
- package/src/ExpoPdfMarkupModule.ts +2 -0
- package/src/ExpoPdfMarkupModule.web.ts +2 -0
- package/src/ExpoPdfMarkupView.tsx +10 -0
- package/src/ExpoPdfMarkupView.web.tsx +12 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
package dev.terhoeven.expopdfmarkup
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.graphics.Bitmap
|
|
5
|
+
import android.graphics.Canvas
|
|
6
|
+
import android.graphics.Color
|
|
7
|
+
import android.graphics.Matrix
|
|
8
|
+
import android.graphics.Paint
|
|
9
|
+
import android.graphics.Path
|
|
10
|
+
import android.graphics.pdf.PdfRenderer
|
|
11
|
+
import android.view.GestureDetector
|
|
12
|
+
import android.view.MotionEvent
|
|
13
|
+
import android.view.ScaleGestureDetector
|
|
14
|
+
import android.view.View
|
|
15
|
+
import android.widget.OverScroller
|
|
16
|
+
import java.util.UUID
|
|
17
|
+
|
|
18
|
+
class ContinuousPdfView(context: Context) : View(context) {
|
|
19
|
+
private val pageBitmaps = mutableListOf<Bitmap>()
|
|
20
|
+
private val pageYOffsets = mutableListOf<Float>()
|
|
21
|
+
private val pageWidths = mutableListOf<Int>()
|
|
22
|
+
private val pageHeights = mutableListOf<Int>()
|
|
23
|
+
private val renderScales = mutableListOf<Float>()
|
|
24
|
+
private var totalHeight = 0f
|
|
25
|
+
private var contentWidth = 0f
|
|
26
|
+
private val pageGap = (8 * resources.displayMetrics.density).toInt()
|
|
27
|
+
var pageBackgroundColor = Color.rgb(235, 235, 235)
|
|
28
|
+
|
|
29
|
+
private var currentScale = 1f
|
|
30
|
+
private var minScale = 1f
|
|
31
|
+
private var maxScale = 5f
|
|
32
|
+
private var panX = 0f
|
|
33
|
+
private var panY = 0f
|
|
34
|
+
|
|
35
|
+
private var lastNotifiedPage = -1
|
|
36
|
+
|
|
37
|
+
private val scroller = OverScroller(context)
|
|
38
|
+
private val scaleDetector = ScaleGestureDetector(context, ScaleListener())
|
|
39
|
+
private val gestureDetector = GestureDetector(context, GestureListener())
|
|
40
|
+
|
|
41
|
+
var onPageChangeListener: ((page: Int, pageCount: Int) -> Unit)? = null
|
|
42
|
+
|
|
43
|
+
// Annotation state
|
|
44
|
+
var annotations: List<AnnotationModel> = emptyList()
|
|
45
|
+
set(value) {
|
|
46
|
+
field = value
|
|
47
|
+
invalidate()
|
|
48
|
+
}
|
|
49
|
+
var annotationMode: String = "none"
|
|
50
|
+
var annotationColor: String = "#000000"
|
|
51
|
+
var annotationLineWidth: Float = 2f
|
|
52
|
+
var onAnnotationsChangedListener: (() -> Unit)? = null
|
|
53
|
+
|
|
54
|
+
// Ink drawing state
|
|
55
|
+
private var currentInkPoints = mutableListOf<AnnotationPoint>()
|
|
56
|
+
private var isDrawingInk = false
|
|
57
|
+
private var inkScreenPath = Path()
|
|
58
|
+
|
|
59
|
+
// Highlight/underline drag state
|
|
60
|
+
private var dragStartPoint: AnnotationPoint? = null
|
|
61
|
+
private var dragCurrentPoint: AnnotationPoint? = null
|
|
62
|
+
private var isDragging = false
|
|
63
|
+
|
|
64
|
+
// Text mode callback
|
|
65
|
+
var onTextInputRequested: ((page: Int, point: AnnotationPoint) -> Unit)? = null
|
|
66
|
+
|
|
67
|
+
fun loadPages(renderer: PdfRenderer, viewWidth: Int) {
|
|
68
|
+
recycle()
|
|
69
|
+
if (viewWidth <= 0) return
|
|
70
|
+
setBackgroundColor(pageBackgroundColor)
|
|
71
|
+
|
|
72
|
+
var yOffset = 0f
|
|
73
|
+
for (i in 0 until renderer.pageCount) {
|
|
74
|
+
if (i > 0) yOffset += pageGap
|
|
75
|
+
|
|
76
|
+
val page = renderer.openPage(i)
|
|
77
|
+
val pdfW = page.width
|
|
78
|
+
val pdfH = page.height
|
|
79
|
+
val scale = viewWidth.toFloat() / pdfW.toFloat()
|
|
80
|
+
val bmpH = (pdfH * scale).toInt()
|
|
81
|
+
|
|
82
|
+
val bmp = Bitmap.createBitmap(viewWidth, bmpH, Bitmap.Config.ARGB_8888)
|
|
83
|
+
Canvas(bmp).drawColor(Color.WHITE)
|
|
84
|
+
|
|
85
|
+
val m = Matrix()
|
|
86
|
+
m.setScale(scale, scale)
|
|
87
|
+
page.render(bmp, null, m, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY)
|
|
88
|
+
page.close()
|
|
89
|
+
|
|
90
|
+
pageYOffsets.add(yOffset)
|
|
91
|
+
pageBitmaps.add(bmp)
|
|
92
|
+
pageWidths.add(pdfW)
|
|
93
|
+
pageHeights.add(pdfH)
|
|
94
|
+
renderScales.add(scale)
|
|
95
|
+
yOffset += bmpH
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
totalHeight = yOffset
|
|
99
|
+
contentWidth = viewWidth.toFloat()
|
|
100
|
+
currentScale = 1f
|
|
101
|
+
minScale = 1f
|
|
102
|
+
maxScale = 5f
|
|
103
|
+
panX = 0f
|
|
104
|
+
panY = 0f
|
|
105
|
+
lastNotifiedPage = -1
|
|
106
|
+
invalidate()
|
|
107
|
+
notifyPageChange()
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
override fun onDraw(canvas: Canvas) {
|
|
111
|
+
super.onDraw(canvas)
|
|
112
|
+
if (pageBitmaps.isEmpty()) return
|
|
113
|
+
|
|
114
|
+
canvas.save()
|
|
115
|
+
canvas.translate(-panX, -panY)
|
|
116
|
+
canvas.scale(currentScale, currentScale)
|
|
117
|
+
|
|
118
|
+
for (i in pageBitmaps.indices) {
|
|
119
|
+
val pageTop = pageYOffsets[i] * currentScale
|
|
120
|
+
val pageBottom = pageTop + pageBitmaps[i].height * currentScale
|
|
121
|
+
val visibleTop = panY
|
|
122
|
+
val visibleBottom = panY + height
|
|
123
|
+
|
|
124
|
+
// Only draw pages that are visible
|
|
125
|
+
if (pageBottom >= visibleTop && pageTop <= visibleBottom) {
|
|
126
|
+
canvas.drawBitmap(pageBitmaps[i], 0f, pageYOffsets[i], null)
|
|
127
|
+
AnnotationRenderer.drawAnnotations(
|
|
128
|
+
canvas,
|
|
129
|
+
annotations,
|
|
130
|
+
i,
|
|
131
|
+
pageYOffsets[i],
|
|
132
|
+
renderScales[i],
|
|
133
|
+
pageHeights[i]
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
canvas.restore()
|
|
139
|
+
|
|
140
|
+
// Draw live ink preview (in screen coordinates)
|
|
141
|
+
if (isDrawingInk && currentInkPoints.size > 1) {
|
|
142
|
+
val paint = Paint().apply {
|
|
143
|
+
color = AnnotationSerializer.colorFromHex(annotationColor)
|
|
144
|
+
strokeWidth = annotationLineWidth * currentScale
|
|
145
|
+
style = Paint.Style.STROKE
|
|
146
|
+
isAntiAlias = true
|
|
147
|
+
strokeCap = Paint.Cap.ROUND
|
|
148
|
+
strokeJoin = Paint.Join.ROUND
|
|
149
|
+
}
|
|
150
|
+
canvas.drawPath(inkScreenPath, paint)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// Draw drag preview for highlight/underline
|
|
154
|
+
if (isDragging && dragStartPoint != null && dragCurrentPoint != null) {
|
|
155
|
+
val startSP = dragStartPoint!!
|
|
156
|
+
val curSP = dragCurrentPoint!!
|
|
157
|
+
val paint = Paint().apply {
|
|
158
|
+
color = AnnotationSerializer.colorFromHex(annotationColor)
|
|
159
|
+
alpha = if (annotationMode == "highlight") 80 else 255
|
|
160
|
+
style = if (annotationMode == "highlight") Paint.Style.FILL else Paint.Style.STROKE
|
|
161
|
+
strokeWidth = if (annotationMode == "underline") 1f * currentScale else 0f
|
|
162
|
+
isAntiAlias = true
|
|
163
|
+
}
|
|
164
|
+
val left = minOf(startSP.x, curSP.x)
|
|
165
|
+
val top = minOf(startSP.y, curSP.y)
|
|
166
|
+
val right = maxOf(startSP.x, curSP.x)
|
|
167
|
+
val bottom = maxOf(startSP.y, curSP.y)
|
|
168
|
+
if (annotationMode == "underline") {
|
|
169
|
+
canvas.drawLine(left, bottom, right, bottom, paint)
|
|
170
|
+
} else {
|
|
171
|
+
canvas.drawRect(left, top, right, bottom, paint)
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
override fun onTouchEvent(event: MotionEvent): Boolean {
|
|
177
|
+
// Always let scale detector handle pinch zoom
|
|
178
|
+
scaleDetector.onTouchEvent(event)
|
|
179
|
+
|
|
180
|
+
// If scaling with two fingers, don't process as annotation gesture
|
|
181
|
+
if (scaleDetector.isInProgress) return true
|
|
182
|
+
|
|
183
|
+
when (annotationMode) {
|
|
184
|
+
"ink" -> {
|
|
185
|
+
if (event.pointerCount == 1) {
|
|
186
|
+
handleInkTouch(event)
|
|
187
|
+
return true
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
"highlight", "underline" -> {
|
|
192
|
+
if (event.pointerCount == 1) {
|
|
193
|
+
handleDragTouch(event)
|
|
194
|
+
return true
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
"eraser" -> {
|
|
199
|
+
if (event.action == MotionEvent.ACTION_UP && event.pointerCount == 1) {
|
|
200
|
+
handleEraserTap(event)
|
|
201
|
+
}
|
|
202
|
+
gestureDetector.onTouchEvent(event)
|
|
203
|
+
return true
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
"text" -> {
|
|
207
|
+
if (event.action == MotionEvent.ACTION_UP && event.pointerCount == 1) {
|
|
208
|
+
handleTextTap(event)
|
|
209
|
+
}
|
|
210
|
+
gestureDetector.onTouchEvent(event)
|
|
211
|
+
return true
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
else -> {
|
|
215
|
+
// "none" mode — normal scroll/zoom
|
|
216
|
+
gestureDetector.onTouchEvent(event)
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
parent?.requestDisallowInterceptTouchEvent(true)
|
|
221
|
+
return true
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// --- Ink mode ---
|
|
225
|
+
|
|
226
|
+
private fun handleInkTouch(event: MotionEvent) {
|
|
227
|
+
when (event.action) {
|
|
228
|
+
MotionEvent.ACTION_DOWN -> {
|
|
229
|
+
isDrawingInk = true
|
|
230
|
+
currentInkPoints.clear()
|
|
231
|
+
inkScreenPath.reset()
|
|
232
|
+
val pt = AnnotationPoint(event.x, event.y)
|
|
233
|
+
currentInkPoints.add(pt)
|
|
234
|
+
inkScreenPath.moveTo(event.x, event.y)
|
|
235
|
+
parent?.requestDisallowInterceptTouchEvent(true)
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
MotionEvent.ACTION_MOVE -> {
|
|
239
|
+
if (isDrawingInk) {
|
|
240
|
+
val pt = AnnotationPoint(event.x, event.y)
|
|
241
|
+
currentInkPoints.add(pt)
|
|
242
|
+
inkScreenPath.lineTo(event.x, event.y)
|
|
243
|
+
invalidate()
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
|
248
|
+
if (isDrawingInk && currentInkPoints.size >= 1) {
|
|
249
|
+
finishInkStroke()
|
|
250
|
+
}
|
|
251
|
+
isDrawingInk = false
|
|
252
|
+
inkScreenPath.reset()
|
|
253
|
+
invalidate()
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
private fun finishInkStroke() {
|
|
259
|
+
// Convert screen points to PDF page coordinates
|
|
260
|
+
val pdfPoints = mutableListOf<AnnotationPoint>()
|
|
261
|
+
var pageIndex = -1
|
|
262
|
+
|
|
263
|
+
for (screenPt in currentInkPoints) {
|
|
264
|
+
val contentX = (screenPt.x + panX) / currentScale
|
|
265
|
+
val contentY = (screenPt.y + panY) / currentScale
|
|
266
|
+
val pi = pageIndexAtContentY(contentY)
|
|
267
|
+
if (pageIndex == -1) pageIndex = pi
|
|
268
|
+
if (pi != pageIndex || pi < 0) continue
|
|
269
|
+
|
|
270
|
+
val localY = contentY - pageYOffsets[pi]
|
|
271
|
+
val pdfX = contentX / renderScales[pi]
|
|
272
|
+
val pdfY = pageHeights[pi] - (localY / renderScales[pi])
|
|
273
|
+
pdfPoints.add(AnnotationPoint(pdfX, pdfY))
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (pdfPoints.isEmpty() || pageIndex < 0) return
|
|
277
|
+
|
|
278
|
+
val annotation = AnnotationModel(
|
|
279
|
+
id = UUID.randomUUID().toString(),
|
|
280
|
+
type = "ink",
|
|
281
|
+
page = pageIndex,
|
|
282
|
+
color = annotationColor,
|
|
283
|
+
lineWidth = annotationLineWidth,
|
|
284
|
+
paths = listOf(pdfPoints),
|
|
285
|
+
createdAt = System.currentTimeMillis() / 1000.0
|
|
286
|
+
)
|
|
287
|
+
annotations = annotations + annotation
|
|
288
|
+
onAnnotationsChangedListener?.invoke()
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// --- Highlight/Underline drag mode ---
|
|
292
|
+
|
|
293
|
+
private fun handleDragTouch(event: MotionEvent) {
|
|
294
|
+
when (event.action) {
|
|
295
|
+
MotionEvent.ACTION_DOWN -> {
|
|
296
|
+
isDragging = true
|
|
297
|
+
dragStartPoint = AnnotationPoint(event.x, event.y)
|
|
298
|
+
dragCurrentPoint = dragStartPoint
|
|
299
|
+
parent?.requestDisallowInterceptTouchEvent(true)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
MotionEvent.ACTION_MOVE -> {
|
|
303
|
+
if (isDragging) {
|
|
304
|
+
dragCurrentPoint = AnnotationPoint(event.x, event.y)
|
|
305
|
+
invalidate()
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
|
310
|
+
if (isDragging && dragStartPoint != null && dragCurrentPoint != null) {
|
|
311
|
+
finishDragAnnotation()
|
|
312
|
+
}
|
|
313
|
+
isDragging = false
|
|
314
|
+
dragStartPoint = null
|
|
315
|
+
dragCurrentPoint = null
|
|
316
|
+
invalidate()
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
private fun finishDragAnnotation() {
|
|
322
|
+
val start = dragStartPoint ?: return
|
|
323
|
+
val end = dragCurrentPoint ?: return
|
|
324
|
+
|
|
325
|
+
// Convert screen points to content space
|
|
326
|
+
val startCX = (start.x + panX) / currentScale
|
|
327
|
+
val startCY = (start.y + panY) / currentScale
|
|
328
|
+
val endCX = (end.x + panX) / currentScale
|
|
329
|
+
val endCY = (end.y + panY) / currentScale
|
|
330
|
+
|
|
331
|
+
val pageIndex = pageIndexAtContentY(startCY)
|
|
332
|
+
if (pageIndex < 0) return
|
|
333
|
+
|
|
334
|
+
val rs = renderScales[pageIndex]
|
|
335
|
+
val ph = pageHeights[pageIndex]
|
|
336
|
+
val localStartY = startCY - pageYOffsets[pageIndex]
|
|
337
|
+
val localEndY = endCY - pageYOffsets[pageIndex]
|
|
338
|
+
|
|
339
|
+
// Convert to PDF coordinates
|
|
340
|
+
val pdfX1 = startCX / rs
|
|
341
|
+
val pdfY1 = ph - (localStartY / rs)
|
|
342
|
+
val pdfX2 = endCX / rs
|
|
343
|
+
val pdfY2 = ph - (localEndY / rs)
|
|
344
|
+
|
|
345
|
+
val left = minOf(pdfX1, pdfX2)
|
|
346
|
+
val right = maxOf(pdfX1, pdfX2)
|
|
347
|
+
val bottom = minOf(pdfY1, pdfY2)
|
|
348
|
+
val top = maxOf(pdfY1, pdfY2)
|
|
349
|
+
val w = right - left
|
|
350
|
+
val h = top - bottom
|
|
351
|
+
|
|
352
|
+
if (w < 2f && h < 2f) return // too small
|
|
353
|
+
|
|
354
|
+
val annotation = AnnotationModel(
|
|
355
|
+
id = UUID.randomUUID().toString(),
|
|
356
|
+
type = annotationMode,
|
|
357
|
+
page = pageIndex,
|
|
358
|
+
color = annotationColor,
|
|
359
|
+
alpha = if (annotationMode == "highlight") 0.5f else null,
|
|
360
|
+
bounds = AnnotationBounds(left, bottom, w, h),
|
|
361
|
+
createdAt = System.currentTimeMillis() / 1000.0
|
|
362
|
+
)
|
|
363
|
+
annotations = annotations + annotation
|
|
364
|
+
onAnnotationsChangedListener?.invoke()
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// --- Eraser mode ---
|
|
368
|
+
|
|
369
|
+
private fun handleEraserTap(event: MotionEvent) {
|
|
370
|
+
val contentX = (event.x + panX) / currentScale
|
|
371
|
+
val contentY = (event.y + panY) / currentScale
|
|
372
|
+
val pageIndex = pageIndexAtContentY(contentY)
|
|
373
|
+
if (pageIndex < 0) return
|
|
374
|
+
|
|
375
|
+
val localY = contentY - pageYOffsets[pageIndex]
|
|
376
|
+
val pdfX = contentX / renderScales[pageIndex]
|
|
377
|
+
val pdfY = pageHeights[pageIndex] - (localY / renderScales[pageIndex])
|
|
378
|
+
|
|
379
|
+
val hit = AnnotationHitTester.hitTest(
|
|
380
|
+
AnnotationPoint(pdfX, pdfY),
|
|
381
|
+
annotations,
|
|
382
|
+
pageIndex
|
|
383
|
+
)
|
|
384
|
+
if (hit != null) {
|
|
385
|
+
annotations = annotations.filter { it.id != hit.id }
|
|
386
|
+
onAnnotationsChangedListener?.invoke()
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// --- Text mode ---
|
|
391
|
+
|
|
392
|
+
private fun handleTextTap(event: MotionEvent) {
|
|
393
|
+
val contentX = (event.x + panX) / currentScale
|
|
394
|
+
val contentY = (event.y + panY) / currentScale
|
|
395
|
+
val pageIndex = pageIndexAtContentY(contentY)
|
|
396
|
+
if (pageIndex < 0) return
|
|
397
|
+
|
|
398
|
+
val localY = contentY - pageYOffsets[pageIndex]
|
|
399
|
+
val pdfX = contentX / renderScales[pageIndex]
|
|
400
|
+
val pdfY = pageHeights[pageIndex] - (localY / renderScales[pageIndex])
|
|
401
|
+
|
|
402
|
+
onTextInputRequested?.invoke(pageIndex, AnnotationPoint(pdfX, pdfY))
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
fun addTextAnnotation(page: Int, point: AnnotationPoint, text: String) {
|
|
406
|
+
val fontSize = 16f
|
|
407
|
+
val paint = Paint().apply { textSize = fontSize }
|
|
408
|
+
val textWidth = paint.measureText(text)
|
|
409
|
+
val annotation = AnnotationModel(
|
|
410
|
+
id = UUID.randomUUID().toString(),
|
|
411
|
+
type = "freeText",
|
|
412
|
+
page = page,
|
|
413
|
+
color = annotationColor,
|
|
414
|
+
bounds = AnnotationBounds(point.x, point.y - fontSize, textWidth, fontSize * 1.2f),
|
|
415
|
+
contents = text,
|
|
416
|
+
fontSize = fontSize,
|
|
417
|
+
createdAt = System.currentTimeMillis() / 1000.0
|
|
418
|
+
)
|
|
419
|
+
annotations = annotations + annotation
|
|
420
|
+
onAnnotationsChangedListener?.invoke()
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// --- Helpers ---
|
|
424
|
+
|
|
425
|
+
private fun pageIndexAtContentY(contentY: Float): Int {
|
|
426
|
+
for (i in pageYOffsets.indices.reversed()) {
|
|
427
|
+
if (pageYOffsets[i] <= contentY) return i
|
|
428
|
+
}
|
|
429
|
+
return 0
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
override fun computeScroll() {
|
|
433
|
+
if (scroller.computeScrollOffset()) {
|
|
434
|
+
panX = scroller.currX.toFloat()
|
|
435
|
+
panY = scroller.currY.toFloat()
|
|
436
|
+
constrainPan()
|
|
437
|
+
invalidate()
|
|
438
|
+
notifyPageChange()
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
private fun constrainPan() {
|
|
443
|
+
val maxX = maxOf(0f, contentWidth * currentScale - width)
|
|
444
|
+
val maxY = maxOf(0f, totalHeight * currentScale - height)
|
|
445
|
+
panX = panX.coerceIn(0f, maxX)
|
|
446
|
+
panY = panY.coerceIn(0f, maxY)
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
private fun getVisiblePage(): Int {
|
|
450
|
+
val centerY = (panY + height / 2f) / currentScale
|
|
451
|
+
for (i in pageYOffsets.indices.reversed()) {
|
|
452
|
+
if (pageYOffsets[i] <= centerY) return i
|
|
453
|
+
}
|
|
454
|
+
return 0
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
fun scrollToPage(pageIndex: Int) {
|
|
458
|
+
if (pageIndex < 0 || pageIndex >= pageYOffsets.size) return
|
|
459
|
+
panY = pageYOffsets[pageIndex] * currentScale
|
|
460
|
+
constrainPan()
|
|
461
|
+
invalidate()
|
|
462
|
+
notifyPageChange()
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
private fun notifyPageChange() {
|
|
466
|
+
val page = getVisiblePage()
|
|
467
|
+
if (page != lastNotifiedPage) {
|
|
468
|
+
lastNotifiedPage = page
|
|
469
|
+
onPageChangeListener?.invoke(page, pageBitmaps.size)
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
fun recycle() {
|
|
474
|
+
pageBitmaps.forEach { it.recycle() }
|
|
475
|
+
pageBitmaps.clear()
|
|
476
|
+
pageYOffsets.clear()
|
|
477
|
+
pageWidths.clear()
|
|
478
|
+
pageHeights.clear()
|
|
479
|
+
renderScales.clear()
|
|
480
|
+
totalHeight = 0f
|
|
481
|
+
lastNotifiedPage = -1
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
private inner class ScaleListener : ScaleGestureDetector.SimpleOnScaleGestureListener() {
|
|
485
|
+
override fun onScale(detector: ScaleGestureDetector): Boolean {
|
|
486
|
+
val oldScale = currentScale
|
|
487
|
+
currentScale = (currentScale * detector.scaleFactor).coerceIn(minScale, maxScale)
|
|
488
|
+
val ratio = currentScale / oldScale
|
|
489
|
+
panX = (panX + detector.focusX) * ratio - detector.focusX
|
|
490
|
+
panY = (panY + detector.focusY) * ratio - detector.focusY
|
|
491
|
+
constrainPan()
|
|
492
|
+
invalidate()
|
|
493
|
+
return true
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
override fun onScaleBegin(detector: ScaleGestureDetector): Boolean = true
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
private inner class GestureListener : GestureDetector.SimpleOnGestureListener() {
|
|
500
|
+
override fun onDown(e: MotionEvent): Boolean = true
|
|
501
|
+
|
|
502
|
+
override fun onScroll(e1: MotionEvent?, e2: MotionEvent, dx: Float, dy: Float): Boolean {
|
|
503
|
+
panX += dx
|
|
504
|
+
panY += dy
|
|
505
|
+
constrainPan()
|
|
506
|
+
invalidate()
|
|
507
|
+
notifyPageChange()
|
|
508
|
+
return true
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
override fun onFling(e1: MotionEvent?, e2: MotionEvent, vx: Float, vy: Float): Boolean {
|
|
512
|
+
scroller.fling(
|
|
513
|
+
panX.toInt(),
|
|
514
|
+
panY.toInt(),
|
|
515
|
+
-vx.toInt(),
|
|
516
|
+
-vy.toInt(),
|
|
517
|
+
0,
|
|
518
|
+
maxOf(0, (contentWidth * currentScale - width).toInt()),
|
|
519
|
+
0,
|
|
520
|
+
maxOf(0, (totalHeight * currentScale - height).toInt())
|
|
521
|
+
)
|
|
522
|
+
invalidate()
|
|
523
|
+
return true
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
override fun onDoubleTap(e: MotionEvent): Boolean {
|
|
527
|
+
val targetScale = if (currentScale > minScale * 1.5f) minScale else minScale * 2.5f
|
|
528
|
+
val oldScale = currentScale
|
|
529
|
+
currentScale = targetScale
|
|
530
|
+
val ratio = currentScale / oldScale
|
|
531
|
+
panX = (panX + e.x) * ratio - e.x
|
|
532
|
+
panY = (panY + e.y) * ratio - e.y
|
|
533
|
+
constrainPan()
|
|
534
|
+
invalidate()
|
|
535
|
+
notifyPageChange()
|
|
536
|
+
return true
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
package dev.terhoeven.expopdfmarkup
|
|
2
|
+
|
|
3
|
+
import expo.modules.kotlin.modules.Module
|
|
4
|
+
import expo.modules.kotlin.modules.ModuleDefinition
|
|
5
|
+
|
|
6
|
+
// android.graphics.Color.parseColor does not support "transparent" or CSS color functions.
|
|
7
|
+
// This helper handles those cases and returns null for unrecognised values (falls back to default).
|
|
8
|
+
internal fun parseColorString(color: String): Int? {
|
|
9
|
+
val trimmed = color.trim()
|
|
10
|
+
|
|
11
|
+
if (trimmed.equals("transparent", ignoreCase = true)) {
|
|
12
|
+
return android.graphics.Color.TRANSPARENT
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Handle rgb(r, g, b) and rgba(r, g, b, a)
|
|
16
|
+
val rgbMatch = Regex(
|
|
17
|
+
"""^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)(?:\s*,\s*([\d.]+))?\s*\)$""",
|
|
18
|
+
RegexOption.IGNORE_CASE
|
|
19
|
+
).matchEntire(trimmed)
|
|
20
|
+
if (rgbMatch != null) {
|
|
21
|
+
val r = rgbMatch.groupValues[1].toIntOrNull()?.coerceIn(0, 255) ?: return null
|
|
22
|
+
val g = rgbMatch.groupValues[2].toIntOrNull()?.coerceIn(0, 255) ?: return null
|
|
23
|
+
val b = rgbMatch.groupValues[3].toIntOrNull()?.coerceIn(0, 255) ?: return null
|
|
24
|
+
val aStr = rgbMatch.groupValues[4]
|
|
25
|
+
val a = if (aStr.isEmpty()) {
|
|
26
|
+
255
|
|
27
|
+
} else {
|
|
28
|
+
(
|
|
29
|
+
(aStr.toFloatOrNull() ?: 1f).coerceIn(
|
|
30
|
+
0f,
|
|
31
|
+
1f
|
|
32
|
+
) * 255
|
|
33
|
+
).toInt()
|
|
34
|
+
}
|
|
35
|
+
return (a shl 24) or (r shl 16) or (g shl 8) or b
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return try {
|
|
39
|
+
android.graphics.Color.parseColor(trimmed)
|
|
40
|
+
} catch (_: IllegalArgumentException) {
|
|
41
|
+
null
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
class ExpoPdfMarkupModule : Module() {
|
|
46
|
+
override fun definition() = ModuleDefinition {
|
|
47
|
+
Name("ExpoPdfMarkup")
|
|
48
|
+
|
|
49
|
+
View(ExpoPdfMarkupView::class) {
|
|
50
|
+
Prop("source") { view: ExpoPdfMarkupView, source: String ->
|
|
51
|
+
view.loadPdf(source)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
Prop("page") { view: ExpoPdfMarkupView, page: Int ->
|
|
55
|
+
view.goToPage(page)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
Prop("backgroundColor") { view: ExpoPdfMarkupView, color: String? ->
|
|
59
|
+
view.setPageBackgroundColor(color?.let { parseColorString(it) })
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
Prop("annotations") { view: ExpoPdfMarkupView, json: String? ->
|
|
63
|
+
view.loadAnnotations(json)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
Prop("annotationMode") { view: ExpoPdfMarkupView, mode: String? ->
|
|
67
|
+
view.setAnnotationMode(mode)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
Prop("annotationColor") { view: ExpoPdfMarkupView, color: String? ->
|
|
71
|
+
view.setAnnotationColor(color)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
Prop("annotationLineWidth") { view: ExpoPdfMarkupView, width: Double? ->
|
|
75
|
+
view.setAnnotationLineWidth(width)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
Events("onPageChanged", "onLoadComplete", "onError", "onAnnotationsChanged")
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|