2020package com .viliussutkus89 .android .pdf2htmlex ;
2121
2222import android .content .Context ;
23- import android .content .res .AssetManager ;
2423import android .os .Build ;
25- import android .util .Log ;
2624
2725import androidx .annotation .NonNull ;
2826
29- import java .io .BufferedInputStream ;
30- import java .io .BufferedOutputStream ;
3127import java .io .File ;
32- import java .io .FileOutputStream ;
3328import java .io .IOException ;
34- import java .io .InputStream ;
35- import java .io .OutputStream ;
3629
3730public final class pdf2htmlEX {
3831 static {
@@ -41,59 +34,6 @@ public final class pdf2htmlEX {
4134
4235 private static final String TAG = "pdf2htmlEX" ;
4336
44- // ExtractAssets adapted from
45- // https://gist.github.com/tylerchesley/6198074
46- private Boolean ExtractAssets (@ NonNull AssetManager assetManager , @ NonNull File outputDir , @ NonNull String name ) {
47- File output_name = new File (outputDir , name );
48- try {
49- String [] assets = assetManager .list (name );
50- if (0 == assets .length ) { // Processing a file
51- InputStream in = assetManager .open (name );
52- OutputStream out = new FileOutputStream (output_name );
53- if (!copyFile (in , out )) {
54- return false ;
55- }
56- } else { // Processing a folder
57- if (!output_name .exists () && !output_name .mkdirs ()) {
58- return false ;
59- }
60- for (String asset : assets ) {
61- if (!ExtractAssets (assetManager , outputDir , name + File .separator + asset )) {
62- return false ;
63- }
64- }
65- }
66- } catch (IOException e ) {
67- Log .e (TAG , e .getMessage ());
68- return false ;
69- }
70- return true ;
71- }
72-
73- private Boolean copyFile (InputStream input , OutputStream output ) {
74- final int buffer_size = 1024 ;
75- byte [] buffer = new byte [buffer_size ];
76-
77- BufferedInputStream in = new BufferedInputStream (input , buffer_size );
78- BufferedOutputStream out = new BufferedOutputStream (output , buffer_size );
79-
80- try {
81- int read ;
82- while (-1 != (read = in .read (buffer ))) {
83- out .write (buffer , 0 , read );
84- }
85- out .flush ();
86- out .close ();
87- output .close ();
88- in .close ();
89- input .close ();
90- } catch (IOException e ) {
91- Log .e (TAG , e .getMessage ());
92- return false ;
93- }
94- return true ;
95- }
96-
9737 private void prepareEnvironmentForFontforge (Context ctx ) {
9838 File homeDir = new File (ctx .getCacheDir (), "homeForFontforge" );
9939 homeDir .mkdir ();
@@ -117,12 +57,12 @@ public pdf2htmlEX(@NonNull Context ctx) {
11757 // @TODO: https://github.com/ViliusSutkus89/pdf2htmlEX-Android/issues/9
11858 // pdf2htmlEX_dataDir is where pdf2htmlEX's share folder contents are
11959 m_pdf2htmlEX_dataDir = new File (filesDir , "pdf2htmlEX" );
120- ExtractAssets (ctx .getAssets (), filesDir , "pdf2htmlEX" );
60+ AssetExtractor . extract (ctx .getAssets (), filesDir , "pdf2htmlEX" );
12161
12262 // @TODO: https://github.com/ViliusSutkus89/pdf2htmlEX-Android/issues/10
12363 // Poppler requires encoding data
12464 m_poppler_dataDir = new File (filesDir , "poppler" );
125- ExtractAssets (ctx .getAssets (), filesDir , "poppler" );
65+ AssetExtractor . extract (ctx .getAssets (), filesDir , "poppler" );
12666
12767 // tmpDir is where pdf2htmlEX does it's work
12868 m_pdf2htmlEX_tmpDir = new File (cacheDir , "pdf2htmlEX-tmp" );
@@ -133,15 +73,6 @@ public pdf2htmlEX(@NonNull Context ctx) {
13373
13474 prepareEnvironmentForFontforge (ctx );
13575
136- // xdg-cache Used by fontconfig
137- File xdgCache = new File (cacheDir , "xdg-cache" );
138- xdgCache .mkdir ();
139- set_environment_value ("XDG_CACHE_HOME" , xdgCache .getAbsolutePath ());
140-
141- // /etc/fonts is provided by fontconfig
142- File fontsConfigDir = new File (filesDir , "etc/fonts" );
143- ExtractAssets (ctx .getAssets (), filesDir , "etc/fonts" );
144- set_environment_value ("FONTCONFIG_PATH" , fontsConfigDir .getAbsolutePath ());
14576 }
14677
14778 public class ConversionFailedException extends Exception {
@@ -176,7 +107,7 @@ public File convert(@NonNull File inputPDF) throws IOException, ConversionFailed
176107 private native int call_pdf2htmlEX (String dataDir , String popplerDir , String tmpDir , String inputFile , String outputFile );
177108
178109 // Because Java cannot setenv for the current process
179- private native void set_environment_value (String key , String value );
110+ static native void set_environment_value (String key , String value );
180111
181112 // Because Java can't set env vars for the current process...
182113 private native void set_env_values_for_fontforge (String homeDir , String tmpDir , String username );
0 commit comments