1 |
769 |
jeremybenn |
/* GlyphMeasurer.java -- Helper for measuring 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 |
|
|
|
39 |
|
|
package gnu.java.awt.font.opentype.truetype;
|
40 |
|
|
|
41 |
|
|
import java.awt.FontFormatException;
|
42 |
|
|
import java.nio.ByteBuffer;
|
43 |
|
|
import java.nio.ShortBuffer;
|
44 |
|
|
|
45 |
|
|
|
46 |
|
|
/**
|
47 |
|
|
* A class for measuring TrueType and OpenType glyphs.
|
48 |
|
|
*
|
49 |
|
|
* <p><b>Lack of Thread Safety:</b> Glyph measurers are intentionally
|
50 |
|
|
* <i>not</i> safe to access from multiple concurrent
|
51 |
|
|
* threads. Synchronization needs to be performed externally. Usually,
|
52 |
|
|
* the font has already obtained a lock before calling the scaler,
|
53 |
|
|
* which in turn calls the GlyphMeasurer. It would thus be wasteful to
|
54 |
|
|
* acquire additional locks for the GlyphMeasurer.
|
55 |
|
|
*
|
56 |
|
|
* @author Sascha Brawer (brawer@dandelis.ch)
|
57 |
|
|
*/
|
58 |
|
|
final class GlyphMeasurer
|
59 |
|
|
{
|
60 |
|
|
/**
|
61 |
|
|
* A view buffer that allows accessing the contents of the
|
62 |
|
|
* font’s <code>hmtx</code> table as shorts.
|
63 |
|
|
*/
|
64 |
|
|
private final ShortBuffer horizontalGlyphMetrics;
|
65 |
|
|
|
66 |
|
|
|
67 |
|
|
/**
|
68 |
|
|
* A view buffer that allows accessing the contents of the
|
69 |
|
|
* font’s <code>vmtx</code> table as shorts.
|
70 |
|
|
*/
|
71 |
|
|
private final ShortBuffer verticalGlyphMetrics;
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
private final int numLongHorizontalMetricsEntries;
|
75 |
|
|
private final int numLongVerticalMetricsEntries;
|
76 |
|
|
|
77 |
|
|
private final int horizontalAscent;
|
78 |
|
|
private final int verticalAscent;
|
79 |
|
|
|
80 |
|
|
private final int horizontalDescent;
|
81 |
|
|
private final int verticalDescent;
|
82 |
|
|
|
83 |
|
|
private final int horizontalLineGap;
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
/**
|
87 |
|
|
* Constructs a GlyphMeasurer from TrueType/OpenType font tables.
|
88 |
|
|
*
|
89 |
|
|
* @param hhea the <code>hhea</code> table, which contains
|
90 |
|
|
* information about horizontal metrics that is common to all
|
91 |
|
|
* glyphs.
|
92 |
|
|
*
|
93 |
|
|
* @param hmtx the <code>hmtx</code> table, which contains
|
94 |
|
|
* glyph-specific information about horizontal metrics.
|
95 |
|
|
*
|
96 |
|
|
* @param vhea the <code>vhea</code> table, which contains
|
97 |
|
|
* information about vertical metrics that is common to all
|
98 |
|
|
* glyphs. If a font does not provide such a table, pass
|
99 |
|
|
* <code>null</code>.
|
100 |
|
|
*
|
101 |
|
|
* @param vmtx the <code>vmtx</code> table, which contains
|
102 |
|
|
* glyph-specific information about vertical metrics. If a font
|
103 |
|
|
* does not provide such a table, pass <code>null</code>.
|
104 |
|
|
*/
|
105 |
|
|
GlyphMeasurer(ByteBuffer hhea, ByteBuffer hmtx,
|
106 |
|
|
ByteBuffer vhea, ByteBuffer vmtx)
|
107 |
|
|
throws FontFormatException
|
108 |
|
|
{
|
109 |
|
|
if ((hhea.getInt(0) != 0x00010000) || (hhea.getInt(30) != 0))
|
110 |
|
|
throw new FontFormatException("unsupported hhea format");
|
111 |
|
|
|
112 |
|
|
horizontalAscent = hhea.getShort(4);
|
113 |
|
|
horizontalDescent = hhea.getShort(6);
|
114 |
|
|
horizontalLineGap = hhea.getShort(8);
|
115 |
|
|
|
116 |
|
|
numLongHorizontalMetricsEntries = hhea.getChar(34);
|
117 |
|
|
horizontalGlyphMetrics = hmtx.asShortBuffer();
|
118 |
|
|
|
119 |
|
|
if (vhea != null)
|
120 |
|
|
{
|
121 |
|
|
verticalAscent = vhea.getShort(4);
|
122 |
|
|
verticalDescent = vhea.getShort(6);
|
123 |
|
|
numLongVerticalMetricsEntries = vhea.getChar(34);
|
124 |
|
|
verticalGlyphMetrics = vmtx.asShortBuffer();
|
125 |
|
|
}
|
126 |
|
|
else
|
127 |
|
|
{
|
128 |
|
|
verticalAscent = /* advanceWidthMax */ hhea.getChar(10) / 2;
|
129 |
|
|
verticalDescent = -verticalAscent;
|
130 |
|
|
numLongVerticalMetricsEntries = 0;
|
131 |
|
|
verticalGlyphMetrics = null;
|
132 |
|
|
}
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
|
136 |
|
|
/**
|
137 |
|
|
* Returns the distance from the baseline to the highest ascender.
|
138 |
|
|
*
|
139 |
|
|
* @param horizontal <code>true</code> for horizontal line layout,
|
140 |
|
|
* <code>false</code> for vertical line layout.
|
141 |
|
|
*
|
142 |
|
|
* @return the maximal ascent, in font units.
|
143 |
|
|
*/
|
144 |
|
|
public int getAscent(boolean horizontal)
|
145 |
|
|
{
|
146 |
|
|
return horizontal ? horizontalAscent : verticalAscent;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
|
150 |
|
|
/**
|
151 |
|
|
* Returns the distance from the baseline to the lowest descender.
|
152 |
|
|
*
|
153 |
|
|
* @param horizontal <code>true</code> for horizontal line layout,
|
154 |
|
|
* <code>false</code> for vertical line layout.
|
155 |
|
|
*
|
156 |
|
|
* @return the maximal descent, in font units.
|
157 |
|
|
*/
|
158 |
|
|
public int getDescent(boolean horizontal)
|
159 |
|
|
{
|
160 |
|
|
return horizontal ? horizontalDescent : verticalDescent;
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
/**
|
165 |
|
|
* Returns the typographic line gap.
|
166 |
|
|
*
|
167 |
|
|
* @param horizontal <code>true</code> for horizontal line layout,
|
168 |
|
|
* <code>false</code> for vertical line layout.
|
169 |
|
|
*
|
170 |
|
|
* @return the line gap, in font units.
|
171 |
|
|
*/
|
172 |
|
|
public int getLineGap(boolean horizontal)
|
173 |
|
|
{
|
174 |
|
|
return horizontalLineGap;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
/**
|
179 |
|
|
* Determines the advance width of a glyph, without considering
|
180 |
|
|
* hinting.
|
181 |
|
|
*
|
182 |
|
|
* @param glyphIndex the index of the glyph whose advance width is
|
183 |
|
|
* to be determined.
|
184 |
|
|
*
|
185 |
|
|
* @param horizontal <code>true</code> for horizontal line layout,
|
186 |
|
|
* <code>false</code> for vertical line layout.
|
187 |
|
|
*
|
188 |
|
|
* @return the advance width, in font units.
|
189 |
|
|
*/
|
190 |
|
|
public int getAdvanceWidth(int glyphIndex, boolean horizontal)
|
191 |
|
|
{
|
192 |
|
|
if (!horizontal)
|
193 |
|
|
return 0;
|
194 |
|
|
|
195 |
|
|
glyphIndex = Math.min(glyphIndex,
|
196 |
|
|
numLongHorizontalMetricsEntries - 1);
|
197 |
|
|
return horizontalGlyphMetrics.get(glyphIndex << 1);
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
|
201 |
|
|
/**
|
202 |
|
|
* Determines the advance width of a glyph, without considering
|
203 |
|
|
* hinting.
|
204 |
|
|
*
|
205 |
|
|
* @param glyphIndex the index of the glyph whose advance width is
|
206 |
|
|
* to be determined.
|
207 |
|
|
*
|
208 |
|
|
* @param horizontal <code>true</code> for horizontal line layout,
|
209 |
|
|
* <code>false</code> for vertical line layout.
|
210 |
|
|
*
|
211 |
|
|
* @return the advance width, in font units.
|
212 |
|
|
*/
|
213 |
|
|
public int getAdvanceHeight(int glyphIndex, boolean horizontal)
|
214 |
|
|
{
|
215 |
|
|
if (horizontal)
|
216 |
|
|
return 0;
|
217 |
|
|
|
218 |
|
|
/* If a font does not provide vertical glyph metrics, advance
|
219 |
|
|
* by the height of one horizontal line.
|
220 |
|
|
*/
|
221 |
|
|
if (verticalGlyphMetrics == null)
|
222 |
|
|
return horizontalAscent - horizontalDescent + horizontalLineGap;
|
223 |
|
|
|
224 |
|
|
glyphIndex = Math.min(glyphIndex,
|
225 |
|
|
numLongVerticalMetricsEntries - 1);
|
226 |
|
|
return verticalGlyphMetrics.get(glyphIndex << 1);
|
227 |
|
|
}
|
228 |
|
|
}
|