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