@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.
@@ -0,0 +1,118 @@
1
+ #pragma once
2
+
3
+ #include <math.h>
4
+ #include <stddef.h>
5
+
6
+ typedef struct {
7
+ double originX;
8
+ double originY;
9
+ double width;
10
+ double height;
11
+ } PapyrusComicLayoutFrame;
12
+
13
+ static inline double PapyrusComicClampZoom(double zoom) {
14
+ return fmax(1.0, fmin(5.0, zoom));
15
+ }
16
+
17
+ static inline double PapyrusComicZoomedExtent(double extent, double zoom) {
18
+ return fmax(1.0, extent) * PapyrusComicClampZoom(zoom);
19
+ }
20
+
21
+ static inline double PapyrusComicContinuousContentWidth(double viewportWidth, double zoom) {
22
+ return PapyrusComicZoomedExtent(viewportWidth, zoom);
23
+ }
24
+
25
+ static inline double PapyrusComicContinuousDocumentHeight(
26
+ const double *prefixHeights,
27
+ size_t pageCount,
28
+ double zoom) {
29
+ if (prefixHeights == NULL || pageCount == 0) return 0.0;
30
+ return fmax(0.0, prefixHeights[pageCount]) * PapyrusComicClampZoom(zoom);
31
+ }
32
+
33
+ static inline PapyrusComicLayoutFrame PapyrusComicContinuousPageFrame(
34
+ const double *prefixHeights,
35
+ size_t pageCount,
36
+ size_t pageIndex,
37
+ double viewportWidth,
38
+ double zoom) {
39
+ PapyrusComicLayoutFrame frame = {0.0, 0.0, 0.0, 0.0};
40
+ if (prefixHeights == NULL || pageIndex >= pageCount) return frame;
41
+ double scale = PapyrusComicClampZoom(zoom);
42
+ double start = fmax(0.0, prefixHeights[pageIndex]);
43
+ double end = fmax(start, prefixHeights[pageIndex + 1]);
44
+ frame.originY = start * scale;
45
+ frame.width = PapyrusComicContinuousContentWidth(viewportWidth, scale);
46
+ frame.height = fmax(1.0, (end - start) * scale);
47
+ return frame;
48
+ }
49
+
50
+ static inline size_t PapyrusComicContinuousFirstVisiblePage(
51
+ const double *prefixHeights,
52
+ size_t pageCount,
53
+ double minY,
54
+ double zoom) {
55
+ if (prefixHeights == NULL || pageCount == 0) return pageCount;
56
+ double scale = PapyrusComicClampZoom(zoom);
57
+ size_t lower = 0;
58
+ size_t upper = pageCount;
59
+ while (lower < upper) {
60
+ size_t middle = lower + (upper - lower) / 2;
61
+ if (prefixHeights[middle + 1] * scale <= minY) lower = middle + 1;
62
+ else upper = middle;
63
+ }
64
+ return lower;
65
+ }
66
+
67
+ static inline void PapyrusComicContinuousApplyHeightDeltas(
68
+ double *prefixHeights,
69
+ const double *heightDeltas,
70
+ size_t pageCount) {
71
+ if (prefixHeights == NULL || heightDeltas == NULL || pageCount == 0) return;
72
+ double accumulatedDelta = 0.0;
73
+ for (size_t boundary = 1; boundary <= pageCount; boundary++) {
74
+ accumulatedDelta += heightDeltas[boundary - 1];
75
+ prefixHeights[boundary] += accumulatedDelta;
76
+ }
77
+ }
78
+
79
+ static inline double PapyrusComicContinuousPageHeight(
80
+ double imageWidth,
81
+ double imageHeight,
82
+ double viewportWidth,
83
+ double viewportHeight,
84
+ int fitPage,
85
+ double zoom) {
86
+ double baseHeight = fmax(1.0, viewportHeight);
87
+ if (imageWidth > 0.0 && imageHeight > 0.0 && viewportWidth > 0.0) {
88
+ baseHeight = viewportWidth * imageHeight / imageWidth;
89
+ if (fitPage && viewportHeight > 0.0) baseHeight = fmin(baseHeight, viewportHeight);
90
+ }
91
+ return fmax(1.0, baseHeight) * PapyrusComicClampZoom(zoom);
92
+ }
93
+
94
+ static inline double PapyrusComicZoomAnchorFraction(
95
+ double contentOffset,
96
+ double focusInViewport,
97
+ double itemOrigin,
98
+ double itemExtent) {
99
+ if (itemExtent <= 0.0) return 0.0;
100
+ double fraction = (contentOffset + focusInViewport - itemOrigin) / itemExtent;
101
+ return fmax(0.0, fmin(1.0, fraction));
102
+ }
103
+
104
+ static inline double PapyrusComicZoomAnchorOffset(
105
+ double itemOrigin,
106
+ double itemExtent,
107
+ double fraction,
108
+ double focusInViewport,
109
+ double contentExtent,
110
+ double viewportExtent,
111
+ double leadingInset,
112
+ double trailingInset) {
113
+ double minimum = -leadingInset;
114
+ double maximum = fmax(minimum, contentExtent - viewportExtent + trailingInset);
115
+ double anchor = itemOrigin + itemExtent * fmax(0.0, fmin(1.0, fraction));
116
+ double offset = anchor - focusInViewport;
117
+ return fmax(minimum, fmin(maximum, offset));
118
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@papyrus-sdk/engine-native",
3
- "version": "0.2.23",
3
+ "version": "0.2.25",
4
4
  "license": "MIT",
5
5
  "keywords": [
6
6
  "papyrus",
@@ -49,12 +49,12 @@
49
49
  "url": "https://github.com/solrachix/Papyrus/issues"
50
50
  },
51
51
  "peerDependencies": {
52
- "@papyrus-sdk/core": "0.2.24",
52
+ "@papyrus-sdk/core": "0.2.25",
53
53
  "@papyrus-sdk/types": "0.2.17",
54
54
  "react-native": ">=0.81.0"
55
55
  },
56
56
  "devDependencies": {
57
- "@papyrus-sdk/core": "0.2.24",
57
+ "@papyrus-sdk/core": "0.2.25",
58
58
  "@papyrus-sdk/types": "0.2.17",
59
59
  "react-native": "^0.81.0"
60
60
  },
@@ -17,6 +17,10 @@
17
17
  #include <unordered_map>
18
18
  #include <vector>
19
19
 
20
+ #if defined(PAPYRUS_COMIC_PREINDEX_DIMENSIONS)
21
+ #include "PapyrusComicImageDimensions.h"
22
+ #endif
23
+
20
24
  namespace fs = std::filesystem;
21
25
 
22
26
  namespace {
@@ -26,12 +30,18 @@ constexpr std::uint64_t kMaximumExtractedPageCacheBytes = 80ULL * 1024ULL * 1024
26
30
  constexpr std::size_t kMaximumEntries = 50000;
27
31
  constexpr std::size_t kMaximumArchiveEntries = 200000;
28
32
  constexpr std::size_t kIoBlockSize = 64 * 1024;
33
+ #if defined(PAPYRUS_COMIC_PREINDEX_DIMENSIONS)
34
+ constexpr std::size_t kMaximumImageHeaderBytesPerPage = 256 * 1024;
35
+ constexpr std::size_t kMaximumImageHeaderScanBytes = 16 * 1024 * 1024;
36
+ #endif
29
37
 
30
38
  struct ComicPage {
31
39
  std::string name;
32
40
  std::string normalized_name;
33
41
  std::uint64_t declared_size = 0;
34
42
  std::size_t archive_ordinal = 0;
43
+ int image_width = 0;
44
+ int image_height = 0;
35
45
  };
36
46
 
37
47
  struct ComicDocument {
@@ -151,6 +161,40 @@ Reader new_reader(std::string &error) {
151
161
  return reader;
152
162
  }
153
163
 
164
+ #if defined(PAPYRUS_COMIC_PREINDEX_DIMENSIONS)
165
+ bool read_image_dimensions(archive *reader, std::size_t &remaining_budget, int *width, int *height) {
166
+ const std::size_t limit = std::min(kMaximumImageHeaderBytesPerPage, remaining_budget);
167
+ if (limit == 0) return false;
168
+
169
+ std::vector<std::uint8_t> prefix;
170
+ std::size_t target = std::min<std::size_t>(1024, limit);
171
+ bool entry_ended = false;
172
+ while (target > 0) {
173
+ const std::size_t previous_size = prefix.size();
174
+ prefix.resize(target);
175
+ std::size_t filled = previous_size;
176
+ while (filled < target) {
177
+ const la_ssize_t count = archive_read_data(reader, prefix.data() + filled, target - filled);
178
+ if (count <= 0) {
179
+ prefix.resize(filled);
180
+ entry_ended = true;
181
+ break;
182
+ }
183
+ filled += static_cast<std::size_t>(count);
184
+ }
185
+
186
+ if (!prefix.empty() && papyrus_comic_parse_image_dimensions(prefix.data(), prefix.size(), width, height)) {
187
+ remaining_budget -= prefix.size();
188
+ return true;
189
+ }
190
+ if (entry_ended || prefix.size() >= limit) break;
191
+ target = std::min(limit, std::max(target + 1, target * 2));
192
+ }
193
+ remaining_budget -= std::min(remaining_budget, prefix.size());
194
+ return false;
195
+ }
196
+ #endif
197
+
154
198
  std::shared_ptr<ComicDocument> find_document(const char *engine_id, int generation) {
155
199
  std::lock_guard<std::mutex> guard(g_documents_lock);
156
200
  const auto found = g_documents.find(key_for(engine_id, generation));
@@ -210,6 +254,9 @@ extern "C" int papyrus_comic_open(const char *engine_id,
210
254
 
211
255
  archive_entry *entry = nullptr;
212
256
  std::size_t ordinal = 0;
257
+ #if defined(PAPYRUS_COMIC_PREINDEX_DIMENSIONS)
258
+ std::size_t remaining_dimension_budget = kMaximumImageHeaderScanBytes;
259
+ #endif
213
260
  for (;;) {
214
261
  const int status = archive_read_next_header(reader.get(), &entry);
215
262
  if (status == ARCHIVE_EOF) break;
@@ -225,7 +272,11 @@ extern "C" int papyrus_comic_open(const char *engine_id,
225
272
  if (archive_entry_filetype(entry) == AE_IFREG && !archive_entry_symlink(entry) &&
226
273
  !archive_entry_hardlink(entry) && is_supported_image(name) &&
227
274
  declared_size >= 0 && static_cast<std::uint64_t>(declared_size) <= kMaximumEntryBytes) {
228
- document->pages.push_back({name, name, static_cast<std::uint64_t>(declared_size), ordinal});
275
+ ComicPage page{name, name, static_cast<std::uint64_t>(declared_size), ordinal};
276
+ #if defined(PAPYRUS_COMIC_PREINDEX_DIMENSIONS)
277
+ read_image_dimensions(reader.get(), remaining_dimension_budget, &page.image_width, &page.image_height);
278
+ #endif
279
+ document->pages.push_back(std::move(page));
229
280
  if (document->pages.size() > kMaximumEntries) {
230
281
  write_error(error_buffer, error_buffer_size, "Comic archive has too many page entries");
231
282
  return 0;
@@ -258,6 +309,20 @@ extern "C" int papyrus_comic_open(const char *engine_id,
258
309
  return 1;
259
310
  }
260
311
 
312
+ extern "C" int papyrus_comic_get_page_dimensions(const char *engine_id,
313
+ int generation,
314
+ int page_index,
315
+ int *width,
316
+ int *height) {
317
+ auto document = find_document(engine_id, generation);
318
+ if (!document || page_index < 0 || static_cast<std::size_t>(page_index) >= document->pages.size()) return 0;
319
+ const ComicPage &page = document->pages[static_cast<std::size_t>(page_index)];
320
+ if (page.image_width <= 0 || page.image_height <= 0) return 0;
321
+ if (width) *width = page.image_width;
322
+ if (height) *height = page.image_height;
323
+ return 1;
324
+ }
325
+
261
326
  extern "C" int papyrus_comic_extract_page(const char *engine_id,
262
327
  int generation,
263
328
  int page_index,
@@ -22,6 +22,12 @@ int papyrus_comic_extract_page(const char *engine_id,
22
22
  char *error_buffer,
23
23
  size_t error_buffer_size);
24
24
 
25
+ int papyrus_comic_get_page_dimensions(const char *engine_id,
26
+ int generation,
27
+ int page_index,
28
+ int *width,
29
+ int *height);
30
+
25
31
  void papyrus_comic_close(const char *engine_id, int generation);
26
32
  void papyrus_comic_close_engine(const char *engine_id);
27
33
 
@@ -0,0 +1,103 @@
1
+ #pragma once
2
+
3
+ #include <cstddef>
4
+ #include <cstdint>
5
+ #include <limits>
6
+
7
+ namespace {
8
+ inline bool papyrus_comic_set_dimensions(std::uint64_t width, std::uint64_t height,
9
+ int *out_width, int *out_height) {
10
+ if (width == 0 || height == 0 || width > static_cast<std::uint64_t>(std::numeric_limits<int>::max()) ||
11
+ height > static_cast<std::uint64_t>(std::numeric_limits<int>::max()) || width > 20000 || height > 20000 ||
12
+ width * height > 80000000ULL) return false;
13
+ if (out_width) *out_width = static_cast<int>(width);
14
+ if (out_height) *out_height = static_cast<int>(height);
15
+ return true;
16
+ }
17
+
18
+ inline std::uint32_t papyrus_comic_read_u24_le(const std::uint8_t *bytes) {
19
+ return static_cast<std::uint32_t>(bytes[0]) |
20
+ (static_cast<std::uint32_t>(bytes[1]) << 8) |
21
+ (static_cast<std::uint32_t>(bytes[2]) << 16);
22
+ }
23
+
24
+ inline bool papyrus_comic_parse_jpeg(const std::uint8_t *bytes, std::size_t size,
25
+ int *width, int *height) {
26
+ if (size < 2 || bytes[0] != 0xff || bytes[1] != 0xd8) return false;
27
+ std::size_t offset = 2;
28
+ while (offset < size) {
29
+ if (bytes[offset] != 0xff) return false;
30
+ while (offset < size && bytes[offset] == 0xff) ++offset;
31
+ if (offset >= size) return false;
32
+ const std::uint8_t marker = bytes[offset++];
33
+ if (marker == 0x00) continue;
34
+ if (marker == 0xd9 || marker == 0xda) return false;
35
+ if (marker == 0x01 || (marker >= 0xd0 && marker <= 0xd7)) continue;
36
+ if (offset + 2 > size) return false;
37
+
38
+ const std::size_t segment_length = (static_cast<std::size_t>(bytes[offset]) << 8) | bytes[offset + 1];
39
+ if (segment_length < 2) return false;
40
+ const bool start_of_frame =
41
+ (marker >= 0xc0 && marker <= 0xc3) || (marker >= 0xc5 && marker <= 0xc7) ||
42
+ (marker >= 0xc9 && marker <= 0xcb) || (marker >= 0xcd && marker <= 0xcf);
43
+ if (start_of_frame) {
44
+ if (segment_length < 8 || offset + segment_length > size) return false;
45
+ const std::uint64_t parsed_height = (static_cast<std::uint16_t>(bytes[offset + 3]) << 8) | bytes[offset + 4];
46
+ const std::uint64_t parsed_width = (static_cast<std::uint16_t>(bytes[offset + 5]) << 8) | bytes[offset + 6];
47
+ return papyrus_comic_set_dimensions(parsed_width, parsed_height, width, height);
48
+ }
49
+ if (segment_length > size - offset) return false;
50
+ offset += segment_length;
51
+ }
52
+ return false;
53
+ }
54
+ } // namespace
55
+
56
+ inline bool papyrus_comic_parse_image_dimensions(const std::uint8_t *bytes, std::size_t size,
57
+ int *width, int *height) {
58
+ if (!bytes) return false;
59
+
60
+ static constexpr std::uint8_t kPngSignature[] = {0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a};
61
+ if (size >= 24) {
62
+ bool png_signature = true;
63
+ for (std::size_t i = 0; i < sizeof(kPngSignature); ++i) png_signature = png_signature && bytes[i] == kPngSignature[i];
64
+ if (png_signature && bytes[12] == 'I' && bytes[13] == 'H' && bytes[14] == 'D' && bytes[15] == 'R') {
65
+ const std::uint64_t parsed_width = (static_cast<std::uint32_t>(bytes[16]) << 24) |
66
+ (static_cast<std::uint32_t>(bytes[17]) << 16) | (static_cast<std::uint32_t>(bytes[18]) << 8) | bytes[19];
67
+ const std::uint64_t parsed_height = (static_cast<std::uint32_t>(bytes[20]) << 24) |
68
+ (static_cast<std::uint32_t>(bytes[21]) << 16) | (static_cast<std::uint32_t>(bytes[22]) << 8) | bytes[23];
69
+ return papyrus_comic_set_dimensions(parsed_width, parsed_height, width, height);
70
+ }
71
+ }
72
+
73
+ if (size >= 10 && bytes[0] == 'G' && bytes[1] == 'I' && bytes[2] == 'F' && bytes[3] == '8' &&
74
+ (bytes[4] == '7' || bytes[4] == '9') && bytes[5] == 'a') {
75
+ const std::uint64_t parsed_width = static_cast<std::uint16_t>(bytes[6] | (bytes[7] << 8));
76
+ const std::uint64_t parsed_height = static_cast<std::uint16_t>(bytes[8] | (bytes[9] << 8));
77
+ return papyrus_comic_set_dimensions(parsed_width, parsed_height, width, height);
78
+ }
79
+
80
+ if (size >= 30 && bytes[0] == 'R' && bytes[1] == 'I' && bytes[2] == 'F' && bytes[3] == 'F' &&
81
+ bytes[8] == 'W' && bytes[9] == 'E' && bytes[10] == 'B' && bytes[11] == 'P') {
82
+ if (bytes[12] == 'V' && bytes[13] == 'P' && bytes[14] == '8' && bytes[15] == 'X') {
83
+ return papyrus_comic_set_dimensions(1ULL + papyrus_comic_read_u24_le(bytes + 24),
84
+ 1ULL + papyrus_comic_read_u24_le(bytes + 27), width, height);
85
+ }
86
+ if (bytes[12] == 'V' && bytes[13] == 'P' && bytes[14] == '8' && bytes[15] == ' ') {
87
+ if (bytes[23] != 0x9d || bytes[24] != 0x01 || bytes[25] != 0x2a) return false;
88
+ const std::uint64_t parsed_width = ((bytes[27] & 0x3f) << 8) | bytes[26];
89
+ const std::uint64_t parsed_height = ((bytes[29] & 0x3f) << 8) | bytes[28];
90
+ return papyrus_comic_set_dimensions(parsed_width, parsed_height, width, height);
91
+ }
92
+ }
93
+ if (size >= 25 && bytes[0] == 'R' && bytes[1] == 'I' && bytes[2] == 'F' && bytes[3] == 'F' &&
94
+ bytes[8] == 'W' && bytes[9] == 'E' && bytes[10] == 'B' && bytes[11] == 'P' &&
95
+ bytes[12] == 'V' && bytes[13] == 'P' && bytes[14] == '8' && bytes[15] == 'L' && bytes[20] == 0x2f) {
96
+ const std::uint64_t parsed_width = 1ULL + bytes[21] + (static_cast<std::uint64_t>(bytes[22] & 0x3f) << 8);
97
+ const std::uint64_t parsed_height = 1ULL + (bytes[22] >> 6) + (static_cast<std::uint64_t>(bytes[23]) << 2) +
98
+ (static_cast<std::uint64_t>(bytes[24] & 0x0f) << 10);
99
+ return papyrus_comic_set_dimensions(parsed_width, parsed_height, width, height);
100
+ }
101
+
102
+ return papyrus_comic_parse_jpeg(bytes, size, width, height);
103
+ }