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