1 /* MacResourceFork.java -- Parses MacOS resource forks. 2 Copyright (C) 2006 Free Software Foundation, Inc. 3 4 This file is part of GNU Classpath. 5 6 GNU Classpath is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 2, or (at your option) 9 any later version. 10 11 GNU Classpath is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with GNU Classpath; see the file COPYING. If not, write to the 18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 19 02110-1301 USA. 20 21 Linking this library statically or dynamically with other modules is 22 making a combined work based on this library. Thus, the terms and 23 conditions of the GNU General Public License cover the whole 24 combination. 25 26 As a special exception, the copyright holders of this library give you 27 permission to link this library with independent modules to produce an 28 executable, regardless of the license terms of these independent 29 modules, and to copy and distribute the resulting executable under 30 terms of your choice, provided that you also meet, for each linked 31 independent module, the terms and conditions of the license of that 32 module. An independent module is a module which is not derived from 33 or based on this library. If you modify this library, you may extend 34 this exception to your version of the library, but you are not 35 obligated to do so. If you do not wish to do so, delete this 36 exception statement from your version. */ 37 38 39 package gnu.java.awt.font.opentype; 40 41 import java.nio.ByteBuffer; 42 43 44 /** 45 * A class for accessing data that is stored in the resource fork of 46 * Macintosh files. Writing resource forks is currently not supported. 47 * 48 * <p>The gnu.java.awt.font package uses this class for accessing 49 * fonts in the MacOS X ".dfont" format, which is is a file in the 50 * format of a Macintosh resource fork, but stored in the normal data 51 * fork of the file. 52 * 53 * <p>The implementation has been designed to work efficiently with 54 * the virtual memory subsystem. It is recommended to pass an 55 * instance of {@link java.nio.MappedByteBuffer} to the constructor. 56 * 57 * <p>Thread Safety: All access is synchronized on the ByteBuffer 58 * that is passed to the constructor. 59 * 60 * @see <a href= 61 * "http://developer.apple.com/documentation/mac/MoreToolbox/MoreToolbox-99.html" 62 * >Apple’ developer documentation about the Resource File 63 * Format</a> 64 * 65 * @author Sascha Brawer (brawer@dandelis.ch) 66 */ 67 final class MacResourceFork 68 { 69 int[] types; 70 Resource[][] resources; 71 ByteBuffer buf; 72 MacResourceFork(ByteBuffer buf)73 public MacResourceFork(ByteBuffer buf) 74 { 75 int typeListOffset; 76 int refListOffset; 77 int nameListOffset; 78 int mapOffset, mapLen; 79 int dataOffset, dataLen; 80 int numTypes; 81 82 synchronized (buf) 83 { 84 buf = buf.duplicate(); 85 this.buf = buf; 86 buf.position(0); 87 dataOffset = buf.getInt(); 88 mapOffset = buf.getInt(); 89 dataLen = buf.getInt(); 90 mapLen = buf.getInt(); 91 buf.position(mapOffset + 24); 92 refListOffset = mapOffset + buf.getChar(); 93 nameListOffset = mapOffset + buf.getChar(); 94 numTypes = buf.getChar() + 1; 95 types = new int[numTypes]; 96 resources = new Resource[numTypes][]; 97 98 /* Parse resource type list. */ 99 typeListOffset = buf.position(); 100 for (int i = 0; i < numTypes; i++) 101 { 102 buf.position(typeListOffset + 8 * i); 103 int resType = buf.getInt(); 104 int numRes = buf.getChar() + 1; 105 106 types[i] = resType; 107 resources[i] = new Resource[numRes]; 108 109 buf.position(refListOffset + buf.getChar()); 110 for (int j = 0; j < numRes; j++) 111 { 112 short resID = buf.getShort(); 113 int resNameOffset = nameListOffset + buf.getChar(); 114 int resDataOffset = buf.getInt(); 115 byte resAttr = (byte) (resDataOffset >> 24); 116 resDataOffset = dataOffset + (resDataOffset & 0x00ffffff); 117 buf.getInt(); /* skip four reserved bytes */ 118 119 Resource rsrc = new Resource(buf, resType, resID, resDataOffset, 120 resNameOffset); 121 resources[i][j] = rsrc; 122 } 123 } 124 } 125 } 126 127 getResources(int type)128 public Resource[] getResources(int type) 129 { 130 synchronized (buf) 131 { 132 for (int i = 0; i < types.length; i++) 133 { 134 if (types[i] == type) 135 return resources[i]; 136 } 137 } 138 return null; 139 } 140 141 getResource(int type, short id)142 public Resource getResource(int type, short id) 143 { 144 Resource[] res; 145 146 synchronized (buf) 147 { 148 for (int i = 0; i < types.length; i++) 149 { 150 if (types[i] != type) 151 continue; 152 153 res = resources[i]; 154 for (int j = 0; j < res.length; j++) 155 if (res[j].getID() == id) 156 return res[j]; 157 } 158 } 159 160 return null; 161 } 162 163 164 /** 165 * A single resource that is contained in a resource fork. 166 */ 167 public static final class Resource 168 { 169 int type; 170 short id; 171 byte attribute; 172 int nameOffset; 173 int dataOffset; 174 ByteBuffer buf; 175 Resource(ByteBuffer buf, int type, short id, int dataOffset, int nameOffset)176 private Resource(ByteBuffer buf, 177 int type, short id, int dataOffset, int nameOffset) 178 { 179 this.buf = buf; 180 this.type = type; 181 this.id = id; 182 this.dataOffset = dataOffset; 183 this.nameOffset = nameOffset; 184 } 185 186 187 /** 188 * Returns the type of this resource. 189 * 190 * @return an <code>int</code> encoding a four-byte type tag, 191 * such as <code>0x464f4e54</code> for <code>'FONT'</code>. 192 */ getType()193 public int getType() 194 { 195 return type; 196 } 197 198 199 /** 200 * Returns the ID of this resource. 201 */ getID()202 public short getID() 203 { 204 return id; 205 } 206 207 208 /** 209 * Retrieves the content of the resource. Only one page of memory 210 * is touched, irrespective of the actual size of the resource. 211 */ getContent()212 public ByteBuffer getContent() 213 { 214 synchronized (buf) 215 { 216 buf.limit(buf.capacity()); 217 int len = buf.getInt(dataOffset); 218 buf.position(dataOffset + 4).limit(dataOffset + 4 + len); 219 return buf.slice(); 220 } 221 } 222 223 224 /** 225 * Determines the length of the resource in bytes. 226 */ getLength()227 public int getLength() 228 { 229 synchronized (buf) 230 { 231 return buf.getInt(dataOffset); 232 } 233 } 234 } 235 } 236