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

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