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