]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/tools/misc/src/com/ibm/icu/dev/tool/layout/ScriptData.java
Added flags.
[Dictionary.git] / jars / icu4j-52_1 / tools / misc / src / com / ibm / icu / dev / tool / layout / ScriptData.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1998-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.tool.layout;
8
9 import java.util.Vector;
10
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;
17
18 public class ScriptData extends TagValueData
19 {
20     public static class Record
21     {
22         private int startChar;
23         private int endChar;
24         private int scriptCode;
25         
26         Record()
27         {
28             // nothing?
29         }
30         
31         Record(int theChar, int theScriptCode)
32         {
33             this(theChar, theChar, theScriptCode);
34         }
35         
36         Record(int theStartChar, int theEndChar, int theScriptCode)
37         {
38             startChar = theStartChar;
39             endChar = theEndChar;
40             scriptCode = theScriptCode;
41         }
42         
43         public int startChar()
44         {
45             return startChar;
46         }
47         
48         public int endChar()
49         {
50             return endChar;
51         }
52         
53         public int scriptCode()
54         {
55             return scriptCode;
56         }
57         
58         public int compareTo(Record that)
59         {
60             return this.startChar - that.startChar;
61         }
62         
63         public String toString()
64         {
65             return "[" +  Utility.hex(startChar, 6) + ".." +
66                    Utility.hex(endChar, 6) + ", " +
67                    UScript.getShortName(scriptCode).toLowerCase() + "ScriptCode]";
68         }
69     }
70     
71     // TODO: Exceptions could be generated algorithmically
72     private static class TagException
73     {
74         private String icuTag;
75         private String otTag;
76         
77         public TagException(String icu, String ot)
78         {
79             icuTag = icu;
80             otTag  = ot;
81         }
82         
83         public String getICUTag()
84         {
85             return icuTag;
86         }
87         
88         public String getOTTag()
89         {
90             return otTag;
91         }
92     }
93     
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  ")
100         };
101         
102     
103     // TODO: binary search the exceptions list?
104     private String getException(String icu)
105     {
106         for(int i = 0; i < exceptions.length; i += 1) {
107             if (exceptions[i].getICUTag().equals(icu)) {
108                 return exceptions[i].getOTTag();
109             }
110         }
111         
112         return icu;
113     }
114         
115     //
116     // Straight insertion sort from Knuth vol. III, pg. 81
117     //
118     private void sort()
119     {
120         for (int j = 1; j < fRecords.length; j += 1) {
121             int i;
122             Record v = fRecords[j];
123
124             for (i = j - 1; i >= 0; i -= 1) {
125                 if (v.compareTo(fRecords[i]) >= 0) {
126                     break;
127                 }
128
129                 fRecords[i + 1] = fRecords[i];
130             }
131
132             fRecords[i + 1] = v;
133         }
134     }
135
136     ScriptData()
137     {
138         int commonScript = UCharacter.getPropertyValueEnum(UProperty.SCRIPT, "COMMON");
139         int scriptCount;
140         Vector rv = new Vector();
141         
142         fMinScript  = UCharacter.getIntPropertyMinValue(UProperty.SCRIPT);
143         fMaxScript  = UCharacter.getIntPropertyMaxValue(UProperty.SCRIPT);
144         scriptCount = fMaxScript - fMinScript + 1;
145         
146         System.out.println("Collecting script data for " + scriptCount + " scripts...");
147         
148         fScriptNames = new String[scriptCount];
149         fScriptTags  = new String[scriptCount];
150         
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();
154             
155             if (script != commonScript) {
156                 UnicodeSet scriptSet  = new UnicodeSet("\\p{" + fScriptTags[script - fMinScript] + "}");
157                 UnicodeSetIterator it = new UnicodeSetIterator(scriptSet);
158             
159                 while (it.nextRange()) {
160                     Record record = new Record(it.codepoint, it.codepointEnd, script);
161                     
162                     rv.addElement(record);
163                 }
164             }
165         }
166         
167         fRecords = new Record[rv.size()];
168         
169         for (int i = 0; i < rv.size(); i += 1) {
170             fRecords[i] = (Record) rv.elementAt(i);
171         }
172         
173         System.out.println("Collected " + rv.size() + " records. Sorting...");
174         sort();
175         
176         System.out.println("Done.");
177     }
178     
179     public int getMinValue()
180     {
181         return fMinScript;
182     }
183     
184     public int getMaxValue()
185     {
186         return fMaxScript;
187     }
188     
189     public int getRecordCount()
190     {
191         return fRecords.length;
192     }
193     
194     public String getTag(int value)
195     {
196         if (value >= fMinScript && value <= fMaxScript) {
197             return getException(fScriptTags[value - fMinScript]);
198         }
199         
200         return "zyyx";
201     }
202     
203     public String getTagLabel(int value)
204     {
205         if (value >= fMinScript && value <= fMaxScript) {
206             return fScriptTags[value - fMinScript];
207         }
208         
209         return "zyyx";
210     }
211     
212     public String makeTag(int value)
213     {
214         if (value >= fMinScript && value <= fMaxScript) {
215             String tag = getException(fScriptTags[value - fMinScript]);
216             
217             return TagUtilities.makeTag(tag);
218         } else {
219             return "0x00000000";
220         }
221     }
222     
223     public String getName(int value)
224     {
225         if (value >= fMinScript && value <= fMaxScript) {
226             return fScriptNames[value - fMinScript];
227         }
228         
229         return "COMMON";
230     }
231     
232     public Record getRecord(int index)
233     {
234         if (fRecords != null && index < fRecords.length) {
235             return fRecords[index];
236         }
237         
238         return null;
239     }
240     
241     private int fMinScript;
242     private int fMaxScript;
243     private String fScriptNames[];
244     private String fScriptTags[];
245     private Record fRecords[];
246 }