]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/tools/misc/src/com/ibm/icu/dev/tool/layout/ThaiStateTable.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / tools / misc / src / com / ibm / icu / dev / tool / layout / ThaiStateTable.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1998-2008, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  *
7  * Created on Dec 09, 2003
8  *
9  *******************************************************************************
10  */
11 package com.ibm.icu.dev.tool.layout;
12
13 import java.io.PrintStream;
14 import java.util.Vector;
15
16 public class ThaiStateTable
17 {
18     static Vector stateTable = null;
19     static int nextState = 0;
20     
21     private final static int newState()
22     {
23         ThaiStateTransition[] stateRow = new ThaiStateTransition[ThaiCharacterClasses.cCount];
24         
25         for (int c = 0; c < ThaiCharacterClasses.cCount; c += 1) {
26             stateRow[c] = null;
27         }
28         
29         stateTable.addElement(stateRow);
30         
31         return nextState++;
32     }
33     
34     private final static boolean isLegalHere(int state, char pairAction)
35     {
36         switch (pairAction) {
37         case 'A':
38             return state == 0;
39             
40         case 'C':
41         case 'D':
42         case 'E':
43         case 'F':
44         case 'G':
45         case 'H':
46             return true;
47         
48         case 'R':
49         case 'S':
50             return false;
51         }
52         
53         return false;
54     }
55     
56     private final static boolean composesWithAnything(int charClass)
57     {
58         for (int c = 0; c < ThaiCharacterClasses.cCount; c += 1) {
59             char action = ThaiCharacterClasses.getPairAction(charClass, c);
60             
61             if (action >= 'C' && action <= 'I') {
62                 return true;
63             }
64         }
65         
66         return false;
67     }
68     
69     private final static void fixNextStates()
70     {
71         ThaiStateTransition[] groundState = (ThaiStateTransition[]) stateTable.elementAt(0);
72         
73         for (int s = 1; s < stateTable.size(); s += 1) {
74             ThaiStateTransition[] state = (ThaiStateTransition[]) stateTable.elementAt(s);
75             
76             for (int c = 0; c < ThaiCharacterClasses.cCount; c += 1) {
77                 ThaiStateTransition transition = state[c];
78                 
79                 if (transition.getNextState() == 0) {
80                     transition.setNextState(groundState[c].getNextState());
81                 }
82             }
83         }
84     }
85     
86     private final static int addState(int prevClass, int prevPrevClass)
87     {
88         int state = newState();
89         ThaiStateTransition[] stateRow = (ThaiStateTransition[]) stateTable.elementAt(state);
90         
91         for (int c = 0; c < ThaiCharacterClasses.cCount; c += 1) {
92             char pairAction = ThaiCharacterClasses.getPairAction(prevClass, c);
93             int nextSt = 0;
94             
95             switch (pairAction) {
96             case 'G':
97                 if (prevClass == ThaiCharacterClasses.NIK &&
98                     prevPrevClass == ThaiCharacterClasses.AV1) {
99                     pairAction = 'R';
100                 } else if (prevPrevClass != ThaiCharacterClasses.COA) {
101                     pairAction = 'C';
102                 }
103                 break;
104                 
105             case 'E':
106                 if (prevPrevClass == ThaiCharacterClasses.COA) {
107                     pairAction = 'F';
108                 }
109                 break;
110                 
111             case 'I':
112                 if (prevClass == ThaiCharacterClasses.TON &&
113                     (prevPrevClass < ThaiCharacterClasses.CON ||
114                      prevPrevClass > ThaiCharacterClasses.COD)) {
115                     pairAction = 'S';
116                 } else {
117                     pairAction = 'A';
118                 }
119                 break;
120                 
121             default:
122                 break;
123             }
124             
125             if (c != prevClass && isLegalHere(state, pairAction) && composesWithAnything(c)) {
126                 nextSt = addState(c, prevClass);
127             }
128             
129             stateRow[c] = new ThaiStateTransition(nextSt, pairAction);
130         }
131         
132         return state;
133     }
134     
135     static
136     {
137         stateTable = new Vector();
138             
139         addState(ThaiCharacterClasses.NON, ThaiCharacterClasses.NON);
140             
141         fixNextStates();
142     }
143     
144     public static ThaiStateTransition getTransition(int state, int currClass)
145     {
146         ThaiStateTransition[] row = (ThaiStateTransition[]) stateTable.elementAt(state);
147         
148         return row[currClass];
149     }
150
151     private static String header0 =
152 "const ThaiShaping::StateTransition ThaiShaping::thaiStateTable[][ThaiShaping::classCount] = {";
153
154     private static String header1 =
155 "    //+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+";
156
157     private static String header2 =
158 "    //|         N         C         C         C         L         F         F         F         B         B         B         T         A         A         A         N         A         A         A    |\n" +
159 "    //|         O         O         O         O         V         V         V         V         V         V         D         O         D         D         D         I         V         V         V    |\n" +
160 "    //|         N         N         A         D         O         1         2         3         1         2         I         N         1         2         3         K         1         2         3    |";
161     
162     public static void writeStateTable(PrintStream output)
163     {
164         System.out.print("Writing state table...");
165         
166         output.println(header0);
167         output.println(header1);
168         output.println(header2);
169         output.println(header1);
170         
171         for (int state = 0; state < stateTable.size(); state += 1) {
172             ThaiStateTransition[] row = (ThaiStateTransition[]) stateTable.elementAt(state);
173             
174             output.print("    /*");
175             
176             if (state < 10) {
177                 output.print("0");
178             }
179             
180             output.print(state);
181             
182             output.print("*/ {");
183             
184             for (int c = 0; c < ThaiCharacterClasses.cCount; c += 1) {
185                 row[c].write(output);
186                 
187                 if (c < ThaiCharacterClasses.cCount - 1) {
188                     output.print(", ");
189                 }
190             }
191             
192             output.print("}");
193                     
194             if (state < stateTable.size() - 1) {
195                 output.print(",");
196             }
197                     
198             output.println();
199         }
200         
201         output.println("};\n");
202         
203         System.out.println(" done.");
204     }
205 }