2 *******************************************************************************
\r
3 * Copyright (C) 1998-2006, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.tool.layout;
\r
11 import com.ibm.icu.lang.UCharacter;
\r
12 import com.ibm.icu.lang.UScript;
\r
13 import com.ibm.icu.lang.UProperty;
\r
14 import com.ibm.icu.text.UnicodeSet;
\r
15 import com.ibm.icu.text.UnicodeSetIterator;
\r
16 import com.ibm.icu.impl.Utility;
\r
18 public class ScriptData extends TagValueData
\r
20 public static class Record
\r
22 private int startChar;
\r
23 private int endChar;
\r
24 private int scriptCode;
\r
31 Record(int theChar, int theScriptCode)
\r
33 this(theChar, theChar, theScriptCode);
\r
36 Record(int theStartChar, int theEndChar, int theScriptCode)
\r
38 startChar = theStartChar;
\r
39 endChar = theEndChar;
\r
40 scriptCode = theScriptCode;
\r
43 public int startChar()
\r
48 public int endChar()
\r
53 public int scriptCode()
\r
58 public int compareTo(Record that)
\r
60 return this.startChar - that.startChar;
\r
63 public String toString()
\r
65 return "[" + Utility.hex(startChar, 6) + ".." +
\r
66 Utility.hex(endChar, 6) + ", " +
\r
67 UScript.getShortName(scriptCode).toLowerCase() + "ScriptCode]";
\r
71 // TODO: Exceptions could be generated algorithmically
\r
72 private static class TagException
\r
74 private String icuTag;
\r
75 private String otTag;
\r
77 public TagException(String icu, String ot)
\r
83 public String getICUTag()
\r
88 public String getOTTag()
\r
94 // TODO: short name longer than long name, replace repeated chars w/ space...
\r
95 private ScriptData.TagException exceptions[] = {
\r
96 new ScriptData.TagException("laoo", "lao "),
\r
97 new ScriptData.TagException("nkoo", "nko "), // New code from ISO 15924, not sure this will be OT tag
\r
98 new ScriptData.TagException("vaii", "vai "), // New code from ISO 15924, not sure this will be OT tag
\r
99 new ScriptData.TagException("yiii", "yi ")
\r
103 // TODO: binary search the exceptions list?
\r
104 private String getException(String icu)
\r
106 for(int i = 0; i < exceptions.length; i += 1) {
\r
107 if (exceptions[i].getICUTag().equals(icu)) {
\r
108 return exceptions[i].getOTTag();
\r
116 // Straight insertion sort from Knuth vol. III, pg. 81
\r
118 private void sort()
\r
120 for (int j = 1; j < fRecords.length; j += 1) {
\r
122 Record v = fRecords[j];
\r
124 for (i = j - 1; i >= 0; i -= 1) {
\r
125 if (v.compareTo(fRecords[i]) >= 0) {
\r
129 fRecords[i + 1] = fRecords[i];
\r
132 fRecords[i + 1] = v;
\r
138 int commonScript = UCharacter.getPropertyValueEnum(UProperty.SCRIPT, "COMMON");
\r
140 Vector rv = new Vector();
\r
142 fMinScript = UCharacter.getIntPropertyMinValue(UProperty.SCRIPT);
\r
143 fMaxScript = UCharacter.getIntPropertyMaxValue(UProperty.SCRIPT);
\r
144 scriptCount = fMaxScript - fMinScript + 1;
\r
146 System.out.println("Collecting script data for " + scriptCount + " scripts...");
\r
148 fScriptNames = new String[scriptCount];
\r
149 fScriptTags = new String[scriptCount];
\r
151 for (int script = fMinScript; script <= fMaxScript; script += 1) {
\r
152 fScriptNames[script - fMinScript] = UScript.getName(script).toUpperCase();
\r
153 fScriptTags[script - fMinScript] = UScript.getShortName(script).toLowerCase();
\r
155 if (script != commonScript) {
\r
156 UnicodeSet scriptSet = new UnicodeSet("\\p{" + fScriptTags[script - fMinScript] + "}");
\r
157 UnicodeSetIterator it = new UnicodeSetIterator(scriptSet);
\r
159 while (it.nextRange()) {
\r
160 Record record = new Record(it.codepoint, it.codepointEnd, script);
\r
162 rv.addElement(record);
\r
167 fRecords = new Record[rv.size()];
\r
169 for (int i = 0; i < rv.size(); i += 1) {
\r
170 fRecords[i] = (Record) rv.elementAt(i);
\r
173 System.out.println("Collected " + rv.size() + " records. Sorting...");
\r
176 System.out.println("Done.");
\r
179 public int getMinValue()
\r
184 public int getMaxValue()
\r
189 public int getRecordCount()
\r
191 return fRecords.length;
\r
194 public String getTag(int value)
\r
196 if (value >= fMinScript && value <= fMaxScript) {
\r
197 return getException(fScriptTags[value - fMinScript]);
\r
203 public String getTagLabel(int value)
\r
205 if (value >= fMinScript && value <= fMaxScript) {
\r
206 return fScriptTags[value - fMinScript];
\r
212 public String makeTag(int value)
\r
214 if (value >= fMinScript && value <= fMaxScript) {
\r
215 String tag = getException(fScriptTags[value - fMinScript]);
\r
217 return TagUtilities.makeTag(tag);
\r
219 return "0x00000000";
\r
223 public String getName(int value)
\r
225 if (value >= fMinScript && value <= fMaxScript) {
\r
226 return fScriptNames[value - fMinScript];
\r
232 public Record getRecord(int index)
\r
234 if (fRecords != null && index < fRecords.length) {
\r
235 return fRecords[index];
\r
241 private int fMinScript;
\r
242 private int fMaxScript;
\r
243 private String fScriptNames[];
\r
244 private String fScriptTags[];
\r
245 private Record fRecords[];
\r