1 31 package org.pdfbox.encoding; 32 33 import java.io.BufferedReader ; 34 import java.io.InputStream ; 35 import java.io.InputStreamReader ; 36 import java.io.IOException ; 37 38 import java.util.HashMap ; 39 import java.util.Iterator ; 40 import java.util.Map ; 41 import java.util.StringTokenizer ; 42 43 import org.pdfbox.cos.COSName; 44 45 import org.pdfbox.util.ResourceLoader; 46 47 import org.pdfbox.pdmodel.common.COSObjectable; 48 49 55 public abstract class Encoding implements COSObjectable 56 { 57 58 59 62 protected Map codeToName = new HashMap (); 63 66 protected Map nameToCode = new HashMap (); 67 68 private static final Map NAME_TO_CHARACTER = new HashMap (); 69 private static final Map CHARACTER_TO_NAME = new HashMap (); 70 71 static 72 { 73 BufferedReader glyphStream = null; 74 try 75 { 76 InputStream resource = ResourceLoader.loadResource( "Resources/glyphlist.txt" ); 77 glyphStream = new BufferedReader ( new InputStreamReader ( resource ) ); 78 String line = null; 79 while( (line = glyphStream.readLine()) != null ) 80 { 81 line = line.trim(); 82 if( !line.startsWith("#" ) ) 84 { 85 int semicolonIndex = line.indexOf( ';' ); 86 if( semicolonIndex >= 0 ) 87 { 88 try 89 { 90 String characterName = line.substring( 0, semicolonIndex ); 91 String unicodeValue = line.substring( semicolonIndex+1, line.length() ); 92 StringTokenizer tokenizer = new StringTokenizer ( unicodeValue, " ", false ); 93 String value = ""; 94 while(tokenizer.hasMoreTokens()) 95 { 96 int characterCode = Integer.parseInt( tokenizer.nextToken(), 16 ); 97 value += (char)characterCode; 98 } 99 100 NAME_TO_CHARACTER.put( COSName.getPDFName( characterName ), value ); 101 } 102 catch( NumberFormatException nfe ) 103 { 104 nfe.printStackTrace(); 105 } 106 } 107 } 108 } 109 } 110 catch( IOException io ) 111 { 112 io.printStackTrace(); 113 } 114 finally 115 { 116 if( glyphStream != null ) 117 { 118 try 119 { 120 glyphStream.close(); 121 } 122 catch( IOException e ) 123 { 124 e.printStackTrace(); 125 } 126 127 } 128 } 129 130 131 NAME_TO_CHARACTER.put( COSName.getPDFName( ".notdef" ), "" ); 132 NAME_TO_CHARACTER.put( COSName.getPDFName( "fi" ), "fi" ); 133 NAME_TO_CHARACTER.put( COSName.getPDFName( "fl" ), "fl" ); 134 NAME_TO_CHARACTER.put( COSName.getPDFName( "ffi" ), "ffi" ); 135 NAME_TO_CHARACTER.put( COSName.getPDFName( "ff" ), "ff" ); 136 NAME_TO_CHARACTER.put( COSName.getPDFName( "pi" ), "pi" ); 137 138 Iterator keys = NAME_TO_CHARACTER.keySet().iterator(); 139 while( keys.hasNext() ) 140 { 141 Object key = keys.next(); 142 Object value = NAME_TO_CHARACTER.get( key ); 143 CHARACTER_TO_NAME.put( value, key ); 144 } 145 } 146 147 148 154 protected void addCharacterEncoding( int code, COSName name ) 155 { 156 Integer intCode = new Integer ( code ); 157 codeToName.put( intCode, name ); 158 nameToCode.put( name, intCode ); 159 } 160 161 170 public int getCode( COSName name ) throws IOException 171 { 172 Integer code = (Integer )nameToCode.get( name ); 173 if( code == null ) 174 { 175 throw new IOException ( "No character code for character name '" + name.getName() + "'" ); 176 } 177 return code.intValue(); 178 } 179 180 189 public COSName getName( int code ) throws IOException 190 { 191 COSName name = (COSName)codeToName.get( new Integer ( code ) ); 192 if( name == null ) 193 { 194 name = COSName.getPDFName( "space" ); 196 } 199 return name; 200 } 201 202 211 public COSName getNameFromCharacter( char c ) throws IOException 212 { 213 COSName name = (COSName)CHARACTER_TO_NAME.get( "" + c ); 214 if( name == null ) 215 { 216 throw new IOException ( "No name for character '" + c + "'" ); 217 } 218 return name; 219 } 220 221 230 public String getCharacter( int code ) throws IOException 231 { 232 String character = getCharacter( getName( code ) ); 233 return character; 234 } 235 236 243 public static String getCharacter( COSName name ) 244 { 245 String character = (String )NAME_TO_CHARACTER.get( name ); 246 if( character == null ) 247 { 248 character = name.getName(); 249 } 250 return character; 251 } 252 } | Popular Tags |