2 # unicode-blocks.pl -- Script to generate java.lang.Character.UnicodeBlock
3 # Copyright (C) 2002, 2004 Free Software Foundation, Inc.
5 # This file is part of GNU Classpath.
7 # GNU Classpath is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2, or (at your option)
12 # GNU Classpath is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with GNU Classpath; see the file COPYING. If not, write to the
19 # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 # Linking this library statically or dynamically with other modules is
23 # making a combined work based on this library. Thus, the terms and
24 # conditions of the GNU General Public License cover the whole
27 # As a special exception, the copyright holders of this library give you
28 # permission to link this library with independent modules to produce an
29 # executable, regardless of the license terms of these independent
30 # modules, and to copy and distribute the resulting executable under
31 # terms of your choice, provided that you also meet, for each linked
32 # independent module, the terms and conditions of the license of that
33 # module. An independent module is a module which is not derived from
34 # or based on this library. If you modify this library, you may extend
35 # this exception to your version of the library, but you are not
36 # obligated to do so. If you do not wish to do so, delete this
37 # exception statement from your version.
40 # Code for reading Blocks.txt and generating (to standard out) the code for
41 # java.lang.Character.UnicodeBlock, for pasting into java/lang/Character.java.
42 # You should probably check that the results are accurate to the
43 # specification, but I made sure it works OOB for Unicode 3.0.0 and JDK 1.4.
44 # As the grammar for the Blocks.txt file is changing in Unicode 3.2.0, you
45 # will have to tweak this some for future use. For now, the relevant
46 # Unicode definition files are found in doc/unicode/.
48 # author Eric Blake <ebb9@email.byu.edu>
50 # usage: unicode-blocks.pl <blocks.txt>
51 # where <blocks.txt> is obtained from www.unicode.org (named Blocks-3.txt
52 # for Unicode version 3.0.0).
55 die "Usage: $0 <blocks.txt>" unless @ARGV == 1;
56 open (BLOCKS
, $ARGV[0]) || die "Can't open Unicode block file: $!\n";
58 # A hash of added fields and the JDK they were added in, to automatically
59 # print @since tags. Maintaining this is optional (and tedious), but nice.
60 my %additions = ("SYRIAC" => "1.4",
66 "UNIFIED_CANADIAN_ABORIGINAL_SYLLABICS" => "1.4",
71 "BRAILLE_PATTERNS" => "1.4",
72 "CJK_RADICALS_SUPPLEMENT" => "1.4",
73 "KANGXI_RADICALS" => "1.4",
74 "IDEOGRAPHIC_DESCRIPTION_CHARACTERS" => "1.4",
75 "BOPOMOFO_EXTENDED" => "1.4",
76 "CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A" => "1.4",
77 "YI_SYLLABLES" => "1.4",
78 "YI_RADICALS" => "1.4",
79 "CYRILLIC_SUPPLEMENTARY" => "1.5",
86 "KHMER_SYMBOLS" => "1.5",
87 "PHONETIC_EXTENSIONS" => "1.5",
88 "MISCELLANEOUS_MATHEMATICAL_SYMBOLS_A" => "1.5",
89 "SUPPLEMENTAL_ARROWS_A" => "1.5",
90 "SUPPLEMENTAL_ARROWS_B" => "1.5",
91 "MISCELLANEOUS_MATHEMATICAL_SYMBOLS_B" => "1.5",
92 "SUPPLEMENTAL_MATHEMATICAL_OPERATORS" => "1.5",
93 "MISCELLANEOUS_SYMBOLS_AND_ARROWS" => "1.5",
94 "KATAKANA_PHONETIC_EXTENSIONS" => "1.5",
95 "YIJING_HEXAGRAM_SYMBOLS" => "1.5",
96 "VARIATION_SELECTORS" => "1.5",
97 "LINEAR_B_SYLLABARY" => "1.5",
98 "LINEAR_B_IDEOGRAMS" => "1.5",
99 "AEGEAN_NUMBERS" => "1.5",
100 "OLD_ITALIC" => "1.5",
106 "CYPRIOT_SYLLABARY" => "1.5",
107 "BYZANTINE_MUSICAL_SYMBOLS" => "1.5",
108 "MUSICAL_SYMBOLS" => "1.5",
109 "TAI_XUAN_JING_SYMBOLS" => "1.5",
110 "MATHEMATICAL_ALPHANUMERIC_SYMBOLS" => "1.5",
111 "CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B" => "1.5",
112 "CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT" => "1.5",
114 "VARIATION_SELECTORS_SUPPLEMENT" => "1.5",
115 "SUPPLEMENTARY_PRIVATE_USE_AREA_A" => "1.5",
116 "SUPPLEMENTARY_PRIVATE_USE_AREA_B" => "1.5",
117 "HIGH_SURROGATES" => "1.5",
118 "HIGH_PRIVATE_USE_SURROGATES" => "1.5",
119 "LOW_SURROGATES" => "1.5"
124 * A family of character subsets in the Unicode specification. A character
125 * is in at most one of these blocks.
127 * This inner class was generated automatically from
128 * <code>$ARGV[0]</code>, by some perl scripts.
129 * This Unicode definition file can be found on the
130 * <a href="http://www.unicode.org">http://www.unicode.org</a> website.
131 * JDK 1.5 uses Unicode version 4.0.0.
133 * @author scripts/unicode-blocks.pl (written by Eric Blake)
136 public static final class UnicodeBlock extends Subset
138 /** The start of the subset. */
139 private final int start;
141 /** The end of the subset. */
142 private final int end;
144 /** The canonical name of the block according to the Unicode standard. */
145 private final String canonicalName;
147 /** Enumeration for the <code>forName()</code> method */
148 private enum NameType { CANONICAL, NO_SPACES, CONSTANT; };
151 * Constructor for strictly defined blocks.
153 * @param start the start character of the range
154 * @param end the end character of the range
155 * @param name the block name
156 * @param canonicalName the name of the block as defined in the Unicode
159 private UnicodeBlock(int start, int end, String name,
160 String canonicalName)
165 this.canonicalName = canonicalName;
169 * Returns the Unicode character block which a character belongs to.
170 * <strong>Note</strong>: This method does not support the use of
171 * supplementary characters. For such support, <code>of(int)</code>
172 * should be used instead.
174 * @param ch the character to look up
175 * @return the set it belongs to, or null if it is not in one
177 public static UnicodeBlock of(char ch)
183 * Returns the Unicode character block which a code point belongs to.
185 * @param codePoint the character to look up
186 * @return the set it belongs to, or null if it is not in one.
187 * @throws IllegalArgumentException if the specified code point is
191 public static UnicodeBlock of(int codePoint)
193 if (codePoint > MAX_CODE_POINT)
194 throw new IllegalArgumentException("The supplied integer value is " +
195 "too large to be a codepoint.");
196 // Simple binary search for the correct block.
198 int hi = sets.length - 1;
201 int mid = (low + hi) >> 1;
202 UnicodeBlock b = sets[mid];
203 if (codePoint < b.start)
205 else if (codePoint > b.end)
215 * Returns the <code>UnicodeBlock</code> with the given name, as defined
216 * by the Unicode standard. The version of Unicode in use is defined by
217 * the <code>Character</code> class, and the names are given in the
218 * <code>Blocks-<version>.txt</code> file corresponding to that version.
219 * The name may be specified in one of three ways:
222 * <li>The canonical, human-readable name used by the Unicode standard.
223 * This is the name with all spaces and hyphens retained. For example,
224 * `Basic Latin' retrieves the block, UnicodeBlock.BASIC_LATIN.</li>
225 * <li>The canonical name with all spaces removed e.g. `BasicLatin'.</li>
226 * <li>The name used for the constants specified by this class, which
227 * is the canonical name with all spaces and hyphens replaced with
228 * underscores e.g. `BASIC_LATIN'</li>
231 * The names are compared case-insensitively using the case comparison
232 * associated with the U.S. English locale. The method recognises the
233 * previous names used for blocks as well as the current ones. At
234 * present, this simply means that the deprecated `SURROGATES_AREA'
235 * will be recognised by this method (the <code>of()</code> methods
236 * only return one of the three new surrogate blocks).
239 * @param blockName the name of the block to look up.
240 * @return the specified block.
241 * @throws NullPointerException if the <code>blockName</code> is
243 * @throws IllegalArgumentException if the name does not match any Unicode
247 public static final UnicodeBlock forName(String blockName)
250 if (blockName.indexOf(' ') != -1)
251 type = NameType.CANONICAL;
252 else if (blockName.indexOf('_') != -1)
253 type = NameType.CONSTANT;
255 type = NameType.NO_SPACES;
256 Collator usCollator = Collator.getInstance(Locale.US);
257 usCollator.setStrength(Collator.PRIMARY);
258 /* Special case for deprecated blocks not in sets */
262 if (usCollator.compare(blockName, "Surrogates Area") == 0)
263 return SURROGATES_AREA;
266 if (usCollator.compare(blockName, "SurrogatesArea") == 0)
267 return SURROGATES_AREA;
270 if (usCollator.compare(blockName, "SURROGATES_AREA") == 0)
271 return SURROGATES_AREA;
278 for (UnicodeBlock block : sets)
279 if (usCollator.compare(blockName, block.canonicalName) == 0)
283 for (UnicodeBlock block : sets)
285 String nsName = block.canonicalName.replaceAll(" ","");
286 if (usCollator.compare(blockName, nsName) == 0)
291 for (UnicodeBlock block : sets)
292 if (usCollator.compare(blockName, block.toString()) == 0)
296 throw new IllegalArgumentException("No Unicode block found for " +
304 my ($range, $block) = split(/; /);
305 my ($start, $end) = split /\.\./, $range;
306 next unless defined $block;
310 # Translate new Unicode names which have the old name in Java
311 $block = "Greek" if $block =~ /Greek and Coptic/;
312 $block = "Combining Marks for Symbols"
313 if $block =~ /Combining Diacritical Marks for Symbols/;
315 (my $name = $block) =~ tr/a-z -/A-Z__/;
317 my $since = (defined $additions{$name}
318 ?
"\n * \@since $additions{$name}" : "");
323 * 0x$start - 0x$end.$since
325 public static final UnicodeBlock $name
326 = new UnicodeBlock(0x$start, 0x$end,
336 * '\uD800' - '\uDFFF'.
337 * \@deprecated As of 1.5, the three areas,
338 * <a href="#HIGH_SURROGATES">HIGH_SURROGATES</a>,
339 * <a href="#HIGH_PRIVATE_USE_SURROGATES">HIGH_PRIVATE_USE_SURROGATES</a>
340 * and <a href="#LOW_SURROGATES">LOW_SURROGATES</a>, as defined
341 * by the Unicode standard, should be used in preference to
342 * this. These are also returned from calls to <code>of(int)</code>
343 * and <code>of(char)</code>.
346 public static final UnicodeBlock SURROGATES_AREA
347 = new UnicodeBlock(0xD800, 0xDFFF,
352 * The defined subsets.
354 private static final UnicodeBlock sets[] = {
363 } // class UnicodeBlock