@papyrus-sdk/engine-native 0.2.22-beta.9.epub.2 → 0.2.22

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.
Files changed (276) hide show
  1. package/PapyrusNativeEngine.podspec +45 -0
  2. package/android/build.gradle +1 -0
  3. package/android/src/main/cpp/CMakeLists.txt +137 -0
  4. package/android/src/main/cpp/PapyrusComicArchiveJni.cpp +68 -0
  5. package/android/src/main/cpp/PapyrusComicArchiveTest.cpp +55 -0
  6. package/android/src/main/cpp/papyrus_outline_loader_test.cpp +240 -0
  7. package/android/src/main/cpp/papyrus_text_word_test.cpp +72 -0
  8. package/android/src/main/java/com/papyrus/engine/PapyrusComicArchiveNative.java +14 -0
  9. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +491 -0
  10. package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentViewManager.java +32 -0
  11. package/android/src/main/java/com/papyrus/engine/PapyrusComicPageLayout.java +38 -0
  12. package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +305 -35
  13. package/android/src/main/java/com/papyrus/engine/PapyrusNativeTextModel.java +195 -0
  14. package/android/src/main/java/com/papyrus/engine/PapyrusPackage.java +3 -1
  15. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerView.java +195 -5
  16. package/android/src/main/java/com/papyrus/engine/PapyrusPdfViewerViewManager.java +20 -1
  17. package/android/src/main/java/com/papyrus/engine/PapyrusSelectionMenuIcon.java +48 -0
  18. package/android/src/main/java/com/papyrus/engine/PapyrusTextAnnotationModel.java +22 -0
  19. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentView.java +661 -0
  20. package/android/src/main/java/com/papyrus/engine/PapyrusTextDocumentViewManager.java +49 -0
  21. package/android/src/main/java/com/papyrus/engine/PapyrusTextStore.java +43 -0
  22. package/dist/index.d.mts +614 -4
  23. package/dist/index.d.ts +614 -4
  24. package/dist/index.js +484 -8
  25. package/dist/index.js.map +1 -1
  26. package/dist/index.mjs +477 -7
  27. package/dist/index.mjs.map +1 -1
  28. package/ios/PapyrusComicDocumentView.h +21 -0
  29. package/ios/PapyrusComicDocumentView.m +554 -0
  30. package/ios/PapyrusComicDocumentViewManager.m +23 -0
  31. package/ios/PapyrusEngineStore.h +3 -0
  32. package/ios/PapyrusEngineStore.m +32 -6
  33. package/ios/PapyrusNativeEngine.m +684 -5
  34. package/ios/PapyrusPageRotationRegistry.h +29 -0
  35. package/ios/PapyrusPageRotationRegistry.m +179 -0
  36. package/ios/PapyrusPageView.m +38 -11
  37. package/ios/PapyrusPdfDocumentView.h +55 -0
  38. package/ios/PapyrusPdfDocumentView.m +2370 -0
  39. package/ios/PapyrusPdfDocumentViewManager.m +61 -0
  40. package/ios/PapyrusPdfPageTheme.h +15 -0
  41. package/ios/PapyrusPdfPageTheme.m +218 -0
  42. package/ios/PapyrusSelectionMenu.h +27 -0
  43. package/ios/PapyrusTextAnnotationRange.h +2 -0
  44. package/ios/PapyrusTextAnnotationRange.m +29 -0
  45. package/ios/PapyrusTextDocumentView.h +32 -0
  46. package/ios/PapyrusTextDocumentView.m +518 -0
  47. package/ios/PapyrusTextDocumentViewManager.m +34 -0
  48. package/ios/PapyrusTextStore.h +13 -0
  49. package/ios/PapyrusTextStore.m +73 -0
  50. package/package.json +10 -8
  51. package/react-native.config.js +1 -1
  52. package/third_party_licenses/BLAKE2-NOTICE.txt +11 -0
  53. package/third_party_licenses/libarchive-COPYING +65 -0
  54. package/vendor/libarchive/PapyrusComicArchive.cpp +376 -0
  55. package/vendor/libarchive/PapyrusComicArchive.h +30 -0
  56. package/vendor/libarchive/README.papyrus.md +15 -0
  57. package/vendor/libarchive/android.h +192 -0
  58. package/vendor/libarchive/config/PapyrusArchiveConfig.h +71 -0
  59. package/vendor/libarchive/libarchive/CMakeLists.txt +293 -0
  60. package/vendor/libarchive/libarchive/android_lf.h +36 -0
  61. package/vendor/libarchive/libarchive/archive.h +1266 -0
  62. package/vendor/libarchive/libarchive/archive_acl.c +2025 -0
  63. package/vendor/libarchive/libarchive/archive_acl_private.h +85 -0
  64. package/vendor/libarchive/libarchive/archive_blake2.h +197 -0
  65. package/vendor/libarchive/libarchive/archive_blake2_impl.h +161 -0
  66. package/vendor/libarchive/libarchive/archive_blake2s_ref.c +369 -0
  67. package/vendor/libarchive/libarchive/archive_blake2sp_ref.c +361 -0
  68. package/vendor/libarchive/libarchive/archive_check_magic.c +183 -0
  69. package/vendor/libarchive/libarchive/archive_cmdline.c +211 -0
  70. package/vendor/libarchive/libarchive/archive_cmdline_private.h +45 -0
  71. package/vendor/libarchive/libarchive/archive_crc32.h +123 -0
  72. package/vendor/libarchive/libarchive/archive_cryptor.c +534 -0
  73. package/vendor/libarchive/libarchive/archive_cryptor_private.h +199 -0
  74. package/vendor/libarchive/libarchive/archive_digest.c +1584 -0
  75. package/vendor/libarchive/libarchive/archive_digest_private.h +431 -0
  76. package/vendor/libarchive/libarchive/archive_disk_acl_darwin.c +558 -0
  77. package/vendor/libarchive/libarchive/archive_disk_acl_freebsd.c +712 -0
  78. package/vendor/libarchive/libarchive/archive_disk_acl_linux.c +760 -0
  79. package/vendor/libarchive/libarchive/archive_disk_acl_sunos.c +824 -0
  80. package/vendor/libarchive/libarchive/archive_endian.h +234 -0
  81. package/vendor/libarchive/libarchive/archive_entry.3 +147 -0
  82. package/vendor/libarchive/libarchive/archive_entry.c +2297 -0
  83. package/vendor/libarchive/libarchive/archive_entry.h +763 -0
  84. package/vendor/libarchive/libarchive/archive_entry_acl.3 +458 -0
  85. package/vendor/libarchive/libarchive/archive_entry_copy_bhfi.c +55 -0
  86. package/vendor/libarchive/libarchive/archive_entry_copy_stat.c +82 -0
  87. package/vendor/libarchive/libarchive/archive_entry_link_resolver.c +472 -0
  88. package/vendor/libarchive/libarchive/archive_entry_linkify.3 +224 -0
  89. package/vendor/libarchive/libarchive/archive_entry_locale.h +90 -0
  90. package/vendor/libarchive/libarchive/archive_entry_misc.3 +63 -0
  91. package/vendor/libarchive/libarchive/archive_entry_paths.3 +153 -0
  92. package/vendor/libarchive/libarchive/archive_entry_perms.3 +210 -0
  93. package/vendor/libarchive/libarchive/archive_entry_private.h +205 -0
  94. package/vendor/libarchive/libarchive/archive_entry_sparse.c +156 -0
  95. package/vendor/libarchive/libarchive/archive_entry_stat.3 +274 -0
  96. package/vendor/libarchive/libarchive/archive_entry_stat.c +121 -0
  97. package/vendor/libarchive/libarchive/archive_entry_strmode.c +69 -0
  98. package/vendor/libarchive/libarchive/archive_entry_time.3 +127 -0
  99. package/vendor/libarchive/libarchive/archive_entry_xattr.c +155 -0
  100. package/vendor/libarchive/libarchive/archive_hmac.c +345 -0
  101. package/vendor/libarchive/libarchive/archive_hmac_private.h +119 -0
  102. package/vendor/libarchive/libarchive/archive_integer.h +267 -0
  103. package/vendor/libarchive/libarchive/archive_match.c +1869 -0
  104. package/vendor/libarchive/libarchive/archive_openssl_evp_private.h +54 -0
  105. package/vendor/libarchive/libarchive/archive_openssl_hmac_private.h +54 -0
  106. package/vendor/libarchive/libarchive/archive_options.c +219 -0
  107. package/vendor/libarchive/libarchive/archive_options_private.h +49 -0
  108. package/vendor/libarchive/libarchive/archive_pack_dev.c +330 -0
  109. package/vendor/libarchive/libarchive/archive_pack_dev.h +49 -0
  110. package/vendor/libarchive/libarchive/archive_parse_date.c +1173 -0
  111. package/vendor/libarchive/libarchive/archive_pathmatch.c +480 -0
  112. package/vendor/libarchive/libarchive/archive_pathmatch.h +50 -0
  113. package/vendor/libarchive/libarchive/archive_platform.h +213 -0
  114. package/vendor/libarchive/libarchive/archive_platform_acl.h +53 -0
  115. package/vendor/libarchive/libarchive/archive_platform_stat.h +45 -0
  116. package/vendor/libarchive/libarchive/archive_platform_xattr.h +45 -0
  117. package/vendor/libarchive/libarchive/archive_ppmd7.c +1173 -0
  118. package/vendor/libarchive/libarchive/archive_ppmd7_private.h +119 -0
  119. package/vendor/libarchive/libarchive/archive_ppmd8.c +1293 -0
  120. package/vendor/libarchive/libarchive/archive_ppmd8_private.h +123 -0
  121. package/vendor/libarchive/libarchive/archive_ppmd_private.h +159 -0
  122. package/vendor/libarchive/libarchive/archive_private.h +209 -0
  123. package/vendor/libarchive/libarchive/archive_random.c +274 -0
  124. package/vendor/libarchive/libarchive/archive_random_private.h +36 -0
  125. package/vendor/libarchive/libarchive/archive_rb.c +709 -0
  126. package/vendor/libarchive/libarchive/archive_rb.h +113 -0
  127. package/vendor/libarchive/libarchive/archive_read.3 +250 -0
  128. package/vendor/libarchive/libarchive/archive_read.c +1814 -0
  129. package/vendor/libarchive/libarchive/archive_read_add_passphrase.3 +72 -0
  130. package/vendor/libarchive/libarchive/archive_read_add_passphrase.c +189 -0
  131. package/vendor/libarchive/libarchive/archive_read_append_filter.c +209 -0
  132. package/vendor/libarchive/libarchive/archive_read_data.3 +128 -0
  133. package/vendor/libarchive/libarchive/archive_read_data_into_fd.c +207 -0
  134. package/vendor/libarchive/libarchive/archive_read_disk.3 +422 -0
  135. package/vendor/libarchive/libarchive/archive_read_disk_entry_from_file.c +1086 -0
  136. package/vendor/libarchive/libarchive/archive_read_disk_posix.c +2639 -0
  137. package/vendor/libarchive/libarchive/archive_read_disk_private.h +97 -0
  138. package/vendor/libarchive/libarchive/archive_read_disk_set_standard_lookup.c +315 -0
  139. package/vendor/libarchive/libarchive/archive_read_disk_windows.c +2603 -0
  140. package/vendor/libarchive/libarchive/archive_read_extract.3 +135 -0
  141. package/vendor/libarchive/libarchive/archive_read_extract.c +59 -0
  142. package/vendor/libarchive/libarchive/archive_read_extract2.c +154 -0
  143. package/vendor/libarchive/libarchive/archive_read_filter.3 +163 -0
  144. package/vendor/libarchive/libarchive/archive_read_format.3 +190 -0
  145. package/vendor/libarchive/libarchive/archive_read_free.3 +91 -0
  146. package/vendor/libarchive/libarchive/archive_read_header.3 +89 -0
  147. package/vendor/libarchive/libarchive/archive_read_new.3 +57 -0
  148. package/vendor/libarchive/libarchive/archive_read_open.3 +247 -0
  149. package/vendor/libarchive/libarchive/archive_read_open_fd.c +236 -0
  150. package/vendor/libarchive/libarchive/archive_read_open_file.c +257 -0
  151. package/vendor/libarchive/libarchive/archive_read_open_filename.c +652 -0
  152. package/vendor/libarchive/libarchive/archive_read_open_memory.c +188 -0
  153. package/vendor/libarchive/libarchive/archive_read_private.h +274 -0
  154. package/vendor/libarchive/libarchive/archive_read_set_format.c +116 -0
  155. package/vendor/libarchive/libarchive/archive_read_set_options.3 +290 -0
  156. package/vendor/libarchive/libarchive/archive_read_set_options.c +132 -0
  157. package/vendor/libarchive/libarchive/archive_read_support_filter_all.c +84 -0
  158. package/vendor/libarchive/libarchive/archive_read_support_filter_by_code.c +68 -0
  159. package/vendor/libarchive/libarchive/archive_read_support_filter_bzip2.c +360 -0
  160. package/vendor/libarchive/libarchive/archive_read_support_filter_compress.c +457 -0
  161. package/vendor/libarchive/libarchive/archive_read_support_filter_grzip.c +108 -0
  162. package/vendor/libarchive/libarchive/archive_read_support_filter_gzip.c +538 -0
  163. package/vendor/libarchive/libarchive/archive_read_support_filter_lrzip.c +119 -0
  164. package/vendor/libarchive/libarchive/archive_read_support_filter_lz4.c +829 -0
  165. package/vendor/libarchive/libarchive/archive_read_support_filter_lzop.c +493 -0
  166. package/vendor/libarchive/libarchive/archive_read_support_filter_none.c +51 -0
  167. package/vendor/libarchive/libarchive/archive_read_support_filter_program.c +497 -0
  168. package/vendor/libarchive/libarchive/archive_read_support_filter_rpm.c +249 -0
  169. package/vendor/libarchive/libarchive/archive_read_support_filter_uu.c +739 -0
  170. package/vendor/libarchive/libarchive/archive_read_support_filter_xz.c +792 -0
  171. package/vendor/libarchive/libarchive/archive_read_support_filter_zstd.c +356 -0
  172. package/vendor/libarchive/libarchive/archive_read_support_format_7zip.c +4650 -0
  173. package/vendor/libarchive/libarchive/archive_read_support_format_all.c +88 -0
  174. package/vendor/libarchive/libarchive/archive_read_support_format_ar.c +597 -0
  175. package/vendor/libarchive/libarchive/archive_read_support_format_by_code.c +76 -0
  176. package/vendor/libarchive/libarchive/archive_read_support_format_cab.c +3410 -0
  177. package/vendor/libarchive/libarchive/archive_read_support_format_cpio.c +1152 -0
  178. package/vendor/libarchive/libarchive/archive_read_support_format_empty.c +95 -0
  179. package/vendor/libarchive/libarchive/archive_read_support_format_iso9660.c +3516 -0
  180. package/vendor/libarchive/libarchive/archive_read_support_format_lha.c +2931 -0
  181. package/vendor/libarchive/libarchive/archive_read_support_format_mtree.c +2113 -0
  182. package/vendor/libarchive/libarchive/archive_read_support_format_rar.c +3932 -0
  183. package/vendor/libarchive/libarchive/archive_read_support_format_rar5.c +4501 -0
  184. package/vendor/libarchive/libarchive/archive_read_support_format_raw.c +194 -0
  185. package/vendor/libarchive/libarchive/archive_read_support_format_tar.c +3785 -0
  186. package/vendor/libarchive/libarchive/archive_read_support_format_warc.c +875 -0
  187. package/vendor/libarchive/libarchive/archive_read_support_format_xar.c +3770 -0
  188. package/vendor/libarchive/libarchive/archive_read_support_format_zip.c +4710 -0
  189. package/vendor/libarchive/libarchive/archive_string.c +4391 -0
  190. package/vendor/libarchive/libarchive/archive_string.h +244 -0
  191. package/vendor/libarchive/libarchive/archive_string_composition.h +2289 -0
  192. package/vendor/libarchive/libarchive/archive_string_sprintf.c +200 -0
  193. package/vendor/libarchive/libarchive/archive_time.c +168 -0
  194. package/vendor/libarchive/libarchive/archive_time_private.h +47 -0
  195. package/vendor/libarchive/libarchive/archive_util.3 +239 -0
  196. package/vendor/libarchive/libarchive/archive_util.c +668 -0
  197. package/vendor/libarchive/libarchive/archive_version_details.c +516 -0
  198. package/vendor/libarchive/libarchive/archive_virtual.c +162 -0
  199. package/vendor/libarchive/libarchive/archive_windows.c +1035 -0
  200. package/vendor/libarchive/libarchive/archive_windows.h +332 -0
  201. package/vendor/libarchive/libarchive/archive_write.3 +266 -0
  202. package/vendor/libarchive/libarchive/archive_write.c +896 -0
  203. package/vendor/libarchive/libarchive/archive_write_add_filter.c +71 -0
  204. package/vendor/libarchive/libarchive/archive_write_add_filter_b64encode.c +329 -0
  205. package/vendor/libarchive/libarchive/archive_write_add_filter_by_name.c +76 -0
  206. package/vendor/libarchive/libarchive/archive_write_add_filter_bzip2.c +411 -0
  207. package/vendor/libarchive/libarchive/archive_write_add_filter_compress.c +459 -0
  208. package/vendor/libarchive/libarchive/archive_write_add_filter_grzip.c +143 -0
  209. package/vendor/libarchive/libarchive/archive_write_add_filter_gzip.c +493 -0
  210. package/vendor/libarchive/libarchive/archive_write_add_filter_lrzip.c +214 -0
  211. package/vendor/libarchive/libarchive/archive_write_add_filter_lz4.c +710 -0
  212. package/vendor/libarchive/libarchive/archive_write_add_filter_lzop.c +500 -0
  213. package/vendor/libarchive/libarchive/archive_write_add_filter_none.c +42 -0
  214. package/vendor/libarchive/libarchive/archive_write_add_filter_program.c +398 -0
  215. package/vendor/libarchive/libarchive/archive_write_add_filter_uuencode.c +320 -0
  216. package/vendor/libarchive/libarchive/archive_write_add_filter_xz.c +547 -0
  217. package/vendor/libarchive/libarchive/archive_write_add_filter_zstd.c +608 -0
  218. package/vendor/libarchive/libarchive/archive_write_blocksize.3 +112 -0
  219. package/vendor/libarchive/libarchive/archive_write_data.3 +88 -0
  220. package/vendor/libarchive/libarchive/archive_write_disk.3 +360 -0
  221. package/vendor/libarchive/libarchive/archive_write_disk_posix.c +4789 -0
  222. package/vendor/libarchive/libarchive/archive_write_disk_private.h +43 -0
  223. package/vendor/libarchive/libarchive/archive_write_disk_set_standard_lookup.c +265 -0
  224. package/vendor/libarchive/libarchive/archive_write_disk_windows.c +2952 -0
  225. package/vendor/libarchive/libarchive/archive_write_filter.3 +140 -0
  226. package/vendor/libarchive/libarchive/archive_write_finish_entry.3 +77 -0
  227. package/vendor/libarchive/libarchive/archive_write_format.3 +187 -0
  228. package/vendor/libarchive/libarchive/archive_write_free.3 +94 -0
  229. package/vendor/libarchive/libarchive/archive_write_header.3 +71 -0
  230. package/vendor/libarchive/libarchive/archive_write_new.3 +56 -0
  231. package/vendor/libarchive/libarchive/archive_write_open.3 +270 -0
  232. package/vendor/libarchive/libarchive/archive_write_open_fd.c +141 -0
  233. package/vendor/libarchive/libarchive/archive_write_open_file.c +102 -0
  234. package/vendor/libarchive/libarchive/archive_write_open_filename.c +272 -0
  235. package/vendor/libarchive/libarchive/archive_write_open_memory.c +110 -0
  236. package/vendor/libarchive/libarchive/archive_write_private.h +166 -0
  237. package/vendor/libarchive/libarchive/archive_write_set_format.c +124 -0
  238. package/vendor/libarchive/libarchive/archive_write_set_format_7zip.c +2571 -0
  239. package/vendor/libarchive/libarchive/archive_write_set_format_ar.c +540 -0
  240. package/vendor/libarchive/libarchive/archive_write_set_format_by_name.c +93 -0
  241. package/vendor/libarchive/libarchive/archive_write_set_format_cpio.c +11 -0
  242. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_binary.c +618 -0
  243. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_newc.c +457 -0
  244. package/vendor/libarchive/libarchive/archive_write_set_format_cpio_odc.c +508 -0
  245. package/vendor/libarchive/libarchive/archive_write_set_format_filter_by_ext.c +141 -0
  246. package/vendor/libarchive/libarchive/archive_write_set_format_gnutar.c +790 -0
  247. package/vendor/libarchive/libarchive/archive_write_set_format_iso9660.c +8216 -0
  248. package/vendor/libarchive/libarchive/archive_write_set_format_mtree.c +2307 -0
  249. package/vendor/libarchive/libarchive/archive_write_set_format_pax.c +2249 -0
  250. package/vendor/libarchive/libarchive/archive_write_set_format_private.h +40 -0
  251. package/vendor/libarchive/libarchive/archive_write_set_format_raw.c +123 -0
  252. package/vendor/libarchive/libarchive/archive_write_set_format_shar.c +650 -0
  253. package/vendor/libarchive/libarchive/archive_write_set_format_ustar.c +768 -0
  254. package/vendor/libarchive/libarchive/archive_write_set_format_v7tar.c +648 -0
  255. package/vendor/libarchive/libarchive/archive_write_set_format_warc.c +491 -0
  256. package/vendor/libarchive/libarchive/archive_write_set_format_xar.c +3799 -0
  257. package/vendor/libarchive/libarchive/archive_write_set_format_zip.c +2548 -0
  258. package/vendor/libarchive/libarchive/archive_write_set_options.3 +779 -0
  259. package/vendor/libarchive/libarchive/archive_write_set_options.c +129 -0
  260. package/vendor/libarchive/libarchive/archive_write_set_passphrase.3 +72 -0
  261. package/vendor/libarchive/libarchive/archive_write_set_passphrase.c +95 -0
  262. package/vendor/libarchive/libarchive/archive_xxhash.h +48 -0
  263. package/vendor/libarchive/libarchive/config_freebsd.h +275 -0
  264. package/vendor/libarchive/libarchive/cpio.5 +389 -0
  265. package/vendor/libarchive/libarchive/filter_fork.h +44 -0
  266. package/vendor/libarchive/libarchive/filter_fork_posix.c +269 -0
  267. package/vendor/libarchive/libarchive/filter_fork_windows.c +270 -0
  268. package/vendor/libarchive/libarchive/libarchive-formats.5 +478 -0
  269. package/vendor/libarchive/libarchive/libarchive.3 +297 -0
  270. package/vendor/libarchive/libarchive/libarchive_changes.3 +339 -0
  271. package/vendor/libarchive/libarchive/libarchive_internals.3 +363 -0
  272. package/vendor/libarchive/libarchive/module.modulemap +34 -0
  273. package/vendor/libarchive/libarchive/mtree.5 +323 -0
  274. package/vendor/libarchive/libarchive/tar.5 +947 -0
  275. package/vendor/libarchive/libarchive/xxhash.c +531 -0
  276. package/ios/PapyrusNativeEngine.podspec +0 -15
