Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / awt / font / opentype / MacResourceFork.java
blob8115e0403ef441e8a227ff43237eb78d15e60176
1 /* MacResourceFork.java -- Parses MacOS resource forks.
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
39 package gnu.java.awt.font.opentype;
41 import java.nio.ByteBuffer;
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.
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.
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.
57 * <p>Thread Safety: All access is synchronized on the ByteBuffer
58 * that is passed to the constructor.
60 * @see <a href=
61 * "http://developer.apple.com/documentation/mac/MoreToolbox/MoreToolbox-99.html"
62 * >Apple&#x2019; developer documentation about the Resource File
63 * Format</a>
65 * @author Sascha Brawer (brawer@dandelis.ch)
67 final class MacResourceFork
69 int[] types;
70 Resource[][] resources;
71 ByteBuffer buf;
73 public MacResourceFork(ByteBuffer buf)
75 int typeListOffset;
76 int refListOffset;
77 int nameListOffset;
78 int mapOffset, mapLen;
79 int dataOffset, dataLen;
80 int numTypes;
82 synchronized (buf)
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][];
98 /* Parse resource type list. */
99 typeListOffset = buf.position();
100 for (int i = 0; i < numTypes; i++)
102 buf.position(typeListOffset + 8 * i);
103 int resType = buf.getInt();
104 int numRes = buf.getChar() + 1;
106 types[i] = resType;
107 resources[i] = new Resource[numRes];
109 buf.position(refListOffset + buf.getChar());
110 for (int j = 0; j < numRes; j++)
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 */
119 Resource rsrc = new Resource(buf, resType, resID, resDataOffset,
120 resNameOffset);
121 resources[i][j] = rsrc;
128 public Resource[] getResources(int type)
130 synchronized (buf)
132 for (int i = 0; i < types.length; i++)
134 if (types[i] == type)
135 return resources[i];
138 return null;
142 public Resource getResource(int type, short id)
144 Resource[] res;
146 synchronized (buf)
148 for (int i = 0; i < types.length; i++)
150 if (types[i] != type)
151 continue;
153 res = resources[i];
154 for (int j = 0; j < res.length; j++)
155 if (res[j].getID() == id)
156 return res[j];
160 return null;
165 * A single resource that is contained in a resource fork.
167 public static final class Resource
169 int type;
170 short id;
171 byte attribute;
172 int nameOffset;
173 int dataOffset;
174 ByteBuffer buf;
176 private Resource(ByteBuffer buf,
177 int type, short id, int dataOffset, int nameOffset)
179 this.buf = buf;
180 this.type = type;
181 this.id = id;
182 this.dataOffset = dataOffset;
183 this.nameOffset = nameOffset;
188 * Returns the type of this resource.
190 * @return an <code>int</code> encoding a four-byte type tag,
191 * such as <code>0x464f4e54</code> for <code>'FONT'</code>.
193 public int getType()
195 return type;
200 * Returns the ID of this resource.
202 public short getID()
204 return id;
209 * Retrieves the content of the resource. Only one page of memory
210 * is touched, irrespective of the actual size of the resource.
212 public ByteBuffer getContent()
214 synchronized (buf)
216 buf.limit(buf.capacity());
217 int len = buf.getInt(dataOffset);
218 buf.position(dataOffset + 4).limit(dataOffset + 4 + len);
219 return buf.slice();
225 * Determines the length of the resource in bytes.
227 public int getLength()
229 synchronized (buf)
231 return buf.getInt(dataOffset);