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: public class ScriptList {
014: static class LangSysRecord extends TaggedRecord {
015: private Feature[] features;
016: private int featureCount;
017:
018: public LangSysRecord(String theLanguageTag) {
019: super (theLanguageTag);
020:
021: features = new Feature[10];
022: featureCount = 0;
023: }
024:
025: public void addFeature(Feature feature) {
026: if (featureCount > features.length) {
027: Feature[] newFeatures = new Feature[features.length + 5];
028:
029: System.arraycopy(features, 0, newFeatures, 0,
030: features.length);
031: features = newFeatures;
032: }
033:
034: features[featureCount++] = feature;
035: }
036:
037: public void writeLangSysRecord(OpenTypeTableWriter writer) {
038: writer.writeData(0); // lookupOrder (must be NULL)
039: writer.writeData(0xFFFF); // reqFeatureIndex (0xFFFF means none)
040:
041: writer.writeData(featureCount);
042:
043: for (int i = 0; i < featureCount; i += 1) {
044: writer.writeData(features[i].getFeatureIndex());
045: }
046: }
047: }
048:
049: static class ScriptRecord extends TaggedRecord {
050: private LangSysRecord[] langSysRecords;
051: private int langSysCount;
052:
053: public ScriptRecord(String theScriptTag) {
054: super (theScriptTag);
055: langSysRecords = new LangSysRecord[10];
056: langSysCount = 0;
057: }
058:
059: public LangSysRecord findLangSysRecord(String languageTag) {
060: for (int i = 0; i < langSysCount; i += 1) {
061: LangSysRecord langSysRecord = langSysRecords[i];
062:
063: if (langSysRecord.getTag().equals(languageTag)) {
064: return langSysRecord;
065: }
066: }
067:
068: if (langSysCount >= langSysRecords.length) {
069: LangSysRecord[] newLangSysRecords = new LangSysRecord[langSysCount + 5];
070:
071: System.arraycopy(langSysRecords, 0, newLangSysRecords,
072: 0, langSysRecords.length);
073: langSysRecords = newLangSysRecords;
074: }
075:
076: LangSysRecord newLangSysRecord = new LangSysRecord(
077: languageTag);
078: langSysRecords[langSysCount] = newLangSysRecord;
079:
080: langSysCount += 1;
081: return newLangSysRecord;
082: }
083:
084: public void writeScriptRecord(OpenTypeTableWriter writer) {
085: TaggedRecord.sort(langSysRecords, langSysCount);
086:
087: int scriptTableBase = writer.getOutputIndex();
088: int firstLangSys = 0;
089:
090: writer.writeData(0); // default langSys offset (fixed later)
091:
092: if (langSysRecords[0].getTag().equals("(default)")) {
093: firstLangSys = 1;
094: }
095:
096: writer.writeData(langSysCount - firstLangSys);
097:
098: int langSysOffset = writer.getOutputIndex();
099:
100: for (int i = firstLangSys; i < langSysCount; i += 1) {
101: writer.writeTag(langSysRecords[i].getTag());
102: writer.writeData(0);
103: }
104:
105: if (firstLangSys > 0) {
106: System.out.print(" (default)");
107: writer.fixOffset(scriptTableBase, scriptTableBase);
108: langSysRecords[0].writeLangSysRecord(writer);
109: }
110:
111: for (int i = firstLangSys; i < langSysCount; i += 1) {
112: // fix the offset in the langSysRecordArray.
113: // The "+2" skips over the tag and the "+3"
114: // skips to the next langSysRecord entry
115: writer.fixOffset(langSysOffset + 2, scriptTableBase);
116: langSysOffset += 3;
117:
118: System.out.print(" '" + langSysRecords[i].getTag()
119: + "'");
120: langSysRecords[i].writeLangSysRecord(writer);
121: }
122: }
123: }
124:
125: private ScriptRecord[] scriptRecords;
126: private int scriptCount;
127:
128: public ScriptList() {
129: scriptRecords = new ScriptRecord[10];
130: scriptCount = 0;
131: }
132:
133: private LangSysRecord findLangSysRecord(String scriptTag,
134: String languageTag) {
135: for (int i = 0; i < scriptCount; i += 1) {
136: ScriptRecord scriptRecord = scriptRecords[i];
137:
138: if (scriptRecord.getTag().equals(scriptTag)) {
139: return scriptRecord.findLangSysRecord(languageTag);
140: }
141: }
142:
143: if (scriptCount >= scriptRecords.length) {
144: ScriptRecord[] newScriptRecords = new ScriptRecord[scriptCount + 5];
145:
146: System.arraycopy(scriptRecords, 0, newScriptRecords, 0,
147: scriptRecords.length);
148: scriptRecords = newScriptRecords;
149: }
150:
151: ScriptRecord newScriptRecord = new ScriptRecord(scriptTag);
152: scriptRecords[scriptCount] = newScriptRecord;
153:
154: scriptCount += 1;
155: return newScriptRecord.findLangSysRecord(languageTag);
156: }
157:
158: public void addFeature(String scriptTag, String languageTag,
159: Feature feature) {
160: LangSysRecord langSysRecord = findLangSysRecord(scriptTag,
161: languageTag);
162:
163: langSysRecord.addFeature(feature);
164: }
165:
166: public void writeScriptList(OpenTypeTableWriter writer) {
167: System.out.println("writing script list...");
168:
169: int scriptListBase = writer.getOutputIndex();
170:
171: TaggedRecord.sort(scriptRecords, scriptCount);
172: writer.writeData(scriptCount);
173:
174: int scriptRecordOffset = writer.getOutputIndex();
175:
176: for (int i = 0; i < scriptCount; i += 1) {
177: writer.writeTag(scriptRecords[i].getTag());
178: writer.writeData(0);
179: }
180:
181: for (int i = 0; i < scriptCount; i += 1) {
182: // fix the offset in the scriptRecordArray.
183: // The "+2" skips over the tag and the "+3"
184: // skips to the next scriptRecord entry
185: writer.fixOffset(scriptRecordOffset + 2, scriptListBase);
186: scriptRecordOffset += 3;
187:
188: System.out.print(" script '" + scriptRecords[i].getTag()
189: + "':");
190: scriptRecords[i].writeScriptRecord(writer);
191: System.out.println();
192: }
193: }
194: }
|