@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.
- package/android/src/main/cpp/CMakeLists.txt +9 -1
- package/android/src/main/cpp/PapyrusComicArchiveJni.cpp +15 -0
- package/android/src/main/cpp/PapyrusComicArchiveTest.cpp +118 -1
- package/android/src/main/cpp/PapyrusComicImageDimensionsTest.cpp +48 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusComicArchiveNative.java +1 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusComicDocumentView.java +280 -55
- package/android/src/main/java/com/papyrus/engine/PapyrusComicPageLayout.java +19 -2
- package/android/src/main/java/com/papyrus/engine/PapyrusComicPreviewExecutor.java +28 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusComicZoomFrame.java +39 -0
- package/android/src/main/java/com/papyrus/engine/PapyrusNativeEngineModule.java +10 -2
- package/ios/PapyrusComicDocumentView.m +373 -49
- package/ios/PapyrusComicZoomGeometry.h +118 -0
- package/package.json +3 -3
- package/vendor/libarchive/PapyrusComicArchive.cpp +66 -1
- package/vendor/libarchive/PapyrusComicArchive.h +6 -0
- package/vendor/libarchive/PapyrusComicImageDimensions.h +103 -0
|
@@ -95,6 +95,7 @@ if(ANDROID)
|
|
|
95
95
|
PapyrusComicArchiveJni.cpp
|
|
96
96
|
)
|
|
97
97
|
papyrus_set_cxx17(papyrus_comic)
|
|
98
|
+
target_compile_definitions(papyrus_comic PRIVATE PAPYRUS_COMIC_PREINDEX_DIMENSIONS=1)
|
|
98
99
|
target_compile_options(papyrus_comic PRIVATE -Wall -Werror)
|
|
99
100
|
target_link_options(papyrus_comic PRIVATE "-Wl,-z,max-page-size=16384")
|
|
100
101
|
target_include_directories(papyrus_comic PRIVATE
|
|
@@ -127,8 +128,14 @@ endif()
|
|
|
127
128
|
|
|
128
129
|
if(PAPYRUS_COMIC_ARCHIVE_HOST_TESTS)
|
|
129
130
|
enable_testing()
|
|
130
|
-
find_package(ZLIB REQUIRED)
|
|
131
131
|
set(PAPYRUS_LIBARCHIVE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../vendor/libarchive)
|
|
132
|
+
add_executable(papyrus_comic_image_dimensions_test PapyrusComicImageDimensionsTest.cpp)
|
|
133
|
+
papyrus_set_cxx17(papyrus_comic_image_dimensions_test)
|
|
134
|
+
target_compile_options(papyrus_comic_image_dimensions_test PRIVATE -Wall -Wextra -Werror)
|
|
135
|
+
target_include_directories(papyrus_comic_image_dimensions_test PRIVATE ${PAPYRUS_LIBARCHIVE_DIR})
|
|
136
|
+
add_test(NAME papyrus_comic_image_dimensions_test COMMAND papyrus_comic_image_dimensions_test)
|
|
137
|
+
|
|
138
|
+
find_package(ZLIB REQUIRED)
|
|
132
139
|
set(PAPYRUS_LIBARCHIVE_SOURCES
|
|
133
140
|
${PAPYRUS_LIBARCHIVE_DIR}/libarchive/archive_acl.c
|
|
134
141
|
${PAPYRUS_LIBARCHIVE_DIR}/libarchive/archive_check_magic.c
|
|
@@ -186,6 +193,7 @@ if(PAPYRUS_COMIC_ARCHIVE_HOST_TESTS)
|
|
|
186
193
|
)
|
|
187
194
|
papyrus_set_cxx17(papyrus_comic_archive_test)
|
|
188
195
|
target_compile_options(papyrus_comic_archive_test PRIVATE -Wall -Wextra -Werror)
|
|
196
|
+
target_compile_definitions(papyrus_comic_archive_test PRIVATE PAPYRUS_COMIC_PREINDEX_DIMENSIONS=1)
|
|
189
197
|
target_include_directories(papyrus_comic_archive_test PRIVATE ${PAPYRUS_LIBARCHIVE_DIR})
|
|
190
198
|
target_link_libraries(papyrus_comic_archive_test PRIVATE papyrus_archive_host)
|
|
191
199
|
add_test(NAME papyrus_comic_archive_test COMMAND papyrus_comic_archive_test
|
|
@@ -53,6 +53,21 @@ Java_com_papyrus_engine_PapyrusComicArchiveNative_extractPage(JNIEnv *env, jclas
|
|
|
53
53
|
return env->NewStringUTF(path);
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
+
extern "C" JNIEXPORT jintArray JNICALL
|
|
57
|
+
Java_com_papyrus_engine_PapyrusComicArchiveNative_getPageDimensions(JNIEnv *env, jclass,
|
|
58
|
+
jstring engine_id,
|
|
59
|
+
jint generation,
|
|
60
|
+
jint page_index) {
|
|
61
|
+
const std::string engine = from_java(env, engine_id);
|
|
62
|
+
int width = 0;
|
|
63
|
+
int height = 0;
|
|
64
|
+
if (!papyrus_comic_get_page_dimensions(engine.c_str(), generation, page_index, &width, &height)) return nullptr;
|
|
65
|
+
jint values[2] = {width, height};
|
|
66
|
+
jintArray dimensions = env->NewIntArray(2);
|
|
67
|
+
if (dimensions) env->SetIntArrayRegion(dimensions, 0, 2, values);
|
|
68
|
+
return dimensions;
|
|
69
|
+
}
|
|
70
|
+
|
|
56
71
|
extern "C" JNIEXPORT void JNICALL
|
|
57
72
|
Java_com_papyrus_engine_PapyrusComicArchiveNative_close(JNIEnv *env, jclass,
|
|
58
73
|
jstring engine_id,
|
|
@@ -2,13 +2,98 @@
|
|
|
2
2
|
|
|
3
3
|
#include <cassert>
|
|
4
4
|
#include <cstdio>
|
|
5
|
+
#include <cstdint>
|
|
5
6
|
#include <cstring>
|
|
6
7
|
#include <filesystem>
|
|
7
8
|
#include <fstream>
|
|
9
|
+
#include <iostream>
|
|
8
10
|
#include <string>
|
|
11
|
+
#include <utility>
|
|
12
|
+
#include <vector>
|
|
13
|
+
|
|
14
|
+
namespace {
|
|
15
|
+
void append_u16(std::vector<unsigned char> &bytes, std::uint16_t value) {
|
|
16
|
+
bytes.push_back(static_cast<unsigned char>(value & 0xff));
|
|
17
|
+
bytes.push_back(static_cast<unsigned char>((value >> 8) & 0xff));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
void append_u32(std::vector<unsigned char> &bytes, std::uint32_t value) {
|
|
21
|
+
append_u16(bytes, static_cast<std::uint16_t>(value & 0xffff));
|
|
22
|
+
append_u16(bytes, static_cast<std::uint16_t>((value >> 16) & 0xffff));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
std::uint32_t zip_crc32(const std::vector<unsigned char> &bytes) {
|
|
26
|
+
std::uint32_t crc = 0xffffffffU;
|
|
27
|
+
for (unsigned char byte : bytes) {
|
|
28
|
+
crc ^= byte;
|
|
29
|
+
for (int bit = 0; bit < 8; ++bit) crc = (crc >> 1) ^ (0xedb88320U & (0U - (crc & 1U)));
|
|
30
|
+
}
|
|
31
|
+
return ~crc;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
void append_zip_entry(std::vector<unsigned char> &zip,
|
|
35
|
+
const std::string &name,
|
|
36
|
+
const std::vector<unsigned char> &data,
|
|
37
|
+
std::uint32_t *local_offset) {
|
|
38
|
+
*local_offset = static_cast<std::uint32_t>(zip.size());
|
|
39
|
+
const std::uint32_t crc = zip_crc32(data);
|
|
40
|
+
append_u32(zip, 0x04034b50U);
|
|
41
|
+
append_u16(zip, 20); append_u16(zip, 0); append_u16(zip, 0);
|
|
42
|
+
append_u16(zip, 0); append_u16(zip, 0);
|
|
43
|
+
append_u32(zip, crc); append_u32(zip, static_cast<std::uint32_t>(data.size()));
|
|
44
|
+
append_u32(zip, static_cast<std::uint32_t>(data.size()));
|
|
45
|
+
append_u16(zip, static_cast<std::uint16_t>(name.size())); append_u16(zip, 0);
|
|
46
|
+
zip.insert(zip.end(), name.begin(), name.end());
|
|
47
|
+
zip.insert(zip.end(), data.begin(), data.end());
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
void append_central_entry(std::vector<unsigned char> &zip,
|
|
51
|
+
const std::string &name,
|
|
52
|
+
const std::vector<unsigned char> &data,
|
|
53
|
+
std::uint32_t local_offset) {
|
|
54
|
+
append_u32(zip, 0x02014b50U);
|
|
55
|
+
append_u16(zip, 20); append_u16(zip, 20); append_u16(zip, 0); append_u16(zip, 0);
|
|
56
|
+
append_u16(zip, 0); append_u16(zip, 0);
|
|
57
|
+
append_u32(zip, zip_crc32(data)); append_u32(zip, static_cast<std::uint32_t>(data.size()));
|
|
58
|
+
append_u32(zip, static_cast<std::uint32_t>(data.size()));
|
|
59
|
+
append_u16(zip, static_cast<std::uint16_t>(name.size())); append_u16(zip, 0);
|
|
60
|
+
append_u16(zip, 0); append_u16(zip, 0); append_u16(zip, 0); append_u32(zip, 0);
|
|
61
|
+
append_u32(zip, local_offset);
|
|
62
|
+
zip.insert(zip.end(), name.begin(), name.end());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
std::vector<unsigned char> png_header(std::uint32_t width, std::uint32_t height) {
|
|
66
|
+
return {0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 13, 'I', 'H', 'D', 'R',
|
|
67
|
+
static_cast<unsigned char>(width >> 24), static_cast<unsigned char>(width >> 16),
|
|
68
|
+
static_cast<unsigned char>(width >> 8), static_cast<unsigned char>(width),
|
|
69
|
+
static_cast<unsigned char>(height >> 24), static_cast<unsigned char>(height >> 16),
|
|
70
|
+
static_cast<unsigned char>(height >> 8), static_cast<unsigned char>(height)};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
std::filesystem::path make_dimensions_archive(const std::filesystem::path &directory) {
|
|
74
|
+
struct Entry { std::string name; std::vector<unsigned char> data; std::uint32_t local_offset; };
|
|
75
|
+
std::vector<Entry> entries = {
|
|
76
|
+
{"page10.png", png_header(320, 480), 0},
|
|
77
|
+
{"page2.png", png_header(1500, 1920), 0},
|
|
78
|
+
};
|
|
79
|
+
std::vector<unsigned char> zip;
|
|
80
|
+
for (Entry &entry : entries) append_zip_entry(zip, entry.name, entry.data, &entry.local_offset);
|
|
81
|
+
const std::uint32_t central_offset = static_cast<std::uint32_t>(zip.size());
|
|
82
|
+
for (const Entry &entry : entries) append_central_entry(zip, entry.name, entry.data, entry.local_offset);
|
|
83
|
+
const std::uint32_t central_size = static_cast<std::uint32_t>(zip.size()) - central_offset;
|
|
84
|
+
append_u32(zip, 0x06054b50U);
|
|
85
|
+
append_u16(zip, 0); append_u16(zip, 0); append_u16(zip, static_cast<std::uint16_t>(entries.size()));
|
|
86
|
+
append_u16(zip, static_cast<std::uint16_t>(entries.size())); append_u32(zip, central_size);
|
|
87
|
+
append_u32(zip, central_offset); append_u16(zip, 0);
|
|
88
|
+
const std::filesystem::path path = directory / "papyrus-comic-dimensions-test.cbz";
|
|
89
|
+
std::ofstream output(path, std::ios::binary | std::ios::trunc);
|
|
90
|
+
output.write(reinterpret_cast<const char *>(zip.data()), static_cast<std::streamsize>(zip.size()));
|
|
91
|
+
return path;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
9
94
|
|
|
10
95
|
int main(int argc, char **argv) {
|
|
11
|
-
assert(argc
|
|
96
|
+
assert(argc >= 2);
|
|
12
97
|
const std::filesystem::path archive_path(argv[1]);
|
|
13
98
|
const std::filesystem::path cache_dir = std::filesystem::temp_directory_path() / "papyrus-comic-archive-test";
|
|
14
99
|
std::error_code ignored;
|
|
@@ -18,6 +103,9 @@ int main(int argc, char **argv) {
|
|
|
18
103
|
char error[512] = {};
|
|
19
104
|
assert(papyrus_comic_open("test-engine", 1, archive_path.string().c_str(), cache_dir.string().c_str(), &count, error, sizeof(error)) == 1);
|
|
20
105
|
assert(count == 2);
|
|
106
|
+
int page_width = 0;
|
|
107
|
+
int page_height = 0;
|
|
108
|
+
assert(!papyrus_comic_get_page_dimensions("test-engine", 1, 0, &page_width, &page_height));
|
|
21
109
|
|
|
22
110
|
char path[1024] = {};
|
|
23
111
|
assert(papyrus_comic_extract_page("test-engine", 1, 0, path, sizeof(path), error, sizeof(error)) == 1);
|
|
@@ -50,6 +138,35 @@ int main(int argc, char **argv) {
|
|
|
50
138
|
assert(papyrus_comic_open("test-corrupt", 1, corrupt_path.string().c_str(), cache_dir.string().c_str(), &count, error, sizeof(error)) == 0);
|
|
51
139
|
assert(count == 0);
|
|
52
140
|
std::filesystem::remove(corrupt_path, ignored);
|
|
141
|
+
|
|
142
|
+
const std::filesystem::path dimensions_path = make_dimensions_archive(cache_dir.parent_path());
|
|
143
|
+
int dimensions_count = 0;
|
|
144
|
+
assert(papyrus_comic_open("test-dimensions", 1, dimensions_path.string().c_str(), cache_dir.string().c_str(),
|
|
145
|
+
&dimensions_count, error, sizeof(error)) == 1);
|
|
146
|
+
assert(dimensions_count == 2);
|
|
147
|
+
assert(papyrus_comic_get_page_dimensions("test-dimensions", 1, 0, &page_width, &page_height));
|
|
148
|
+
assert(page_width == 1500 && page_height == 1920);
|
|
149
|
+
assert(papyrus_comic_get_page_dimensions("test-dimensions", 1, 1, &page_width, &page_height));
|
|
150
|
+
assert(page_width == 320 && page_height == 480);
|
|
151
|
+
papyrus_comic_close_engine("test-dimensions");
|
|
152
|
+
std::filesystem::remove(dimensions_path, ignored);
|
|
153
|
+
|
|
154
|
+
if (argc > 2) {
|
|
155
|
+
int real_count = 0;
|
|
156
|
+
const std::filesystem::path real_archive(argv[2]);
|
|
157
|
+
assert(papyrus_comic_open("test-real-cbr", 1, real_archive.string().c_str(), cache_dir.string().c_str(),
|
|
158
|
+
&real_count, error, sizeof(error)) == 1);
|
|
159
|
+
int indexed_count = 0;
|
|
160
|
+
for (int page = 0; page < real_count; ++page) {
|
|
161
|
+
if (papyrus_comic_get_page_dimensions("test-real-cbr", 1, page, &page_width, &page_height)) {
|
|
162
|
+
++indexed_count;
|
|
163
|
+
if (page < 5) std::cout << "page " << page << " dimensions=" << page_width << 'x' << page_height << '\n';
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
std::cout << "real CBR pages=" << real_count << " with indexed dimensions=" << indexed_count << '\n';
|
|
167
|
+
assert(real_count > 0 && indexed_count > 0);
|
|
168
|
+
papyrus_comic_close_engine("test-real-cbr");
|
|
169
|
+
}
|
|
53
170
|
std::filesystem::remove_all(cache_dir, ignored);
|
|
54
171
|
return 0;
|
|
55
172
|
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
#include "PapyrusComicImageDimensions.h"
|
|
2
|
+
|
|
3
|
+
#include <cassert>
|
|
4
|
+
#include <cstdint>
|
|
5
|
+
#include <vector>
|
|
6
|
+
|
|
7
|
+
namespace {
|
|
8
|
+
void appendJpegSegment(std::vector<std::uint8_t> &bytes, std::uint8_t marker,
|
|
9
|
+
const std::vector<std::uint8_t> &payload) {
|
|
10
|
+
bytes.push_back(0xff);
|
|
11
|
+
bytes.push_back(marker);
|
|
12
|
+
const std::uint16_t length = static_cast<std::uint16_t>(payload.size() + 2);
|
|
13
|
+
bytes.push_back(static_cast<std::uint8_t>(length >> 8));
|
|
14
|
+
bytes.push_back(static_cast<std::uint8_t>(length & 0xff));
|
|
15
|
+
bytes.insert(bytes.end(), payload.begin(), payload.end());
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
int main() {
|
|
20
|
+
int width = 0;
|
|
21
|
+
int height = 0;
|
|
22
|
+
|
|
23
|
+
std::vector<std::uint8_t> jpeg = {0xff, 0xd8};
|
|
24
|
+
appendJpegSegment(jpeg, 0xe0, {0x4a, 0x46, 0x49, 0x46, 0x00});
|
|
25
|
+
appendJpegSegment(jpeg, 0xc0, {0x08, 0x0c, 0xbf, 0x09, 0xf6, 0x03, 0x01, 0x11, 0x00,
|
|
26
|
+
0x02, 0x11, 0x00, 0x03, 0x11, 0x00});
|
|
27
|
+
assert(papyrus_comic_parse_image_dimensions(jpeg.data(), jpeg.size(), &width, &height));
|
|
28
|
+
assert(width == 2550);
|
|
29
|
+
assert(height == 3263);
|
|
30
|
+
|
|
31
|
+
const std::vector<std::uint8_t> png = {
|
|
32
|
+
0x89, 'P', 'N', 'G', 0x0d, 0x0a, 0x1a, 0x0a,
|
|
33
|
+
0x00, 0x00, 0x00, 0x0d, 'I', 'H', 'D', 'R',
|
|
34
|
+
0x00, 0x00, 0x05, 0xdc, 0x00, 0x00, 0x07, 0x80};
|
|
35
|
+
assert(papyrus_comic_parse_image_dimensions(png.data(), png.size(), &width, &height));
|
|
36
|
+
assert(width == 1500);
|
|
37
|
+
assert(height == 1920);
|
|
38
|
+
|
|
39
|
+
const std::vector<std::uint8_t> gif = {'G', 'I', 'F', '8', '9', 'a', 0x20, 0x03, 0x80, 0x07};
|
|
40
|
+
assert(papyrus_comic_parse_image_dimensions(gif.data(), gif.size(), &width, &height));
|
|
41
|
+
assert(width == 800);
|
|
42
|
+
assert(height == 1920);
|
|
43
|
+
|
|
44
|
+
assert(!papyrus_comic_parse_image_dimensions(png.data(), 12, &width, &height));
|
|
45
|
+
const std::vector<std::uint8_t> unknown = {'n', 'o', 't', ' ', 'i', 'm', 'a', 'g', 'e'};
|
|
46
|
+
assert(!papyrus_comic_parse_image_dimensions(unknown.data(), unknown.size(), &width, &height));
|
|
47
|
+
return 0;
|
|
48
|
+
}
|
|
@@ -9,6 +9,7 @@ final class PapyrusComicArchiveNative {
|
|
|
9
9
|
|
|
10
10
|
static native int open(String engineId, int generation, String archivePath, String cachePath);
|
|
11
11
|
static native String extractPage(String engineId, int generation, int pageIndex);
|
|
12
|
+
static native int[] getPageDimensions(String engineId, int generation, int pageIndex);
|
|
12
13
|
static native void close(String engineId, int generation);
|
|
13
14
|
static native void closeEngine(String engineId);
|
|
14
15
|
}
|