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.cff.encoding; 18 19 import java.util.ArrayList; 20 import java.util.List; 21 22 /** 23 * This is the superclass for all CFFFont encodings. 24 * 25 * @author Villu Ruusmann 26 * @version $Revision$ 27 */ 28 public abstract class CFFEncoding 29 { 30 31 private List<Entry> entries = new ArrayList<Entry>(); 32 33 /** 34 * Determines if the encoding is font specific or not. 35 * @return if the encoding is font specific 36 */ 37 public boolean isFontSpecific() 38 { 39 return false; 40 } 41 42 /** 43 * Returns the code corresponding to the given SID. 44 * @param sid the given SID 45 * @return the corresponding code 46 */ 47 public int getCode(int sid) 48 { 49 for(Entry entry : entries) 50 { 51 if(entry.entrySID == sid) 52 { 53 return entry.entryCode; 54 } 55 } 56 return -1; 57 } 58 59 /** 60 * Returns the SID corresponding to the given code. 61 * @param code the given code 62 * @return the corresponding SID 63 */ 64 public int getSID(int code) 65 { 66 for(Entry entry : entries) 67 { 68 if(entry.entryCode == code) 69 { 70 return entry.entrySID; 71 } 72 } 73 return -1; 74 } 75 76 /** 77 * Adds a new code/SID combination to the encoding. 78 * @param code the given code 79 * @param sid the given SID 80 */ 81 public void register(int code, int sid) 82 { 83 entries.add(new Entry(code, sid)); 84 } 85 86 /** 87 * A list of all entries within this encoding. 88 * @return a list of all entries 89 */ 90 public List<Entry> getEntries() 91 { 92 return entries; 93 } 94 95 /** 96 * This class represents a single code/SID mapping of the encoding. 97 * 98 */ 99 public static class Entry 100 { 101 private int entryCode; 102 private int entrySID; 103 104 /** 105 * Create a new instance of Entry with the given values. 106 * @param code the code 107 * @param sid the SID 108 */ 109 protected Entry(int code, int sid) 110 { 111 this.entryCode = code; 112 this.entrySID = sid; 113 } 114 115 /** 116 * The code of the entry. 117 * @return the code 118 */ 119 public int getCode() 120 { 121 return this.entryCode; 122 } 123 124 /** 125 * The SID of the entry. 126 * @return the SID 127 */ 128 public int getSID() 129 { 130 return this.entrySID; 131 } 132 133 /** 134 * {@inheritDoc} 135 */ 136 public String toString() 137 { 138 return "[code=" + entryCode + ", sid=" + entrySID + "]"; 139 } 140 } 141 }