Skip to content

Commit 1aef5f0

Browse files
committed
deal with assets; cleanup core wrapper
1 parent bd72cf8 commit 1aef5f0

6 files changed

Lines changed: 124 additions & 102 deletions

File tree

app/src/main/cpp/CoreWrapper.cpp

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,54 +6,76 @@
66
#include <odr/odr.hpp>
77
#include <odr/exceptions.hpp>
88
#include <odr/http_server.hpp>
9+
#include <odr/global_params.hpp>
910

1011
#include <android/log.h>
1112

1213
#include <string>
1314
#include <optional>
1415
#include <filesystem>
1516

17+
namespace {
18+
19+
std::string convertString(JNIEnv *env, jstring string) {
20+
jboolean isCopy;
21+
const char* cstring = env->GetStringUTFChars(string, &isCopy);
22+
auto cppstring = std::string(cstring, env->GetStringUTFLength(string));
23+
env->ReleaseStringUTFChars(string, cstring);
24+
return cppstring;
25+
}
26+
27+
std::string getStringField(JNIEnv *env, jclass clazz, const char *name) {
28+
jfieldID field = env->GetFieldID(clazz, name, "Ljava/lang/String;");
29+
auto string = (jstring) env->GetObjectField(clazz, field);
30+
return convertString(env, string);
31+
}
32+
33+
}
34+
1635
std::optional<odr::Html> s_html;
1736

