001: /*
002: *******************************************************************************
003: * Copyright (C) 1998-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: *
007: * Created on Dec 3, 2003
008: *
009: *******************************************************************************
010: */
011: package com.ibm.icu.dev.tool.layout;
012:
013: import java.util.Vector;
014:
015: import com.ibm.icu.impl.Utility;
016:
017: public class LigatureTreeWalker extends TreeWalker implements
018: LookupSubtable {
019: protected int[] componentChars;
020: protected int componentCount;
021: protected int lastFirstComponent;
022:
023: protected Vector ligatureSets;
024: protected Vector ligatureSet;
025:
026: public LigatureTreeWalker() {
027: componentChars = new int[30];
028: componentCount = 0;
029: lastFirstComponent = -1;
030: ligatureSet = null;
031: ligatureSets = new Vector();
032: }
033:
034: public void down(int ch) {
035: componentChars[componentCount] = ch;
036: componentCount += 1;
037: }
038:
039: public void up() {
040: if (componentCount > 0) {
041: componentCount -= 1;
042: }
043: }
044:
045: public void ligature(int lig) {
046: int firstComponent = componentChars[0];
047:
048: if (lastFirstComponent != firstComponent) {
049: if (ligatureSet != null) {
050: ligatureSets.addElement(ligatureSet);
051: }
052:
053: ligatureSet = new Vector();
054: lastFirstComponent = firstComponent;
055: }
056:
057: ligatureSet.addElement(new LigatureEntry(lig, componentChars,
058: componentCount));
059: }
060:
061: public void done() {
062: if (ligatureSet != null) {
063: ligatureSets.addElement(ligatureSet);
064: }
065: }
066:
067: protected int firstComponentChar(int ligatureSetIndex) {
068: Vector ligatureSet = (Vector) ligatureSets
069: .elementAt(ligatureSetIndex);
070: LigatureEntry firstEntry = (LigatureEntry) ligatureSet
071: .elementAt(0);
072:
073: return firstEntry.getComponentChar(0);
074: }
075:
076: protected void writeCoverageTable(OpenTypeTableWriter writer) {
077: int ligatureSetCount = ligatureSets.size();
078:
079: writer.writeData(1);
080: writer.writeData(ligatureSetCount);
081:
082: for (int set = 0; set < ligatureSetCount; set += 1) {
083: writer.writeData(firstComponentChar(set));
084: }
085: }
086:
087: public void writeLookupSubtable(OpenTypeTableWriter writer) {
088: int coverageOffset, ligatureSetOffset, ligatureTableOffset;
089: int ligatureSubstitutionBase = writer.getOutputIndex();
090: int ligatureSetCount = ligatureSets.size();
091:
092: //System.out.println("Writing " + tableName + "...");
093:
094: writer.writeData(1); // substFormat
095:
096: coverageOffset = writer.getOutputIndex();
097: writer.writeData(0); // coverageTableOffset (will fix later)
098:
099: writer.writeData(ligatureSetCount);
100:
101: ligatureSetOffset = writer.getOutputIndex();
102: for (int set = 0; set < ligatureSetCount; set += 1) {
103: writer.writeData(0); // ligatureSet offset - will fix later
104: }
105:
106: for (int set = 0; set < ligatureSetCount; set += 1) {
107: System.out.print(Utility.hex(firstComponentChar(set), 6)
108: + ": ");
109:
110: Vector ligatureSet = (Vector) ligatureSets.elementAt(set);
111: int ligatureCount = ligatureSet.size();
112: int ligatureSetAddress = writer.getOutputIndex();
113:
114: System.out.println(ligatureCount + " ligatures.");
115:
116: writer.fixOffset(ligatureSetOffset++,
117: ligatureSubstitutionBase);
118: writer.writeData(ligatureCount);
119:
120: ligatureTableOffset = writer.getOutputIndex();
121: for (int lig = 0; lig < ligatureCount; lig += 1) {
122: writer.writeData(0); // ligatureTableOffset (will fix later)
123: }
124:
125: for (int lig = 0; lig < ligatureCount; lig += 1) {
126: LigatureEntry entry = (LigatureEntry) ligatureSet
127: .elementAt(lig);
128: int componentCount = entry.getComponentCount();
129:
130: writer.fixOffset(ligatureTableOffset++,
131: ligatureSetAddress);
132: writer.writeData(entry.getLigature());
133: writer.writeData(componentCount);
134:
135: for (int comp = 1; comp < componentCount; comp += 1) {
136: writer.writeData(entry.getComponentChar(comp));
137: }
138: }
139: }
140:
141: writer.fixOffset(coverageOffset, ligatureSubstitutionBase);
142: writeCoverageTable(writer);
143: }
144: }
|