jiterbug 2649: initial port of ThaiStateTableBuilder to ICU4J.

X-SVN-Rev: 14068
This commit is contained in:
Eric Mader 2003-12-09 23:55:16 +00:00
parent 7599f4255c
commit 46a95a27d5
5 changed files with 531 additions and 0 deletions

View File

@ -0,0 +1,168 @@
/*
*******************************************************************************
* Copyright (C) 1998-2003, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* Created on Dec 09, 2003
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/tool/layout/ThaiCharacterClasses.java,v $
* $Date: 2003/12/09 23:55:16 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu.dev.tool.layout;
import com.ibm.icu.impl.Utility;
import java.io.PrintStream;
public class ThaiCharacterClasses
{
public static final int NON = 0;
public static final int CON = 1;
public static final int COA = 2;
public static final int COD = 3;
public static final int LVO = 4;
public static final int FV1 = 5;
public static final int FV2 = 6;
public static final int FV3 = 7;
public static final int BV1 = 8;
public static final int BV2 = 9;
public static final int BDI = 10;
public static final int TON = 11;
public static final int AD1 = 12;
public static final int AD2 = 13;
public static final int AD3 = 14;
public static final int NIK = 15;
public static final int AV1 = 16;
public static final int AV2 = 17;
public static final int AV3 = 18;
public static final int cCount = 19;
// Indexed by unicode - '\u0E00'
// FIXME: MS Fonts - Should 0E2E has no ascender (it does in WT font)
// FIXME: MS Fonts - 0E47 (MAITAIKHU) and 0E4D (NIKHAHIT) only have vowel forms
// FIXME: MS Fonts - 0E4E (YAMAKKAN) only has one form
private static final int classTable[] = {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
// -------------------------------------------------------------------------------
/*0E00*/ NON, CON, CON, CON, CON, CON, CON, CON, CON, CON, CON, CON, CON, COD, COD, COD,
/*0E10*/ COD, CON, CON, CON, CON, CON, CON, CON, CON, CON, CON, COA, CON, COA, CON, COA,
/*0E20*/ CON, CON, CON, CON, FV3, CON, FV3, CON, CON, CON, CON, CON, CON, CON, CON, NON,
/*0E30*/ FV1, AV2, FV1, FV1, AV1, AV3, AV2, AV3, BV1, BV2, BDI, NON, NON, NON, NON, NON,
/*0E40*/ LVO, LVO, LVO, LVO, LVO, FV2, NON, AD2, TON, TON, TON, TON, AD1, NIK, AD3, NON,
/*0E50*/ NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON, NON
};
private static String[] classNames =
{
"NON",
"CON",
"COA",
"COD",
"LVO",
"FV1",
"FV2",
"FV3",
"BV1",
"BV2",
"BDI",
"TON",
"AD1",
"AD2",
"AD3",
"NIK",
"AV1",
"AV2",
"AV3"
};
private static final char pairTable[][] = {
//------------------------------------------------------------------------------------------------
// N C C C L F F F B B B T A A A N A A A
// O O O O V V V V V V D O D D D I V V V
// N N A D O 1 2 3 1 2 I N 1 2 3 K 1 2 3
//------------------------------------------------------------------------------------------------
/*NON*/ {'A', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*CON*/ {'A', 'A', 'A', 'A', 'A', 'A', 'S', 'A', 'C', 'C', 'C', 'E', 'E', 'E', 'C', 'E', 'C', 'C', 'C'},
/*COA*/ {'A', 'A', 'A', 'A', 'A', 'A', 'S', 'A', 'C', 'C', 'C', 'F', 'F', 'F', 'D', 'F', 'D', 'D', 'D'},
/*COD*/ {'A', 'A', 'A', 'A', 'A', 'A', 'S', 'A', 'H', 'H', 'H', 'E', 'E', 'E', 'C', 'E', 'C', 'C', 'C'},
/*LVO*/ {'S', 'A', 'A', 'A', 'S', 'S', 'S', 'S', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*FV1*/ {'S', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*FV2*/ {'A', 'A', 'A', 'A', 'A', 'A', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*FV3*/ {'A', 'A', 'A', 'A', 'A', 'S', 'A', 'S', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*BV1*/ {'A', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'E', 'E', 'R', 'R', 'E', 'R', 'R', 'R'},
/*BV2*/ {'A', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'E', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*BDI*/ {'A', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*TON*/ {'A', 'A', 'A', 'A', 'A', 'I', 'A', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*AD1*/ {'A', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*AD2*/ {'A', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*AD3*/ {'A', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*NIK*/ {'A', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'G', 'G', 'R', 'R', 'R', 'R', 'R', 'R'},
/*AV1*/ {'A', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'G', 'G', 'R', 'R', 'G', 'R', 'R', 'R'},
/*AV2*/ {'A', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'G', 'R', 'R', 'R', 'R', 'R', 'R', 'R'},
/*AV3*/ {'A', 'A', 'A', 'A', 'A', 'S', 'S', 'A', 'R', 'R', 'R', 'G', 'R', 'G', 'R', 'R', 'R', 'R', 'R'}
};
public static int getCharClass(char ch)
{
int charClass = NON;
if (ch >= '\u0E00' && ch <= '\u0E5B') {
charClass = classTable[ch - '\u0E00'];
}
return charClass;
}
public static String getClassName(int classID)
{
if (classID < 0 || classID >= cCount) {
return "***";
}
return classNames[classID];
}
public static char getPairAction(int prevClass, int currClass)
{
if (prevClass < 0 || prevClass >= cCount |
currClass < 0 || currClass >= cCount) {
return 'A';
}
return pairTable[prevClass][currClass];
}
private static String classTableHeader =
"const le_uint8 ThaiShaping::classTable[] = {\n" +
" // 0 1 2 3 4 5 6 7 8 9 A B C D E F\n" +
" // -------------------------------------------------------------------------------";
public static void writeClassTable(PrintStream output)
{
System.out.print("Writing class table...");
output.print(classTableHeader);
for (char ch = '\u0E00'; ch <= '\u0E5B'; ch += 1) {
int charClass = getCharClass(ch);
if ((ch & 0x000F) == 0) {
output.print("\n /*" + Utility.hex(ch, 4) + "*/ ");
}
output.print(getClassName(charClass));
if (ch < '\u0E5B') {
output.print(", ");
} else {
output.print("\n};\n\n");
}
}
System.out.println(" done.");
}
}

View File

@ -0,0 +1,213 @@
/*
*******************************************************************************
* Copyright (C) 1998-2003, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* Created on Dec 09, 2003
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/tool/layout/ThaiStateTable.java,v $
* $Date: 2003/12/09 23:55:16 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu.dev.tool.layout;
import com.ibm.icu.impl.Utility;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
public class ThaiStateTable
{
static Vector stateTable = null;
static int nextState = 0;
private final static int newState()
{
ThaiStateTransition[] stateRow = new ThaiStateTransition[ThaiCharacterClasses.cCount];
for (int c = 0; c < ThaiCharacterClasses.cCount; c += 1) {
stateRow[c] = null;
}
stateTable.addElement(stateRow);
return nextState++;
}
private final static boolean isLegalHere(int state, char pairAction)
{
switch (pairAction) {
case 'A':
return state == 0;
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
return true;
case 'R':
case 'S':
return false;
}
return false;
};
private final static boolean composesWithAnything(int charClass)
{
for (int c = 0; c < ThaiCharacterClasses.cCount; c += 1) {
char action = ThaiCharacterClasses.getPairAction(charClass, c);
if (action >= 'C' && action <= 'I') {
return true;
}
}
return false;
}
private final static void fixNextStates()
{
ThaiStateTransition[] groundState = (ThaiStateTransition[]) stateTable.elementAt(0);
for (int s = 1; s < stateTable.size(); s += 1) {
ThaiStateTransition[] state = (ThaiStateTransition[]) stateTable.elementAt(s);
for (int c = 0; c < ThaiCharacterClasses.cCount; c += 1) {
ThaiStateTransition transition = state[c];
if (transition.getNextState() == 0) {
transition.setNextState(groundState[c].getNextState());
}
}
}
}
private final static int addState(int prevClass, int prevPrevClass)
{
int state = newState();
ThaiStateTransition[] stateRow = (ThaiStateTransition[]) stateTable.elementAt(state);
for (int c = 0; c < ThaiCharacterClasses.cCount; c += 1) {
char pairAction = ThaiCharacterClasses.getPairAction(prevClass, c);
int nextState = 0;
switch (pairAction) {
case 'G':
if (prevClass == ThaiCharacterClasses.NIK &&
prevPrevClass == ThaiCharacterClasses.AV1) {
pairAction = 'R';
} else if (prevPrevClass != ThaiCharacterClasses.COA) {
pairAction = 'C';
}
break;
case 'E':
if (prevPrevClass == ThaiCharacterClasses.COA) {
pairAction = 'F';
}
break;
case 'I':
if (prevClass == ThaiCharacterClasses.TON &&
(prevPrevClass < ThaiCharacterClasses.CON ||
prevPrevClass > ThaiCharacterClasses.COD)) {
pairAction = 'S';
} else {
pairAction = 'A';
}
break;
default:
break;
}
if (c != prevClass && isLegalHere(state, pairAction) && composesWithAnything(c)) {
nextState = addState(c, prevClass);
}
stateRow[c] = new ThaiStateTransition(nextState, pairAction);
}
return state;
}
static
{
stateTable = new Vector();
addState(ThaiCharacterClasses.NON, ThaiCharacterClasses.NON);
fixNextStates();
}
public static ThaiStateTransition getTransition(int state, int currClass)
{
ThaiStateTransition[] row = (ThaiStateTransition[]) stateTable.elementAt(state);
return row[currClass];
}
private static String header0 =
"const ThaiShaping::StateTransition ThaiShaping::thaiStateTable[][ThaiShaping::classCount] = {";
private static String header1 =
" //+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+";
private static String header2 =
" //| N C C C L F F F B B B T A A A N A A A |\n" +
" //| O O O O V V V V V V D O D D D I V V V |\n" +
" //| N N A D O 1 2 3 1 2 I N 1 2 3 K 1 2 3 |";
public static void writeStateTable(PrintStream output)
{
System.out.print("Writing state table...");
output.println(header0);
output.println(header1);
output.println(header2);
output.println(header1);
for (int state = 0; state < stateTable.size(); state += 1) {
ThaiStateTransition[] row = (ThaiStateTransition[]) stateTable.elementAt(state);
output.print(" /*");
if (state < 10) {
output.print("0");
}
output.print(state);
output.print("*/ {");
for (int c = 0; c < ThaiCharacterClasses.cCount; c += 1) {
row[c].write(output);
if (c < ThaiCharacterClasses.cCount - 1) {
output.print(", ");
}
}
output.print("}");
if (state < stateTable.size() - 1) {
output.print(",");
}
output.println();
}
output.println("};\n");
System.out.println(" done.");
}
}

View File

@ -0,0 +1,38 @@
/*
*******************************************************************************
* Copyright (C) 1998-2003, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* Created on Dec 09, 2003
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/tool/layout/ThaiStateTableBuilder.java,v $
* $Date: 2003/12/09 23:55:16 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu.dev.tool.layout;
import com.ibm.icu.impl.Utility;
import java.io.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ThaiStateTableBuilder
{
public static void main(String args[])
{
ThaiStateTableModuleWriter writer = new ThaiStateTableModuleWriter();
writer.openFile(args[0]);
writer.writeTables();
writer.closeFile();
}
}

View File

@ -0,0 +1,41 @@
/*
*******************************************************************************
* Copyright (C) 1998-2003, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* Created on Dec 09, 2003
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/tool/layout/ThaiStateTableModuleWriter.java,v $
* $Date: 2003/12/09 23:55:16 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu.dev.tool.layout;
import com.ibm.icu.impl.Utility;
import java.io.PrintStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;
public class ThaiStateTableModuleWriter extends ModuleWriter
{
public ThaiStateTableModuleWriter()
{
super();
}
public void writeTables()
{
writeHeader(null, includeFiles);
ThaiCharacterClasses.writeClassTable(output);
ThaiStateTable.writeStateTable(output);
writeTrailer();
}
private static final String[] includeFiles = {"LETypes.h", "ThaiShaping.h"};
}

View File

@ -0,0 +1,71 @@
/*
*******************************************************************************
* Copyright (C) 1998-2003, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* Created on Dec 09, 2003
*
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/tool/layout/ThaiStateTransition.java,v $
* $Date: 2003/12/09 23:55:16 $
* $Revision: 1.1 $
*
*******************************************************************************
*/
package com.ibm.icu.dev.tool.layout;
import com.ibm.icu.impl.Utility;
import java.io.PrintStream;
public class ThaiStateTransition
{
int nextState;
char action;
public ThaiStateTransition(int nextState, char action)
{
this.nextState = nextState;
this.action = action;
}
public final int getNextState()
{
return nextState;
}
public final char getAction()
{
return action;
}
public final void setNextState(int newNextState)
{
nextState = newNextState;
}
public final void setAction(char newAction)
{
action = newAction;
}
public String toString()
{
return ((nextState < 10) ? "0" : "") + nextState + "/" + action + " ";
}
public void write(PrintStream output)
{
output.print("{");
if (nextState < 10) {
output.print(" ");
}
output.print(nextState);
output.print(", t");
output.print(action);
output.print("}");
}
}