You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnicodeClasses.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.hyphenation;
  19. import java.io.BufferedReader;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.FileOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStreamReader;
  25. import java.io.OutputStreamWriter;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. /**
  29. * Create the default classes file classes.xml,
  30. * for use in building hyphenation patterns
  31. * from pattern files which do not contain their own classes
  32. */
  33. public class UnicodeClasses {
  34. // default path relative to the FOP base directory
  35. static String CLASSES_XML = "src/java/org/apache/fop/hyphenation/classes.xml";
  36. /**
  37. * Generate classes.xml from Java's compiled-in Unicode Character Database
  38. * @param hexcode whether to prefix each class with the hexcode (only for debugging purposes)
  39. * @param outfilePath output file
  40. * @throws IOException
  41. */
  42. public static void fromJava(boolean hexcode, String outfilePath) throws IOException {
  43. File f = new File(outfilePath);
  44. if (f.exists()) {
  45. f.delete();
  46. }
  47. f.createNewFile();
  48. FileOutputStream fw = new FileOutputStream(f);
  49. OutputStreamWriter ow = new OutputStreamWriter(fw, "utf-8");
  50. int maxChar;
  51. maxChar = Character.MAX_VALUE;
  52. ow.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  53. "<classes>\n");
  54. // loop over the first Unicode plane
  55. for (int code = Character.MIN_VALUE; code <= maxChar; ++code) {
  56. // skip surrogate area
  57. if (code == Character.MIN_SURROGATE) {
  58. code = Character.MAX_SURROGATE;
  59. continue;
  60. }
  61. // we are only interested in LC, UC and TC letters which are their own LC, and in 'other letters'
  62. if (!(((Character.isLowerCase(code) || Character.isUpperCase(code) || Character.isTitleCase(code))
  63. && code == Character.toLowerCase(code))
  64. || Character.getType(code) == Character.OTHER_LETTER)) {
  65. continue;
  66. }
  67. // skip a number of blocks
  68. Character.UnicodeBlock ubi = Character.UnicodeBlock.of(code);
  69. if (ubi.equals(Character.UnicodeBlock.SUPERSCRIPTS_AND_SUBSCRIPTS)
  70. || ubi.equals(Character.UnicodeBlock.LETTERLIKE_SYMBOLS)
  71. || ubi.equals(Character.UnicodeBlock.ALPHABETIC_PRESENTATION_FORMS)
  72. || ubi.equals(Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)
  73. || ubi.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
  74. || ubi.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A)
  75. || ubi.equals(Character.UnicodeBlock.HANGUL_SYLLABLES)) {
  76. continue;
  77. }
  78. int uppercode = Character.toUpperCase(code);
  79. int titlecode = Character.toTitleCase(code);
  80. StringBuilder s = new StringBuilder();
  81. if (hexcode) {
  82. s.append("0x" + Integer.toHexString(code) + " ");
  83. }
  84. s.append(Character.toChars(code));
  85. if (uppercode != code) {
  86. s.append(Character.toChars(uppercode));
  87. }
  88. if (titlecode != code && titlecode != uppercode) {
  89. s.append(Character.toChars(titlecode));
  90. }
  91. ow.write(s.toString() + "\n");
  92. }
  93. ow.write("</classes>\n");
  94. ow.flush();
  95. ow.close();
  96. }
  97. static public int UNICODE = 0, GENERAL_CATEGORY = 2, SIMPLE_UPPERCASE_MAPPING = 12,
  98. SIMPLE_LOWERCASE_MAPPING = 13, SIMPLE_TITLECASE_MAPPING = 14, NUM_FIELDS = 15;
  99. /**
  100. * Generate classes.xml from Unicode Character Database files
  101. * @param hexcode whether to prefix each class with the hexcode (only for debugging purposes)
  102. * @param unidataPath path to the directory with UCD files
  103. * @param outfilePath output file
  104. * @throws IOException
  105. */
  106. public static void fromUCD(boolean hexcode, String unidataPath, String outfilePath) throws IOException {
  107. File unidata = new File(unidataPath);
  108. File f = new File(outfilePath);
  109. if (f.exists()) {
  110. f.delete();
  111. }
  112. f.createNewFile();
  113. FileOutputStream fw = new FileOutputStream(f);
  114. OutputStreamWriter ow = new OutputStreamWriter(fw, "utf-8");
  115. File in = new File(unidata, "Blocks.txt");
  116. FileInputStream inis = new FileInputStream(in);
  117. InputStreamReader insr = new InputStreamReader(inis, "utf-8");
  118. BufferedReader inbr = new BufferedReader(insr);
  119. Map blocks = new HashMap();
  120. for (String line = inbr.readLine(); line != null; line = inbr.readLine()) {
  121. if (line.startsWith("#") || line.matches("^\\s*$")) {
  122. continue;
  123. }
  124. String[] parts = line.split(";");
  125. String block = parts[1].trim();
  126. String[] indices = parts[0].split("\\.\\.");
  127. int[] ind = {Integer.parseInt(indices[0], 16), Integer.parseInt(indices[1], 16)};
  128. blocks.put(block, ind);
  129. }
  130. inbr.close();
  131. in = new File(unidata, "UnicodeData.txt");
  132. inis = new FileInputStream(in);
  133. insr = new InputStreamReader(inis, "utf-8");
  134. inbr = new BufferedReader(insr);
  135. int maxChar;
  136. maxChar = Character.MAX_VALUE;
  137. ow.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  138. "<classes>\n");
  139. for (String line = inbr.readLine(); line != null; line = inbr.readLine()) {
  140. String[] fields = line.split(";", NUM_FIELDS);
  141. int code = Integer.parseInt(fields[UNICODE], 16);
  142. if (code > maxChar) {
  143. break;
  144. }
  145. if (((fields[GENERAL_CATEGORY].equals("Ll") || fields[GENERAL_CATEGORY].equals("Lu")
  146. || fields[GENERAL_CATEGORY].equals("Lt"))
  147. && ("".equals(fields[SIMPLE_LOWERCASE_MAPPING])
  148. || fields[UNICODE].equals(fields[SIMPLE_LOWERCASE_MAPPING])))
  149. || fields[GENERAL_CATEGORY].equals("Lo")) {
  150. String[] blockNames = {"Superscripts and Subscripts", "Letterlike Symbols",
  151. "Alphabetic Presentation Forms", "Halfwidth and Fullwidth Forms",
  152. "CJK Unified Ideographs", "CJK Unified Ideographs Extension A",
  153. "Hangul Syllables"};
  154. int j;
  155. for (j = 0; j < blockNames.length; ++j) {
  156. int[] ind = (int[]) blocks.get(blockNames[j]);
  157. if (code >= ind[0] && code <= ind[1]) {
  158. break;
  159. }
  160. }
  161. if (j < blockNames.length) {
  162. continue;
  163. }
  164. int uppercode = -1, titlecode = -1;
  165. if (!"".equals(fields[SIMPLE_UPPERCASE_MAPPING])) {
  166. uppercode = Integer.parseInt(fields[SIMPLE_UPPERCASE_MAPPING], 16);
  167. }
  168. if (!"".equals(fields[SIMPLE_TITLECASE_MAPPING])) {
  169. titlecode = Integer.parseInt(fields[SIMPLE_TITLECASE_MAPPING], 16);
  170. }
  171. StringBuilder s = new StringBuilder();
  172. if (hexcode) {
  173. s.append("0x" + fields[UNICODE].replaceFirst("^0+", "").toLowerCase() + " ");
  174. }
  175. s.append(Character.toChars(code));
  176. if (uppercode != -1 && uppercode != code) {
  177. s.append(Character.toChars(uppercode));
  178. }
  179. if (titlecode != -1 && titlecode != code && titlecode != uppercode) {
  180. s.append(Character.toChars(titlecode));
  181. }
  182. ow.write(s.toString() + "\n");
  183. }
  184. }
  185. ow.write("</classes>\n");
  186. ow.flush();
  187. ow.close();
  188. inbr.close();
  189. }
  190. /**
  191. * Generate classes.xml from XeTeX's Unicode letters file
  192. * @param hexcode whether to prefix each class with the hexcode (only for debugging purposes)
  193. * @param lettersPath path to XeTeX's Unicode letters file unicode-letters-XeTeX.tex
  194. * @param outfilePath output file
  195. * @throws IOException
  196. */
  197. public static void fromTeX(boolean hexcode, String lettersPath, String outfilePath) throws IOException {
  198. File in = new File(lettersPath);
  199. File f = new File(outfilePath);
  200. if (f.exists()) {
  201. f.delete();
  202. }
  203. f.createNewFile();
  204. FileOutputStream fw = new FileOutputStream(f);
  205. OutputStreamWriter ow = new OutputStreamWriter(fw, "utf-8");
  206. FileInputStream inis = new FileInputStream(in);
  207. InputStreamReader insr = new InputStreamReader(inis, "utf-8");
  208. BufferedReader inbr = new BufferedReader(insr);
  209. ow.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  210. "<classes>\n");
  211. for (String line = inbr.readLine(); line != null; line = inbr.readLine()) {
  212. String[] codes = line.split("\\s+");
  213. if (!(codes[0].equals("\\L") || codes[0].equals("\\l"))) {
  214. continue;
  215. }
  216. if (codes.length == 3) {
  217. ow.write("\"" + line + "\" has two codes");
  218. continue;
  219. }
  220. if (codes[0].equals("\\l") && codes.length != 2) {
  221. ow.write("\"" + line + "\" should have one code");
  222. continue;
  223. }
  224. else if (codes[0].equals("\\L") && codes.length != 4) {
  225. ow.write("\"" + line + "\" should have three codes");
  226. continue;
  227. }
  228. if (codes[0].equals("\\l") || (codes[0].equals("\\L") && codes[1].equals(codes[3]))) {
  229. StringBuilder s = new StringBuilder();
  230. if (hexcode) {
  231. s.append("0x" + codes[1].replaceFirst("^0+", "").toLowerCase() + " ");
  232. }
  233. s.append(Character.toChars(Integer.parseInt(codes[1], 16)));
  234. if (codes[0].equals("\\L")) {
  235. s.append(Character.toChars(Integer.parseInt(codes[2], 16)));
  236. }
  237. ow.write(s.toString() + "\n");
  238. }
  239. }
  240. ow.write("</classes>\n");
  241. ow.flush();
  242. ow.close();
  243. inbr.close();
  244. }
  245. public static void main(String[] args) throws IOException {
  246. String type = "ucd", prefix = "--", infile = null, outfile = CLASSES_XML;
  247. boolean hexcode = false;
  248. for (int i = 0; i < args.length; ++i) {
  249. if (args[i].startsWith(prefix)) {
  250. String option = args[i].substring(prefix.length());
  251. if (option.equals("java") || option.equals("ucd") || option.equals("tex")) {
  252. type = option;
  253. } else if (option.equals("hexcode")) {
  254. hexcode = true;
  255. } else if (option.equals("out")) {
  256. outfile = args[++i];
  257. } else {
  258. System.err.println("Unknown option: " + option);
  259. System.exit(1);
  260. }
  261. } else {
  262. if (infile != null) {
  263. System.err.println("Only one non-option argument can be given, for infile");
  264. System.exit(1);
  265. }
  266. infile = args[i];
  267. }
  268. }
  269. if (type.equals("java")) {
  270. if (infile != null) {
  271. System.err.println("Type java does not allow an infile");
  272. System.exit(1);
  273. }
  274. } else {
  275. if (infile == null) {
  276. System.err.println("Types ucd and tex require an infile");
  277. System.exit(1);
  278. }
  279. }
  280. if (type.equals("java")) {
  281. fromJava(hexcode, outfile);
  282. } else if (type.equals("ucd")) {
  283. fromUCD(hexcode, infile, outfile);
  284. } else if (type.equals("tex")) {
  285. fromTeX(hexcode, infile, outfile);
  286. } else {
  287. System.err.println("Unknown type: " + type + ", nothing done");
  288. System.exit(1);
  289. }
  290. }
  291. }