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