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