Skip to content

Commit

Permalink
Update DocLoader. Call wvWare/pdf2htmlEX/ODR in CoreWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ViliusSutkus89 committed Aug 25, 2024
1 parent 9ed1196 commit 067abac
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 30 deletions.
1 change: 1 addition & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ endfunction()

copy_assets("PDF2HTMLEX_RES_DIR" "pdf2htmlEX")
copy_assets("POPPLER_DATA_RES_DIR" "poppler-data")
copy_assets("WVWARE_RES_DIR" "wv")
copy_assets("FONTCONFIG_RES_DIR" "fontconfig")
8 changes: 6 additions & 2 deletions app/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

class OdrDroidConan(ConanFile):
settings = "os", "compiler", "build_type", "arch"
requires = "odrcore/4.1.0-pdf2htmlex-git"

def requirements(self):
self.requires("odrcore/4.1.0-pdf2htmlex-git", options={
"with_pdf2htmlEX": True,
"with_wvWare": True,
})

def generate(self):
deps = CMakeDeps(self)
Expand All @@ -18,4 +23,3 @@ def generate(self):
tc.variables["PDF2HTMLEX_RES_DIR"] = self.dependencies['pdf2htmlex'].cpp_info.resdirs[0]
tc.variables["FONTCONFIG_RES_DIR"] = self.dependencies['fontconfig'].cpp_info.resdirs[0]
tc.generate()

28 changes: 22 additions & 6 deletions app/src/main/cpp/CoreWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject
jfieldID txtField = env->GetFieldID(optionsClass, "txt", "Z");
jboolean txt = env->GetBooleanField(options, txtField);

jfieldID pdf2htmlEXField = env->GetFieldID(optionsClass, "pdf2htmlEX", "Z");
jboolean pdf2htmlEX = env->GetBooleanField(options, pdf2htmlEXField);

jfieldID wvWareField = env->GetFieldID(optionsClass, "wvWare", "Z");
jboolean wvWare = env->GetBooleanField(options, wvWareField);

jfieldID pagingField = env->GetFieldID(optionsClass, "paging", "Z");
jboolean paging = env->GetBooleanField(options, pagingField);

Expand Down Expand Up @@ -119,12 +125,22 @@ Java_at_tomtasche_reader_background_CoreWrapper_parseNative(JNIEnv *env, jobject
config.text_document_margin = true;
}

html = odr::OpenDocumentReader::html(inputPathCpp, [&passwordCpp]() -> std::string {
if (passwordCpp.has_value()) {
return passwordCpp.value();
}
return "";
}, outputPathCpp, config);
if (pdf2htmlEX) {
// @TODO: pass password
html = odr::OpenDocumentReader::pdf2htmlEX(inputPathCpp, outputPathCpp, config);
}
else if (wvWare) {
// @TODO: pass password
html = odr::OpenDocumentReader::wvHtml(inputPathCpp, outputPathCpp, config);
}
else {
html = odr::OpenDocumentReader::html(inputPathCpp, [&passwordCpp]() -> std::string {
if (passwordCpp.has_value()) {
return passwordCpp.value();
}
return "";
}, outputPathCpp, config);
}

