Debian Codegraph @

extra/src/uk/me/parabola/util/TableIcuCreator.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2009.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.util;
14
15 import java.io.File;
16 import java.io.FileWriter;
17 import java.io.IOException;
18 import java.io.PrintWriter;
19 import java.util.Enumeration;
20
21 import uk.me.parabola.imgfmt.Utils;
22
23 import com.ibm.icu.text.Transliterator;
24
25 /**
26 * Call this with a unicode row number and it will produce an empty table
27 * that can be modified.
28 *
29 * @author Steve Ratcliffe
30 */
31 public class TableIcuCreator {
32 private static Transliterator trans;
33
34 public static void main(String[] args) {
35 int count = 0;
36 Enumeration<String> targets = Transliterator.getAvailableIDs();
37 while (targets.hasMoreElements()) {
38 String s = targets.nextElement();
39 System.out.println(s);
40 count++;
41 }
42
43 System.out.println("number " + count);
44 //System.exit(0);
45 //trans = Transliterator.getInstance("Any-en_US; nfd; [\u0301\u0302\u0304\u0306\u0307\u0308\u030c\u0328] remove; nfc"); // [:nonspacing mark:] remove; nfc");
46
47 trans = Transliterator.getInstance("Any-Latin"); // [:nonspacing mark:] remove; nfc");
48
49 for (int row = 0; row < 256; row++) {
50 String name = String.format("row%02x.trans", row);
51 PrintWriter out = null;
52 try {
53 out = new PrintWriter(new FileWriter(name));
54 printRow(out, row);
55 } catch (IOException e) {
56 System.out.println("Could not open " + name + " for write");
57 } catch (UselessException e) {
58 //System.out.println("Deleting " + name);
59 File f = new File(name);
60 f.delete();
61 } finally {
62 Utils.closeFile(out);
63 }
64 }
65 }
66
67 private static void printRow(PrintWriter out, int row) throws UselessException {
68 out.println("#");
69 out.println("# This is a table for transliterating characters.");
70 out.println("# It was created using icu4j");
71 out.println("#");
72 out.println("# All resulting strings that contained characters outside the");
73 out.println("# range of iso 8859-1 are commented out");
74 out.println("#");
75 out.println();
76
77 int count = 0;
78 for (int i = 0; i < 256; i++) {
79 char c = (char) ((row << 8) + i);
80 String single = "" + c;
81 String result = trans.transliterate(single);
82
83 if (result.length() == 1 && result.charAt(0) == c)
84 result = "?";
85 else
86 count++;
87
88 boolean inRange = true;
89 for (char rc : result.toCharArray()) {
90 if (rc > 0xff) {
91 //System.out.printf("out of range result %c for row %d\n", rc, row);
92 inRange = false;
93 break;
94 }
95 }
96
97 if (!inRange) {
98 count--;
99 out.print("#");
100 }
101
102 out.format("U+%02x%02x %-12.12s # Character %s", row, i, result, single);
103
104 out.println();
105 }
106
107 if (count == 0)
108 throw new UselessException("count of 0");
109 }
110
111 private static class UselessException extends Exception {
112 private UselessException(String msg) {
113 super(msg);
114 }
115 }
116 }