KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > ttf > CMAPEncodingEntry


1 /**
2  * Copyright (c) 2004, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31 package org.pdfbox.ttf;
32
33 import java.io.IOException JavaDoc;
34
35 /**
36  * An encoding entry for a cmap.
37  *
38  * @author Ben Litchfield (ben@csh.rit.edu)
39  * @version $Revision: 1.1 $
40  */

41 public class CMAPEncodingEntry
42 {
43
44     private int platformId;
45     private int platformEncodingId;
46     private long subTableOffset;
47     private int[] glyphIdToCharacterCode;
48     /**
49      * This will read the required data from the stream.
50      *
51      * @param ttf The font that is being read.
52      * @param data The stream to read the data from.
53      * @throws IOException If there is an error reading the data.
54      */

55     public void initData( TrueTypeFont ttf, TTFDataStream data ) throws IOException JavaDoc
56     {
57         platformId = data.readUnsignedShort();
58         platformEncodingId = data.readUnsignedShort();
59         subTableOffset = data.readUnsignedInt();
60     }
61     
62     /**
63      * This will read the required data from the stream.
64      *
65      * @param ttf The font that is being read.
66      * @param data The stream to read the data from.
67      * @throws IOException If there is an error reading the data.
68      */

69     public void initSubtable( TrueTypeFont ttf, TTFDataStream data ) throws IOException JavaDoc
70     {
71         data.seek( ttf.getCMAP().getOffset() + subTableOffset );
72         int subtableFormat = data.readUnsignedShort();
73         int length = data.readUnsignedShort();
74         int version = data.readUnsignedShort();
75         int numGlyphs = ttf.getMaximumProfile().getNumGlyphs();
76         if( subtableFormat == 0 )
77         {
78             byte[] glyphMapping = data.read( 256 );
79             glyphIdToCharacterCode = new int[256];
80             for( int i=0;i<glyphMapping.length; i++ )
81             {
82                 glyphIdToCharacterCode[i]=(glyphMapping[i]+256)%256;
83             }
84         }
85         else if( subtableFormat == 2 )
86         {
87             int[] subHeaderKeys = new int[256];
88             for( int i=0; i<256; i++)
89             {
90                 subHeaderKeys[i] = data.readUnsignedShort();
91             }
92             int firstCode = data.readUnsignedShort();
93             int entryCount = data.readUnsignedShort();
94             short idDelta = data.readSignedShort();
95             int idRangeOffset = data.readUnsignedShort();
96             //BJL
97
//HMM the TTF spec is not very clear about what is suppose to
98
//happen here. If you know please submit a patch or point
99
//me to some better documentation.
100
throw new IOException JavaDoc( "Not yet implemented:" + subtableFormat );
101         }
102         else if( subtableFormat == 4 )
103         {
104             int segCountX2 = data.readUnsignedShort();
105             int segCount = segCountX2/2;
106             int searchRange = data.readUnsignedShort();
107             int entrySelector = data.readUnsignedShort();
108             int rangeShift = data.readUnsignedShort();
109             int[] endCount = data.readUnsignedShortArray( segCount );
110             int reservedPad = data.readUnsignedShort();
111             int[] startCount = data.readUnsignedShortArray( segCount );
112             int[] idDelta = data.readUnsignedShortArray( segCount );
113             int[] idRangeOffset = data.readUnsignedShortArray( segCount );
114             
115             //this is the final result
116
//key=glyphId, value is character codes
117
glyphIdToCharacterCode = new int[numGlyphs];
118             
119             long currentPosition = data.getCurrentPosition();
120             
121             for( int i=0; i<segCount; i++ )
122             {
123                 int start = startCount[i];
124                 int end = endCount[i];
125                 int delta = idDelta[i];
126                 int rangeOffset = idRangeOffset[i];
127                 if( start != 65536 && end != 65536 )
128                 {
129                     for( int j=start; j<=end; j++ )
130                     {
131                         if( rangeOffset == 0 )
132                         {
133                             glyphIdToCharacterCode[ ((j+delta)%65536) ]=j;
134                         }
135                         else
136                         {
137                             long glyphOffset = currentPosition +
138                                 ((rangeOffset/2) + //idRangeOffset[i]/2
139
(j-start) + //(c - startCount[i])
140
(i-segCount))*2; //&idRangeOffset[i]);
141
data.seek( glyphOffset );
142                             int glyphIndex = data.readUnsignedShort();
143                             if( glyphIndex != 0 )
144                             {
145                                 glyphIndex += delta;
146                                 glyphIndex = glyphIndex % 65536;
147                                 if( glyphIdToCharacterCode[glyphIndex] == 0 )
148                                 {
149                                     glyphIdToCharacterCode[glyphIndex] = j;
150                                 }
151                             }
152                             
153                         }
154                     }
155                 }
156             }
157         }
158         else if( subtableFormat == 6 )
159         {
160             int firstCode = data.readUnsignedShort();
161             int entryCount = data.readUnsignedShort();
162             glyphIdToCharacterCode = new int[numGlyphs];
163             int[] glyphIdArray = data.readUnsignedShortArray( entryCount );
164             for( int i=0; i<entryCount; i++)
165             {
166                 glyphIdToCharacterCode[glyphIdArray[i]] = firstCode+i;
167             }
168         }
169         else
170         {
171             throw new IOException JavaDoc( "Unknown cmap format:" + subtableFormat );
172         }
173     }
174     
175
176     /**
177      * @return Returns the glyphIdToCharacterCode.
178      */

179     public int[] getGlyphIdToCharacterCode()
180     {
181         return glyphIdToCharacterCode;
182     }
183     /**
184      * @param glyphIdToCharacterCodeValue The glyphIdToCharacterCode to set.
185      */

186     public void setGlyphIdToCharacterCode(int[] glyphIdToCharacterCodeValue)
187     {
188         this.glyphIdToCharacterCode = glyphIdToCharacterCodeValue;
189     }
190     
191     /**
192      * @return Returns the platformEncodingId.
193      */

194     public int getPlatformEncodingId()
195     {
196         return platformEncodingId;
197     }
198     /**
199      * @param platformEncodingIdValue The platformEncodingId to set.
200      */

201     public void setPlatformEncodingId(int platformEncodingIdValue)
202     {
203         this.platformEncodingId = platformEncodingIdValue;
204     }
205     /**
206      * @return Returns the platformId.
207      */

208     public int getPlatformId()
209     {
210         return platformId;
211     }
212     /**
213      * @param platformIdValue The platformId to set.
214      */

215     public void setPlatformId(int platformIdValue)
216     {
217         this.platformId = platformIdValue;
218     }
219 }
220
Popular Tags