@papyrus-sdk/engine-native 0.2.24 → 0.2.26
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/android/src/main/cpp/CMakeLists.txt +9 -1
- package/android/src/main/cpp/PapyrusComicArchiveJni.cpp +15 -0
- package/android/src/main/cpp/PapyrusComicArchiveTest.cpp +118 -1
- package/android/src/main/cpp/PapyrusComicImageDimensionsTest.cpp +48 -0
- package/android/src/main/cpp/papyrus_outline_loader_test.cpp +240 -0
- package/android/src/main/cpp/papyrus_text_word_test.cpp +72 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusComicArchiveNative.java +1 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +294 -58
- package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentViewManager.java +1 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusComicPageLayout.java +19 -2
- package/android/src/main/java/com/papyrus/engine/PapyrusComicPreviewExecutor.java +28 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusComicZoomFrame.java +39 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +10 -2
- package/dist/index.d.mts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/ios/PapyrusComicDocumentView.h +1 -0
- package/ios/PapyrusComicDocumentView.m +15 -0
- package/ios/PapyrusComicDocumentViewManager.m +1 -0
- package/package.json +5 -5
- package/vendor/libarchive/PapyrusComicArchive.cpp +66 -1
- package/vendor/libarchive/PapyrusComicArchive.h +6 -0
- package/vendor/libarchive/PapyrusComicImageDimensions.h +103 -0
|
@@ -9,11 +9,13 @@ import android.view.GestureDetector;
|
|
|
9
9
|
import android.view.MotionEvent;
|
|
10
10
|
import android.view.ScaleGestureDetector;
|
|
11
11
|
import android.view.View;
|
|
12
|
+
import android.view.ViewGroup;
|
|
12
13
|
import android.widget.FrameLayout;
|
|
13
14
|
import android.widget.ImageView;
|
|
14
15
|
import android.widget.LinearLayout;
|
|
15
16
|
import android.widget.ScrollView;
|
|
16
17
|
import android.widget.TextView;
|
|
18
|
+
import android.util.SparseArray;
|
|
17
19
|
|
|
18
20
|
import androidx.annotation.NonNull;
|
|
19
21
|
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
@@ -52,6 +54,7 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
52
54
|
private long lastPageEvent;
|
|
53
55
|
private final ScaleGestureDetector scaleDetector;
|
|
54
56
|
private final GestureDetector tapDetector;
|
|
57
|
+
private final PapyrusComicZoomFrame zoomFrame = new PapyrusComicZoomFrame();
|
|
55
58
|
private final Set<String> emittedErrors = new HashSet<>();
|
|
56
59
|
private float lastPanX;
|
|
57
60
|
private float lastPanY;
|
|
@@ -72,13 +75,14 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
72
75
|
});
|
|
73
76
|
scaleDetector = new ScaleGestureDetector(context, new ScaleGestureDetector.SimpleOnScaleGestureListener() {
|
|
74
77
|
@Override public boolean onScale(ScaleGestureDetector detector) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
78
|
+
float next = zoomFrame.nextZoom(zoom, detector.getScaleFactor());
|
|
79
|
+
if (zoomFrame.offer(next, detector.getFocusX(), detector.getFocusY())) {
|
|
80
|
+
PapyrusComicDocumentView.this.postOnAnimation(PapyrusComicDocumentView.this::applyPendingZoom);
|
|
81
|
+
}
|
|
79
82
|
return true;
|
|
80
83
|
}
|
|
81
84
|
@Override public void onScaleEnd(ScaleGestureDetector detector) {
|
|
85
|
+
applyPendingZoom();
|
|
82
86
|
emitZoom();
|
|
83
87
|
}
|
|
84
88
|
});
|
|
@@ -86,10 +90,16 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
86
90
|
@Override public boolean onDown(MotionEvent event) { return true; }
|
|
87
91
|
@Override public boolean onDoubleTap(MotionEvent event) {
|
|
88
92
|
if (scaleDetector.isInProgress()) return false;
|
|
93
|
+
zoomFrame.clear();
|
|
89
94
|
applyZoom(zoom > 1.05f ? 1f : 2f, event.getX(), event.getY());
|
|
90
95
|
emitZoom();
|
|
91
96
|
return true;
|
|
92
97
|
}
|
|
98
|
+
@Override public boolean onSingleTapConfirmed(MotionEvent event) {
|
|
99
|
+
if (scaleDetector.isInProgress() || pageCount <= 0) return false;
|
|
100
|
+
emitPageTap();
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
93
103
|
});
|
|
94
104
|
setLayoutMode(layoutMode);
|
|
95
105
|
applyTheme();
|
|
@@ -105,9 +115,32 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
105
115
|
}
|
|
106
116
|
}
|
|
107
117
|
|
|
108
|
-
public void setEngineId(String value) {
|
|
109
|
-
|
|
110
|
-
|
|
118
|
+
public void setEngineId(String value) {
|
|
119
|
+
if (value == null ? engineId != null : !value.equals(engineId)) {
|
|
120
|
+
zoomFrame.clear();
|
|
121
|
+
adapter.clearPageDimensions();
|
|
122
|
+
}
|
|
123
|
+
engineId = value;
|
|
124
|
+
adapter.notifyDataSetChanged();
|
|
125
|
+
}
|
|
126
|
+
public void setDocumentGeneration(int value) {
|
|
127
|
+
if (generation != value) {
|
|
128
|
+
zoomFrame.clear();
|
|
129
|
+
generation = value;
|
|
130
|
+
adapter.clearPageDimensions();
|
|
131
|
+
adapter.notifyDataSetChanged();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
public void setPageCount(int value) {
|
|
135
|
+
int nextCount = Math.max(0, value);
|
|
136
|
+
if (pageCount != nextCount) {
|
|
137
|
+
zoomFrame.clear();
|
|
138
|
+
adapter.clearPageDimensions();
|
|
139
|
+
}
|
|
140
|
+
pageCount = nextCount;
|
|
141
|
+
adapter.notifyDataSetChanged();
|
|
142
|
+
scrollToCurrentPage();
|
|
143
|
+
}
|
|
111
144
|
public void setCurrentPage(int value) {
|
|
112
145
|
int clamped = pageCount > 0 ? Math.max(1, Math.min(pageCount, value)) : Math.max(1, value);
|
|
113
146
|
if (clamped != currentPage) { currentPage = clamped; scrollToCurrentPage(); }
|
|
@@ -115,6 +148,7 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
115
148
|
public void setLayoutMode(String value) {
|
|
116
149
|
String nextMode = "continuous".equals(value) ? "continuous" : "single";
|
|
117
150
|
if (!nextMode.equals(layoutMode)) {
|
|
151
|
+
applyPendingZoom();
|
|
118
152
|
panX = PapyrusComicPageLayout.panForLayout(panX, recyclerView.getWidth(), zoom, "continuous".equals(nextMode));
|
|
119
153
|
panY = 0;
|
|
120
154
|
adapter.setPan(panX, panY);
|
|
@@ -133,36 +167,75 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
133
167
|
public void setFitMode(String value) { fitMode = value == null ? "width" : value; adapter.notifyDataSetChanged(); }
|
|
134
168
|
public void setReadingDirection(String value) { readingDirection = "rtl".equals(value) ? "rtl" : "ltr"; setLayoutMode(layoutMode); }
|
|
135
169
|
public void setZoom(float value) {
|
|
170
|
+
zoomFrame.clear();
|
|
136
171
|
applyZoom(value, recyclerView.getWidth() / 2f, recyclerView.getHeight() / 2f);
|
|
137
172
|
}
|
|
173
|
+
private void applyPendingZoom() {
|
|
174
|
+
PapyrusComicZoomFrame.Update update = zoomFrame.takePending();
|
|
175
|
+
if (update != null) applyZoom(update.zoom, update.focusX, update.focusY);
|
|
176
|
+
}
|
|
177
|
+
private void restoreVisibleAnchor(String sourceEngineId, int sourceGeneration,
|
|
178
|
+
LinearLayoutManager sourceLayoutManager, int position, int top) {
|
|
179
|
+
if (sourceLayoutManager != layoutManager ||
|
|
180
|
+
!PapyrusComicPageLayout.canRestoreAnchor(sourceEngineId, sourceGeneration,
|
|
181
|
+
engineId, generation, "continuous".equals(layoutMode), position, pageCount)) return;
|
|
182
|
+
sourceLayoutManager.scrollToPositionWithOffset(position, top);
|
|
183
|
+
}
|
|
138
184
|
private void applyZoom(float value, float focusX, float focusY) {
|
|
139
185
|
float next = Math.max(1f, Math.min(5f, value));
|
|
140
186
|
if (Math.abs(next - zoom) < 0.0001f) return;
|
|
141
187
|
float previous = zoom;
|
|
142
|
-
|
|
143
|
-
|
|
188
|
+
View anchor = "continuous".equals(layoutMode) ? recyclerView.findChildViewUnder(focusX, focusY) : null;
|
|
189
|
+
if ("continuous".equals(layoutMode) && anchor == null) {
|
|
190
|
+
anchor = findClosestVisiblePage(focusX, focusY);
|
|
191
|
+
}
|
|
192
|
+
int position = anchor == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(anchor);
|
|
144
193
|
int top = anchor == null ? 0 : anchor.getTop();
|
|
194
|
+
int height = anchor == null ? 0 : anchor.getHeight();
|
|
145
195
|
zoom = next;
|
|
146
196
|
if ("continuous".equals(layoutMode)) {
|
|
147
197
|
panX = PapyrusComicPageLayout.clampPanX(focusX - (focusX - panX) * next / previous, recyclerView.getWidth(), next);
|
|
148
198
|
panY = 0;
|
|
149
199
|
adapter.setZoom(next);
|
|
150
200
|
adapter.setPan(panX, 0);
|
|
151
|
-
if (
|
|
152
|
-
|
|
201
|
+
if (layoutManager != null && position != RecyclerView.NO_POSITION && height > 0) {
|
|
202
|
+
layoutManager.scrollToPositionWithOffset(position,
|
|
203
|
+
PapyrusComicPageLayout.zoomAnchorTopForItem(top, focusY, height, previous, next));
|
|
204
|
+
}
|
|
153
205
|
recyclerView.requestLayout();
|
|
154
206
|
} else {
|
|
155
207
|
if (next == 1f) { panX = 0; panY = 0; adapter.setPan(0, 0); }
|
|
156
208
|
adapter.setZoom(next);
|
|
157
209
|
}
|
|
158
210
|
}
|
|
211
|
+
|
|
212
|
+
private View findClosestVisiblePage(float focusX, float focusY) {
|
|
213
|
+
View closest = null;
|
|
214
|
+
float bestDistance = Float.MAX_VALUE;
|
|
215
|
+
for (int i = 0; i < recyclerView.getChildCount(); i++) {
|
|
216
|
+
View child = recyclerView.getChildAt(i);
|
|
217
|
+
float left = child.getLeft() + child.getTranslationX();
|
|
218
|
+
float top = child.getTop() + child.getTranslationY();
|
|
219
|
+
float right = left + child.getWidth();
|
|
220
|
+
float bottom = top + child.getHeight();
|
|
221
|
+
float dx = focusX < left ? left - focusX : focusX > right ? focusX - right : 0f;
|
|
222
|
+
float dy = focusY < top ? top - focusY : focusY > bottom ? focusY - bottom : 0f;
|
|
223
|
+
float distance = dx * dx + dy * dy;
|
|
224
|
+
if (distance < bestDistance) {
|
|
225
|
+
bestDistance = distance;
|
|
226
|
+
closest = child;
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return closest;
|
|
230
|
+
}
|
|
231
|
+
|
|
159
232
|
public void setPageTheme(String value) { pageTheme = value == null ? "normal" : value; applyTheme(); }
|
|
160
233
|
|
|
161
234
|
@Override public boolean dispatchTouchEvent(android.view.MotionEvent event) {
|
|
162
235
|
scaleDetector.onTouchEvent(event);
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
236
|
+
// Feed pointer transitions too so a pinch cancels a pending single tap,
|
|
237
|
+
// while the detector still arbitrates a single tap against double-tap zoom.
|
|
238
|
+
tapDetector.onTouchEvent(event);
|
|
166
239
|
if (event.getActionMasked() == android.view.MotionEvent.ACTION_POINTER_UP && event.getPointerCount() > 1) {
|
|
167
240
|
int remaining = event.getActionIndex() == 0 ? 1 : 0;
|
|
168
241
|
lastPanX = event.getX(remaining); lastPanY = event.getY(remaining);
|
|
@@ -242,6 +315,12 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
242
315
|
payload.putDouble("zoom", zoom);
|
|
243
316
|
context.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onZoomChanged", payload);
|
|
244
317
|
}
|
|
318
|
+
private void emitPageTap() {
|
|
319
|
+
ReactContext context = (ReactContext) getContext();
|
|
320
|
+
com.facebook.react.bridge.WritableMap payload = Arguments.createMap();
|
|
321
|
+
payload.putInt("page", currentPage);
|
|
322
|
+
context.getJSModule(RCTEventEmitter.class).receiveEvent(getId(), "onPageTap", payload);
|
|
323
|
+
}
|
|
245
324
|
private void emitError(String key, String message, String sourceEngineId, int sourceGeneration) {
|
|
246
325
|
synchronized (emittedErrors) {
|
|
247
326
|
if (!emittedErrors.add(key)) return;
|
|
@@ -264,11 +343,50 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
264
343
|
private float imageZoom = 1f;
|
|
265
344
|
private float imagePanX;
|
|
266
345
|
private float imagePanY;
|
|
346
|
+
private final SparseArray<PageDimensions> pageDimensions = new SparseArray<>();
|
|
347
|
+
|
|
348
|
+
void clearPageDimensions() { pageDimensions.clear(); }
|
|
349
|
+
|
|
350
|
+
private PageDimensions pageDimensionsFor(int position) {
|
|
351
|
+
if (position < 0 || position >= pageCount || engineId == null) return null;
|
|
352
|
+
PageDimensions cached = pageDimensions.get(position);
|
|
353
|
+
if (cached != null) return cached.isKnown() ? cached : null;
|
|
354
|
+
int[] dimensions = PapyrusComicArchiveNative.getPageDimensions(engineId, generation, position);
|
|
355
|
+
PageDimensions result = dimensions != null && dimensions.length >= 2 && dimensions[0] > 0 && dimensions[1] > 0
|
|
356
|
+
? new PageDimensions(dimensions[0], dimensions[1]) : PageDimensions.UNKNOWN;
|
|
357
|
+
pageDimensions.put(position, result);
|
|
358
|
+
return result.isKnown() ? result : null;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
private PageDimensions layoutDimensionsFor(int position) {
|
|
362
|
+
PageDimensions exact = pageDimensionsFor(position);
|
|
363
|
+
if (exact != null) return exact;
|
|
364
|
+
for (int distance = 1; distance <= 8; distance++) {
|
|
365
|
+
PageDimensions before = pageDimensionsFor(position - distance);
|
|
366
|
+
if (before != null) return before;
|
|
367
|
+
PageDimensions after = pageDimensionsFor(position + distance);
|
|
368
|
+
if (after != null) return after;
|
|
369
|
+
}
|
|
370
|
+
return null;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
private int itemHeightFor(int position) {
|
|
374
|
+
PageDimensions dimensions = layoutDimensionsFor(position);
|
|
375
|
+
return PapyrusComicPageLayout.zoomedHeight(
|
|
376
|
+
dimensions == null ? 0 : dimensions.width,
|
|
377
|
+
dimensions == null ? 0 : dimensions.height,
|
|
378
|
+
recyclerView.getWidth(), recyclerView.getHeight(), layoutMode, fitMode, zoom);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
private void rememberPageDimensions(int position, int width, int height) {
|
|
382
|
+
if (position >= 0 && width > 0 && height > 0) pageDimensions.put(position, new PageDimensions(width, height));
|
|
383
|
+
}
|
|
384
|
+
|
|
267
385
|
@NonNull @Override public ComicHolder onCreateViewHolder(@NonNull android.view.ViewGroup parent, int type) {
|
|
268
386
|
FrameLayout cell = new FrameLayout(parent.getContext());
|
|
269
387
|
cell.setLayoutParams(new RecyclerView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
|
270
388
|
cell.setBackgroundColor(Color.TRANSPARENT);
|
|
271
|
-
|
|
389
|
+
ComicPageScrollView pageScrollView = new ComicPageScrollView(parent.getContext());
|
|
272
390
|
pageScrollView.setFillViewport(true);
|
|
273
391
|
pageScrollView.setVerticalScrollBarEnabled(false);
|
|
274
392
|
ImageView image = new ImageView(parent.getContext());
|
|
@@ -280,7 +398,7 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
280
398
|
@Override public void onBindViewHolder(@NonNull ComicHolder holder, int position) {
|
|
281
399
|
RecyclerView.LayoutParams itemParams = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
|
|
282
400
|
itemParams.width = "continuous".equals(layoutMode) ? PapyrusComicPageLayout.zoomedWidth(recyclerView.getWidth(), zoom) : Math.max(1, recyclerView.getWidth());
|
|
283
|
-
itemParams.height = Math.max(1, recyclerView.getHeight());
|
|
401
|
+
itemParams.height = "continuous".equals(layoutMode) ? itemHeightFor(position) : Math.max(1, recyclerView.getHeight());
|
|
284
402
|
holder.itemView.setLayoutParams(itemParams);
|
|
285
403
|
final int logicalPage = position;
|
|
286
404
|
final String id = engineId;
|
|
@@ -293,12 +411,15 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
293
411
|
holder.image.setTranslationX(continuous ? 0f : imagePanX);
|
|
294
412
|
holder.image.setTranslationY(continuous ? 0f : imagePanY);
|
|
295
413
|
holder.itemView.setTranslationX(continuous ? imagePanX : 0f);
|
|
414
|
+
PageDimensions indexedDimensions = layoutDimensionsFor(position);
|
|
415
|
+
configurePageImage(holder, indexedDimensions == null ? 0 : indexedDimensions.width,
|
|
416
|
+
indexedDimensions == null ? 0 : indexedDimensions.height);
|
|
296
417
|
String key = id + ":" + docGeneration + ":" + logicalPage;
|
|
297
418
|
Bitmap cached = BITMAPS.get(key);
|
|
298
419
|
if (cached != null) {
|
|
299
|
-
applyPageHeightForImage(holder, cached.getWidth(), cached.getHeight());
|
|
420
|
+
applyPageHeightForImage(holder, logicalPage, cached.getWidth(), cached.getHeight(), false);
|
|
300
421
|
holder.image.setImageBitmap(cached);
|
|
301
|
-
configurePageImage(holder, cached);
|
|
422
|
+
configurePageImage(holder, cached.getWidth(), cached.getHeight());
|
|
302
423
|
return;
|
|
303
424
|
}
|
|
304
425
|
DECODE_EXECUTOR.execute(() -> {
|
|
@@ -318,7 +439,9 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
318
439
|
}
|
|
319
440
|
if (bounds.outWidth > 0 && bounds.outHeight > 0) {
|
|
320
441
|
post(() -> {
|
|
321
|
-
if (holder.bindToken == token && id.equals(engineId) && docGeneration == generation)
|
|
442
|
+
if (holder.bindToken == token && id.equals(engineId) && docGeneration == generation) {
|
|
443
|
+
applyPageHeightForImage(holder, logicalPage, bounds.outWidth, bounds.outHeight, true);
|
|
444
|
+
}
|
|
322
445
|
});
|
|
323
446
|
int maxEdge = Math.max(getWidth(), getHeight()) * 2;
|
|
324
447
|
int sample = 1;
|
|
@@ -337,7 +460,7 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
337
460
|
post(() -> {
|
|
338
461
|
if (holder.bindToken == token && id.equals(engineId) && docGeneration == generation) {
|
|
339
462
|
holder.image.setImageBitmap(result);
|
|
340
|
-
configurePageImage(holder, result);
|
|
463
|
+
configurePageImage(holder, result.getWidth(), result.getHeight());
|
|
341
464
|
}
|
|
342
465
|
});
|
|
343
466
|
});
|
|
@@ -356,64 +479,132 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
356
479
|
holder.image.setScaleY(continuous ? 1f : imageZoom);
|
|
357
480
|
holder.image.setTranslationX(continuous ? 0f : imagePanX);
|
|
358
481
|
holder.image.setTranslationY(continuous ? 0f : imagePanY);
|
|
359
|
-
|
|
482
|
+
int position = holder.getBindingAdapterPosition();
|
|
483
|
+
PageDimensions indexedDimensions = layoutDimensionsFor(position);
|
|
484
|
+
if (indexedDimensions != null && continuous) {
|
|
485
|
+
applyPageHeightForDimensions(holder, position, indexedDimensions.width, indexedDimensions.height);
|
|
486
|
+
} else if (holder.image.getDrawable() instanceof android.graphics.drawable.BitmapDrawable) {
|
|
360
487
|
Bitmap bitmap = ((android.graphics.drawable.BitmapDrawable) holder.image.getDrawable()).getBitmap();
|
|
361
|
-
applyPageHeightForImage(holder, bitmap.getWidth(), bitmap.getHeight());
|
|
488
|
+
applyPageHeightForImage(holder, position, bitmap.getWidth(), bitmap.getHeight(), false);
|
|
489
|
+
} else if (continuous) {
|
|
490
|
+
applyPageHeightForDimensions(holder, position, 0, 0);
|
|
362
491
|
}
|
|
363
492
|
holder.itemView.forceLayout();
|
|
364
493
|
recyclerView.requestLayout();
|
|
365
494
|
}
|
|
366
|
-
private void applyPageHeightForImage(ComicHolder holder, int imageWidth, int imageHeight) {
|
|
495
|
+
private void applyPageHeightForImage(ComicHolder holder, int position, int imageWidth, int imageHeight, boolean preserveVisibleAnchor) {
|
|
496
|
+
int oldHeight = holder.itemView.getLayoutParams() instanceof RecyclerView.LayoutParams
|
|
497
|
+
? ((RecyclerView.LayoutParams) holder.itemView.getLayoutParams()).height : 0;
|
|
498
|
+
int anchorPosition = RecyclerView.NO_POSITION;
|
|
499
|
+
int anchorTop = 0;
|
|
500
|
+
if (preserveVisibleAnchor && "continuous".equals(layoutMode) && layoutManager != null) {
|
|
501
|
+
anchorPosition = layoutManager.findFirstVisibleItemPosition();
|
|
502
|
+
View anchorView = anchorPosition == RecyclerView.NO_POSITION ? null : layoutManager.findViewByPosition(anchorPosition);
|
|
503
|
+
if (anchorView != null) anchorTop = anchorView.getTop();
|
|
504
|
+
}
|
|
505
|
+
rememberPageDimensions(position, imageWidth, imageHeight);
|
|
506
|
+
applyPageHeightForDimensions(holder, position, imageWidth, imageHeight);
|
|
507
|
+
int newHeight = holder.itemView.getLayoutParams() instanceof RecyclerView.LayoutParams
|
|
508
|
+
? ((RecyclerView.LayoutParams) holder.itemView.getLayoutParams()).height : oldHeight;
|
|
509
|
+
if (preserveVisibleAnchor && oldHeight != newHeight && position >= 0 &&
|
|
510
|
+
anchorPosition != RecyclerView.NO_POSITION && position <= anchorPosition && layoutManager != null) {
|
|
511
|
+
final int preservePosition = anchorPosition;
|
|
512
|
+
final int preserveTop = anchorTop;
|
|
513
|
+
final String sourceEngineId = engineId;
|
|
514
|
+
final int sourceGeneration = generation;
|
|
515
|
+
final LinearLayoutManager sourceLayoutManager = layoutManager;
|
|
516
|
+
recyclerView.post(() -> restoreVisibleAnchor(sourceEngineId, sourceGeneration,
|
|
517
|
+
sourceLayoutManager, preservePosition, preserveTop));
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
private void applyPageHeightForDimensions(ComicHolder holder, int position, int imageWidth, int imageHeight) {
|
|
367
522
|
if (!(holder.itemView.getLayoutParams() instanceof RecyclerView.LayoutParams)) return;
|
|
368
523
|
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
|
|
369
|
-
int height
|
|
370
|
-
|
|
371
|
-
imageHeight
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
524
|
+
int height;
|
|
525
|
+
if ("continuous".equals(layoutMode)) {
|
|
526
|
+
PageDimensions dimensions = imageWidth > 0 && imageHeight > 0
|
|
527
|
+
? new PageDimensions(imageWidth, imageHeight) : layoutDimensionsFor(position);
|
|
528
|
+
height = dimensions == null ? PapyrusComicPageLayout.zoomedFallbackHeight(
|
|
529
|
+
recyclerView.getWidth(), recyclerView.getHeight(), layoutMode, fitMode, zoom) : PapyrusComicPageLayout.zoomedHeight(
|
|
530
|
+
dimensions.width, dimensions.height, recyclerView.getWidth(), recyclerView.getHeight(), layoutMode, fitMode, zoom);
|
|
531
|
+
} else {
|
|
532
|
+
height = Math.max(1, recyclerView.getHeight());
|
|
533
|
+
}
|
|
378
534
|
if (params.height != height) {
|
|
379
535
|
params.height = height;
|
|
380
536
|
holder.itemView.setLayoutParams(params);
|
|
381
537
|
}
|
|
538
|
+
configurePageImage(holder, imageWidth, imageHeight);
|
|
382
539
|
}
|
|
383
540
|
|
|
384
|
-
private void
|
|
541
|
+
private void updatePageGeometryForZoom(ComicHolder holder, int position, float value) {
|
|
542
|
+
if (position < 0 || !(holder.itemView.getLayoutParams() instanceof RecyclerView.LayoutParams)) return;
|
|
543
|
+
PageDimensions dimensions = layoutDimensionsFor(position);
|
|
544
|
+
if (dimensions == null && holder.image.getDrawable() instanceof android.graphics.drawable.BitmapDrawable) {
|
|
545
|
+
Bitmap bitmap = ((android.graphics.drawable.BitmapDrawable) holder.image.getDrawable()).getBitmap();
|
|
546
|
+
dimensions = new PageDimensions(bitmap.getWidth(), bitmap.getHeight());
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
|
|
550
|
+
int width = PapyrusComicPageLayout.zoomedWidth(recyclerView.getWidth(), value);
|
|
551
|
+
int height = dimensions == null ? PapyrusComicPageLayout.zoomedFallbackHeight(
|
|
552
|
+
recyclerView.getWidth(), recyclerView.getHeight(), layoutMode, fitMode, value) : PapyrusComicPageLayout.zoomedHeight(
|
|
553
|
+
dimensions.width, dimensions.height, recyclerView.getWidth(), recyclerView.getHeight(), layoutMode, fitMode, value);
|
|
554
|
+
boolean changed = params.width != width || params.height != height;
|
|
555
|
+
if (params.width != width) params.width = width;
|
|
556
|
+
if (params.height != height) params.height = height;
|
|
557
|
+
if (changed) holder.itemView.setLayoutParams(params);
|
|
558
|
+
holder.image.setScaleX(1f);
|
|
559
|
+
holder.image.setScaleY(1f);
|
|
560
|
+
configurePageImage(holder, dimensions == null ? 0 : dimensions.width, dimensions == null ? 0 : dimensions.height);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
private void configurePageImage(ComicHolder holder, int imageWidth, int imageHeight) {
|
|
385
564
|
boolean widthFit = "single".equals(layoutMode) && "width".equals(fitMode) && recyclerView.getWidth() > 0;
|
|
386
565
|
ScrollView.LayoutParams params;
|
|
387
|
-
FrameLayout cell = (FrameLayout) holder.itemView;
|
|
388
566
|
if ("continuous".equals(layoutMode)) {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
cell.addView(holder.image, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
|
|
392
|
-
}
|
|
393
|
-
holder.pageScrollView.setVisibility(View.GONE);
|
|
567
|
+
holder.pageScrollView.setVisibility(View.VISIBLE);
|
|
568
|
+
holder.pageScrollView.setFixedPageHeight(true);
|
|
394
569
|
holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
|
395
|
-
holder.pageScrollView.scrollTo(0, 0);
|
|
570
|
+
if (!"continuous".equals(holder.configuredLayoutMode)) holder.pageScrollView.scrollTo(0, 0);
|
|
571
|
+
holder.pageScrollView.setVerticalScrollBarEnabled(false);
|
|
572
|
+
holder.pageScrollView.setFillViewport(false);
|
|
573
|
+
RecyclerView.LayoutParams itemParams = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
|
|
574
|
+
int pageHeight = itemParams.height > 0 ? itemParams.height : Math.max(1, recyclerView.getHeight());
|
|
575
|
+
setImageLayoutParamsIfChanged(holder, new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, pageHeight));
|
|
576
|
+
holder.configuredLayoutMode = layoutMode;
|
|
396
577
|
return;
|
|
397
578
|
}
|
|
398
|
-
if (holder.image.getParent() != holder.pageScrollView) {
|
|
399
|
-
((android.view.ViewGroup) holder.image.getParent()).removeView(holder.image);
|
|
400
|
-
holder.pageScrollView.addView(holder.image);
|
|
401
|
-
}
|
|
402
579
|
holder.pageScrollView.setVisibility(View.VISIBLE);
|
|
403
|
-
|
|
580
|
+
holder.pageScrollView.setFixedPageHeight(false);
|
|
581
|
+
if (widthFit && imageWidth > 0 && imageHeight > 0) {
|
|
404
582
|
int width = recyclerView.getWidth();
|
|
405
|
-
int height = Math.max(1, Math.round(width * ((float)
|
|
583
|
+
int height = Math.max(1, Math.round(width * ((float) imageHeight / (float) imageWidth)));
|
|
406
584
|
params = new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, height);
|
|
407
585
|
holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
|
|
408
586
|
holder.pageScrollView.setVerticalScrollBarEnabled(height > recyclerView.getHeight());
|
|
409
587
|
holder.pageScrollView.setFillViewport(false);
|
|
588
|
+
} else if (widthFit) {
|
|
589
|
+
params = new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, Math.max(1, recyclerView.getHeight()));
|
|
590
|
+
holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
|
591
|
+
holder.pageScrollView.setVerticalScrollBarEnabled(false);
|
|
592
|
+
holder.pageScrollView.setFillViewport(true);
|
|
410
593
|
} else {
|
|
411
594
|
params = new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, Math.max(1, recyclerView.getHeight()));
|
|
412
595
|
holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
|
|
413
596
|
holder.pageScrollView.setVerticalScrollBarEnabled(false);
|
|
414
597
|
holder.pageScrollView.setFillViewport(true);
|
|
415
598
|
}
|
|
416
|
-
holder
|
|
599
|
+
setImageLayoutParamsIfChanged(holder, params);
|
|
600
|
+
holder.configuredLayoutMode = layoutMode;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
private void setImageLayoutParamsIfChanged(ComicHolder holder, ScrollView.LayoutParams params) {
|
|
604
|
+
ViewGroup.LayoutParams current = holder.image.getLayoutParams();
|
|
605
|
+
if (current == null || current.width != params.width || current.height != params.height) {
|
|
606
|
+
holder.image.setLayoutParams(params);
|
|
607
|
+
}
|
|
417
608
|
}
|
|
418
609
|
void setZoom(float value) {
|
|
419
610
|
imageZoom = value;
|
|
@@ -421,16 +612,11 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
421
612
|
View child = recyclerView.getChildAt(i);
|
|
422
613
|
RecyclerView.ViewHolder holder = recyclerView.getChildViewHolder(child);
|
|
423
614
|
if (holder instanceof ComicHolder) {
|
|
424
|
-
|
|
615
|
+
ComicHolder comicHolder = (ComicHolder) holder;
|
|
616
|
+
ImageView image = comicHolder.image;
|
|
425
617
|
if ("continuous".equals(layoutMode)) {
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
child.setLayoutParams(params);
|
|
429
|
-
image.setScaleX(1f); image.setScaleY(1f);
|
|
430
|
-
if (image.getDrawable() instanceof android.graphics.drawable.BitmapDrawable) {
|
|
431
|
-
Bitmap bitmap = ((android.graphics.drawable.BitmapDrawable) image.getDrawable()).getBitmap();
|
|
432
|
-
applyPageHeightForImage((ComicHolder) holder, bitmap.getWidth(), bitmap.getHeight());
|
|
433
|
-
}
|
|
618
|
+
int position = recyclerView.getChildAdapterPosition(child);
|
|
619
|
+
updatePageGeometryForZoom(comicHolder, position, value);
|
|
434
620
|
} else {
|
|
435
621
|
image.setScaleX(value); image.setScaleY(value);
|
|
436
622
|
}
|
|
@@ -478,14 +664,64 @@ public final class PapyrusComicDocumentView extends FrameLayout {
|
|
|
478
664
|
}
|
|
479
665
|
}
|
|
480
666
|
|
|
667
|
+
private static final class ComicPageScrollView extends ScrollView {
|
|
668
|
+
private boolean fixedPageHeight;
|
|
669
|
+
|
|
670
|
+
ComicPageScrollView(@NonNull Context context) { super(context); }
|
|
671
|
+
|
|
672
|
+
void setFixedPageHeight(boolean fixed) {
|
|
673
|
+
if (fixedPageHeight == fixed) return;
|
|
674
|
+
fixedPageHeight = fixed;
|
|
675
|
+
requestLayout();
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// ScrollView normally measures its child with an unspecified height so it can scroll.
|
|
679
|
+
// Continuous comics already scroll between RecyclerView cells, so that would let the
|
|
680
|
+
// bitmap's intrinsic height exceed the cell and create artificial bands around the art.
|
|
681
|
+
@Override protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
|
|
682
|
+
ViewGroup.LayoutParams params = child.getLayoutParams();
|
|
683
|
+
if (!fixedPageHeight || params.height <= 0) {
|
|
684
|
+
super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
int childWidthSpec = getChildMeasureSpec(parentWidthMeasureSpec,
|
|
688
|
+
getPaddingLeft() + getPaddingRight(), params.width);
|
|
689
|
+
int childHeightSpec = MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY);
|
|
690
|
+
child.measure(childWidthSpec, childHeightSpec);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
@Override protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
|
|
694
|
+
int parentHeightMeasureSpec, int heightUsed) {
|
|
695
|
+
ViewGroup.LayoutParams rawParams = child.getLayoutParams();
|
|
696
|
+
if (!fixedPageHeight || rawParams.height <= 0 || !(rawParams instanceof MarginLayoutParams)) {
|
|
697
|
+
super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
|
|
698
|
+
return;
|
|
699
|
+
}
|
|
700
|
+
MarginLayoutParams params = (MarginLayoutParams) rawParams;
|
|
701
|
+
int childWidthSpec = getChildMeasureSpec(parentWidthMeasureSpec,
|
|
702
|
+
getPaddingLeft() + getPaddingRight() + params.leftMargin + params.rightMargin + widthUsed, params.width);
|
|
703
|
+
int childHeightSpec = MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY);
|
|
704
|
+
child.measure(childWidthSpec, childHeightSpec);
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
|
|
481
708
|
private static final class ComicHolder extends RecyclerView.ViewHolder {
|
|
482
|
-
final
|
|
709
|
+
final ComicPageScrollView pageScrollView;
|
|
483
710
|
final ImageView image;
|
|
484
711
|
int bindToken;
|
|
485
|
-
|
|
712
|
+
String configuredLayoutMode;
|
|
713
|
+
ComicHolder(@NonNull View itemView, ComicPageScrollView pageScrollView, ImageView image) {
|
|
486
714
|
super(itemView);
|
|
487
715
|
this.pageScrollView = pageScrollView;
|
|
488
716
|
this.image = image;
|
|
489
717
|
}
|
|
490
718
|
}
|
|
719
|
+
|
|
720
|
+
private static final class PageDimensions {
|
|
721
|
+
static final PageDimensions UNKNOWN = new PageDimensions(0, 0);
|
|
722
|
+
final int width;
|
|
723
|
+
final int height;
|
|
724
|
+
PageDimensions(int width, int height) { this.width = width; this.height = height; }
|
|
725
|
+
boolean isKnown() { return width > 0 && height > 0; }
|
|
726
|
+
}
|
|
491
727
|
}
|
|
@@ -16,6 +16,7 @@ public final class PapyrusComicDocumentViewManager extends SimpleViewManager<Pap
|
|
|
16
16
|
@Nullable @Override public Map<String, Object> getExportedCustomBubblingEventTypeConstants() {
|
|
17
17
|
return MapBuilder.<String, Object>builder()
|
|
18
18
|
.put("onPageChanged", MapBuilder.of("phasedRegistrationNames", MapBuilder.of("bubbled", "onPageChanged")))
|
|
19
|
+
.put("onPageTap", MapBuilder.of("phasedRegistrationNames", MapBuilder.of("bubbled", "onPageTap")))
|
|
19
20
|
.put("onZoomChanged", MapBuilder.of("phasedRegistrationNames", MapBuilder.of("bubbled", "onZoomChanged")))
|
|
20
21
|
.put("onError", MapBuilder.of("phasedRegistrationNames", MapBuilder.of("bubbled", "onError")))
|
|
21
22
|
.build();
|
|
@@ -22,11 +22,28 @@ final class PapyrusComicPageLayout {
|
|
|
22
22
|
int base = itemHeight(imageWidth, imageHeight, viewportWidth, viewportHeight, layoutMode, fitMode);
|
|
23
23
|
return "continuous".equals(layoutMode) ? Math.max(1, Math.round(base * Math.max(1f, Math.min(5f, zoom)))) : base;
|
|
24
24
|
}
|
|
25
|
+
static int zoomedFallbackHeight(int viewportWidth, int viewportHeight,
|
|
26
|
+
String layoutMode, String fitMode, float zoom) {
|
|
27
|
+
return zoomedHeight(0, 0, viewportWidth, viewportHeight, layoutMode, fitMode, zoom);
|
|
28
|
+
}
|
|
25
29
|
static float clampPanX(float x, int viewportWidth, float zoom) {
|
|
26
30
|
return Math.max(-Math.max(0, zoomedWidth(viewportWidth, zoom) - viewportWidth), Math.min(0f, x));
|
|
27
31
|
}
|
|
28
|
-
static int
|
|
29
|
-
|
|
32
|
+
static int zoomAnchorTopForItem(int oldTop, float focusY, int oldHeight, float oldZoom, float newZoom) {
|
|
33
|
+
int safeOldHeight = Math.max(1, oldHeight);
|
|
34
|
+
float fraction = Math.max(0f, Math.min(1f, (focusY - oldTop) / safeOldHeight));
|
|
35
|
+
float scale = Math.max(1f, newZoom) / Math.max(1f, oldZoom);
|
|
36
|
+
int newHeight = Math.max(1, Math.round(oldHeight * scale));
|
|
37
|
+
return Math.round(focusY - fraction * newHeight);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static boolean canRestoreAnchor(String sourceEngineId, int sourceGeneration,
|
|
41
|
+
String currentEngineId, int currentGeneration, boolean continuous,
|
|
42
|
+
int position, int currentPageCount) {
|
|
43
|
+
boolean sameEngine = sourceEngineId == null
|
|
44
|
+
? currentEngineId == null : sourceEngineId.equals(currentEngineId);
|
|
45
|
+
return sameEngine && sourceGeneration == currentGeneration && continuous &&
|
|
46
|
+
position >= 0 && position < currentPageCount;
|
|
30
47
|
}
|
|
31
48
|
|
|
32
49
|
static float panForLayout(float x, int viewportWidth, float zoom, boolean toContinuous) {
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
package com.papyrus.engine;
|
|
2
|
+
|
|
3
|
+
import java.util.concurrent.ArrayBlockingQueue;
|
|
4
|
+
import java.util.concurrent.ThreadPoolExecutor;
|
|
5
|
+
import java.util.concurrent.TimeUnit;
|
|
6
|
+
|
|
7
|
+
/** Keeps optional thumbnail work bounded and away from the archive load queue. */
|
|
8
|
+
final class PapyrusComicPreviewExecutor {
|
|
9
|
+
static final String QUEUE_FULL_ERROR_CODE = "E_COMIC_PREVIEW_QUEUE_FULL";
|
|
10
|
+
|
|
11
|
+
private PapyrusComicPreviewExecutor() {}
|
|
12
|
+
|
|
13
|
+
static ThreadPoolExecutor create() {
|
|
14
|
+
return new ThreadPoolExecutor(
|
|
15
|
+
1,
|
|
16
|
+
1,
|
|
17
|
+
30L,
|
|
18
|
+
TimeUnit.SECONDS,
|
|
19
|
+
new ArrayBlockingQueue<>(2),
|
|
20
|
+
runnable -> {
|
|
21
|
+
Thread thread = new Thread(runnable, "papyrus-comic-preview");
|
|
22
|
+
thread.setPriority(Thread.MIN_PRIORITY);
|
|
23
|
+
return thread;
|
|
24
|
+
},
|
|
25
|
+
new ThreadPoolExecutor.AbortPolicy()
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
package com.papyrus.engine;
|
|
2
|
+
|
|
3
|
+
/** Coalesces high-frequency scale callbacks into one geometry update per display frame. */
|
|
4
|
+
final class PapyrusComicZoomFrame {
|
|
5
|
+
private Update pending;
|
|
6
|
+
|
|
7
|
+
boolean offer(float zoom, float focusX, float focusY) {
|
|
8
|
+
boolean shouldScheduleFrame = pending == null;
|
|
9
|
+
pending = new Update(zoom, focusX, focusY);
|
|
10
|
+
return shouldScheduleFrame;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
float nextZoom(float committedZoom, float scaleFactor) {
|
|
14
|
+
float baseZoom = pending == null ? committedZoom : pending.zoom;
|
|
15
|
+
return Math.max(1f, Math.min(5f, baseZoom * scaleFactor));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
Update takePending() {
|
|
19
|
+
Update update = pending;
|
|
20
|
+
pending = null;
|
|
21
|
+
return update;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
void clear() {
|
|
25
|
+
pending = null;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static final class Update {
|
|
29
|
+
final float zoom;
|
|
30
|
+
final float focusX;
|
|
31
|
+
final float focusY;
|
|
32
|
+
|
|
33
|
+
Update(float zoom, float focusX, float focusY) {
|
|
34
|
+
this.zoom = zoom;
|
|
35
|
+
this.focusX = focusX;
|
|
36
|
+
this.focusY = focusY;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|