KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > font > table > CmapFormat4


1 /*
2
3    Copyright 2001,2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.svggen.font.table;
19
20 import java.io.IOException JavaDoc;
21 import java.io.RandomAccessFile JavaDoc;
22
23 /**
24  * @version $Id: CmapFormat4.java,v 1.6 2004/09/01 09:35:23 deweese Exp $
25  * @author <a HREF="mailto:david@steadystate.co.uk">David Schweinsberg</a>
26  */

27 public class CmapFormat4 extends CmapFormat {
28
29     public int language;
30     private int segCountX2;
31     private int searchRange;
32     private int entrySelector;
33     private int rangeShift;
34     private int[] endCode;
35     private int[] startCode;
36     private int[] idDelta;
37     private int[] idRangeOffset;
38     private int[] glyphIdArray;
39     private int segCount;
40     private int first, last;
41
42     protected CmapFormat4(RandomAccessFile JavaDoc raf) throws IOException JavaDoc {
43         super(raf);
44         format = 4;
45         segCountX2 = raf.readUnsignedShort();
46         segCount = segCountX2 / 2;
47         endCode = new int[segCount];
48         startCode = new int[segCount];
49         idDelta = new int[segCount];
50         idRangeOffset = new int[segCount];
51         searchRange = raf.readUnsignedShort();
52         entrySelector = raf.readUnsignedShort();
53         rangeShift = raf.readUnsignedShort();
54         last = -1;
55         for (int i = 0; i < segCount; i++) {
56             endCode[i] = raf.readUnsignedShort();
57             if (endCode[i] > last) last = endCode[i];
58         }
59         raf.readUnsignedShort(); // reservePad
60
for (int i = 0; i < segCount; i++) {
61             startCode[i] = raf.readUnsignedShort();
62             if ((i==0 ) || (startCode[i] < first)) first = startCode[i];
63         }
64         for (int i = 0; i < segCount; i++) {
65             idDelta[i] = raf.readUnsignedShort();
66         }
67         for (int i = 0; i < segCount; i++) {
68             idRangeOffset[i] = raf.readUnsignedShort();
69         }
70
71         // Whatever remains of this header belongs in glyphIdArray
72
int count = (length - 16 - (segCount*8)) / 2;
73         glyphIdArray = new int[count];
74         for (int i = 0; i < count; i++) {
75             glyphIdArray[i] = raf.readUnsignedShort();
76         }
77     }
78
79     public int getFirst() { return first; }
80     public int getLast() { return last; }
81
82     public int mapCharCode(int charCode) {
83         try {
84             /*
85               Quoting :
86               http://developer.apple.com/fonts/TTRefMan/RM06/Chap6cmap.html#Surrogates
87                
88               The original architecture of the Unicode Standard
89               allowed for all encoded characters to be represented
90               using sixteen bit code points. This allowed for up to
91               65,354 characters to be encoded. (Unicode code points
92               U+FFFE and U+FFFF are reserved and unavailable to
93               represent characters. For more details, see The Unicode
94               Standard.)
95                
96               My comment : Isn't there a typo here ? Shouldn't we
97               rather read 65,534 ?
98               */

99             if ((charCode < 0) || (charCode >= 0xFFFE))
100                 return 0;
101
102             for (int i = 0; i < segCount; i++) {
103                 if (endCode[i] >= charCode) {
104                     if (startCode[i] <= charCode) {
105                         if (idRangeOffset[i] > 0) {
106                             return glyphIdArray[idRangeOffset[i]/2 +
107                                                 (charCode - startCode[i]) -
108                                                 (segCount - i)];
109                         } else {
110                             return (idDelta[i] + charCode) % 65536;
111                         }
112                     } else {
113                         break;
114                     }
115                 }
116             }
117         } catch (ArrayIndexOutOfBoundsException JavaDoc e) {
118             System.err.println("error: Array out of bounds - " + e.getMessage());
119         }
120         return 0;
121     }
122
123     public String JavaDoc toString() {
124         return new StringBuffer JavaDoc()
125         .append(super.toString())
126         .append(", segCountX2: ")
127         .append(segCountX2)
128         .append(", searchRange: ")
129         .append(searchRange)
130         .append(", entrySelector: ")
131         .append(entrySelector)
132         .append(", rangeShift: ")
133         .append(rangeShift)
134         .append(", endCode: ")
135         .append(endCode)
136         .append(", startCode: ")
137         .append(endCode)
138         .append(", idDelta: ")
139         .append(idDelta)
140         .append(", idRangeOffset: ")
141         .append(idRangeOffset).toString();
142     }
143 }
144
Popular Tags