1 31 package org.pdfbox.cmaptypes; 32 33 import java.io.IOException ; 34 35 import java.util.ArrayList ; 36 import java.util.HashMap ; 37 import java.util.List ; 38 import java.util.Map ; 39 40 46 public class CMap 47 { 48 private List codeSpaceRanges = new ArrayList (); 49 private Map singleByteMappings = new HashMap (); 50 private Map doubleByteMappings = new HashMap (); 51 52 55 public CMap() 56 { 57 } 59 60 65 public boolean hasOneByteMappings() 66 { 67 return singleByteMappings.size() > 0; 68 } 69 70 75 public boolean hasTwoByteMappings() 76 { 77 return doubleByteMappings.size() > 0; 78 } 79 80 89 public String lookup( byte[] code, int offset, int length ) 90 { 91 92 String result = null; 93 Integer key = null; 94 if( length == 1 ) 95 { 96 97 key = new Integer ( (code[offset]+256)%256 ); 98 result = (String )singleByteMappings.get( key ); 99 } 100 else if( length == 2 ) 101 { 102 int intKey = (code[offset]+256)%256; 103 intKey <<= 8; 104 intKey += (code[offset+1]+256)%256; 105 key = new Integer ( intKey ); 106 107 result = (String )doubleByteMappings.get( key ); 108 } 109 110 return result; 111 } 112 113 121 public void addMapping( byte[] src, String dest ) throws IOException 122 { 123 if( src.length == 1 ) 124 { 125 singleByteMappings.put( new Integer ( src[0] ), dest ); 126 } 127 else if( src.length == 2 ) 128 { 129 int intSrc = src[0]; 130 intSrc <<= 8; 131 intSrc |= (src[1]&0xFF); 132 doubleByteMappings.put( new Integer ( intSrc ), dest ); 133 } 134 else 135 { 136 throw new IOException ( "Mapping code should be 1 or two bytes and not " + src.length ); 137 } 138 } 139 140 141 146 public void addCodespaceRange( CodespaceRange range ) 147 { 148 codeSpaceRanges.add( range ); 149 } 150 151 156 public List getCodeSpaceRanges() 157 { 158 return codeSpaceRanges; 159 } 160 161 } | Popular Tags |