1 |
769 |
jeremybenn |
/* GlyphLocator.java -- Locates outlines of TrueType glyphs.
|
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 |
|
|
package gnu.java.awt.font.opentype.truetype;
|
39 |
|
|
|
40 |
|
|
import java.awt.FontFormatException;
|
41 |
|
|
import java.nio.ByteBuffer;
|
42 |
|
|
import java.nio.CharBuffer;
|
43 |
|
|
import java.nio.IntBuffer;
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
/**
|
47 |
|
|
* Locates glyph outlines in a TrueType or OpenType <code>glyf</code>
|
48 |
|
|
* table.
|
49 |
|
|
*
|
50 |
|
|
* @see <a href=
|
51 |
|
|
* "http://partners.adobe.com/asn/tech/type/opentype/loca.html"
|
52 |
|
|
* >Adobe’s specification of the OpenType ‘loca’
|
53 |
|
|
* table</a>
|
54 |
|
|
*
|
55 |
|
|
* @author Sascha Brawer (brawer@dandelis.ch)
|
56 |
|
|
*/
|
57 |
|
|
abstract class GlyphLocator
|
58 |
|
|
{
|
59 |
|
|
/**
|
60 |
|
|
* The actual glyph data of the font, which is contained in the
|
61 |
|
|
* 'glyf' table.
|
62 |
|
|
*/
|
63 |
|
|
protected ByteBuffer glyfTable;
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
/**
|
67 |
|
|
* Creates a new GlyphLocator for a <code>loca</code> table.
|
68 |
|
|
*
|
69 |
|
|
* @param format the format of the <code>loca</code> table. The
|
70 |
|
|
* value must be 0 for two-byte offsets, or 1 for four-byte
|
71 |
|
|
* offsets. TrueType and OpenType fonts indicate the format in the
|
72 |
|
|
* <code>indexToLoc</code> field of the <a href=
|
73 |
|
|
* "http://partners.adobe.com/asn/tech/type/opentype/head.html"
|
74 |
|
|
* >font header</a>.
|
75 |
|
|
*
|
76 |
|
|
* @param loca the <code>loca</code> table of the font, which
|
77 |
|
|
* contains the position of each glyph in the <code>glyf</code>
|
78 |
|
|
* table.
|
79 |
|
|
*
|
80 |
|
|
* @param glyf the <code>glyf</code> table of the font, which
|
81 |
|
|
* contains the outline data of each glyph.
|
82 |
|
|
*
|
83 |
|
|
* @throws FontFormatException if <code>format</code> is neither 0
|
84 |
|
|
* nor 1.
|
85 |
|
|
*/
|
86 |
|
|
public static GlyphLocator forTable(int format, ByteBuffer loca,
|
87 |
|
|
ByteBuffer glyf)
|
88 |
|
|
throws FontFormatException
|
89 |
|
|
{
|
90 |
|
|
switch (format)
|
91 |
|
|
{
|
92 |
|
|
case 0:
|
93 |
|
|
return new GlyphLocator.TwoByte(loca, glyf);
|
94 |
|
|
|
95 |
|
|
case 1:
|
96 |
|
|
return new GlyphLocator.FourByte(loca, glyf);
|
97 |
|
|
|
98 |
|
|
default:
|
99 |
|
|
throw new FontFormatException("unsupported loca format");
|
100 |
|
|
}
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
|
104 |
|
|
/**
|
105 |
|
|
* Locates the outline data for a glyph.
|
106 |
|
|
*
|
107 |
|
|
* <p>For efficiency, the glyph locator does not create a new buffer
|
108 |
|
|
* for each invocation. Instead, this method always returns the same
|
109 |
|
|
* buffer object. Therefore, the data of a glyph must have been read
|
110 |
|
|
* completely before another glyph of the same font gets requested
|
111 |
|
|
* through this method.
|
112 |
|
|
*
|
113 |
|
|
* @param glyph the number of the glyph whose outlines are to be
|
114 |
|
|
* retrieved.
|
115 |
|
|
*
|
116 |
|
|
* @return a buffer whose position is set to the first byte of glyph
|
117 |
|
|
* data, and whose limit is set to disallow accessing any data that
|
118 |
|
|
* does not belong to the glyph. If there is no outline data for the
|
119 |
|
|
* requested glyph, as would be the case for the space glyph, the
|
120 |
|
|
* result will be <code>null</code>.
|
121 |
|
|
*/
|
122 |
|
|
public abstract ByteBuffer getGlyphData(int glyph);
|
123 |
|
|
|
124 |
|
|
|
125 |
|
|
/**
|
126 |
|
|
* A GlyphLocator that locates glyphs using two-byte offsets,
|
127 |
|
|
* interpreting <code>loca</code> tables of format 0.
|
128 |
|
|
*
|
129 |
|
|
* @author Sascha Brawer (brawer@dandelis.ch)
|
130 |
|
|
*/
|
131 |
|
|
private final static class TwoByte
|
132 |
|
|
extends GlyphLocator
|
133 |
|
|
{
|
134 |
|
|
final CharBuffer indexToLoc;
|
135 |
|
|
|
136 |
|
|
TwoByte(ByteBuffer loca, ByteBuffer glyf)
|
137 |
|
|
{
|
138 |
|
|
this.glyfTable = glyf;
|
139 |
|
|
indexToLoc = loca.asCharBuffer();
|
140 |
|
|
}
|
141 |
|
|
|
142 |
|
|
|
143 |
|
|
public ByteBuffer getGlyphData(int glyph)
|
144 |
|
|
{
|
145 |
|
|
int offset, limit;
|
146 |
|
|
offset = ((int) indexToLoc.get(glyph)) << 1;
|
147 |
|
|
limit = ((int) indexToLoc.get(glyph + 1)) << 1;
|
148 |
|
|
if (offset >= limit)
|
149 |
|
|
return null;
|
150 |
|
|
|
151 |
|
|
glyfTable.limit(limit).position(offset);
|
152 |
|
|
return glyfTable;
|
153 |
|
|
}
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
|
157 |
|
|
/**
|
158 |
|
|
* A GlyphLocator that locates glyphs using four-byte offsets,
|
159 |
|
|
* interpreting <code>loca</code> tables of format 1.
|
160 |
|
|
*
|
161 |
|
|
* @author Sascha Brawer (brawer@dandelis.ch)
|
162 |
|
|
*/
|
163 |
|
|
private final static class FourByte
|
164 |
|
|
extends GlyphLocator
|
165 |
|
|
{
|
166 |
|
|
final IntBuffer indexToLoc;
|
167 |
|
|
|
168 |
|
|
FourByte(ByteBuffer loca, ByteBuffer glyf)
|
169 |
|
|
{
|
170 |
|
|
this.glyfTable = glyf;
|
171 |
|
|
indexToLoc = loca.asIntBuffer();
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
public ByteBuffer getGlyphData(int glyph)
|
176 |
|
|
{
|
177 |
|
|
int offset, limit;
|
178 |
|
|
offset = indexToLoc.get(glyph);
|
179 |
|
|
limit = indexToLoc.get(glyph + 1);
|
180 |
|
|
if (offset >= limit)
|
181 |
|
|
return null;
|
182 |
|
|
|
183 |
|
|
glyfTable.limit(limit).position(offset);
|
184 |
|
|
return glyfTable;
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
}
|