37+
JNIEXPORT void JNICALL
38+
Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jclass clazz,
39+
jobject params) {
40+
jboolean isCopy;
41+
42+
jclass paramsClass = env->GetObjectClass(params);
43+
44+
std::string odrCoreDataPath = getStringField(env, paramsClass, "coreDataPath");
45+
std::string fontconfigDataPath = getStringField(env, paramsClass, "fontconfigDataPath");
46+
std::string popplerDataPath = getStringField(env, paramsClass, "popplerDataPath");
47+
std::string pdf2htmlexDataPath = getStringField(env, paramsClass, "pdf2htmlexDataPath");
48+
49+
odr::GlobalParams::set_odr_core_data_path(odrCoreDataPath);
50+
odr::GlobalParams::set_fontconfig_data_path(fontconfigDataPath);
51+
odr::GlobalParams::set_poppler_data_path(popplerDataPath);
52+
odr::GlobalParams::set_pdf2htmlex_data_path(pdf2htmlexDataPath);
53+
}
54+
1855
JNIEXPORT jobject JNICALL
1956
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz,
2057
jobject options) {
21-
jboolean isCopy;
22-
2358
jclass resultClass = env->FindClass("at/tomtasche/reader/background/CoreWrapper$CoreResult");
2459
jmethodID resultConstructor = env->GetMethodID(resultClass, "<init>", "()V");
2560
jobject result = env->NewObject(resultClass, resultConstructor);
2661

2762
jfieldID errorField = env->GetFieldID(resultClass, "errorCode", "I");
2863

2964
jclass optionsClass = env->GetObjectClass(options);
30-
jfieldID inputPathField = env->GetFieldID(optionsClass, "inputPath", "Ljava/lang/String;");
31-
auto inputPath = (jstring) env->GetObjectField(options, inputPathField);
32-
33-
const auto inputPathC = env->GetStringUTFChars(inputPath, &isCopy);
34-
auto inputPathCpp = std::string(inputPathC, env->GetStringUTFLength(inputPath));
35-
env->ReleaseStringUTFChars(inputPath, inputPathC);
65+
std::string inputPathCpp = getStringField(env, optionsClass, "inputPath");
3666

3767
try {
3868
std::optional<std::string> passwordCpp;
3969
jfieldID passwordField = env->GetFieldID(optionsClass, "password", "Ljava/lang/String;");
4070
auto password = (jstring) env->GetObjectField(options, passwordField);
4171
if (password != nullptr) {
42-
const auto passwordC = env->GetStringUTFChars(password, &isCopy);
43-
passwordCpp = std::string(passwordC, env->GetStringUTFLength(password));
44-
env->ReleaseStringUTFChars(password, passwordC);
72+
passwordCpp = convertString(env, password);
4573
}
4674

4775
jfieldID editableField = env->GetFieldID(optionsClass, "editable", "Z");
4876
jboolean editable = env->GetBooleanField(options, editableField);
4977

50-
jfieldID outputPathField = env->GetFieldID(optionsClass, "outputPath",
51-
"Ljava/lang/String;");
52-
auto outputPath = (jstring) env->GetObjectField(options, outputPathField);
53-
54-
const auto outputPathC = env->GetStringUTFChars(outputPath, &isCopy);
55-
auto outputPathCpp = std::string(outputPathC, env->GetStringUTFLength(outputPath));
56-
env->ReleaseStringUTFChars(outputPath, outputPathC);
78+
std::string outputPathCpp = getStringField(env, optionsClass, "outputPath");
5779

5880
jclass listClass = env->FindClass("java/util/List");
5981
jmethodID addMethod = env->GetMethodID(listClass, "add", "(Ljava/lang/Object;)Z");
@@ -122,20 +144,24 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass
122144
return result;
123145
}
124146

147+
if (file.is_document_file()) {
148+
odr::DocumentFile document_file = file.document_file();
149+
if (document_file.password_encrypted()) {
150+
if (!passwordCpp.has_value() || !document_file.decrypt(passwordCpp.value())) {
151+
env->SetIntField(result, errorField, -2);
152+
return result;
153+
}
154+
}
155+
}
156+
125157
odr::HtmlConfig config;
126158
config.editable = editable;
127159

128160
if (paging) {
129161
config.text_document_margin = true;
130162
}
131163

132-
s_html = odr::html::translate(odr::File(inputPathCpp), outputPathCpp, config,
133-
[&passwordCpp]() -> std::string {
134-
if (passwordCpp.has_value()) {
135-
return passwordCpp.value();
136-
}
137-
return "";
138-
});
164+
s_html = odr::html::translate(file, outputPathCpp, config);
139165

140166
{
141167
const auto extensionCpp = odr::type_to_string(
@@ -158,9 +184,6 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass
158184
} catch (odr::UnknownFileType &) {
159185
env->SetIntField(result, errorField, -5);
160186
return result;
161-
} catch (odr::WrongPassword &) {
162-
env->SetIntField(result, errorField, -2);
163-
return result;
164187
} catch (odr::UnsupportedFileType &) {
165188
env->SetIntField(result, errorField, -5);
166189
return result;
@@ -181,8 +204,6 @@ JNIEXPORT jobject JNICALL
181204
Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env, jclass clazz,
182205
jobject options,
183206
jstring htmlDiff) {
184-
jboolean isCopy;
185-
186207
jclass optionsClass = env->GetObjectClass(options);
187208

188209
jclass resultClass = env->FindClass("at/tomtasche/reader/background/CoreWrapper$CoreResult");
@@ -192,15 +213,9 @@ Java_at_tomtasche_reader_background_CoreWrapper_backtranslateNative(JNIEnv *env,
192213
jfieldID errorField = env->GetFieldID(resultClass, "errorCode", "I");
193214

194215
try {
195-
jfieldID outputPathPrefixField = env->GetFieldID(optionsClass, "outputPath",
196-
"Ljava/lang/String;");
197-
jstring outputPathPrefix = (jstring) env->GetObjectField(options, outputPathPrefixField);
198-
199-
const auto outputPathPrefixC = env->GetStringUTFChars(outputPathPrefix, &isCopy);
200-
auto outputPathPrefixCpp = std::string(outputPathPrefixC,
201-
env->GetStringUTFLength(outputPathPrefix));
202-
env->ReleaseStringUTFChars(outputPathPrefix, outputPathPrefixC);
216+
std::string outputPathPrefixCpp = getStringField(env, optionsClass, "outputPath");
203217

218+
jboolean isCopy;
204219
const auto htmlDiffC = env->GetStringUTFChars(htmlDiff, &isCopy);
205220

206221
const auto extension = odr::type_to_string(s_html->file_type());
@@ -246,10 +261,8 @@ Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass
246261
std::optional<odr::HttpServer> s_server;
247262

248263
JNIEXPORT void JNICALL
249-
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jclass clazz, jstring outputPath) {
250-
const char* outputPathC = env->GetStringUTFChars(outputPath, nullptr);
251-
std::string output_path = outputPathC;
252-
env->ReleaseStringUTFChars(outputPath, outputPathC);
264+
Java_at_tomtasche_reader_background_CoreWrapper_createServer(JNIEnv *env, jclass clazz, jstring outputPath) {
265+
std::string output_path = convertString(env, outputPath);
253266

254267
std::filesystem::create_directories(output_path);
255268

@@ -258,17 +271,14 @@ Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env,
258271
s_server = odr::HttpServer(config);
259272
}
260273

261-
JNIEXPORT jstring JNICALL
262-
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jclass clazz, jobject options) {
274+
JNIEXPORT void JNICALL
275+
Java_at_tomtasche_reader_background_CoreWrapper_hostFile(JNIEnv *env, jclass clazz, jstring prefix, jobject options) {
263276
jboolean isCopy;
264277

265278
jclass optionsClass = env->GetObjectClass(options);
266-
jfieldID inputPathField = env->GetFieldID(optionsClass, "inputPath", "Ljava/lang/String;");
267-
auto inputPath = (jstring) env->GetObjectField(options, inputPathField);
268279

269-
const auto inputPathC = env->GetStringUTFChars(inputPath, &isCopy);
270-
auto inputPathCpp = std::string(inputPathC, env->GetStringUTFLength(inputPath));
271-
env->ReleaseStringUTFChars(inputPath, inputPathC);
280+
std::string inputPathCpp = getStringField(env, optionsClass, "inputPath");
281+
std::string prefixCpp = convertString(env, prefix);
272282

273283
odr::DecodePreference decodePreference;
274284
decodePreference.engine_priority = {
@@ -278,22 +288,19 @@ decodePreference.engine_priority = {
278288
__android_log_print(ANDROID_LOG_INFO, "smn", "file type %i", file.file_type());
279289

280290
try {
281-
std::string prefix = "hi";
282-
s_server->serve_file(file, prefix, odr::HtmlConfig());
283-
return env->NewStringUTF(prefix.c_str());
291+
s_server->serve_file(file, prefixCpp, odr::HtmlConfig());
284292
} catch (...) {
285293
__android_log_print(ANDROID_LOG_ERROR, "smn", "error");
286-
return env->NewStringUTF("error");
287294
}
288295
}
289296

290297
JNIEXPORT void JNICALL
291-
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jclass clazz) {
292-
s_server->listen("127.0.0.1", 29665);
298+
Java_at_tomtasche_reader_background_CoreWrapper_listenServer(JNIEnv *env, jclass clazz, jint port) {
299+
s_server->listen("127.0.0.1", port);
293300
}
294301

295302
JNIEXPORT void JNICALL
296-
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jclass clazz) {
303+
Java_at_tomtasche_reader_background_CoreWrapper_stopServer(JNIEnv *env, jclass clazz) {
297304
s_server->stop();
298305
s_server.reset();
299306
}

app/src/main/cpp/CoreWrapper.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
extern "C" {
77

8+
JNIEXPORT void JNICALL
9+
Java_at_tomtasche_reader_background_CoreWrapper_setGlobalParams(JNIEnv *env, jclass clazz,
10+
jobject params);
11+
812
JNIEXPORT jobject JNICALL
913
Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jclass clazz, jobject options);
1014

@@ -15,16 +19,16 @@ extern "C" {
1519
Java_at_tomtasche_reader_background_CoreWrapper_closeNative(JNIEnv *env, jclass clazz, jobject options);
1620

1721
JNIEXPORT void JNICALL
18-
Java_at_tomtasche_reader_background_CoreWrapper_createServerNative(JNIEnv *env, jclass clazz, jstring outputPath);
22+
Java_at_tomtasche_reader_background_CoreWrapper_createServer(JNIEnv *env, jclass clazz, jstring outputPath);
1923

20-
JNIEXPORT jstring JNICALL
21-
Java_at_tomtasche_reader_background_CoreWrapper_hostFileNative(JNIEnv *env, jclass clazz, jobject options);
24+
JNIEXPORT void JNICALL
25+
Java_at_tomtasche_reader_background_CoreWrapper_hostFile(JNIEnv *env, jclass clazz, jstring prefix, jobject options);
2226

2327
JNIEXPORT void JNICALL
24-
Java_at_tomtasche_reader_background_CoreWrapper_listenServerNative(JNIEnv *env, jclass clazz);
28+
Java_at_tomtasche_reader_background_CoreWrapper_listenServer(JNIEnv *env, jclass clazz, jint port);
2529

2630
JNIEXPORT void JNICALL
27-
Java_at_tomtasche_reader_background_CoreWrapper_stopServerNative(JNIEnv *env, jclass clazz);
31+
Java_at_tomtasche_reader_background_CoreWrapper_stopServer(JNIEnv *env, jclass clazz);
2832
}
2933

3034
#endif //ANDROID_CORE_WRAPPER_H

app/src/main/java/at/tomtasche/reader/background/CoreHttpLoader.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@ public CoreHttpLoader(Context context, ConfigManager configManager) {
2020

2121
this.configManager = configManager;
2222

23+
File assetsDirectory = new File(context.getFilesDir(), "assets");
24+
2325
AssetExtractor ae = new AssetExtractor(context.getAssets());
2426
ae.setNoOverwrite();
25-
ae.extract(new File(context.getFilesDir(), "assets"), ".");
27+
ae.extract(assetsDirectory, "");
28+
29+
CoreWrapper.GlobalParams globalParams = new CoreWrapper.GlobalParams();
30+
globalParams.coreDataPath = new File(assetsDirectory, "odrcore").getPath();
31+
globalParams.fontconfigDataPath = new File(assetsDirectory, "fontconfig").getPath();
32+
globalParams.popplerDataPath = new File(assetsDirectory, "poppler").getPath();
33+
globalParams.pdf2htmlexDataPath = new File(assetsDirectory, "pdf2htmlex").getPath();
2634
}
2735

2836
@Override
@@ -78,10 +86,11 @@ private void translate(Options options, Result result) throws Exception {
7886
coreOptions.paging = true;
7987
}
8088

81-
String id = CoreWrapper.hostFile(coreOptions);
89+
String prefix = "hi";
90+
CoreWrapper.hostFile(prefix, coreOptions);
8291

8392
result.partTitles.add("document");
84-
result.partUris.add(Uri.parse("http://localhost:29665/" + id + "/document.html"));
93+
result.partUris.add(Uri.parse("http://localhost:29665/" + prefix + "/document.html"));
8594
}
8695

8796
@Override

app/src/main/java/at/tomtasche/reader/background/CoreLoader.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ public CoreLoader(Context context, ConfigManager configManager, boolean doOOXML)
2525
this.configManager = configManager;
2626
this.doOoxml = doOOXML;
2727

28+
File assetsDirectory = new File(context.getFilesDir(), "assets");
29+
2830
AssetExtractor ae = new AssetExtractor(context.getAssets());
2931
ae.setNoOverwrite();
30-
ae.extract(new File(context.getFilesDir(), "assets"), ".");
32+
ae.extract(assetsDirectory, "");
33+
34+
CoreWrapper.GlobalParams globalParams = new CoreWrapper.GlobalParams();
35+
globalParams.coreDataPath = new File(assetsDirectory, "odrcore").getPath();
36+
globalParams.fontconfigDataPath = new File(assetsDirectory, "fontconfig").getPath();
37+
globalParams.popplerDataPath = new File(assetsDirectory, "poppler").getPath();
38+
globalParams.pdf2htmlexDataPath = new File(assetsDirectory, "pdf2htmlex").getPath();
3139
}
3240

3341
@Override

app/src/main/java/at/tomtasche/reader/background/CoreWrapper.java

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,32 @@ public class CoreWrapper {
99
System.loadLibrary("odr-core");
1010
}
1111

12+
public static class GlobalParams {
13+
14+
public String coreDataPath;
15+
public String fontconfigDataPath;
16+
public String popplerDataPath;
17+
public String pdf2htmlexDataPath;
18+
}
19+
20+
private static native void setGlobalParams(GlobalParams params);
21+
22+
public static class CoreOptions {
23+
24+
public boolean ooxml;
25+
public boolean txt;
26+
public boolean pdf;
27+
28+
public boolean editable;
29+
30+
public boolean paging;
31+
32+
public String password;
33+
34+
public String inputPath;
35+
public String outputPath;
36+
}
37+
1238
public static CoreResult parse(CoreOptions options) {
1339
CoreResult result = parseNative(options);
1440

@@ -78,45 +104,13 @@ public static void close() {
78104

79105
private static native void closeNative(CoreOptions options);
80106

81-
public static void createServer(String outputPath) {
82-
createServerNative(outputPath);
83-
}
84-
85-
private static native void createServerNative(String outputPath);
107+
public static native void createServer(String outputPath);
86108

87-
public static String hostFile(CoreOptions options) {
88-
return hostFileNative(options);
89-
}
90-
91-
private static native String hostFileNative(CoreOptions options);
92-
93-
public static void listenServer() {
94-
listenServerNative();
95-
}
96-
97-
private static native void listenServerNative();
98-
99-
public static void stopServer() {
100-
stopServerNative();
101-
}
102-
103-
private static native void stopServerNative();
104-
105-
public static class CoreOptions {
106-
107-
public boolean ooxml;
108-
public boolean txt;
109-
public boolean pdf;
109+
public static native void hostFile(String prefix, CoreOptions options);
110110

111-
public boolean editable;
112-
113-
public boolean paging;
111+
public static native void listenServer(int port);
114112

115-
public String password;
116-
117-
public String inputPath;
118-
public String outputPath;
119-
}
113+
public static native void stopServer();
120114

121115
public static class CoreResult {
122116

0 commit comments

Comments
 (0)