@papyrus-sdk/engine-native 0.2.23 → 0.2.25

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.
@@ -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
- // Keep gesture frames native. Reflect the final zoom to React Native
76
- // only once; per-frame bridge writes can feed stale zoom back into
77
- // the view and cause visible jitter during a pinch.
78
- applyZoom(zoom * detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY());
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,6 +90,7 @@ 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;
@@ -105,9 +110,32 @@ public final class PapyrusComicDocumentView extends FrameLayout {
105
110
  }
106
111
  }
107
112
 
108
- public void setEngineId(String value) { engineId = value; adapter.notifyDataSetChanged(); }
109
- public void setDocumentGeneration(int value) { if (generation != value) { generation = value; adapter.notifyDataSetChanged(); } }
110
- public void setPageCount(int value) { pageCount = Math.max(0, value); adapter.notifyDataSetChanged(); scrollToCurrentPage(); }
113
+ public void setEngineId(String value) {
114
+ if (value == null ? engineId != null : !value.equals(engineId)) {
115
+ zoomFrame.clear();
116
+ adapter.clearPageDimensions();
117
+ }
118
+ engineId = value;
119
+ adapter.notifyDataSetChanged();
120
+ }
121
+ public void setDocumentGeneration(int value) {
122
+ if (generation != value) {
123
+ zoomFrame.clear();
124
+ generation = value;
125
+ adapter.clearPageDimensions();
126
+ adapter.notifyDataSetChanged();
127
+ }
128
+ }
129
+ public void setPageCount(int value) {
130
+ int nextCount = Math.max(0, value);
131
+ if (pageCount != nextCount) {
132
+ zoomFrame.clear();
133
+ adapter.clearPageDimensions();
134
+ }
135
+ pageCount = nextCount;
136
+ adapter.notifyDataSetChanged();
137
+ scrollToCurrentPage();
138
+ }
111
139
  public void setCurrentPage(int value) {
112
140
  int clamped = pageCount > 0 ? Math.max(1, Math.min(pageCount, value)) : Math.max(1, value);
113
141
  if (clamped != currentPage) { currentPage = clamped; scrollToCurrentPage(); }
@@ -115,6 +143,7 @@ public final class PapyrusComicDocumentView extends FrameLayout {
115
143
  public void setLayoutMode(String value) {
116
144
  String nextMode = "continuous".equals(value) ? "continuous" : "single";
117
145
  if (!nextMode.equals(layoutMode)) {
146
+ applyPendingZoom();
118
147
  panX = PapyrusComicPageLayout.panForLayout(panX, recyclerView.getWidth(), zoom, "continuous".equals(nextMode));
119
148
  panY = 0;
120
149
  adapter.setPan(panX, panY);
@@ -133,29 +162,68 @@ public final class PapyrusComicDocumentView extends FrameLayout {
133
162
  public void setFitMode(String value) { fitMode = value == null ? "width" : value; adapter.notifyDataSetChanged(); }
134
163
  public void setReadingDirection(String value) { readingDirection = "rtl".equals(value) ? "rtl" : "ltr"; setLayoutMode(layoutMode); }
135
164
  public void setZoom(float value) {
165
+ zoomFrame.clear();
136
166
  applyZoom(value, recyclerView.getWidth() / 2f, recyclerView.getHeight() / 2f);
137
167
  }
168
+ private void applyPendingZoom() {
169
+ PapyrusComicZoomFrame.Update update = zoomFrame.takePending();
170
+ if (update != null) applyZoom(update.zoom, update.focusX, update.focusY);
171
+ }
172
+ private void restoreVisibleAnchor(String sourceEngineId, int sourceGeneration,
173
+ LinearLayoutManager sourceLayoutManager, int position, int top) {
174
+ if (sourceLayoutManager != layoutManager ||
175
+ !PapyrusComicPageLayout.canRestoreAnchor(sourceEngineId, sourceGeneration,
176
+ engineId, generation, "continuous".equals(layoutMode), position, pageCount)) return;
177
+ sourceLayoutManager.scrollToPositionWithOffset(position, top);
178
+ }
138
179
  private void applyZoom(float value, float focusX, float focusY) {
139
180
  float next = Math.max(1f, Math.min(5f, value));
140
181
  if (Math.abs(next - zoom) < 0.0001f) return;
141
182
  float previous = zoom;
142
- int position = layoutManager == null ? RecyclerView.NO_POSITION : layoutManager.findFirstVisibleItemPosition();
143
- View anchor = position == RecyclerView.NO_POSITION ? null : layoutManager.findViewByPosition(position);
183
+ View anchor = "continuous".equals(layoutMode) ? recyclerView.findChildViewUnder(focusX, focusY) : null;
184
+ if ("continuous".equals(layoutMode) && anchor == null) {
185
+ anchor = findClosestVisiblePage(focusX, focusY);
186
+ }
187
+ int position = anchor == null ? RecyclerView.NO_POSITION : recyclerView.getChildAdapterPosition(anchor);
144
188
  int top = anchor == null ? 0 : anchor.getTop();
189
+ int height = anchor == null ? 0 : anchor.getHeight();
145
190
  zoom = next;
146
191
  if ("continuous".equals(layoutMode)) {
147
192
  panX = PapyrusComicPageLayout.clampPanX(focusX - (focusX - panX) * next / previous, recyclerView.getWidth(), next);
148
193
  panY = 0;
149
194
  adapter.setZoom(next);
150
195
  adapter.setPan(panX, 0);
151
- if (anchor != null) layoutManager.scrollToPositionWithOffset(position,
152
- PapyrusComicPageLayout.zoomAnchorTop(top, focusY, previous, next));
196
+ if (layoutManager != null && position != RecyclerView.NO_POSITION && height > 0) {
197
+ layoutManager.scrollToPositionWithOffset(position,
198
+ PapyrusComicPageLayout.zoomAnchorTopForItem(top, focusY, height, previous, next));
199
+ }
153
200
  recyclerView.requestLayout();
154
201
  } else {
155
202
  if (next == 1f) { panX = 0; panY = 0; adapter.setPan(0, 0); }
156
203
  adapter.setZoom(next);
157
204
  }
158
205
  }
206
+
207
+ private View findClosestVisiblePage(float focusX, float focusY) {
208
+ View closest = null;
209
+ float bestDistance = Float.MAX_VALUE;
210
+ for (int i = 0; i < recyclerView.getChildCount(); i++) {
211
+ View child = recyclerView.getChildAt(i);
212
+ float left = child.getLeft() + child.getTranslationX();
213
+ float top = child.getTop() + child.getTranslationY();
214
+ float right = left + child.getWidth();
215
+ float bottom = top + child.getHeight();
216
+ float dx = focusX < left ? left - focusX : focusX > right ? focusX - right : 0f;
217
+ float dy = focusY < top ? top - focusY : focusY > bottom ? focusY - bottom : 0f;
218
+ float distance = dx * dx + dy * dy;
219
+ if (distance < bestDistance) {
220
+ bestDistance = distance;
221
+ closest = child;
222
+ }
223
+ }
224
+ return closest;
225
+ }
226
+
159
227
  public void setPageTheme(String value) { pageTheme = value == null ? "normal" : value; applyTheme(); }
160
228
 
161
229
  @Override public boolean dispatchTouchEvent(android.view.MotionEvent event) {
@@ -264,11 +332,50 @@ public final class PapyrusComicDocumentView extends FrameLayout {
264
332
  private float imageZoom = 1f;
265
333
  private float imagePanX;
266
334
  private float imagePanY;
335
+ private final SparseArray<PageDimensions> pageDimensions = new SparseArray<>();
336
+
337
+ void clearPageDimensions() { pageDimensions.clear(); }
338
+
339
+ private PageDimensions pageDimensionsFor(int position) {
340
+ if (position < 0 || position >= pageCount || engineId == null) return null;
341
+ PageDimensions cached = pageDimensions.get(position);
342
+ if (cached != null) return cached.isKnown() ? cached : null;
343
+ int[] dimensions = PapyrusComicArchiveNative.getPageDimensions(engineId, generation, position);
344
+ PageDimensions result = dimensions != null && dimensions.length >= 2 && dimensions[0] > 0 && dimensions[1] > 0
345
+ ? new PageDimensions(dimensions[0], dimensions[1]) : PageDimensions.UNKNOWN;
346
+ pageDimensions.put(position, result);
347
+ return result.isKnown() ? result : null;
348
+ }
349
+
350
+ private PageDimensions layoutDimensionsFor(int position) {
351
+ PageDimensions exact = pageDimensionsFor(position);
352
+ if (exact != null) return exact;
353
+ for (int distance = 1; distance <= 8; distance++) {
354
+ PageDimensions before = pageDimensionsFor(position - distance);
355
+ if (before != null) return before;
356
+ PageDimensions after = pageDimensionsFor(position + distance);
357
+ if (after != null) return after;
358
+ }
359
+ return null;
360
+ }
361
+
362
+ private int itemHeightFor(int position) {
363
+ PageDimensions dimensions = layoutDimensionsFor(position);
364
+ return PapyrusComicPageLayout.zoomedHeight(
365
+ dimensions == null ? 0 : dimensions.width,
366
+ dimensions == null ? 0 : dimensions.height,
367
+ recyclerView.getWidth(), recyclerView.getHeight(), layoutMode, fitMode, zoom);
368
+ }
369
+
370
+ private void rememberPageDimensions(int position, int width, int height) {
371
+ if (position >= 0 && width > 0 && height > 0) pageDimensions.put(position, new PageDimensions(width, height));
372
+ }
373
+
267
374
  @NonNull @Override public ComicHolder onCreateViewHolder(@NonNull android.view.ViewGroup parent, int type) {
268
375
  FrameLayout cell = new FrameLayout(parent.getContext());
269
376
  cell.setLayoutParams(new RecyclerView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
270
377
  cell.setBackgroundColor(Color.TRANSPARENT);
271
- ScrollView pageScrollView = new ScrollView(parent.getContext());
378
+ ComicPageScrollView pageScrollView = new ComicPageScrollView(parent.getContext());
272
379
  pageScrollView.setFillViewport(true);
273
380
  pageScrollView.setVerticalScrollBarEnabled(false);
274
381
  ImageView image = new ImageView(parent.getContext());
@@ -280,7 +387,7 @@ public final class PapyrusComicDocumentView extends FrameLayout {
280
387
  @Override public void onBindViewHolder(@NonNull ComicHolder holder, int position) {
281
388
  RecyclerView.LayoutParams itemParams = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
282
389
  itemParams.width = "continuous".equals(layoutMode) ? PapyrusComicPageLayout.zoomedWidth(recyclerView.getWidth(), zoom) : Math.max(1, recyclerView.getWidth());
283
- itemParams.height = Math.max(1, recyclerView.getHeight());
390
+ itemParams.height = "continuous".equals(layoutMode) ? itemHeightFor(position) : Math.max(1, recyclerView.getHeight());
284
391
  holder.itemView.setLayoutParams(itemParams);
285
392
  final int logicalPage = position;
286
393
  final String id = engineId;
@@ -293,12 +400,15 @@ public final class PapyrusComicDocumentView extends FrameLayout {
293
400
  holder.image.setTranslationX(continuous ? 0f : imagePanX);
294
401
  holder.image.setTranslationY(continuous ? 0f : imagePanY);
295
402
  holder.itemView.setTranslationX(continuous ? imagePanX : 0f);
403
+ PageDimensions indexedDimensions = layoutDimensionsFor(position);
404
+ configurePageImage(holder, indexedDimensions == null ? 0 : indexedDimensions.width,
405
+ indexedDimensions == null ? 0 : indexedDimensions.height);
296
406
  String key = id + ":" + docGeneration + ":" + logicalPage;
297
407
  Bitmap cached = BITMAPS.get(key);
298
408
  if (cached != null) {
299
- applyPageHeightForImage(holder, cached.getWidth(), cached.getHeight());
409
+ applyPageHeightForImage(holder, logicalPage, cached.getWidth(), cached.getHeight(), false);
300
410
  holder.image.setImageBitmap(cached);
301
- configurePageImage(holder, cached);
411
+ configurePageImage(holder, cached.getWidth(), cached.getHeight());
302
412
  return;
303
413
  }
304
414
  DECODE_EXECUTOR.execute(() -> {
@@ -318,7 +428,9 @@ public final class PapyrusComicDocumentView extends FrameLayout {
318
428
  }
319
429
  if (bounds.outWidth > 0 && bounds.outHeight > 0) {
320
430
  post(() -> {
321
- if (holder.bindToken == token && id.equals(engineId) && docGeneration == generation) applyPageHeightForImage(holder, bounds.outWidth, bounds.outHeight);
431
+ if (holder.bindToken == token && id.equals(engineId) && docGeneration == generation) {
432
+ applyPageHeightForImage(holder, logicalPage, bounds.outWidth, bounds.outHeight, true);
433
+ }
322
434
  });
323
435
  int maxEdge = Math.max(getWidth(), getHeight()) * 2;
324
436
  int sample = 1;
@@ -337,7 +449,7 @@ public final class PapyrusComicDocumentView extends FrameLayout {
337
449
  post(() -> {
338
450
  if (holder.bindToken == token && id.equals(engineId) && docGeneration == generation) {
339
451
  holder.image.setImageBitmap(result);
340
- configurePageImage(holder, result);
452
+ configurePageImage(holder, result.getWidth(), result.getHeight());
341
453
  }
342
454
  });
343
455
  });
@@ -356,64 +468,132 @@ public final class PapyrusComicDocumentView extends FrameLayout {
356
468
  holder.image.setScaleY(continuous ? 1f : imageZoom);
357
469
  holder.image.setTranslationX(continuous ? 0f : imagePanX);
358
470
  holder.image.setTranslationY(continuous ? 0f : imagePanY);
359
- if (holder.image.getDrawable() instanceof android.graphics.drawable.BitmapDrawable) {
471
+ int position = holder.getBindingAdapterPosition();
472
+ PageDimensions indexedDimensions = layoutDimensionsFor(position);
473
+ if (indexedDimensions != null && continuous) {
474
+ applyPageHeightForDimensions(holder, position, indexedDimensions.width, indexedDimensions.height);
475
+ } else if (holder.image.getDrawable() instanceof android.graphics.drawable.BitmapDrawable) {
360
476
  Bitmap bitmap = ((android.graphics.drawable.BitmapDrawable) holder.image.getDrawable()).getBitmap();
361
- applyPageHeightForImage(holder, bitmap.getWidth(), bitmap.getHeight());
477
+ applyPageHeightForImage(holder, position, bitmap.getWidth(), bitmap.getHeight(), false);
478
+ } else if (continuous) {
479
+ applyPageHeightForDimensions(holder, position, 0, 0);
362
480
  }
363
481
  holder.itemView.forceLayout();
364
482
  recyclerView.requestLayout();
365
483
  }
366
- private void applyPageHeightForImage(ComicHolder holder, int imageWidth, int imageHeight) {
484
+ private void applyPageHeightForImage(ComicHolder holder, int position, int imageWidth, int imageHeight, boolean preserveVisibleAnchor) {
485
+ int oldHeight = holder.itemView.getLayoutParams() instanceof RecyclerView.LayoutParams
486
+ ? ((RecyclerView.LayoutParams) holder.itemView.getLayoutParams()).height : 0;
487
+ int anchorPosition = RecyclerView.NO_POSITION;
488
+ int anchorTop = 0;
489
+ if (preserveVisibleAnchor && "continuous".equals(layoutMode) && layoutManager != null) {
490
+ anchorPosition = layoutManager.findFirstVisibleItemPosition();
491
+ View anchorView = anchorPosition == RecyclerView.NO_POSITION ? null : layoutManager.findViewByPosition(anchorPosition);
492
+ if (anchorView != null) anchorTop = anchorView.getTop();
493
+ }
494
+ rememberPageDimensions(position, imageWidth, imageHeight);
495
+ applyPageHeightForDimensions(holder, position, imageWidth, imageHeight);
496
+ int newHeight = holder.itemView.getLayoutParams() instanceof RecyclerView.LayoutParams
497
+ ? ((RecyclerView.LayoutParams) holder.itemView.getLayoutParams()).height : oldHeight;
498
+ if (preserveVisibleAnchor && oldHeight != newHeight && position >= 0 &&
499
+ anchorPosition != RecyclerView.NO_POSITION && position <= anchorPosition && layoutManager != null) {
500
+ final int preservePosition = anchorPosition;
501
+ final int preserveTop = anchorTop;
502
+ final String sourceEngineId = engineId;
503
+ final int sourceGeneration = generation;
504
+ final LinearLayoutManager sourceLayoutManager = layoutManager;
505
+ recyclerView.post(() -> restoreVisibleAnchor(sourceEngineId, sourceGeneration,
506
+ sourceLayoutManager, preservePosition, preserveTop));
507
+ }
508
+ }
509
+
510
+ private void applyPageHeightForDimensions(ComicHolder holder, int position, int imageWidth, int imageHeight) {
367
511
  if (!(holder.itemView.getLayoutParams() instanceof RecyclerView.LayoutParams)) return;
368
512
  RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
369
- int height = PapyrusComicPageLayout.zoomedHeight(
370
- imageWidth,
371
- imageHeight,
372
- recyclerView.getWidth(),
373
- recyclerView.getHeight(),
374
- layoutMode,
375
- fitMode,
376
- zoom
377
- );
513
+ int height;
514
+ if ("continuous".equals(layoutMode)) {
515
+ PageDimensions dimensions = imageWidth > 0 && imageHeight > 0
516
+ ? new PageDimensions(imageWidth, imageHeight) : layoutDimensionsFor(position);
517
+ height = dimensions == null ? PapyrusComicPageLayout.zoomedFallbackHeight(
518
+ recyclerView.getWidth(), recyclerView.getHeight(), layoutMode, fitMode, zoom) : PapyrusComicPageLayout.zoomedHeight(
519
+ dimensions.width, dimensions.height, recyclerView.getWidth(), recyclerView.getHeight(), layoutMode, fitMode, zoom);
520
+ } else {
521
+ height = Math.max(1, recyclerView.getHeight());
522
+ }
378
523
  if (params.height != height) {
379
524
  params.height = height;
380
525
  holder.itemView.setLayoutParams(params);
381
526
  }
527
+ configurePageImage(holder, imageWidth, imageHeight);
382
528
  }
383
529
 
384
- private void configurePageImage(ComicHolder holder, Bitmap bitmap) {
530
+ private void updatePageGeometryForZoom(ComicHolder holder, int position, float value) {
531
+ if (position < 0 || !(holder.itemView.getLayoutParams() instanceof RecyclerView.LayoutParams)) return;
532
+ PageDimensions dimensions = layoutDimensionsFor(position);
533
+ if (dimensions == null && holder.image.getDrawable() instanceof android.graphics.drawable.BitmapDrawable) {
534
+ Bitmap bitmap = ((android.graphics.drawable.BitmapDrawable) holder.image.getDrawable()).getBitmap();
535
+ dimensions = new PageDimensions(bitmap.getWidth(), bitmap.getHeight());
536
+ }
537
+
538
+ RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
539
+ int width = PapyrusComicPageLayout.zoomedWidth(recyclerView.getWidth(), value);
540
+ int height = dimensions == null ? PapyrusComicPageLayout.zoomedFallbackHeight(
541
+ recyclerView.getWidth(), recyclerView.getHeight(), layoutMode, fitMode, value) : PapyrusComicPageLayout.zoomedHeight(
542
+ dimensions.width, dimensions.height, recyclerView.getWidth(), recyclerView.getHeight(), layoutMode, fitMode, value);
543
+ boolean changed = params.width != width || params.height != height;
544
+ if (params.width != width) params.width = width;
545
+ if (params.height != height) params.height = height;
546
+ if (changed) holder.itemView.setLayoutParams(params);
547
+ holder.image.setScaleX(1f);
548
+ holder.image.setScaleY(1f);
549
+ configurePageImage(holder, dimensions == null ? 0 : dimensions.width, dimensions == null ? 0 : dimensions.height);
550
+ }
551
+
552
+ private void configurePageImage(ComicHolder holder, int imageWidth, int imageHeight) {
385
553
  boolean widthFit = "single".equals(layoutMode) && "width".equals(fitMode) && recyclerView.getWidth() > 0;
386
554
  ScrollView.LayoutParams params;
387
- FrameLayout cell = (FrameLayout) holder.itemView;
388
555
  if ("continuous".equals(layoutMode)) {
389
- if (holder.image.getParent() != cell) {
390
- ((android.view.ViewGroup) holder.image.getParent()).removeView(holder.image);
391
- cell.addView(holder.image, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
392
- }
393
- holder.pageScrollView.setVisibility(View.GONE);
556
+ holder.pageScrollView.setVisibility(View.VISIBLE);
557
+ holder.pageScrollView.setFixedPageHeight(true);
394
558
  holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
395
- holder.pageScrollView.scrollTo(0, 0);
559
+ if (!"continuous".equals(holder.configuredLayoutMode)) holder.pageScrollView.scrollTo(0, 0);
560
+ holder.pageScrollView.setVerticalScrollBarEnabled(false);
561
+ holder.pageScrollView.setFillViewport(false);
562
+ RecyclerView.LayoutParams itemParams = (RecyclerView.LayoutParams) holder.itemView.getLayoutParams();
563
+ int pageHeight = itemParams.height > 0 ? itemParams.height : Math.max(1, recyclerView.getHeight());
564
+ setImageLayoutParamsIfChanged(holder, new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, pageHeight));
565
+ holder.configuredLayoutMode = layoutMode;
396
566
  return;
397
567
  }
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
568
  holder.pageScrollView.setVisibility(View.VISIBLE);
403
- if (widthFit) {
569
+ holder.pageScrollView.setFixedPageHeight(false);
570
+ if (widthFit && imageWidth > 0 && imageHeight > 0) {
404
571
  int width = recyclerView.getWidth();
405
- int height = Math.max(1, Math.round(width * ((float) bitmap.getHeight() / (float) bitmap.getWidth())));
572
+ int height = Math.max(1, Math.round(width * ((float) imageHeight / (float) imageWidth)));
406
573
  params = new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, height);
407
574
  holder.image.setScaleType(ImageView.ScaleType.FIT_XY);
408
575
  holder.pageScrollView.setVerticalScrollBarEnabled(height > recyclerView.getHeight());
409
576
  holder.pageScrollView.setFillViewport(false);
577
+ } else if (widthFit) {
578
+ params = new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, Math.max(1, recyclerView.getHeight()));
579
+ holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
580
+ holder.pageScrollView.setVerticalScrollBarEnabled(false);
581
+ holder.pageScrollView.setFillViewport(true);
410
582
  } else {
411
583
  params = new ScrollView.LayoutParams(LayoutParams.MATCH_PARENT, Math.max(1, recyclerView.getHeight()));
412
584
  holder.image.setScaleType(ImageView.ScaleType.FIT_CENTER);
413
585
  holder.pageScrollView.setVerticalScrollBarEnabled(false);
414
586
  holder.pageScrollView.setFillViewport(true);
415
587
  }
416
- holder.image.setLayoutParams(params);
588
+ setImageLayoutParamsIfChanged(holder, params);
589
+ holder.configuredLayoutMode = layoutMode;
590
+ }
591
+
592
+ private void setImageLayoutParamsIfChanged(ComicHolder holder, ScrollView.LayoutParams params) {
593
+ ViewGroup.LayoutParams current = holder.image.getLayoutParams();
594
+ if (current == null || current.width != params.width || current.height != params.height) {
595
+ holder.image.setLayoutParams(params);
596
+ }
417
597
  }
418
598
  void setZoom(float value) {
419
599
  imageZoom = value;
@@ -421,16 +601,11 @@ public final class PapyrusComicDocumentView extends FrameLayout {
421
601
  View child = recyclerView.getChildAt(i);
422
602
  RecyclerView.ViewHolder holder = recyclerView.getChildViewHolder(child);
423
603
  if (holder instanceof ComicHolder) {
424
- ImageView image = ((ComicHolder) holder).image;
604
+ ComicHolder comicHolder = (ComicHolder) holder;
605
+ ImageView image = comicHolder.image;
425
606
  if ("continuous".equals(layoutMode)) {
426
- RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
427
- params.width = PapyrusComicPageLayout.zoomedWidth(recyclerView.getWidth(), value);
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
- }
607
+ int position = recyclerView.getChildAdapterPosition(child);
608
+ updatePageGeometryForZoom(comicHolder, position, value);
434
609
  } else {
435
610
  image.setScaleX(value); image.setScaleY(value);
436
611
  }
@@ -478,14 +653,64 @@ public final class PapyrusComicDocumentView extends FrameLayout {
478
653
  }
479
654
  }
480
655
 
656
+ private static final class ComicPageScrollView extends ScrollView {
657
+ private boolean fixedPageHeight;
658
+
659
+ ComicPageScrollView(@NonNull Context context) { super(context); }
660
+
661
+ void setFixedPageHeight(boolean fixed) {
662
+ if (fixedPageHeight == fixed) return;
663
+ fixedPageHeight = fixed;
664
+ requestLayout();
665
+ }
666
+
667
+ // ScrollView normally measures its child with an unspecified height so it can scroll.
668
+ // Continuous comics already scroll between RecyclerView cells, so that would let the
669
+ // bitmap's intrinsic height exceed the cell and create artificial bands around the art.
670
+ @Override protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) {
671
+ ViewGroup.LayoutParams params = child.getLayoutParams();
672
+ if (!fixedPageHeight || params.height <= 0) {
673
+ super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
674
+ return;
675
+ }
676
+ int childWidthSpec = getChildMeasureSpec(parentWidthMeasureSpec,
677
+ getPaddingLeft() + getPaddingRight(), params.width);
678
+ int childHeightSpec = MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY);
679
+ child.measure(childWidthSpec, childHeightSpec);
680
+ }
681
+
682
+ @Override protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
683
+ int parentHeightMeasureSpec, int heightUsed) {
684
+ ViewGroup.LayoutParams rawParams = child.getLayoutParams();
685
+ if (!fixedPageHeight || rawParams.height <= 0 || !(rawParams instanceof MarginLayoutParams)) {
686
+ super.measureChildWithMargins(child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
687
+ return;
688
+ }
689
+ MarginLayoutParams params = (MarginLayoutParams) rawParams;
690
+ int childWidthSpec = getChildMeasureSpec(parentWidthMeasureSpec,
691
+ getPaddingLeft() + getPaddingRight() + params.leftMargin + params.rightMargin + widthUsed, params.width);
692
+ int childHeightSpec = MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY);
693
+ child.measure(childWidthSpec, childHeightSpec);
694
+ }
695
+ }
696
+
481
697
  private static final class ComicHolder extends RecyclerView.ViewHolder {
482
- final ScrollView pageScrollView;
698
+ final ComicPageScrollView pageScrollView;
483
699
  final ImageView image;
484
700
  int bindToken;
485
- ComicHolder(@NonNull View itemView, ScrollView pageScrollView, ImageView image) {
701
+ String configuredLayoutMode;
702
+ ComicHolder(@NonNull View itemView, ComicPageScrollView pageScrollView, ImageView image) {
486
703
  super(itemView);
487
704
  this.pageScrollView = pageScrollView;
488
705
  this.image = image;
489
706
  }
490
707
  }
708
+
709
+ private static final class PageDimensions {
710
+ static final PageDimensions UNKNOWN = new PageDimensions(0, 0);
711
+ final int width;
712
+ final int height;
713
+ PageDimensions(int width, int height) { this.width = width; this.height = height; }
714
+ boolean isKnown() { return width > 0 && height > 0; }
715
+ }
491
716
  }
@@ -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 zoomAnchorTop(int oldTop, float focusY, float oldZoom, float newZoom) {
29
- return Math.round(focusY + (oldTop - focusY) * newZoom / Math.max(1f, oldZoom));
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
+ }
@@ -38,6 +38,8 @@ import java.util.List;
38
38
  import java.util.concurrent.ConcurrentHashMap;
39
39
  import java.util.concurrent.ExecutorService;
40
40
  import java.util.concurrent.Executors;
41
+ import java.util.concurrent.RejectedExecutionException;
42
+ import java.util.concurrent.ThreadPoolExecutor;
41
43
 
42
44
  public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
43
45
  private static final Object COMIC_THUMBNAIL_CACHE_LOCK = new Object();
@@ -46,6 +48,7 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
46
48
  private static final long MAX_COMIC_ARCHIVE_BYTES = 512L * 1024L * 1024L;
47
49
  private final ReactApplicationContext reactContext;
48
50
  private final ExecutorService executor = Executors.newSingleThreadExecutor();
51
+ private final ThreadPoolExecutor comicPreviewExecutor = PapyrusComicPreviewExecutor.create();
49
52
  private final ConcurrentHashMap<String, File> comicSourceFiles = new ConcurrentHashMap<>();
50
53
  private long renderRequestCounter = 0;
51
54
 
@@ -200,7 +203,8 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
200
203
 
201
204
  @ReactMethod
202
205
  public void getComicPagePreview(String engineId, int generation, int pageIndex, int maxEdge, Promise promise) {
203
- executor.execute(() -> {
206
+ try {
207
+ comicPreviewExecutor.execute(() -> {
204
208
  Bitmap bitmap = null;
205
209
  try {
206
210
  String path = PapyrusComicArchiveNative.extractPage(engineId, generation, pageIndex);
@@ -252,7 +256,11 @@ public class PapyrusNativeEngineModule extends ReactContextBaseJavaModule {
252
256
  } finally {
253
257
  if (bitmap != null && !bitmap.isRecycled()) bitmap.recycle();
254
258
  }
255
- });
259
+ });
260
+ } catch (RejectedExecutionException queueFull) {
261
+ promise.reject(PapyrusComicPreviewExecutor.QUEUE_FULL_ERROR_CODE,
262
+ "Comic preview queue is busy; retry shortly.", queueFull);
263
+ }
256
264
  }
257
265
 
258
266
  private static void pruneComicThumbnailCache(File directory, File preserve) {