Skip to content

Commit b6ad46e

Browse files
Hold upstream converter C++ object as a member in java class. Run parameter setters on it
1 parent 9b0db56 commit b6ad46e

11 files changed

Lines changed: 335 additions & 207 deletions

File tree

dependency-builder/src/main/cpp/packages/pdf2htmlEX-Patch-Source-make-a-library.patch

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,7 @@ new file mode 100644
761761
new file mode 100644
762762
--- /dev/null
763763
+++ src/pdf2htmlEX.h
764-
@@ -0,0 +1,197 @@
764+
@@ -0,0 +1,198 @@
765765
+/*
766766
+ * pdf2htmlEX.h
767767
+ *
@@ -784,6 +784,7 @@ new file mode 100644
784784
+
785785
+#pragma once
786786
+
787+
+#include <string>
787788
+#include <exception>
788789
+#include <memory>
789790
+

pdf2htmlEX/src/androidTest/java/com/viliussutkus89/android/pdf2htmlex/ConversionTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,7 @@ public void convertPDF() throws pdf2htmlEX.ConversionFailedException, IOExceptio
8686
assertTrue("Converted HTML file empty! " + pdfFile.getName(), htmlFile.length() > 0);
8787

8888
htmlFile.delete();
89+
90+
converter.close();
8991
}
9092
}

pdf2htmlEX/src/androidTest/java/com/viliussutkus89/android/pdf2htmlex/EncryptedPdfTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public void cleanUp() {
7474
assertTrue("Converted HTML file empty!", convertedHtml.length() > 0);
7575
convertedHtml.delete();
7676
}
77+
converter.close();
7778
}
7879

7980
@Test(expected = pdf2htmlEX.PasswordRequiredException.class)

pdf2htmlEX/src/main/cpp/CCharGC.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* CCharGC.h
3+
*
4+
* Copyright (c) 2019 ViliusSutkus89.com
5+
*
6+
* CCharGC is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License version 3 as
8+
* published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*/
18+
19+
#ifndef PDF2HTMLEX_ANDROID_CCHARGC_H
20+
#define PDF2HTMLEX_ANDROID_CCHARGC_H
21+
22+
#include <jni.h>
23+
24+
class CCharGC {
25+
private:
26+
JNIEnv *env;
27+
jstring input;
28+
const char * cstr;
29+
30+
public:
31+
CCharGC(JNIEnv *env, jstring input) : env(env), input(input) {
32+
this->cstr = env->GetStringUTFChars(input, nullptr);
33+
}
34+
35+
const char * c_str() const {
36+
return this->cstr;
37+
}
38+
39+
bool isEmpty() const { return this->cstr[0] == '\0'; }
40+
41+
~CCharGC() {
42+
env->ReleaseStringUTFChars(this->input, this->cstr);
43+
}
44+
};
45+
46+
#endif //PDF2HTMLEX_ANDROID_CCHARGC_H

pdf2htmlEX/src/main/cpp/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,16 @@ pkg_search_module(pdf2htmlEX REQUIRED pdf2htmlEX)
2929

3030
link_directories(${pdf2htmlEX_LIBRARY_DIRS})
3131

32-
add_library(pdf2htmlEX-android SHARED pdf2htmlEX.cc)
32+
add_library(envvar SHARED EnvVar.c)
33+
add_library(pdf2htmlEX-android SHARED pdf2htmlEX.cc CCharGC.h)
3334

3435
find_library(log-lib log)
3536

3637
find_package(tmpfile REQUIRED CONFIG)
3738

3839
target_link_libraries(pdf2htmlEX-android
3940
${pdf2htmlEX_STATIC_LIBRARIES}
41+
envvar
4042
tmpfile::tmpfile
4143
${log-lib}
4244
)