@@ -0,0 +1,554 @@
1
+ #import "PapyrusComicDocumentView.h"
2
+ #import "../vendor/libarchive/PapyrusComicArchive.h"
3
+ #import <ImageIO/ImageIO.h>
4
+ #import <math.h>
5
+
6
+ static NSString * const PapyrusComicCellId = @"PapyrusComicPageCell";
7
+ static NSCache<NSString *, UIImage *> *PapyrusComicImageCache(void) {
8
+ static NSCache<NSString *, UIImage *> *cache;
9
+ static dispatch_once_t once;
10
+ dispatch_once(&once, ^{
11
+ cache = [[NSCache alloc] init];
12
+ cache.totalCostLimit = 64 * 1024 * 1024;
13
+ });
14
+ return cache;
15
+ }
16
+
17
+ @interface PapyrusComicPageCell : UICollectionViewCell
18
+ @property (nonatomic, strong) UIScrollView *pageScrollView;
19
+ @property (nonatomic, strong) UIImageView *imageView;
20
+ @property (nonatomic, copy) NSString *representedKey;
21
+ @property (nonatomic, assign) BOOL fitWidthInSingleMode;
22
+ @end
23
+
24
+ @implementation PapyrusComicPageCell
25
+ - (instancetype)initWithFrame:(CGRect)frame {
26
+ self = [super initWithFrame:frame];
27
+ if (self) {
28
+ _pageScrollView = [[UIScrollView alloc] initWithFrame:self.contentView.bounds];
29
+ _pageScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
30
+ _pageScrollView.alwaysBounceVertical = NO;
31
+ _pageScrollView.showsVerticalScrollIndicator = NO;
32
+ _pageScrollView.showsHorizontalScrollIndicator = NO;
33
+ [self.contentView addSubview:_pageScrollView];
34
+ _imageView = [[UIImageView alloc] initWithFrame:_pageScrollView.bounds];
35
+ _imageView.contentMode = UIViewContentModeScaleAspectFit;
36
+ [_pageScrollView addSubview:_imageView];
37
+ }
38
+ return self;
39
+ }
40
+ - (void)layoutSubviews {
41
+ [super layoutSubviews];
42
+ self.pageScrollView.frame = self.contentView.bounds;
43
+ CGAffineTransform transform = self.imageView.transform;
44
+ self.imageView.transform = CGAffineTransformIdentity;
45
+ CGSize size = self.pageScrollView.bounds.size;
46
+ UIImage *image = self.imageView.image;
47
+ if (self.fitWidthInSingleMode && image.size.width > 0 && image.size.height > 0) {
48
+ size.height = MAX(1, size.width * image.size.height / image.size.width);
49
+ }
50
+ self.imageView.frame = (CGRect){CGPointZero, size};
51
+ self.pageScrollView.contentSize = size;
52
+ BOOL canScroll = self.fitWidthInSingleMode && size.height > self.pageScrollView.bounds.size.height;
53
+ self.pageScrollView.scrollEnabled = canScroll;
54
+ self.pageScrollView.alwaysBounceVertical = canScroll;
55
+ self.imageView.transform = transform;
56
+ }
57
+ - (void)prepareForReuse {
58
+ [super prepareForReuse];
59
+ self.pageScrollView.contentOffset = CGPointZero;
60
+ self.pageScrollView.contentSize = self.pageScrollView.bounds.size;
61
+ self.pageScrollView.scrollEnabled = NO;
62
+ self.pageScrollView.alwaysBounceVertical = NO;
63
+ self.imageView.image = nil;
64
+ self.imageView.transform = CGAffineTransformIdentity;
65
+ self.fitWidthInSingleMode = NO;
66
+ }
67
+ @end
68
+
69
+ @interface PapyrusComicDocumentView () <UIGestureRecognizerDelegate>
70
+ @property (nonatomic, strong) UICollectionView *collectionView;
71
+ @property (nonatomic, strong) UICollectionViewFlowLayout *flowLayout;
72
+ @property (nonatomic, strong) UIPinchGestureRecognizer *pinchRecognizer;
73
+ @property (nonatomic, strong) UITapGestureRecognizer *doubleTapRecognizer;
74
+ @property (nonatomic, strong) UIPanGestureRecognizer *pagePanRecognizer;
75
+ @property (nonatomic, assign) BOOL applyingProgrammaticPage;
76
+ @property (nonatomic, strong) NSMutableSet<NSString *> *reportedErrors;
77
+ @property (nonatomic, strong) NSMutableDictionary<NSNumber *, NSValue *> *pageSizes;
78
+ @property (nonatomic, strong) NSMutableDictionary<NSNumber *, NSValue *> *pendingPageSizes;
79
+ @property (nonatomic, assign) BOOL pendingViewportLayout;
80
+ @property (nonatomic, assign) CGSize viewportSize;
81
+ @property (nonatomic, assign) NSInteger requestedPage;
82
+ @property (nonatomic, assign) BOOL pendingNavigation;
83
+ @property (nonatomic, assign) CGPoint panOffset;
84
+ @property (nonatomic, assign) CGPoint panGestureStartOffset;
85
+ @end
86
+
87
+ @implementation PapyrusComicDocumentView
88
+
89
+ - (instancetype)initWithFrame:(CGRect)frame {
90
+ self = [super initWithFrame:frame];
91
+ if (self) {
92
+ _pageCount = 0;
93
+ _currentPage = 1;
94
+ _requestedPage = 1;
95
+ _pendingNavigation = YES;
96
+ _pageSizes = [NSMutableDictionary dictionary];
97
+ _pendingPageSizes = [NSMutableDictionary dictionary];
98
+ _layoutMode = @"single";
99
+ _fitMode = @"width";
100
+ _readingDirection = @"ltr";
101
+ _zoom = 1;
102
+ _reportedErrors = [NSMutableSet set];
103
+ _pageTheme = @"normal";
104
+ _flowLayout = [[UICollectionViewFlowLayout alloc] init];
105
+ _flowLayout.minimumLineSpacing = 0;
106
+ _flowLayout.minimumInteritemSpacing = 0;
107
+ _collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:_flowLayout];
108
+ _collectionView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
109
+ _collectionView.dataSource = self;
110
+ _collectionView.delegate = self;
111
+ _collectionView.backgroundColor = UIColor.blackColor;
112
+ _collectionView.pagingEnabled = YES;
113
+ _collectionView.showsVerticalScrollIndicator = YES;
114
+ _collectionView.showsHorizontalScrollIndicator = NO;
115
+ [_collectionView registerClass:PapyrusComicPageCell.class forCellWithReuseIdentifier:PapyrusComicCellId];
116
+ [self addSubview:_collectionView];
117
+ _pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
118
+ _pinchRecognizer.cancelsTouchesInView = NO;
119
+ _pinchRecognizer.delegate = self;
120
+ [_collectionView addGestureRecognizer:_pinchRecognizer];
121
+ _doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
122
+ _doubleTapRecognizer.numberOfTapsRequired = 2;
123
+ _doubleTapRecognizer.numberOfTouchesRequired = 1;
124
+ _doubleTapRecognizer.cancelsTouchesInView = YES;
125
+ _doubleTapRecognizer.delegate = self;
126
+ [_collectionView addGestureRecognizer:_doubleTapRecognizer];
127
+ _pagePanRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePagePan:)];
128
+ _pagePanRecognizer.minimumNumberOfTouches = 1;
129
+ _pagePanRecognizer.maximumNumberOfTouches = 1;
130
+ _pagePanRecognizer.cancelsTouchesInView = NO;
131
+ _pagePanRecognizer.delegate = self;
132
+ [_collectionView addGestureRecognizer:_pagePanRecognizer];
133
+ [self applyLayoutMode];
134
+ [self applyTheme];
135
+ }
136
+ return self;
137
+ }
138
+
139
+ // Anchors reference the page, not an absolute document offset. Heights can change
140
+ // above the viewport as images arrive; bitmap eviction must not change geometry.
141
+ - (NSDictionary *)captureScrollAnchorForViewport:(CGSize)viewport {
142
+ if (self.pendingNavigation || viewport.width <= 0 || viewport.height <= 0) return nil;
143
+ CGPoint center = CGPointMake(self.collectionView.contentOffset.x + viewport.width / 2,
144
+ self.collectionView.contentOffset.y + viewport.height / 2);
145
+ UICollectionViewLayoutAttributes *nearest = nil;
146
+ CGFloat distance = CGFLOAT_MAX;
147
+ for (NSIndexPath *path in self.collectionView.indexPathsForVisibleItems) {
148
+ UICollectionViewLayoutAttributes *attributes = [self.flowLayout layoutAttributesForItemAtIndexPath:path];
149
+ if (!attributes) continue;
150
+ CGFloat next = ABS(CGRectGetMidY(attributes.frame) - center.y);
151
+ if (next < distance) { nearest = attributes; distance = next; }
152
+ if (CGRectContainsPoint(attributes.frame, center)) { nearest = attributes; break; }
153
+ }
154
+ if (!nearest || nearest.frame.size.height <= 0) return nil;
155
+ return @{ @"pageIndex": @([self logicalPageForItem:nearest.indexPath.item]),
156
+ @"fraction": @(MAX(0, MIN(1, (center.y - nearest.frame.origin.y) / nearest.frame.size.height))) };
157
+ }
158
+
159
+ - (void)restoreScrollAnchor:(NSDictionary *)anchor {
160
+ if (self.collectionView.dragging || self.collectionView.decelerating) return;
161
+ if (!anchor || self.pendingNavigation || ![self.layoutMode isEqualToString:@"continuous"]) return;
162
+ NSInteger page = [anchor[@"pageIndex"] integerValue];
163
+ if (page < 0 || page >= self.pageCount) return;
164
+ NSInteger item = [self itemForLogicalPage:page + 1];
165
+ UICollectionViewLayoutAttributes *attributes = [self.flowLayout layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:0]];
166
+ if (!attributes) return;
167
+ CGFloat y = attributes.frame.origin.y + [anchor[@"fraction"] doubleValue] * attributes.frame.size.height - self.collectionView.bounds.size.height / 2;
168
+ CGFloat minimum = -self.collectionView.adjustedContentInset.top;
169
+ CGFloat maximum = MAX(minimum, self.collectionView.contentSize.height - self.collectionView.bounds.size.height + self.collectionView.adjustedContentInset.bottom);
170
+ BOOL wasApplying = self.applyingProgrammaticPage;
171
+ self.applyingProgrammaticPage = YES;
172
+ [self.collectionView setContentOffset:CGPointMake(self.collectionView.contentOffset.x, MAX(minimum, MIN(maximum, y))) animated:NO];
173
+ self.applyingProgrammaticPage = wasApplying;
174
+ }
175
+
176
+ - (void)layoutSubviews {
177
+ NSDictionary *anchor = [self captureScrollAnchorForViewport:self.viewportSize];
178
+ [super layoutSubviews];
179
+ CGSize size = self.collectionView.bounds.size;
180
+ if (CGSizeEqualToSize(size, self.viewportSize)) {
181
+ if (self.pendingNavigation) [self scrollToCurrentPageAnimated:NO];
182
+ return;
183
+ }
184
+ if (self.collectionView.dragging || self.collectionView.decelerating) {
185
+ self.pendingViewportLayout = YES;
186
+ return;
187
+ }
188
+ self.pendingViewportLayout = NO;
189
+ BOOL wasApplying = self.applyingProgrammaticPage;
190
+ self.applyingProgrammaticPage = YES;
191
+ self.viewportSize = size;
192
+ self.flowLayout.itemSize = size;
193
+ [self.flowLayout invalidateLayout];
194
+ [self.collectionView layoutIfNeeded];
195
+ for (PapyrusComicPageCell *cell in self.collectionView.visibleCells) {
196
+ [self configureCell:cell withImage:cell.imageView.image];
197
+ cell.imageView.transform = [self imageTransform];
198
+ }
199
+ if (self.pendingNavigation || ![self.layoutMode isEqualToString:@"continuous"]) {
200
+ [self scrollToCurrentPageAnimated:NO];
201
+ } else {
202
+ [self restoreScrollAnchor:anchor];
203
+ }
204
+ self.applyingProgrammaticPage = wasApplying;
205
+ }
206
+
207
+ - (void)setEngineId:(NSString *)engineId {
208
+ if ([_engineId isEqualToString:engineId]) return;
209
+ _engineId = [engineId copy];
210
+ [self.pageSizes removeAllObjects];
211
+ [self.pendingPageSizes removeAllObjects];
212
+ @synchronized (self.reportedErrors) { [self.reportedErrors removeAllObjects]; }
213
+ self.pendingNavigation = YES;
214
+ [self.collectionView reloadData];
215
+ [self scrollToCurrentPageAnimated:NO];
216
+ }
217
+ - (void)setDocumentGeneration:(NSInteger)value {
218
+ if (_documentGeneration == value) return;
219
+ _documentGeneration = value;
220
+ [self.pageSizes removeAllObjects];
221
+ [self.pendingPageSizes removeAllObjects];
222
+ @synchronized (self.reportedErrors) { [self.reportedErrors removeAllObjects]; }
223
+ self.pendingNavigation = YES;
224
+ [self.collectionView reloadData];
225
+ [self scrollToCurrentPageAnimated:NO];
226
+ }
227
+ - (void)setPageCount:(NSInteger)value {
228
+ NSInteger count = MAX(0, value);
229
+ if (_pageCount == count) return;
230
+ _pageCount = count;
231
+ _currentPage = count > 0 ? MIN(self.requestedPage, count) : self.requestedPage;
232
+ self.pendingNavigation = YES;
233
+ [self.collectionView reloadData];
234
+ [self scrollToCurrentPageAnimated:NO];
235
+ }
236
+ - (void)setCurrentPage:(NSInteger)value {
237
+ NSInteger requested = MAX(value, 1);
238
+ NSInteger safe = self.pageCount > 0 ? MIN(requested, self.pageCount) : requested;
239
+ if (_currentPage == safe && self.requestedPage == requested) return;
240
+ self.requestedPage = requested;
241
+ _currentPage = safe;
242
+ self.pendingNavigation = YES;
243
+ [self scrollToCurrentPageAnimated:NO];
244
+ }
245
+ - (void)setLayoutMode:(NSString *)value {
246
+ NSString *mode = [value isEqualToString:@"continuous"] ? @"continuous" : @"single";
247
+ if ([_layoutMode isEqualToString:mode]) return;
248
+ _layoutMode = mode;
249
+ [self applyLayoutMode];
250
+ self.pendingNavigation = YES;
251
+ [self.collectionView reloadData];
252
+ [self scrollToCurrentPageAnimated:NO];
253
+ }
254
+ - (void)setFitMode:(NSString *)value {
255
+ NSString *mode = [value isEqualToString:@"page"] ? @"page" : @"width";
256
+ if ([_fitMode isEqualToString:mode]) return;
257
+ BOOL wasApplying = self.applyingProgrammaticPage;
258
+ self.applyingProgrammaticPage = YES;
259
+ NSDictionary *anchor = [self captureScrollAnchorForViewport:self.collectionView.bounds.size];
260
+ _fitMode = mode;
261
+ [self.flowLayout invalidateLayout];
262
+ [self.collectionView reloadData];
263
+ [self.collectionView layoutIfNeeded];
264
+ if ([self.layoutMode isEqualToString:@"continuous"]) [self restoreScrollAnchor:anchor];
265
+ else [self scrollToCurrentPageAnimated:NO];
266
+ self.applyingProgrammaticPage = wasApplying;
267
+ }
268
+ - (void)setReadingDirection:(NSString *)value {
269
+ NSString *direction = [value isEqualToString:@"rtl"] ? @"rtl" : @"ltr";
270
+ if ([_readingDirection isEqualToString:direction]) return;
271
+ BOOL wasApplying = self.applyingProgrammaticPage;
272
+ self.applyingProgrammaticPage = YES;
273
+ NSDictionary *anchor = [self captureScrollAnchorForViewport:self.collectionView.bounds.size];
274
+ _readingDirection = direction;
275
+ [self applyLayoutMode];
276
+ [self.collectionView reloadData];
277
+ [self.collectionView layoutIfNeeded];
278
+ if ([self.layoutMode isEqualToString:@"continuous"]) [self restoreScrollAnchor:anchor];
279
+ else [self scrollToCurrentPageAnimated:NO];
280
+ self.applyingProgrammaticPage = wasApplying;
281
+ }
282
+ - (void)setZoom:(CGFloat)value {
283
+ _zoom = MAX(1, MIN(5, value));
284
+ self.collectionView.panGestureRecognizer.enabled = _zoom <= 1.0;
285
+ self.pagePanRecognizer.enabled = _zoom > 1.0;
286
+ if (_zoom <= 1.0) self.panOffset = CGPointZero;
287
+ for (PapyrusComicPageCell *cell in self.collectionView.visibleCells) {
288
+ cell.imageView.transform = [self imageTransform];
289
+ }
290
+ }
291
+
292
+ - (CGAffineTransform)imageTransform {
293
+ CGAffineTransform transform = CGAffineTransformMakeTranslation(self.panOffset.x, self.panOffset.y);
294
+ return CGAffineTransformScale(transform, self.zoom, self.zoom);
295
+ }
296
+ - (void)setPageTheme:(NSString *)value {
297
+ _pageTheme = [value copy] ?: @"normal";
298
+ [self applyTheme];
299
+ }
300
+
301
+ - (void)applyLayoutMode {
302
+ BOOL continuous = [self.layoutMode isEqualToString:@"continuous"];
303
+ self.flowLayout.scrollDirection = continuous ? UICollectionViewScrollDirectionVertical : UICollectionViewScrollDirectionHorizontal;
304
+ self.collectionView.pagingEnabled = !continuous;
305
+ self.collectionView.semanticContentAttribute = [self.readingDirection isEqualToString:@"rtl"] ? UISemanticContentAttributeForceRightToLeft : UISemanticContentAttributeForceLeftToRight;
306
+ [self.flowLayout invalidateLayout];
307
+ }
308
+
309
+ - (void)applyTheme {
310
+ UIColor *background = [self.pageTheme isEqualToString:@"sepia"] ? [UIColor colorWithRed:0.90 green:0.86 blue:0.77 alpha:1] :
311
+ [self.pageTheme isEqualToString:@"dark"] ? [UIColor colorWithRed:0.08 green:0.08 blue:0.09 alpha:1] : UIColor.blackColor;
312
+ self.backgroundColor = background;
313
+ self.collectionView.backgroundColor = background;
314
+ }
315
+
316
+ - (void)configureCell:(PapyrusComicPageCell *)cell withImage:(UIImage *)image {
317
+ cell.fitWidthInSingleMode = [self.layoutMode isEqualToString:@"single"] &&
318
+ [self.fitMode isEqualToString:@"width"] && image.size.width > 0 && image.size.height > 0;
319
+ [cell setNeedsLayout];
320
+ [cell layoutIfNeeded];
321
+ }
322
+
323
+ - (void)emitErrorForKey:(NSString *)key message:(NSString *)message {
324
+ @synchronized (self.reportedErrors) {
325
+ if ([self.reportedErrors containsObject:key]) return;
326
+ [self.reportedErrors addObject:key];
327
+ }
328
+ dispatch_async(dispatch_get_main_queue(), ^{
329
+ if (self.onError) self.onError(@{@"message": message.length ? message : @"Unsupported or corrupt comic image"});
330
+ });
331
+ }
332
+
333
+ - (NSInteger)logicalPageForItem:(NSInteger)item {
334
+ return item;
335
+ }
336
+ - (NSInteger)itemForLogicalPage:(NSInteger)page {
337
+ return MIN(MAX(0, page - 1), MAX(0, self.pageCount - 1));
338
+ }
339
+ - (void)scrollToCurrentPageAnimated:(BOOL)animated {
340
+ if (self.pageCount <= 0 || self.collectionView.bounds.size.width <= 0) return;
341
+ [self.collectionView layoutIfNeeded];
342
+ NSInteger item = [self itemForLogicalPage:self.currentPage];
343
+ if (item >= [self.collectionView numberOfItemsInSection:0]) return;
344
+ self.pendingNavigation = NO;
345
+ self.applyingProgrammaticPage = YES;
346
+ [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:item inSection:0]
347
+ atScrollPosition:[self.layoutMode isEqualToString:@"continuous"] ? UICollectionViewScrollPositionTop : UICollectionViewScrollPositionCenteredHorizontally
348
+ animated:animated];
349
+ dispatch_async(dispatch_get_main_queue(), ^{ self.applyingProgrammaticPage = NO; });
350
+ }
351
+
352
+ - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
353
+ #pragma unused(collectionView)
354
+ return 1;
355
+ }
356
+ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
357
+ #pragma unused(collectionView, section)
358
+ return self.pageCount;
359
+ }
360
+
361
+ - (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
362
+ PapyrusComicPageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:PapyrusComicCellId forIndexPath:indexPath];
363
+ NSInteger pageIndex = [self logicalPageForItem:indexPath.item];
364
+ NSString *engineId = self.engineId;
365
+ NSInteger generation = self.documentGeneration;
366
+ NSString *key = [NSString stringWithFormat:@"%@:%ld:%ld", engineId ?: @"", (long)generation, (long)pageIndex];
367
+ cell.representedKey = key;
368
+ cell.imageView.image = [PapyrusComicImageCache() objectForKey:key];
369
+ [self configureCell:cell withImage:cell.imageView.image];
370
+ cell.imageView.transform = [self imageTransform];
371
+ if (!cell.imageView.image && engineId.length > 0 && generation > 0) {
372
+ NSInteger maximumEdge = MAX(1024, (NSInteger)MAX(self.bounds.size.width, self.bounds.size.height) * 2);
373
+ dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
374
+ char path[4096] = {};
375
+ char error[512] = {};
376
+ if (!papyrus_comic_extract_page(engineId.UTF8String, (int)generation, (int)pageIndex, path, sizeof(path), error, sizeof(error))) {
377
+ [self emitErrorForKey:key message:error[0] ? [NSString stringWithUTF8String:error] : nil];
378
+ return;
379
+ }
380
+ NSURL *url = [NSURL fileURLWithPath:[NSString stringWithUTF8String:path]];
381
+ CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)url, NULL);
382
+ if (!source) { [self emitErrorForKey:key message:@"Unsupported or corrupt comic image"]; return; }
383
+ CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
384
+ NSDictionary *metadata = properties ? CFBridgingRelease(properties) : @{};
385
+ NSUInteger width = [metadata[(id)kCGImagePropertyPixelWidth] unsignedIntegerValue];
386
+ NSUInteger height = [metadata[(id)kCGImagePropertyPixelHeight] unsignedIntegerValue];
387
+ if (width == 0 || height == 0 || width > 20000 || height > 20000 || width * height > 80000000ULL) {
388
+ CFRelease(source);
389
+ [self emitErrorForKey:key message:@"Comic image exceeds supported dimensions"];
390
+ return;
391
+ }
392
+ CGFloat scale = MIN(1.0, MIN((CGFloat)maximumEdge / (CGFloat)MAX(width, height), sqrt(8000000.0 / ((double)width * (double)height))));
393
+ NSInteger boundedMaximumEdge = MAX(1, (NSInteger)ceil((double)MAX(width, height) * scale));
394
+ NSDictionary *options = @{
395
+ (__bridge NSString *)kCGImageSourceCreateThumbnailFromImageAlways: @YES,
396
+ (__bridge NSString *)kCGImageSourceThumbnailMaxPixelSize: @(boundedMaximumEdge),
397
+ (__bridge NSString *)kCGImageSourceCreateThumbnailWithTransform: @YES,
398
+ };
399
+ CGImageRef image = CGImageSourceCreateThumbnailAtIndex(source, 0, (__bridge CFDictionaryRef)options);
400
+ CFRelease(source);
401
+ if (!image) { [self emitErrorForKey:key message:@"Unsupported or corrupt comic image"]; return; }
402
+ UIImage *page = [UIImage imageWithCGImage:image scale:UIScreen.mainScreen.scale orientation:UIImageOrientationUp];
403
+ NSUInteger cost = CGImageGetBytesPerRow(image) * CGImageGetHeight(image);
404
+ CGImageRelease(image);
405
+ [PapyrusComicImageCache() setObject:page forKey:key cost:cost];
406
+ dispatch_async(dispatch_get_main_queue(), ^{
407
+ if (![self.engineId isEqualToString:engineId] || self.documentGeneration != generation) return;
408
+ self.pendingPageSizes[@(pageIndex)] = [NSValue valueWithCGSize:page.size];
409
+ [self applyPendingPageSizes];
410
+ if ([cell.representedKey isEqualToString:key]) {
411
+ cell.imageView.image = page;
412
+ [self configureCell:cell withImage:page];
413
+ cell.imageView.transform = [self imageTransform];
414
+ }
415
+ });
416
+ });
417
+ }
418
+ return cell;
419
+ }
420
+
421
+ // Keep geometry fixed during the native gesture; one compensation happens at idle.
422
+ - (void)applyPendingPageSizes {
423
+ if (self.collectionView.dragging || self.collectionView.decelerating) return;
424
+ if (self.pendingViewportLayout) { [self setNeedsLayout]; [self layoutIfNeeded]; }
425
+ if (self.pendingPageSizes.count == 0) return;
426
+ NSDictionary *anchor = [self captureScrollAnchorForViewport:self.collectionView.bounds.size];
427
+ BOOL wasApplying = self.applyingProgrammaticPage;
428
+ self.applyingProgrammaticPage = YES;
429
+ [self.pageSizes addEntriesFromDictionary:self.pendingPageSizes];
430
+ [self.pendingPageSizes removeAllObjects];
431
+ [self.flowLayout invalidateLayout];
432
+ [self.collectionView layoutIfNeeded];
433
+ [self restoreScrollAnchor:anchor];
434
+ self.applyingProgrammaticPage = wasApplying;
435
+ }
436
+
437
+ - (CGSize)pageSizeAtIndex:(NSInteger)page {
438
+ NSValue *known = self.pageSizes[@(page)];
439
+ if (known) return known.CGSizeValue;
440
+ NSString *key = [NSString stringWithFormat:@"%@:%ld:%ld", self.engineId ?: @"", (long)self.documentGeneration, (long)page];
441
+ if (self.pendingPageSizes[@(page)]) return CGSizeZero;
442
+ UIImage *cached = [PapyrusComicImageCache() objectForKey:key];
443
+ if (cached) self.pageSizes[@(page)] = [NSValue valueWithCGSize:cached.size];
444
+ return cached ? cached.size : CGSizeZero;
445
+ }
446
+
447
+ - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)layout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
448
+ #pragma unused(layout)
449
+ if (![self.layoutMode isEqualToString:@"continuous"]) return collectionView.bounds.size;
450
+ NSInteger page = [self logicalPageForItem:indexPath.item];
451
+ CGSize imageSize = [self pageSizeAtIndex:page];
452
+ CGSize viewport = self.pendingViewportLayout && self.viewportSize.width > 0 ? self.viewportSize : collectionView.bounds.size;
453
+ if (imageSize.width <= 0 || imageSize.height <= 0) return viewport;
454
+ CGFloat width = viewport.width;
455
+ CGFloat height = width * imageSize.height / imageSize.width;
456
+ if ([self.fitMode isEqualToString:@"page"]) height = MIN(height, viewport.height);
457
+ return CGSizeMake(width, MAX(1, height));
458
+ }
459
+
460
+ - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
461
+ #pragma unused(scrollView)
462
+ if (!decelerate) [self applyPendingPageSizes];
463
+ [self emitVisiblePage];
464
+ }
465
+
466
+ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
467
+ #pragma unused(scrollView)
468
+ [self applyPendingPageSizes];
469
+ [self emitVisiblePage];
470
+ }
471
+ - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
472
+ #pragma unused(scrollView)
473
+ [self applyPendingPageSizes];
474
+ [self emitVisiblePage];
475
+ }
476
+ - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
477
+ #pragma unused(scrollView)
478
+ if (!self.applyingProgrammaticPage) [self emitVisiblePage];
479
+ }
480
+
481
+ - (void)emitVisiblePage {
482
+ if (self.applyingProgrammaticPage || self.pendingNavigation || self.pageCount <= 0) return;
483
+ CGPoint center = CGPointMake(self.collectionView.contentOffset.x + self.collectionView.bounds.size.width / 2,
484
+ self.collectionView.contentOffset.y + self.collectionView.bounds.size.height / 2);
485
+ NSIndexPath *indexPath = [self.collectionView indexPathForItemAtPoint:center];
486
+ if (!indexPath) return;
487
+ NSInteger page = [self logicalPageForItem:indexPath.item] + 1;
488
+ if (page == self.currentPage) return;
489
+ _currentPage = page;
490
+ self.requestedPage = page;
491
+ if (self.onPageChanged) self.onPageChanged(@{ @"page": @(page) });
492
+ }
493
+
494
+ - (void)handleDoubleTap:(UITapGestureRecognizer *)recognizer {
495
+ if (recognizer.state != UIGestureRecognizerStateRecognized ||
496
+ self.pinchRecognizer.state == UIGestureRecognizerStateChanged) return;
497
+ CGFloat previous = MAX(1, self.zoom);
498
+ CGFloat target = previous > 1.05 ? 1.0 : 2.0;
499
+ CGPoint focus = [recognizer locationInView:self.collectionView];
500
+ CGPoint center = CGPointMake(CGRectGetMidX(self.collectionView.bounds),
501
+ CGRectGetMidY(self.collectionView.bounds));
502
+ if (target <= 1.0) {
503
+ self.panOffset = CGPointZero;
504
+ } else {
505
+ CGFloat ratio = target / previous;
506
+ CGFloat maxX = self.collectionView.bounds.size.width * (target - 1.0) / 2.0;
507
+ CGFloat maxY = self.collectionView.bounds.size.height * (target - 1.0) / 2.0;
508
+ CGFloat x = ratio * self.panOffset.x + (1 - ratio) * (focus.x - center.x);
509
+ CGFloat y = ratio * self.panOffset.y + (1 - ratio) * (focus.y - center.y);
510
+ self.panOffset = CGPointMake(MAX(-maxX, MIN(maxX, x)), MAX(-maxY, MIN(maxY, y)));
511
+ }
512
+ self.zoom = target;
513
+ if (self.onZoomChanged) self.onZoomChanged(@{ @"zoom": @(self.zoom) });
514
+ }
515
+
516
+ - (void)handlePinch:(UIPinchGestureRecognizer *)recognizer {
517
+ if (recognizer.state == UIGestureRecognizerStateChanged) {
518
+ self.zoom = MAX(1, MIN(5, self.zoom * recognizer.scale));
519
+ recognizer.scale = 1;
520
+ } else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled || recognizer.state == UIGestureRecognizerStateFailed) {
521
+ [self applyPendingPageSizes];
522
+ if (self.onZoomChanged) self.onZoomChanged(@{ @"zoom": @(self.zoom) });
523
+ }
524
+ }
525
+
526
+ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
527
+ return gestureRecognizer == self.pinchRecognizer || otherGestureRecognizer == self.pinchRecognizer;
528
+ }
529
+
530
+ - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
531
+ #pragma unused(touch)
532
+ if (gestureRecognizer == self.pagePanRecognizer) return self.zoom > 1.0;
533
+ return YES;
534
+ }
535
+
536
+ - (void)handlePagePan:(UIPanGestureRecognizer *)recognizer {
537
+ if (self.zoom <= 1.0) return;
538
+ if (recognizer.state == UIGestureRecognizerStateBegan) {
539
+ self.panGestureStartOffset = self.panOffset;
540
+ } else if (recognizer.state == UIGestureRecognizerStateChanged) {
541
+ CGPoint translation = [recognizer translationInView:self.collectionView];
542
+ CGFloat maxX = self.collectionView.bounds.size.width * (self.zoom - 1.0) / 2.0;
543
+ CGFloat maxY = self.collectionView.bounds.size.height * (self.zoom - 1.0) / 2.0;
544
+ self.panOffset = CGPointMake(
545
+ MAX(-maxX, MIN(maxX, translation.x + self.panGestureStartOffset.x)),
546
+ MAX(-maxY, MIN(maxY, translation.y + self.panGestureStartOffset.y))
547
+ );
548
+ for (PapyrusComicPageCell *cell in self.collectionView.visibleCells) cell.imageView.transform = [self imageTransform];
549
+ } else if (recognizer.state == UIGestureRecognizerStateEnded || recognizer.state == UIGestureRecognizerStateCancelled || recognizer.state == UIGestureRecognizerStateFailed) {
550
+ [self applyPendingPageSizes];
551
+ }
552
+ }
553
+
554
+ @end
@@ -0,0 +1,23 @@
1
+ #import <React/RCTViewManager.h>
2
+ #import "PapyrusComicDocumentView.h"
3
+
4
+ @interface PapyrusComicDocumentViewManager : RCTViewManager
5
+ @end
6
+
7
+ @implementation PapyrusComicDocumentViewManager
8
+ RCT_EXPORT_MODULE(PapyrusComicDocumentView)
9
+ RCT_EXPORT_VIEW_PROPERTY(engineId, NSString)
10
+ RCT_EXPORT_VIEW_PROPERTY(documentGeneration, NSInteger)
11
+ RCT_EXPORT_VIEW_PROPERTY(pageCount, NSInteger)
12
+ RCT_EXPORT_VIEW_PROPERTY(currentPage, NSInteger)
13
+ RCT_EXPORT_VIEW_PROPERTY(layoutMode, NSString)
14
+ RCT_EXPORT_VIEW_PROPERTY(fitMode, NSString)
15
+ RCT_EXPORT_VIEW_PROPERTY(readingDirection, NSString)
16
+ RCT_EXPORT_VIEW_PROPERTY(zoom, CGFloat)
17
+ RCT_EXPORT_VIEW_PROPERTY(pageTheme, NSString)
18
+ RCT_EXPORT_VIEW_PROPERTY(onPageChanged, RCTBubblingEventBlock)
19
+ RCT_EXPORT_VIEW_PROPERTY(onZoomChanged, RCTBubblingEventBlock)
20
+ RCT_EXPORT_VIEW_PROPERTY(onError, RCTBubblingEventBlock)
21
+ + (BOOL)requiresMainQueueSetup { return YES; }
22
+ - (UIView *)view { return [[PapyrusComicDocumentView alloc] initWithFrame:CGRectZero]; }
23
+ @end
@@ -3,6 +3,9 @@
3
3
 
