2 *******************************************************************************
3 * Copyright (C) 1998-2004, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
7 * Created on Dec 3, 2003
9 *******************************************************************************
11 package com.ibm.icu.dev.tool.layout;
14 import java.io.PrintStream;
16 import com.ibm.icu.impl.Utility;
18 abstract class OpenTypeTableWriter
20 static class OpenTypeTableDumper
22 private short[] table;
23 private int tableLength;
25 OpenTypeTableDumper(short[] data, int outputIndex)
28 tableLength = outputIndex;
36 void appendValue(StringBuffer line, int index)
38 short value = table[index];
41 line.append(Utility.hex((value >> 8) & 0xFF, 2));
45 line.append(Utility.hex(value & 0xFF, 2));
48 void dumpTable(PrintStream output, int valuesPerLine) {
49 StringBuffer line = new StringBuffer(" "); // four spaces
50 int maxIndex = length();
52 for (int i = 0; i < maxIndex; i += 1) {
54 if (i > 0 && i % valuesPerLine == 0) {
55 output.println(line.toString());
63 line.setLength(line.length() - 2);
65 output.println(line.toString());
69 protected short[] data;
70 protected int outputIndex;
72 public OpenTypeTableWriter(int initialBufferSize)
74 data = new short[initialBufferSize];
78 public OpenTypeTableWriter()
83 public int getOutputIndex()
88 public void writeData(int value)
90 if (outputIndex >= data.length)
92 short[] newData = new short[data.length + 512];
94 System.arraycopy(data, 0, newData, 0, data.length);
99 data[outputIndex] = (short) value;
103 public void writeTag(String tag)
105 char[] charArray = {'\0', '\0', '\0', '\0'};
106 int max = Math.min(tag.length(), 4);
108 tag.getChars(0, max, charArray, 0);
110 writeData(((charArray[0] & 0xFF) << 8) + (charArray[1] & 0xFF));
111 writeData(((charArray[2] & 0xFF) << 8) + (charArray[3] & 0xFF));
114 public void fixOffset(int offset, int base)
116 // * 2 to convert from short to byte index
117 data[offset] = (short) ((outputIndex - base) * 2);
120 public void dumpTable(PrintStream output, int valuesPerLine)
122 OpenTypeTableDumper dumper = new OpenTypeTableDumper(data, outputIndex);
124 dumper.dumpTable(output, valuesPerLine);
127 abstract public void writeTable(PrintStream output);