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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. /** The 1st column in the UCD file. */
  136. public static final int UNICODE = 0;
  137. /** The 3rd column in the UCD file. */
  138. public static final int GENERAL_CATEGORY = 2;
  139. /** The 13th column in the UCD file. */
  140. public static final int SIMPLE_UPPERCASE_MAPPING = 12;
  141. /** The 14th column in the UCD file. */
  142. public static final int SIMPLE_LOWERCASE_MAPPING = 13;
  143. /** The 15th column in the UCD file. */
  144. public static final int SIMPLE_TITLECASE_MAPPING = 14;
  145. /** The number of columns in the UCD file. */
  146. public static final int NUM_FIELDS = 15;
  147. /**
  148. * Generate classes.xml from Unicode Character Database files
  149. * @param hexcode whether to prefix each class with the hexcode (only for debugging purposes)
  150. * @param unidataPath path to the directory with UCD files
  151. * @param outfilePath output file
  152. * @throws IOException if the input files are not found
  153. * @throws URISyntaxException if {@code unidataPath} cannot be converted to a URI
  154. */
  155. public static void fromUCD(boolean hexcode, String unidataPath, String outfilePath)
  156. throws IOException, URISyntaxException {
  157. URI unidata;
  158. if (unidataPath.endsWith("/")) {
  159. unidata = new URI(unidataPath);
  160. } else {
  161. unidata = new URI(unidataPath + "/");
  162. }
  163. String scheme = unidata.getScheme();
  164. if (scheme == null || !(scheme.equals("file") || scheme.equals("http"))) {
  165. throw new FileNotFoundException(
  166. "URI with file or http scheme required for UNIDATA input directory");
  167. }
  168. File f = new File(outfilePath);
  169. if (f.exists()) {
  170. f.delete();
  171. }
  172. f.createNewFile();
  173. FileOutputStream fw = new FileOutputStream(f);
  174. OutputStreamWriter ow = new OutputStreamWriter(fw, "utf-8");
  175. URI inuri = unidata.resolve("Blocks.txt");
  176. InputStream inis = null;
  177. if (scheme.equals("file")) {
  178. File in = new File(inuri);
  179. inis = new FileInputStream(in);
  180. } else if (scheme.equals("http")) {
  181. inis = inuri.toURL().openStream();
  182. }
  183. InputStreamReader insr = new InputStreamReader(inis, "utf-8");
  184. BufferedReader inbr = new BufferedReader(insr);
  185. Map blocks = new HashMap();
  186. for (String line = inbr.readLine(); line != null; line = inbr.readLine()) {
  187. if (line.startsWith("#") || line.matches("^\\s*$")) {
  188. continue;
  189. }
  190. String[] parts = line.split(";");
  191. String block = parts[1].trim();
  192. String[] indices = parts[0].split("\\.\\.");
  193. int[] ind = {Integer.parseInt(indices[0], 16), Integer.parseInt(indices[1], 16)};
  194. blocks.put(block, ind);
  195. }
  196. inbr.close();
  197. inuri = unidata.resolve("UnicodeData.txt");
  198. if (scheme.equals("file")) {
  199. File in = new File(inuri);
  200. inis = new FileInputStream(in);
  201. } else if (scheme.equals("http")) {
  202. inis = inuri.toURL().openStream();
  203. }
  204. insr = new InputStreamReader(inis, "utf-8");
  205. inbr = new BufferedReader(insr);
  206. int maxChar;
  207. maxChar = Character.MAX_VALUE;
  208. ow.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  209. License.writeXMLLicenseId(ow);
  210. ow.write("\n");
  211. writeGenerated(ow);
  212. ow.write("\n");
  213. ow.write("<classes>\n");
  214. for (String line = inbr.readLine(); line != null; line = inbr.readLine()) {
  215. String[] fields = line.split(";", NUM_FIELDS);
  216. int code = Integer.parseInt(fields[UNICODE], 16);
  217. if (code > maxChar) {
  218. break;
  219. }
  220. if (((fields[GENERAL_CATEGORY].equals("Ll") || fields[GENERAL_CATEGORY].equals("Lu")
  221. || fields[GENERAL_CATEGORY].equals("Lt"))
  222. && ("".equals(fields[SIMPLE_LOWERCASE_MAPPING])
  223. || fields[UNICODE].equals(fields[SIMPLE_LOWERCASE_MAPPING])))
  224. || fields[GENERAL_CATEGORY].equals("Lo")) {
  225. String[] blockNames = {"Superscripts and Subscripts",
  226. "Letterlike Symbols",
  227. "Alphabetic Presentation Forms",
  228. "Halfwidth and Fullwidth Forms",
  229. "CJK Unified Ideographs",
  230. "CJK Unified Ideographs Extension A",
  231. "Hangul Syllables"};
  232. int j;
  233. for (j = 0; j < blockNames.length; ++j) {
  234. int[] ind = (int[]) blocks.get(blockNames[j]);
  235. if (code >= ind[0] && code <= ind[1]) {
  236. break;
  237. }
  238. }
  239. if (j < blockNames.length) {
  240. continue;
  241. }
  242. int uppercode = -1;
  243. int titlecode = -1;
  244. if (!"".equals(fields[SIMPLE_UPPERCASE_MAPPING])) {
  245. uppercode = Integer.parseInt(fields[SIMPLE_UPPERCASE_MAPPING], 16);
  246. }
  247. if (!"".equals(fields[SIMPLE_TITLECASE_MAPPING])) {
  248. titlecode = Integer.parseInt(fields[SIMPLE_TITLECASE_MAPPING], 16);
  249. }
  250. StringBuilder s = new StringBuilder();
  251. if (hexcode) {
  252. s.append("0x" + fields[UNICODE].replaceFirst("^0+", "").toLowerCase() + " ");
  253. }
  254. s.append(Character.toChars(code));
  255. if (uppercode != -1 && uppercode != code) {
  256. s.append(Character.toChars(uppercode));
  257. }
  258. if (titlecode != -1 && titlecode != code && titlecode != uppercode) {
  259. s.append(Character.toChars(titlecode));
  260. }
  261. ow.write(s.toString() + "\n");
  262. }
  263. }
  264. ow.write("</classes>\n");
  265. ow.flush();
  266. ow.close();
  267. inbr.close();
  268. }
  269. /**
  270. * Generate classes.xml from XeTeX's Unicode letters file
  271. * @param hexcode whether to prefix each class with the hexcode (only for debugging purposes)
  272. * @param lettersPath path to XeTeX's Unicode letters file unicode-letters-XeTeX.tex
  273. * @param outfilePath output file
  274. * @throws IOException in case of an I/O exception
  275. */
  276. public static void fromTeX(boolean hexcode, String lettersPath, String outfilePath)
  277. throws IOException {
  278. File in = new File(lettersPath);
  279. File f = new File(outfilePath);
  280. if (f.exists()) {
  281. f.delete();
  282. }
  283. f.createNewFile();
  284. FileOutputStream fw = new FileOutputStream(f);
  285. OutputStreamWriter ow = new OutputStreamWriter(fw, "utf-8");
  286. FileInputStream inis = new FileInputStream(in);
  287. InputStreamReader insr = new InputStreamReader(inis, "utf-8");
  288. BufferedReader inbr = new BufferedReader(insr);
  289. ow.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
  290. License.writeXMLLicenseId(ow);
  291. ow.write("\n");
  292. writeGenerated(ow);
  293. ow.write("\n");
  294. ow.write("<classes>\n");
  295. for (String line = inbr.readLine(); line != null; line = inbr.readLine()) {
  296. String[] codes = line.split("\\s+");
  297. if (!(codes[0].equals("\\L") || codes[0].equals("\\l"))) {
  298. continue;
  299. }
  300. if (codes.length == 3) {
  301. ow.write("\"" + line + "\" has two codes");
  302. continue;
  303. }
  304. if (codes[0].equals("\\l") && codes.length != 2) {
  305. ow.write("\"" + line + "\" should have one code");
  306. continue;
  307. } else if (codes[0].equals("\\L") && codes.length != 4) {
  308. ow.write("\"" + line + "\" should have three codes");
  309. continue;
  310. }
  311. if (codes[0].equals("\\l") || (codes[0].equals("\\L") && codes[1].equals(codes[3]))) {
  312. StringBuilder s = new StringBuilder();
  313. if (hexcode) {
  314. s.append("0x" + codes[1].replaceFirst("^0+", "").toLowerCase() + " ");
  315. }
  316. s.append(Character.toChars(Integer.parseInt(codes[1], 16)));
  317. if (codes[0].equals("\\L")) {
  318. s.append(Character.toChars(Integer.parseInt(codes[2], 16)));
  319. }
  320. ow.write(s.toString() + "\n");
  321. }
  322. }
  323. ow.write("</classes>\n");
  324. ow.flush();
  325. ow.close();
  326. inbr.close();
  327. }
  328. /**
  329. * @param args [--hexcode] [--java|--ucd|--tex] outfile [infile]
  330. * @throws IOException if the input file cannot be found
  331. * @throws URISyntaxException if the input URI is incorrect
  332. */
  333. public static void main(String[] args) throws IOException, URISyntaxException {
  334. String type = "ucd";
  335. String prefix = "--";
  336. String infile = null;
  337. String outfile = null;
  338. boolean hexcode = false;
  339. int i;
  340. for (i = 0; i < args.length && args[i].startsWith(prefix); ++i) {
  341. String option = args[i].substring(prefix.length());
  342. if (option.equals("java") || option.equals("ucd") || option.equals("tex")) {
  343. type = option;
  344. } else if (option.equals("hexcode")) {
  345. hexcode = true;
  346. } else {
  347. System.err.println("Unknown option: " + option);
  348. System.exit(1);
  349. }
  350. }
  351. if (i < args.length) {
  352. outfile = args[i];
  353. } else {
  354. System.err.println("Output file is required; aborting");
  355. System.exit(1);
  356. }
  357. if (++i < args.length) {
  358. infile = args[i];
  359. }
  360. if (type.equals("java") && infile != null) {
  361. System.err.println("Type java does not allow an infile");
  362. System.exit(1);
  363. } else if (type.equals("ucd") && infile == null) {
  364. infile = UNICODE_DIR;
  365. } else if (type.equals("tex") && infile == null) {
  366. System.err.println("Type tex requires an input file");
  367. System.exit(1);
  368. }
  369. if (type.equals("java")) {
  370. fromJava(hexcode, outfile);
  371. } else if (type.equals("ucd")) {
  372. fromUCD(hexcode, infile, outfile);
  373. } else if (type.equals("tex")) {
  374. fromTeX(hexcode, infile, outfile);
  375. } else {
  376. System.err.println("Unknown type: " + type + ", nothing done");
  377. System.exit(1);
  378. }
  379. }
  380. }