4
4
  NS_ASSUME_NONNULL_BEGIN
5
5
 
6
+ FOUNDATION_EXPORT NSNotificationName const PapyrusEngineStoreDocumentDidChangeNotification;
7
+ FOUNDATION_EXPORT NSString *const PapyrusEngineStoreEngineIdKey;
8
+
6
9
  @interface PapyrusEngineStore : NSObject
7
10
 
8
11
  + (instancetype)shared;
@@ -1,7 +1,13 @@
1
1
  #import "PapyrusEngineStore.h"
2
+ #import "PapyrusPdfPageTheme.h"
3
+
4
+ NSNotificationName const PapyrusEngineStoreDocumentDidChangeNotification =
5
+ @"PapyrusEngineStoreDocumentDidChangeNotification";
6
+ NSString *const PapyrusEngineStoreEngineIdKey = @"engineId";
2
7
 
3
8
  @interface PapyrusEngineStore ()
4
9
  @property (nonatomic, strong) NSMutableDictionary<NSString *, PDFDocument *> *documents;
10
+ - (void)postDocumentChangeForEngineId:(NSString *)engineId;
5
11
  @end
6
12
 
7
13
  @implementation PapyrusEngineStore
@@ -29,20 +35,40 @@
29
35
  }
30
36
 
31
37
  - (void)destroyEngine:(NSString *)engineId {
32
- [self.documents removeObjectForKey:engineId];
38
+ if (engineId.length == 0) return;
39
+ @synchronized(self) {
40
+ [self.documents removeObjectForKey:engineId];
41
+ }
42
+ [self postDocumentChangeForEngineId:engineId];
33
43
  }
