2 *******************************************************************************
3 * Copyright (C) 1998-2010, 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.IOException;
15 import java.io.PrintWriter;
16 import java.io.Writer;
18 import com.ibm.icu.impl.Utility;
19 import com.ibm.icu.text.UTF16;
21 public class LigatureTree
27 Lignode[] subnodes = null;
41 return ligature != -1;
49 Lignode subnode(int c)
51 if (subnodes != null) {
52 int len = subnodes.length;
54 if (c <= subnodes[len - 1].target) {
55 for (int i = 0; i < len; i+= 1) {
56 int t = subnodes[i].target;
72 String ligatureString(int[] chars)
74 StringBuffer result = new StringBuffer();
75 int len = chars.length - 1;
77 for (int i = 0; i < len; i += 1) {
82 result.append(Utility.hex(chars[i], 6));
85 result.append(" => " + Utility.hex(chars[len], 6));
87 return result.toString();
90 void insert(int[] chars, int index)
93 int len = chars.length;
95 if (len == index + 1) {
97 System.out.println("ignoring ligature " + ligatureString(chars) +
98 ": already have " + Utility.hex(ligature, 6));
106 if (subnodes == null) {
107 subnodes = new Lignode[1];
108 subnodes[0] = new Lignode(c);
109 subnodes[0].insert(chars, index + 1);
113 for (i = 0; i < subnodes.length; i += 1)
115 int t = subnodes[i].target;
118 subnodes[i].insert(chars, index + 1);
125 Lignode[] nnodes = new Lignode[subnodes.length + 1];
128 System.arraycopy(subnodes, 0, nnodes, 0, i);
131 nnodes[i] = new Lignode(c);
133 if (i < subnodes.length) {
134 System.arraycopy(subnodes, i, nnodes, i + 1, subnodes.length - i);
139 subnodes[i].insert(chars, index + 1);
143 public void walk(TreeWalker walker)
149 if (subnodes != null) {
150 for (int i = 0; i < subnodes.length; i += 1)
152 subnodes[i].walk(walker);
156 if (ligature != -1) {
157 walker.ligature(ligature);
163 static final String ind = " ";
166 * Write debugging information to w, starting at the provided indentation level.
168 public void dump(Writer w, int indent)
170 String tab = ind.substring(0, Math.min(indent, ind.length()));
175 w.write(Utility.hex(target, 6));
181 w.write(Utility.hex(ligature, 6));
186 if (subnodes != null) {
191 for (int i = 0; i < subnodes.length; i += 1) {
192 subnodes[i].dump(w, indent);
198 } catch (IOException e) {
199 System.out.println(e);
205 private Lignode root = new Lignode();
207 public LigatureTree()
212 private int[] toIntArray(String s)
214 int count = UTF16.countCodePoint(s);
215 int[] result = new int[count];
217 for (int i = 0; i < count; i += 1) {
218 result[i] = UTF16.charAt(s, i);
224 public void insert(String string)
226 root.insert(toIntArray(string), 0);
229 public void insert(int[] chars)
231 root.insert(chars, 0);
234 public void walk(TreeWalker walker)
242 PrintWriter pw = new PrintWriter(System.out);