]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/tools/misc/src/com/ibm/icu/dev/tool/layout/DecompTable.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / tools / misc / src / com / ibm / icu / dev / tool / layout / DecompTable.java
1 /**
2  *******************************************************************************
3  * Copyright (C) 2002-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 package com.ibm.icu.dev.tool.layout;
9
10 import java.util.Vector;
11
12 import com.ibm.icu.text.UTF16;
13
14 /**
15  * @author Owner
16  *
17  * To change the template for this generated type comment go to
18  * Window>Preferences>Java>Code Generation>Code and Comments
19  */
20 public class DecompTable implements LookupSubtable
21 {
22     static class DecompEntry
23     {
24         private int composed;
25         private int[] decomp;
26         
27         DecompEntry(int composedChar, String decomposition)
28         {
29             int decompCount = UTF16.countCodePoint(decomposition);
30             
31             composed = composedChar;
32             decomp = new int[decompCount];
33             
34             int out = 0, cp;
35             
36             for (int in = 0; in < decomposition.length(); in += UTF16.getCharCount(cp)) {
37                 cp = UTF16.charAt(decomposition, in);
38                 decomp[out++] = cp;
39             }
40         }
41         
42         public int getComposedCharacter()
43         {
44             return composed;
45         }
46         
47         public int[] getDecomposition()
48         {
49             return decomp;
50         }
51         
52         public int getDecompositionCount()
53         {
54             return decomp.length;
55         }
56         
57         public int getDecomposedCharacter(int i)
58         {
59             if (i >= 0 && i < decomp.length) {
60                 return decomp[i];
61             }
62             
63             return -1;
64         }
65         
66         public int compareTo(DecompEntry that)
67         {
68             return this.composed - that.composed;
69         }
70         
71         //
72         // Straight insertion sort from Knuth vol. III, pg. 81
73         //
74         public static void sort(DecompEntry[] table, Vector decompVector)
75         {
76             for (int j = 0; j < table.length; j += 1) {
77                 int i;
78                 DecompEntry v = (DecompEntry) decompVector.elementAt(j);
79
80                 for (i = j - 1; i >= 0; i -= 1) {
81                     if (v.compareTo(table[i]) >= 0) {
82                       break;
83                     }
84
85                     table[i + 1] = table[i];
86                 }
87
88                 table[i + 1] = v;
89             }
90         }
91     }
92     
93     private Vector decompVector;
94     private DecompEntry[] decompEntries;
95     private int snapshotSize;
96     
97     public DecompTable()
98     {
99         decompVector = new Vector();
100         decompEntries = null;
101         snapshotSize = -1;
102     }
103     
104     public void add(int composed, String decomposition)
105     {
106         DecompEntry entry = new DecompEntry(composed, decomposition);
107         
108         decompVector.addElement(entry);
109     }
110     
111     public int getComposedCharacter(int i)
112     {
113         if (i < 0 || i > decompEntries.length) {
114             return -1;
115         }
116         
117         return decompEntries[i].getComposedCharacter();
118     }
119     
120     public int getDecompositionCount(int i)
121     {
122         if (i < 0 || i > decompEntries.length) {
123             return -1;
124         }
125         
126         return decompEntries[i].getDecompositionCount();
127     }
128     
129     public boolean hasEntries()
130     {
131         return decompVector.size() > 0;
132     }
133     
134     private void snapshot()
135     {
136         if (snapshotSize != decompVector.size()) {
137             snapshotSize = decompVector.size();
138             decompEntries = new DecompEntry[snapshotSize];
139             DecompEntry.sort(decompEntries, decompVector);
140         }
141     }
142
143     public void writeLookupSubtable(OpenTypeTableWriter writer)
144     {
145         snapshot();
146         
147         int multipleSubstitutionsBase = writer.getOutputIndex();
148         int coverageTableIndex, sequenceOffsetIndex;
149         int sequenceCount = decompEntries.length;
150         
151         writer.writeData(1); // format = 1
152         
153         coverageTableIndex = writer.getOutputIndex();
154         writer.writeData(0); // coverage table offset (fixed later)
155         
156         writer.writeData(sequenceCount);
157         
158         sequenceOffsetIndex = writer.getOutputIndex();
159         for (int s = 0; s < sequenceCount; s += 1) {
160             writer.writeData(0); // offset to sequence table (fixed later);
161         }
162         
163         for (int s = 0; s < sequenceCount; s += 1) {
164             DecompEntry entry = decompEntries[s];
165             int decompCount = entry.getDecompositionCount();
166             
167             writer.fixOffset(sequenceOffsetIndex++, multipleSubstitutionsBase);
168             
169             writer.writeData(decompCount); // glyphCount
170             
171             for (int g = 0; g < decompCount; g += 1) {
172                 writer.writeData(entry.getDecomposedCharacter(g));
173             }
174         }
175         
176         // write a format 1 coverage table
177         writer.fixOffset(coverageTableIndex, multipleSubstitutionsBase);
178         writer.writeData(1); // format = 1
179         writer.writeData(sequenceCount);  // glyphCount
180         
181         for (int i = 0; i < sequenceCount; i += 1) {
182             writer.writeData(decompEntries[i].getComposedCharacter());
183         }
184     }
185 }