pdf2htmlEX/src/main/cpp/EnvVar.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <stdlib.h>
2+
#include <jni.h>
3+
4+
JNIEXPORT void JNICALL
5+
Java_com_viliussutkus89_android_pdf2htmlex_EnvVar_set(JNIEnv *env, __attribute__((unused)) jclass clazz,
6+
jstring key, jstring value) {
7+
const char *key_c = (*env)->GetStringUTFChars(env, key, JNI_FALSE);
8+
const char *value_c = (*env)->GetStringUTFChars(env, value, JNI_FALSE);
9+
setenv(key_c, value_c, 1);
10+
(*env)->ReleaseStringUTFChars(env, value, value_c);
11+
(*env)->ReleaseStringUTFChars(env, key, key_c);
12+
}

pdf2htmlEX/src/main/cpp/pdf2htmlEX.cc

Lines changed: 109 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* pdf2htmlEX-Android (https://github.com/ViliusSutkus89/pdf2htmlEX-Android)
55
* Android port of pdf2htmlEX - Convert PDF to HTML without losing text or format.
66
*
7-
* Copyright (c) 2019 Vilius Sutkus <ViliusSutkus89@gmail.com>
7+
* Copyright (c) 2019, 2022 ViliusSutkus89.com
88
*
9-
* This program is free software: you can redistribute it and/or modify
9+
* pdf2htmlEX-Android is free software: you can redistribute it and/or modify
1010
* it under the terms of the GNU General Public License version 3 as
1111
* published by the Free Software Foundation.
1212
*
@@ -19,107 +19,131 @@
1919
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2020
*/
2121

22-
#include <cstdlib>
23-
#include <string>
2422
#include <jni.h>
2523
#include <android/log.h>
24+
#include "CCharGC.h"
2625
#include "pdf2htmlEX.h"
2726

2827
#define retValOK 0
2928
#define retValError 1
3029
#define retValEncryptionError 2
3130
#define retValCopyProtected 3
3231

33-
class CCharGC {
34-
private:
35-
JNIEnv *env;
36-
jstring input;
37-
const char * cstr;
32+
extern "C"
33+
JNIEXPORT jlong JNICALL
34+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_createNewConverterObject(JNIEnv *env, jclass,
35+
jstring tmp_dir,
36+
jstring data_dir,
37+
jstring poppler_dir) {
38+
auto * pdf2htmlEX = new pdf2htmlEX::pdf2htmlEX();
3839

39-
public:
40-
CCharGC(JNIEnv *env, jstring input) : env(env), input(input) {
41-
this->cstr = env->GetStringUTFChars(input, nullptr);
42-
}
40+
pdf2htmlEX->setTMPDir(CCharGC(env, tmp_dir).c_str());
41+
pdf2htmlEX->setDataDir(CCharGC(env, data_dir).c_str());
42+
pdf2htmlEX->setPopplerDataDir(CCharGC(env, poppler_dir).c_str());
4343

44-
const char * c_str() const {
45-
return this->cstr;
46-
}
44+
return (jlong) pdf2htmlEX;
45+
}
4746

48-
bool isEmpty() const { return this->cstr[0] == '\0'; }
47+
extern "C"
48+
JNIEXPORT void JNICALL
49+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_dealloc(JNIEnv *, jclass, jlong converter) {
50+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
51+
delete pdf2htmlEX;
52+
}
4953

50-
~CCharGC() {
51-
env->ReleaseStringUTFChars(this->input, this->cstr);
54+
extern "C"
55+
JNIEXPORT jint JNICALL
56+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_convert(JNIEnv *, jclass, jlong converter) {
57+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
58+
try {
59+
pdf2htmlEX->convert();
60+
} catch (const pdf2htmlEX::EncryptionPasswordException & e) {
61+
return retValEncryptionError;
62+
} catch (const pdf2htmlEX::DocumentCopyProtectedException & e) {
63+
return retValCopyProtected;
64+
} catch (const pdf2htmlEX::ConversionFailedException & e) {
65+
__android_log_print(ANDROID_LOG_ERROR, "pdf2htmlEX-Android" , "%s", e.what());
66+
return retValError;
5267
}
53-
};
68+
return retValOK;
69+
}
5470

5571
extern "C"
5672
JNIEXPORT void JNICALL
57-
Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_set_1environment_1value(JNIEnv *env, jclass,
58-
jstring key_,
59-
jstring value_) {
60-
CCharGC key(env, key_);
61-
CCharGC value(env, value_);
62-
setenv(key.c_str(), value.c_str(), 1);
73+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setInputFile(JNIEnv *env, jclass, jlong converter,
74+
jstring input_file) {
75+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
76+
pdf2htmlEX->setInputFilename(CCharGC(env, input_file).c_str());
6377
}
6478

6579
extern "C"
66-
JNIEXPORT jint JNICALL
67-
Java_com_viliussutkus89_android_pdf2htmlex_pdf2htmlEX_call_1pdf2htmlEX(JNIEnv *env, jobject,
68-
jstring dataDir_,
69-
jstring popplerDir_, jstring tmpDir_,
70-
jstring inputFile_,
71-
jstring outputFile_,
72-
jstring ownerPassword_,
73-
jstring userPassword_,
74-
jboolean enableOutline,
75-
jboolean enableDrm,
76-
jstring backgroundFormat_,
77-
jboolean enableEmbedFont,
78-
jboolean enableEmbedExternalFont,
79-
jboolean processAnnotation
80-
) {
81-
CCharGC dataDir(env, dataDir_);
82-
CCharGC popplerDir(env, popplerDir_);
83-
CCharGC tmpDir(env, tmpDir_);
84-
CCharGC inputFile(env, inputFile_);
85-
CCharGC outputFile(env, outputFile_);
86-
CCharGC ownerPassword(env, ownerPassword_);
87-
CCharGC userPassword(env, userPassword_);
88-
CCharGC backgroundFormat(env, backgroundFormat_);
89-
90-
pdf2htmlEX::pdf2htmlEX converter;
91-
converter.setProcessOutline(enableOutline == JNI_TRUE);
92-
converter.setDRM(enableDrm == JNI_TRUE);
93-
converter.setDataDir(dataDir.c_str());
94-
converter.setPopplerDataDir(popplerDir.c_str());
95-
converter.setTMPDir(tmpDir.c_str());
96-
converter.setInputFilename(inputFile.c_str());
97-
converter.setOutputFilename(outputFile.c_str());
98-
converter.setEmbedFont(enableEmbedFont == JNI_TRUE);
99-
converter.setEmbedExternalFont(enableEmbedExternalFont == JNI_TRUE);
100-
converter.setProcessAnnotation(processAnnotation == JNI_TRUE);
101-
102-
if (!backgroundFormat.isEmpty()) {
103-
converter.setBackgroundImageFormat(backgroundFormat.c_str());
104-
}
105-
106-
if (!ownerPassword.isEmpty()) {
107-
converter.setOwnerPassword(ownerPassword.c_str());
108-
}
109-
110-
if (!userPassword.isEmpty()) {
111-
converter.setUserPassword(userPassword.c_str());
112-
}
113-
114-
try {
115-
converter.convert();
116-
} catch (const pdf2htmlEX::EncryptionPasswordException & e) {
117-
return retValEncryptionError;
118-
} catch (const pdf2htmlEX::DocumentCopyProtectedException & e) {
119-
return retValCopyProtected;
120-
} catch (const pdf2htmlEX::ConversionFailedException & e) {
121-
__android_log_print(ANDROID_LOG_ERROR, "pdf2htmlEX-Android" , "%s", e.what());
122-
return retValError;
123-
}
124-
return retValOK;
80+
JNIEXPORT void JNICALL
81+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setOutputFile(JNIEnv *env, jclass, jlong converter,
82+
jstring output_file) {
83+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
84+
pdf2htmlEX->setOutputFilename(CCharGC(env, output_file).c_str());
85+
}
86+
87+
extern "C"
88+
JNIEXPORT void JNICALL
89+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setOwnerPassword(JNIEnv *env, jclass, jlong converter,
90+
jstring owner_password) {
91+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
92+
pdf2htmlEX->setOwnerPassword(CCharGC(env, owner_password).c_str());
93+
}
94+
95+
extern "C"
96+
JNIEXPORT void JNICALL
97+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setUserPassword(JNIEnv *env, jclass, jlong converter,
98+
jstring user_password) {
99+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
100+
pdf2htmlEX->setUserPassword(CCharGC(env, user_password).c_str());
101+
}
102+
103+
extern "C"
104+
JNIEXPORT void JNICALL
105+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setOutline(JNIEnv *env, jclass, jlong converter,
106+
jobject enable) {
107+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
108+
pdf2htmlEX->setProcessOutline(enable);
109+
}
110+
111+
extern "C"
112+
JNIEXPORT void JNICALL
113+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setDrm(JNIEnv *env, jclass, jlong converter,
114+
jobject enable) {
115+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
116+
pdf2htmlEX->setDRM(enable);
117+
}
118+
119+
extern "C"
120+
JNIEXPORT void JNICALL
121+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setEmbedFont(JNIEnv *, jclass, jlong converter,
122+
jobject embed) {
123+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
124+
pdf2htmlEX->setEmbedFont(embed);
125+
}
126+
127+
extern "C"
128+
JNIEXPORT void JNICALL
129+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setEmbedExternalFont(JNIEnv *, jclass, jlong converter,
130+
jobject embed) {
131+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
132+
pdf2htmlEX->setEmbedExternalFont(embed);
133+
}
134+
135+
extern "C"
136+
JNIEXPORT void JNICALL
137+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setProcessAnnotation(JNIEnv *, jclass, jlong converter,
138+
jobject process) {
139+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
140+
pdf2htmlEX->setProcessAnnotation(process);
141+
}
142+
143+
extern "C"
144+
JNIEXPORT void JNICALL
145+
Java_com_viliussutkus89_android_pdf2htmlex_NativeConverter_setBackgroundFormat(JNIEnv *env, jclass, jlong converter,
146+
jstring background_format) {
147+
auto * pdf2htmlEX = (pdf2htmlEX::pdf2htmlEX *) converter;
148+
pdf2htmlEX->setBackgroundImageFormat(CCharGC(env, background_format).c_str());
125149
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.viliussutkus89.android.pdf2htmlex;
2+
3+
public class EnvVar {
4+
static {
5+
System.loadLibrary("envvar");
6+
}
7+
public static native void set(String key, String value);
8+
}

pdf2htmlEX/src/main/java/com/viliussutkus89/android/pdf2htmlex/FontconfigAndroid.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
import com.viliussutkus89.android.fontconfigtranslator.FontconfigTranslator;
99

1010
import java.io.File;
11-
import java.util.Map;
11+
1212

1313
final class FontconfigAndroid {
14-
static void init(@NonNull AssetManager assetManager, @NonNull File cacheDir, @NonNull File filesDir, @NonNull Map<String, String> environment) {
14+
static void init(@NonNull AssetManager assetManager, @NonNull File cacheDir, @NonNull File filesDir) {
1515
File xdgCache = new File(cacheDir, "xdg-cache");
1616
xdgCache.mkdir();
17-
environment.put("XDG_CACHE_HOME", xdgCache.getAbsolutePath());
17+
EnvVar.set("XDG_CACHE_HOME", xdgCache.getAbsolutePath());
1818

1919
AssetExtractor ae = new AssetExtractor(assetManager).setNoOverwrite();
2020
File fontsConfigDir = ae.extract(new File(filesDir, "etc"), "pdf2htmlEX/etc/fonts");
21-
environment.put("FONTCONFIG_PATH", fontsConfigDir.getAbsolutePath());
21+
EnvVar.set("FONTCONFIG_PATH", fontsConfigDir.getAbsolutePath());
2222

2323
ae.extract(new File(filesDir, "share"), "pdf2htmlEX/share/fonts");
2424

0 commit comments

Comments
 (0)