|
1 | 1 | /* |
2 | 2 | * pdf2htmlEX.java |
3 | 3 | * |
4 | | - * Copyright (C) 2019, 2020, 2022, 2023 ViliusSutkus89.com |
| 4 | + * Copyright (C) 2019, 2020, 2022, 2023, 2024 ViliusSutkus89.com |
5 | 5 | * |
6 | 6 | * pdf2htmlEX-Android is free software: you can redistribute it and/or modify |
7 | 7 | * it under the terms of the GNU General Public License as published by |
|
28 | 28 | import com.viliussutkus89.android.assetextractor.AssetExtractor; |
29 | 29 | import com.viliussutkus89.android.tmpfile.Tmpfile; |
30 | 30 |
|
| 31 | +import java.io.BufferedInputStream; |
31 | 32 | import java.io.Closeable; |
32 | 33 | import java.io.File; |
33 | 34 | import java.io.FileInputStream; |
34 | 35 | import java.io.FileNotFoundException; |
35 | | -import java.io.FileOutputStream; |
36 | 36 | import java.io.IOException; |
| 37 | +import java.io.RandomAccessFile; |
37 | 38 | import java.nio.charset.StandardCharsets; |
| 39 | +import java.util.LinkedList; |
| 40 | +import java.util.List; |
38 | 41 |
|
39 | 42 |
|
40 | 43 | @SuppressWarnings("unused") |
@@ -181,24 +184,37 @@ public File convert() throws IOException, ConversionFailedException { |
181 | 184 |
|
182 | 185 | int retVal = NativeConverter.convert(nc.mConverter); |
183 | 186 | if (0 == retVal) { |
184 | | - // Workaround for https://github.com/opendocument-app/pdf2htmlEX-Android/issues/94 |
185 | | - String convertedDocumentText; |
186 | | - try (FileInputStream fis = new FileInputStream(outputFile)) { |
187 | | - byte[] data = new byte[(int) outputFile.length()]; |
188 | | - fis.read(data); |
189 | | - convertedDocumentText = new String(data, StandardCharsets.UTF_8); |
| 187 | + // Workaround for https://github.com/opendocument-app/pdf2htmlEX-Android/issues/94 |
| 188 | + |
| 189 | + byte[] needle = "{font-family:sans-serif;visibility:hidden;}".getBytes(StandardCharsets.UTF_8); |
| 190 | + byte[] replacement = "{font-family:sans-serif;visibility:visible}".getBytes(StandardCharsets.UTF_8); |
| 191 | + List<Long> needlePositions = new LinkedList<>(); |
| 192 | + try (FileInputStream fis = new FileInputStream(outputFile)) { |
| 193 | + try (BufferedInputStream bis = new BufferedInputStream(fis)) { |
| 194 | + long totalRead = 0; |
| 195 | + int matchedNeedle = 0; |
| 196 | + byte[] buffer = new byte[1]; |
| 197 | + while (1 == bis.read(buffer)) { |
| 198 | + if (buffer[0] == needle[matchedNeedle]) { |
| 199 | + if (++matchedNeedle == needle.length) { |
| 200 | + needlePositions.add(totalRead - needle.length + 1); |
| 201 | + matchedNeedle = 0; |
| 202 | + } |
| 203 | + } else { |
| 204 | + matchedNeedle = 0; |
| 205 | + } |
| 206 | + totalRead++; |
| 207 | + } |
190 | 208 | } |
191 | | - if (!convertedDocumentText.isEmpty()) { |
192 | | - convertedDocumentText = convertedDocumentText.replace( |
193 | | - "{font-family:sans-serif;visibility:hidden;}", |
194 | | - "{font-family:sans-serif;visibility:visible;}" |
195 | | - ); |
196 | | - File defaultFontVisibleFile = new File(outputFile + ".default-font-visible.html"); |
197 | | - try (FileOutputStream fos = new FileOutputStream(defaultFontVisibleFile)) { |
198 | | - fos.write(convertedDocumentText.getBytes(StandardCharsets.UTF_8)); |
199 | | - defaultFontVisibleFile.renameTo(outputFile); |
| 209 | + } |
| 210 | + if (!needlePositions.isEmpty()) { |
| 211 | + try (RandomAccessFile raf = new RandomAccessFile(outputFile, "rw")) { |
| 212 | + for (long i : needlePositions) { |
| 213 | + raf.seek(i); |
| 214 | + raf.write(replacement, 0, replacement.length); |
200 | 215 | } |
201 | 216 | } |
| 217 | + } |
202 | 218 | return outputFile; |
203 | 219 | } |
204 | 220 |
|
|
0 commit comments