34
44
 
35
45
  - (void)setDocument:(PDFDocument *)document forEngine:(NSString *)engineId {
36
- if (!engineId) return;
46
+ if (engineId.length == 0) return;
37
47
  if (document) {
38
- self.documents[engineId] = document;
39
- } else {
40
- [self.documents removeObjectForKey:engineId];
48
+ PapyrusInstallPdfPageThemeDelegate(document);
41
49
  }
50
+ @synchronized(self) {
51
+ if (document) {
52
+ self.documents[engineId] = document;
53
+ } else {
54
+ [self.documents removeObjectForKey:engineId];
55
+ }
56
+ }
57
+ [self postDocumentChangeForEngineId:engineId];
42
58
  }
43
59
 
44
60
  - (PDFDocument *_Nullable)documentForEngine:(NSString *)engineId {
45
- return self.documents[engineId];
61
+ if (engineId.length == 0) return nil;
62
+ @synchronized(self) {
63
+ return self.documents[engineId];
64
+ }
65
+ }
66
+
67
+ - (void)postDocumentChangeForEngineId:(NSString *)engineId {
68
+ [[NSNotificationCenter defaultCenter]
69
+ postNotificationName:PapyrusEngineStoreDocumentDidChangeNotification
70
+ object:self
71
+ userInfo:@{PapyrusEngineStoreEngineIdKey : engineId}];
46
72
  }
47
73
 
48
74
  @end