1 /*
2  *******************************************************************************
3  * Copyright (C) 1998-2004, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  *
7  * Created on Dec 3, 2003
8  *
9  *******************************************************************************
10  */
11 package com.ibm.icu.dev.tool.layout;
12 
13 
14 import java.io.PrintStream;
15 
16 import com.ibm.icu.impl.Utility;
17 
18 abstract class OpenTypeTableWriter
19 {
20     static class OpenTypeTableDumper
21     {
22         private short[] table;
23         private int tableLength;
24 
OpenTypeTableDumper(short[] data, int outputIndex)25         OpenTypeTableDumper(short[] data, int outputIndex)
26         {
27             table = data;
28             tableLength = outputIndex;
29         }
30 
length()31         int length()
32         {
33             return tableLength;
34         }
35 
appendValue(StringBuffer line, int index)36         void appendValue(StringBuffer line, int index)
37         {
38             short value = table[index];
39 
40             line.append("0x");
41             line.append(Utility.hex((value >> 8) & 0xFF, 2));
42             line.append(", ");
43 
44             line.append("0x");
45             line.append(Utility.hex(value & 0xFF, 2));
46         }
47 
dumpTable(PrintStream output, int valuesPerLine)48         void dumpTable(PrintStream output, int valuesPerLine) {
49             StringBuffer line = new StringBuffer("    "); // four spaces
50             int maxIndex = length();
51 
52             for (int i = 0; i < maxIndex; i += 1) {
53 
54                 if (i > 0 && i % valuesPerLine == 0) {
55                     output.println(line.toString());
56                     line.setLength(4);
57                 }
58 
59                 appendValue(line, i);
60                 line.append(", ");
61             }
62 
63             line.setLength(line.length() - 2);
64 
65             output.println(line.toString());
66         }
67     }
68 
69     protected short[] data;
70     protected int outputIndex;
71 
OpenTypeTableWriter(int initialBufferSize)72     public OpenTypeTableWriter(int initialBufferSize)
73     {
74         data = new short[initialBufferSize];
75         outputIndex = 0;
76     }
77 
OpenTypeTableWriter()78     public OpenTypeTableWriter()
79     {
80         this(1024);
81     }
82 
getOutputIndex()83     public int getOutputIndex()
84     {
85         return outputIndex;
86     }
87 
writeData(int value)88     public void writeData(int value)
89     {
90         if (outputIndex >= data.length)
91         {
92             short[] newData = new short[data.length + 512];
93 
94             System.arraycopy(data, 0, newData, 0, data.length);
95 
96             data = newData;
97         }
98 
99         data[outputIndex] = (short) value;
100         outputIndex += 1;
101     }
102 
writeTag(String tag)103     public void writeTag(String tag)
104     {
105         char[] charArray = {'\0', '\0', '\0', '\0'};
106         int max = Math.min(tag.length(), 4);
107 
108         tag.getChars(0, max, charArray, 0);
109 
110         writeData(((charArray[0] & 0xFF) << 8) + (charArray[1] & 0xFF));
111         writeData(((charArray[2] & 0xFF) << 8) + (charArray[3] & 0xFF));
112     }
113 
fixOffset(int offset, int base)114     public void fixOffset(int offset, int base)
115     {
116         // * 2 to convert from short to byte index
117         data[offset] = (short) ((outputIndex - base) * 2);
118     }
119 
dumpTable(PrintStream output, int valuesPerLine)120     public void dumpTable(PrintStream output, int valuesPerLine)
121     {
122         OpenTypeTableDumper dumper = new OpenTypeTableDumper(data, outputIndex);
123 
124         dumper.dumpTable(output, valuesPerLine);
125     }
126 
writeTable(PrintStream output)127     abstract public void writeTable(PrintStream output);
128 }
129