Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / gnu / java / awt / font / opentype / truetype / GlyphMeasurer.java
blobbbd0b9bffbc228089242113f177d96d44ff25232
1 /* GlyphMeasurer.java -- Helper for measuring TrueType glyphs.
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.truetype;
41 import java.awt.FontFormatException;
42 import java.nio.ByteBuffer;
43 import java.nio.ShortBuffer;
46 /**
47 * A class for measuring TrueType and OpenType glyphs.
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.
56 * @author Sascha Brawer (brawer@dandelis.ch)
58 final class GlyphMeasurer
60 /**
61 * A view buffer that allows accessing the contents of the
62 * font&#x2019;s <code>hmtx</code> table as shorts.
64 private final ShortBuffer horizontalGlyphMetrics;
67 /**
68 * A view buffer that allows accessing the contents of the
69 * font&#x2019;s <code>vmtx</code> table as shorts.
71 private final ShortBuffer verticalGlyphMetrics;
74 private final int numLongHorizontalMetricsEntries;
75 private final int numLongVerticalMetricsEntries;
77 private final int horizontalAscent;
78 private final int verticalAscent;
80 private final int horizontalDescent;
81 private final int verticalDescent;
83 private final int horizontalLineGap;
86 /**
87 * Constructs a GlyphMeasurer from TrueType/OpenType font tables.
89 * @param hhea the <code>hhea</code> table, which contains
90 * information about horizontal metrics that is common to all
91 * glyphs.
93 * @param hmtx the <code>hmtx</code> table, which contains
94 * glyph-specific information about horizontal metrics.
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>.
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>.
105 GlyphMeasurer(ByteBuffer hhea, ByteBuffer hmtx,
106 ByteBuffer vhea, ByteBuffer vmtx)
107 throws FontFormatException
109 if ((hhea.getInt(0) != 0x00010000) || (hhea.getInt(30) != 0))
110 throw new FontFormatException("unsupported hhea format");
112 horizontalAscent = hhea.getShort(4);
113 horizontalDescent = hhea.getShort(6);
114 horizontalLineGap = hhea.getShort(8);
116 numLongHorizontalMetricsEntries = hhea.getChar(34);
117 horizontalGlyphMetrics = hmtx.asShortBuffer();
119 if (vhea != null)
121 verticalAscent = vhea.getShort(4);
122 verticalDescent = vhea.getShort(6);
123 numLongVerticalMetricsEntries = vhea.getChar(34);
124 verticalGlyphMetrics = vmtx.asShortBuffer();
126 else
128 verticalAscent = /* advanceWidthMax */ hhea.getChar(10) / 2;
129 verticalDescent = -verticalAscent;
130 numLongVerticalMetricsEntries = 0;
131 verticalGlyphMetrics = null;
137 * Returns the distance from the baseline to the highest ascender.
139 * @param horizontal <code>true</code> for horizontal line layout,
140 * <code>false</code> for vertical line layout.
142 * @return the maximal ascent, in font units.
144 public int getAscent(boolean horizontal)
146 return horizontal ? horizontalAscent : verticalAscent;
151 * Returns the distance from the baseline to the lowest descender.
153 * @param horizontal <code>true</code> for horizontal line layout,
154 * <code>false</code> for vertical line layout.
156 * @return the maximal descent, in font units.
158 public int getDescent(boolean horizontal)
160 return horizontal ? horizontalDescent : verticalDescent;
165 * Returns the typographic line gap.
167 * @param horizontal <code>true</code> for horizontal line layout,
168 * <code>false</code> for vertical line layout.
170 * @return the line gap, in font units.
172 public int getLineGap(boolean horizontal)
174 return horizontalLineGap;
179 * Determines the advance width of a glyph, without considering
180 * hinting.
182 * @param glyphIndex the index of the glyph whose advance width is
183 * to be determined.
185 * @param horizontal <code>true</code> for horizontal line layout,
186 * <code>false</code> for vertical line layout.
188 * @return the advance width, in font units.
190 public int getAdvanceWidth(int glyphIndex, boolean horizontal)
192 if (!horizontal)
193 return 0;
195 glyphIndex = Math.min(glyphIndex,
196 numLongHorizontalMetricsEntries - 1);
197 return horizontalGlyphMetrics.get(glyphIndex << 1);
202 * Determines the advance width of a glyph, without considering
203 * hinting.
205 * @param glyphIndex the index of the glyph whose advance width is
206 * to be determined.
208 * @param horizontal <code>true</code> for horizontal line layout,
209 * <code>false</code> for vertical line layout.
211 * @return the advance width, in font units.
213 public int getAdvanceHeight(int glyphIndex, boolean horizontal)
215 if (horizontal)
216 return 0;
218 /* If a font does not provide vertical glyph metrics, advance
219 * by the height of one horizontal line.
221 if (verticalGlyphMetrics == null)
222 return horizontalAscent - horizontalDescent + horizontalLineGap;
224 glyphIndex = Math.min(glyphIndex,
225 numLongVerticalMetricsEntries - 1);
226 return verticalGlyphMetrics.get(glyphIndex << 1);