{
const auto extensionCpp = odr::OpenDocumentReader::type_to_string(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public static class CoreOptions {

public boolean ooxml;
public boolean txt;
public boolean pdf2htmlEX;
public boolean wvWare;

public boolean editable;

Expand Down
97 changes: 75 additions & 22 deletions app/src/main/java/at/tomtasche/reader/background/DocLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

import android.content.Context;
import android.net.Uri;
import android.webkit.MimeTypeMap;

import com.viliussutkus89.android.wvware.wvWare;
import com.viliussutkus89.android.assetextractor.AssetExtractor;

import java.io.File;

import app.opendocument.android.pdf2htmlex.EnvVar;

public class DocLoader extends FileLoader {

private CoreWrapper lastCore;
private CoreWrapper.CoreOptions lastCoreOptions;

public DocLoader(Context context) {
super(context, LoaderType.DOC);
}
Expand All @@ -18,43 +24,90 @@ public boolean isSupported(Options options) {
return options.fileType.startsWith("application/msword");
}

@Override
public void loadSync(Options options) {
AssetExtractor ae = new AssetExtractor(context.getAssets()).setNoOverwrite();

// @TODO: use asset files without extracting
File wv_data_dir = ae.extract(context.getFilesDir(), "wv/share/wv");
EnvVar.set("WVDATADIR", wv_data_dir.getAbsolutePath());

final Result result = new Result();
result.options = options;
result.loaderType = type;

try {
File cacheFile = AndroidFileCache.getCacheFile(context, options.cacheUri);
File cacheDirectory = AndroidFileCache.getCacheDirectory(cacheFile);

wvWare docConverter = new wvWare(context).setInputDOC(cacheFile);
if (options.password != null) {
docConverter.setPassword(options.password);
translate(options, result);
callOnSuccess(result);
} catch (Throwable e) {
if (e instanceof CoreWrapper.CoreEncryptedException) {
e = new EncryptedDocumentException();
}

File output = docConverter.convertToHTML();
callOnError(result, e);
}
}

File htmlFile = new File(cacheDirectory, "doc.html");
StreamUtil.copy(output, htmlFile);
private void translate(Options options, Result result) throws Exception {
File cachedFile = AndroidFileCache.getCacheFile(context, options.cacheUri);

// library does not delete output files automatically
output.delete();
if (lastCore != null) {
lastCore.close();
lastCore = null;
}

Uri finalUri = Uri.fromFile(htmlFile);
CoreWrapper core = new CoreWrapper();
try {
core.initialize();

options.fileType = "application/msword";
lastCore = core;
} catch (Throwable e) {
crashManager.log(e);
}

result.partTitles.add(null);
result.partUris.add(finalUri);
File cacheDirectory = AndroidFileCache.getCacheDirectory(cachedFile);

callOnSuccess(result);
} catch (Throwable e) {
if (e instanceof wvWare.PasswordRequiredException || e instanceof wvWare.WrongPasswordException) {
e = new EncryptedDocumentException();
CoreWrapper.CoreOptions coreOptions = new CoreWrapper.CoreOptions();
coreOptions.inputPath = cachedFile.getPath();
coreOptions.outputPath = cacheDirectory.getPath();
coreOptions.password = options.password;
coreOptions.editable = options.translatable;
coreOptions.ooxml = false;
coreOptions.wvWare = true;

lastCoreOptions = coreOptions;

CoreWrapper.CoreResult coreResult = lastCore.parse(coreOptions);

String coreExtension = coreResult.extension;
// "unnamed" refers to default of Meta::typeToString
if (coreExtension != null && !coreExtension.equals("unnamed")) {
options.fileExtension = coreExtension;

String fileType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(coreExtension);
if (fileType != null) {
options.fileType = fileType;
}
}

callOnError(result, e);
if (coreResult.exception != null) {
throw coreResult.exception;
}

for (int i = 0; i < coreResult.pagePaths.size(); i++) {
File entryFile = new File(coreResult.pagePaths.get(i));

result.partTitles.add(coreResult.pageNames.get(i));
result.partUris.add(Uri.fromFile(entryFile));
}
}

@Override
public void close() {
super.close();

if (lastCore != null) {
lastCore.close();
lastCore = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ private void translate(Options options, Result result) throws Exception {
coreOptions.password = options.password;
coreOptions.editable = options.translatable;
coreOptions.ooxml = false;
coreOptions.pdf2htmlEX = true;

Boolean usePaging = configManager.getBooleanConfig("use_paging");
if (usePaging == null || usePaging) {
Expand Down

0 comments on commit 067abac

Please sign in to comment.