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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. * The class contains three methods to do that.
  33. * The method fromJava gets its infirmation from Java's compiled-in Unicode Character Database,
  34. * the method fromUCD gets its information from the UCD files,
  35. * the method fromTeX gets its information from the file unicode-letters-XeTeX.tex,
  36. * which is the basis of XeTeX's unicode support.
  37. * In the build file only the method from UCD is used;
  38. * the other two methods are there for demonstration.
  39. * The methods fromJava and fromTeX are commented out because they are not Java 1.4 compliant.
  40. */
  41. public final class UnicodeClasses {
  42. /**
  43. * default path relative to the FOP base directory
  44. */
  45. public static final String CLASSES_XML = "src/java/org/apache/fop/hyphenation/classes.xml";
  46. /**
  47. * Disallow constructor for this utility class
  48. */
  49. private UnicodeClasses() { }
  50. /**
  51. * Generate classes.xml from Java's compiled-in Unicode Character Database
  52. * @param hexcode whether to prefix each class with the hexcode (only for debugging purposes)
  53. * @param outfilePath output file
  54. * @throws IOException
  55. */
  56. /* public static void fromJava(boolean hexcode, String outfilePath) throws IOException {
  57. File f = new File(outfilePath);
  58. if (f.exists()) {
  59. f.delete();
  60. }
  61. f.createNewFile();
  62. FileOutputStream fw = new FileOutputStream(f);
  63. OutputStreamWriter ow = new OutputStreamWriter(fw, "utf-8");
  64. int maxChar;
  65. maxChar = Character.MAX_VALUE;
  66. ow.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  67. "<classes>\n");
  68. // loop over the first Unicode plane
  69. for (int code = Character.MIN_VALUE; code <= maxChar; ++code) {
  70. // skip surrogate area
  71. if (code == Character.MIN_SURROGATE) {
  72. code = Character.MAX_SURROGATE;
  73. continue;
  74. }
  75. // we are only interested in LC, UC and TC letters which are their own LC,
  76. // and in 'other letters'
  77. if (!(((Character.isLowerCase(code) || Character.isUpperCase(code)
  78. || Character.isTitleCase(code))
  79. && code == Character.toLowerCase(code))
  80. || Character.getType(code) == Character.OTHER_LETTER)) {
  81. continue;
  82. }
  83. // skip a number of blocks
  84. Character.UnicodeBlock ubi = Character.UnicodeBlock.of(code);
  85. if (ubi.equals(Character.UnicodeBlock.SUPERSCRIPTS_AND_SUBSCRIPTS)
  86. || ubi.equals(Character.UnicodeBlock.LETTERLIKE_SYMBOLS)
  87. || ubi.equals(Character.UnicodeBlock.ALPHABETIC_PRESENTATION_FORMS)
  88. || ubi.equals(Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS)
  89. || ubi.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS)
  90. || ubi.equals(Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A)
  91. || ubi.equals(Character.UnicodeBlock.HANGUL_SYLLABLES)) {
  92. continue;
  93. }
  94. int uppercode = Character.toUpperCase(code);
  95. int titlecode = Character.toTitleCase(code);
  96. StringBuilder s = new StringBuilder();
  97. if (hexcode) {
  98. s.append("0x" + Integer.toHexString(code) + " ");
  99. }
  100. s.append(Character.toChars(code));
  101. if (uppercode != code) {
  102. s.append(Character.toChars(uppercode));
  103. }
  104. if (titlecode != code && titlecode != uppercode) {
  105. s.append(Character.toChars(titlecode));
  106. }
  107. ow.write(s.toString() + "\n");
  108. }
  109. ow.write("</classes>\n");
  110. ow.flush();
  111. ow.close();
  112. }
  113. */
  114. /**
  115. * The column numbers in the UCD file
  116. */
  117. public static final int UNICODE = 0, GENERAL_CATEGORY = 2, SIMPLE_UPPERCASE_MAPPING = 12,
  118. SIMPLE_LOWERCASE_MAPPING = 13, SIMPLE_TITLECASE_MAPPING = 14, NUM_FIELDS = 15;
  119. /**
  120. * Generate classes.xml from Unicode Character Database files
  121. * @param hexcode whether to prefix each class with the hexcode (only for debugging purposes)
  122. * @param unidataPath path to the directory with UCD files
  123. * @param outfilePath output file
  124. * @throws IOException if the input files are not found
  125. */
  126. public static void fromUCD(boolean hexcode, String unidataPath, String outfilePath)
  127. throws IOException {
  128. File unidata = new File(unidataPath);
  129. File f = new File(outfilePath);
  130. if (f.exists()) {
  131. f.delete();
  132. }
  133. f.createNewFile();
  134. FileOutputStream fw = new FileOutputStream(f);
  135. OutputStreamWriter ow = new OutputStreamWriter(fw, "utf-8");
  136. File in = new File(unidata, "Blocks.txt");
  137. FileInputStream inis = new FileInputStream(in);
  138. InputStreamReader insr = new InputStreamReader(inis, "utf-8");
  139. BufferedReader inbr = new BufferedReader(insr);
  140. Map blocks = new HashMap();
  141. for (String line = inbr.readLine(); line != null; line = inbr.readLine()) {
  142. if (line.startsWith("#") || line.matches("^\\s*$")) {
  143. continue;
  144. }
  145. String[] parts = line.split(";");
  146. String block = parts[1].trim();
  147. String[] indices = parts[0].split("\\.\\.");
  148. int[] ind = {Integer.parseInt(indices[0], 16), Integer.parseInt(indices[1], 16)};
  149. blocks.put(block, ind);
  150. }
  151. inbr.close();
  152. in = new File(unidata, "UnicodeData.txt");
  153. inis = new FileInputStream(in);
  154. insr = new InputStreamReader(inis, "utf-8");
  155. inbr = new BufferedReader(insr);
  156. int maxChar;
  157. maxChar = Character.MAX_VALUE;
  158. ow.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  159. + "<classes>\n");
  160. for (String line = inbr.readLine(); line != null; line = inbr.readLine()) {
  161. String[] fields = line.split(";", NUM_FIELDS);
  162. int code = Integer.parseInt(fields[UNICODE], 16);
  163. if (code > maxChar) {
  164. break;
  165. }
  166. if (((fields[GENERAL_CATEGORY].equals("Ll") || fields[GENERAL_CATEGORY].equals("Lu")
  167. || fields[GENERAL_CATEGORY].equals("Lt"))
  168. && ("".equals(fields[SIMPLE_LOWERCASE_MAPPING])
  169. || fields[UNICODE].equals(fields[SIMPLE_LOWERCASE_MAPPING])))
  170. || fields[GENERAL_CATEGORY].equals("Lo")) {
  171. String[] blockNames = {"Superscripts and Subscripts",
  172. "Letterlike Symbols",
  173. "Alphabetic Presentation Forms",
  174. "Halfwidth and Fullwidth Forms",
  175. "CJK Unified Ideographs",
  176. "CJK Unified Ideographs Extension A",
  177. "Hangul Syllables"};
  178. int j;
  179. for (j = 0; j < blockNames.length; ++j) {
  180. int[] ind = (int[]) blocks.get(blockNames[j]);
  181. if (code >= ind[0] && code <= ind[1]) {
  182. break;
  183. }
  184. }
  185. if (j < blockNames.length) {
  186. continue;
  187. }
  188. int uppercode = -1, titlecode = -1;
  189. if (!"".equals(fields[SIMPLE_UPPERCASE_MAPPING])) {
  190. uppercode = Integer.parseInt(fields[SIMPLE_UPPERCASE_MAPPING], 16);
  191. }
  192. if (!"".equals(fields[SIMPLE_TITLECASE_MAPPING])) {
  193. titlecode = Integer.parseInt(fields[SIMPLE_TITLECASE_MAPPING], 16);
  194. }
  195. StringBuilder s = new StringBuilder();
  196. if (hexcode) {
  197. s.append("0x" + fields[UNICODE].replaceFirst("^0+", "").toLowerCase() + " ");
  198. }
  199. // s.append(Character.toChars(code));
  200. /* This cast only works correctly when we do not exceed Character.MAX_VALUE */
  201. s.append((char) code);
  202. if (uppercode != -1 && uppercode != code) {
  203. // s.append(Character.toChars(uppercode));
  204. s.append((char) uppercode);
  205. }
  206. if (titlecode != -1 && titlecode != code && titlecode != uppercode) {
  207. // s.append(Character.toChars(titlecode));
  208. s.append((char) titlecode);
  209. }
  210. ow.write(s.toString() + "\n");
  211. }
  212. }
  213. ow.write("</classes>\n");
  214. ow.flush();
  215. ow.close();
  216. inbr.close();
  217. }
  218. /**
  219. * Generate classes.xml from XeTeX's Unicode letters file
  220. * @param hexcode whether to prefix each class with the hexcode (only for debugging purposes)
  221. * @param lettersPath path to XeTeX's Unicode letters file unicode-letters-XeTeX.tex
  222. * @param outfilePath output file
  223. * @throws IOException
  224. */
  225. /* public static void fromTeX(boolean hexcode, String lettersPath, String outfilePath)
  226. throws IOException {
  227. File in = new File(lettersPath);
  228. File f = new File(outfilePath);
  229. if (f.exists()) {
  230. f.delete();
  231. }
  232. f.createNewFile();
  233. FileOutputStream fw = new FileOutputStream(f);
  234. OutputStreamWriter ow = new OutputStreamWriter(fw, "utf-8");
  235. FileInputStream inis = new FileInputStream(in);
  236. InputStreamReader insr = new InputStreamReader(inis, "utf-8");
  237. BufferedReader inbr = new BufferedReader(insr);
  238. ow.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
  239. "<classes>\n");
  240. for (String line = inbr.readLine(); line != null; line = inbr.readLine()) {
  241. String[] codes = line.split("\\s+");
  242. if (!(codes[0].equals("\\L") || codes[0].equals("\\l"))) {
  243. continue;
  244. }
  245. if (codes.length == 3) {
  246. ow.write("\"" + line + "\" has two codes");
  247. continue;
  248. }
  249. if (codes[0].equals("\\l") && codes.length != 2) {
  250. ow.write("\"" + line + "\" should have one code");
  251. continue;
  252. }
  253. else if (codes[0].equals("\\L") && codes.length != 4) {
  254. ow.write("\"" + line + "\" should have three codes");
  255. continue;
  256. }
  257. if (codes[0].equals("\\l") || (codes[0].equals("\\L") && codes[1].equals(codes[3]))) {
  258. StringBuilder s = new StringBuilder();
  259. if (hexcode) {
  260. s.append("0x" + codes[1].replaceFirst("^0+", "").toLowerCase() + " ");
  261. }
  262. s.append((char) Integer.parseInt(codes[1], 16));
  263. if (codes[0].equals("\\L")) {
  264. s.append((char) Integer.parseInt(codes[2], 16));
  265. }
  266. ow.write(s.toString() + "\n");
  267. }
  268. }
  269. ow.write("</classes>\n");
  270. ow.flush();
  271. ow.close();
  272. inbr.close();
  273. }
  274. */
  275. /**
  276. * @param args [--hexcode] [--java|--ucd|--tex] [--out outfile] infile
  277. * @throws IOException if the input file cannot be found
  278. */
  279. public static void main(String[] args) throws IOException {
  280. String type = "ucd", prefix = "--", infile = null, outfile = CLASSES_XML;
  281. boolean hexcode = false;
  282. for (int i = 0; i < args.length; ++i) {
  283. if (args[i].startsWith(prefix)) {
  284. String option = args[i].substring(prefix.length());
  285. if (option.equals("java") || option.equals("ucd") || option.equals("tex")) {
  286. type = option;
  287. } else if (option.equals("hexcode")) {
  288. hexcode = true;
  289. } else if (option.equals("out")) {
  290. outfile = args[++i];
  291. } else {
  292. System.err.println("Unknown option: " + option);
  293. System.exit(1);
  294. }
  295. } else {
  296. if (infile != null) {
  297. System.err.println("Only one non-option argument can be given, for infile");
  298. System.exit(1);
  299. }
  300. infile = args[i];
  301. }
  302. }
  303. if (type.equals("java")) {
  304. if (infile != null) {
  305. System.err.println("Type java does not allow an infile");
  306. System.exit(1);
  307. }
  308. } else {
  309. if (infile == null) {
  310. System.err.println("Types ucd and tex require an infile");
  311. System.exit(1);
  312. }
  313. }
  314. /* if (type.equals("java")) {
  315. fromJava(hexcode, outfile);
  316. } else
  317. */
  318. if (type.equals("ucd")) {
  319. fromUCD(hexcode, infile, outfile);
  320. /* } else if (type.equals("tex")) {
  321. fromTeX(hexcode, infile, outfile);
  322. */
  323. } else {
  324. System.err.println("Unknown type: " + type + ", nothing done");
  325. System.exit(1);
  326. }
  327. }
  328. }