1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. 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 package org.apache.fontbox.encoding; 18 19 import java.io.IOException; 20 21 import java.util.HashMap; 22 import java.util.Map; 23 24 /** 25 * This is an interface to a text encoder. 26 * 27 * @author Ben Litchfield 28 * @version $Revision: 1.1 $ 29 */ 30 public abstract class Encoding 31 { 32 33 34 /** 35 * This is a mapping from a character code to a character name. 36 */ 37 protected Map<Integer,String> codeToName = new HashMap<Integer,String>(); 38 /** 39 * This is a mapping from a character name to a character code. 40 */ 41 protected Map<String,Integer> nameToCode = new HashMap<String,Integer>(); 42 43 private static final Map<String,String> NAME_TO_CHARACTER = new HashMap<String,String>(); 44 private static final Map<String,String> CHARACTER_TO_NAME = new HashMap<String,String>(); 45 46 static 47 { 48 } 49 50 51 /** 52 * This will add a character encoding. 53 * 54 * @param code The character code that matches the character. 55 * @param name The name of the character. 56 */ 57 protected void addCharacterEncoding( int code, String name ) 58 { 59 codeToName.put( code, name ); 60 nameToCode.put( name, code ); 61 } 62 63 /** 64 * This will get the character code for the name. 65 * 66 * @param name The name of the character. 67 * 68 * @return The code for the character. 69 * 70 * @throws IOException If there is no character code for the name. 71 */ 72 public int getCode( String name ) throws IOException 73 { 74 Integer code = nameToCode.get( name ); 75 if( code == null ) 76 { 77 throw new IOException( "No character code for character name '" + name + "'" ); 78 } 79 return code.intValue(); 80 } 81 82 /** 83 * This will take a character code and get the name from the code. 84 * 85 * @param code The character code. 86 * 87 * @return The name of the character. 88 * 89 * @throws IOException If there is no name for the code. 90 */ 91 public String getName( int code ) throws IOException 92 { 93 String name = codeToName.get( code ); 94 if( name == null ) 95 { 96 //lets be forgiving for now 97 name = "space"; 98 //throw new IOException( getClass().getName() + 99 // ": No name for character code '" + code + "'" ); 100 } 101 return name; 102 } 103 104 /** 105 * This will take a character code and get the name from the code. 106 * 107 * @param c The character. 108 * 109 * @return The name of the character. 110 * 111 * @throws IOException If there is no name for the character. 112 */ 113 public String getNameFromCharacter( char c ) throws IOException 114 { 115 String name = CHARACTER_TO_NAME.get( c ); 116 if( name == null ) 117 { 118 throw new IOException( "No name for character '" + c + "'" ); 119 } 120 return name; 121 } 122 123 /** 124 * This will get the character from the code. 125 * 126 * @param code The character code. 127 * 128 * @return The printable character for the code. 129 * 130 * @throws IOException If there is not name for the character. 131 */ 132 public String getCharacter( int code ) throws IOException 133 { 134 return getCharacter( getName( code ) ); 135 } 136 137 /** 138 * This will get the character from the name. 139 * 140 * @param name The name of the character. 141 * 142 * @return The printable character for the code. 143 */ 144 public static String getCharacter( String name ) 145 { 146 String character = NAME_TO_CHARACTER.get( name ); 147 if( character == null ) 148 { 149 character = name; 150 } 151 return character; 152 } 153 }