| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
package uk.me.parabola.imgfmt.app.labelenc; |
| 15 |
|
| 16 |
import java.io.BufferedReader; |
| 17 |
import java.io.IOException; |
| 18 |
import java.io.InputStream; |
| 19 |
import java.io.InputStreamReader; |
| 20 |
import java.nio.charset.StandardCharsets; |
| 21 |
import java.util.Arrays; |
| 22 |
import java.util.Locale; |
| 23 |
|
| 24 |
import uk.me.parabola.log.Logger; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class TableTransliterator implements Transliterator { |
| 36 |
private static final Logger log = Logger.getLogger(TableTransliterator.class); |
| 37 |
|
| 38 |
private final String[][] rows = new String[256][]; |
| 39 |
private final boolean useLatin; |
| 40 |
private boolean forceUppercase; |
| 41 |
|
| 42 |
public TableTransliterator(String targetCharset) { |
| 43 |
if ("latin1".equals(targetCharset) || "cp1252".equals(targetCharset)) |
| 44 |
useLatin = true; |
| 45 |
else |
| 46 |
useLatin = false; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public String transliterate(String s) { |
| 58 |
if (s == null) |
| 59 |
return null; |
| 60 |
|
| 61 |
StringBuilder sb = new StringBuilder(s.length() + 5); |
| 62 |
for (char c : s.toCharArray()) { |
| 63 |
if (c <= (useLatin? 0xff: 0x7f)) { |
| 64 |
sb.append(c); |
| 65 |
} else { |
| 66 |
int row = c >>> 8; |
| 67 |
|
| 68 |
String[] rowmap = rows[row]; |
| 69 |
if (rowmap == null) |
| 70 |
rowmap = loadRow(row); |
| 71 |
sb.append(rowmap[c & 0xff]); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
String text = sb.toString(); |
| 76 |
if (forceUppercase) |
| 77 |
text = text.toUpperCase(Locale.ENGLISH); |
| 78 |
return text; |
| 79 |
} |
| 80 |
|
| 81 |
public void forceUppercase(boolean uc) { |
| 82 |
forceUppercase = uc; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
private String[] loadRow(int row) { |
| 95 |
if (rows[row] != null) |
| 96 |
return rows[row]; |
| 97 |
|
| 98 |
String[] newRow = new String[256]; |
| 99 |
rows[row] = newRow; |
| 100 |
|
| 101 |
|
| 102 |
Arrays.fill(newRow, "?"); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if (useLatin) { |
| 107 |
String name = String.format("/chars/latin1/row%02x.trans", row); |
| 108 |
readCharFile(name, newRow); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
String name = String.format("/chars/ascii/row%02x.trans", row); |
| 113 |
readCharFile(name, newRow); |
| 114 |
|
| 115 |
return newRow; |
| 116 |
} |
| 117 |
|
| 118 |
private void readCharFile(String name, String[] newRow) { |
| 119 |
try (InputStream is = getClass().getResourceAsStream(name)) { |
| 120 |
if (is == null) |
| 121 |
return; |
| 122 |
BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8)); |
| 123 |
|
| 124 |
String line; |
| 125 |
while ((line = br.readLine()) != null) { |
| 126 |
line = line.trim(); |
| 127 |
if (line.isEmpty() || line.charAt(0) == '#') |
| 128 |
continue; |
| 129 |
|
| 130 |
String[] fields = line.split("\\s+"); |
| 131 |
if (fields.length < 2) |
| 132 |
continue; |
| 133 |
|
| 134 |
String upoint = fields[0]; |
| 135 |
String translation = fields[1]; |
| 136 |
|
| 137 |
if ("?".equals(translation)) continue; |
| 138 |
if (upoint.length() != 6 || upoint.charAt(0) != 'U') continue; |
| 139 |
|
| 140 |
|
| 141 |
int index = Integer.parseInt(upoint.substring(4), 16); |
| 142 |
if ("?".equals(newRow[index])) { |
| 143 |
if (forceUppercase) |
| 144 |
newRow[index] = translation.toUpperCase(Locale.ENGLISH); |
| 145 |
else |
| 146 |
newRow[index] = translation; |
| 147 |
} |
| 148 |
} |
| 149 |
} catch (IOException e) { |
| 150 |
log.error("Could not read character translation table"); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
} |