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.io.PrintStream;
014:
015: import com.ibm.icu.impl.Utility;
016:
017: abstract class OpenTypeTableWriter {
018: static class OpenTypeTableDumper {
019: private short[] table;
020: private int tableLength;
021:
022: OpenTypeTableDumper(short[] data, int outputIndex) {
023: table = data;
024: tableLength = outputIndex;
025: }
026:
027: int length() {
028: return tableLength;
029: }
030:
031: void appendValue(StringBuffer line, int index) {
032: short value = table[index];
033:
034: line.append("0x");
035: line.append(Utility.hex((value >> 8) & 0xFF, 2));
036: line.append(", ");
037:
038: line.append("0x");
039: line.append(Utility.hex(value & 0xFF, 2));
040: }
041:
042: void dumpTable(PrintStream output, int valuesPerLine) {
043: StringBuffer line = new StringBuffer(" "); // four spaces
044: int maxIndex = length();
045:
046: for (int i = 0; i < maxIndex; i += 1) {
047:
048: if (i > 0 && i % valuesPerLine == 0) {
049: output.println(line.toString());
050: line.setLength(4);
051: }
052:
053: appendValue(line, i);
054: line.append(", ");
055: }
056:
057: line.setLength(line.length() - 2);
058:
059: output.println(line.toString());
060: }
061: }
062:
063: protected short[] data;
064: protected int outputIndex;
065:
066: public OpenTypeTableWriter(int initialBufferSize) {
067: data = new short[initialBufferSize];
068: outputIndex = 0;
069: }
070:
071: public OpenTypeTableWriter() {
072: this (1024);
073: }
074:
075: public int getOutputIndex() {
076: return outputIndex;
077: }
078:
079: public void writeData(int value) {
080: if (outputIndex >= data.length) {
081: short[] newData = new short[data.length + 512];
082:
083: System.arraycopy(data, 0, newData, 0, data.length);
084:
085: data = newData;
086: }
087:
088: data[outputIndex] = (short) value;
089: outputIndex += 1;
090: }
091:
092: public void writeTag(String tag) {
093: char[] charArray = { '\0', '\0', '\0', '\0' };
094: int max = Math.min(tag.length(), 4);
095:
096: tag.getChars(0, max, charArray, 0);
097:
098: writeData(((charArray[0] & 0xFF) << 8) + (charArray[1] & 0xFF));
099: writeData(((charArray[2] & 0xFF) << 8) + (charArray[3] & 0xFF));
100: }
101:
102: public void fixOffset(int offset, int base) {
103: // * 2 to convert from short to byte index
104: data[offset] = (short) ((outputIndex - base) * 2);
105: }
106:
107: public void dumpTable(PrintStream output, int valuesPerLine) {
108: OpenTypeTableDumper dumper = new OpenTypeTableDumper(data,
109: outputIndex);
110:
111: dumper.dumpTable(output, valuesPerLine);
112: }
113:
114: abstract public void writeTable(PrintStream output);
115: }
|