FontDesignMetrics.java revision 1686
1472N/A * Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. Sun designates this 0N/A * particular file as subject to the "Classpath" exception as provided 0N/A * by Sun in the LICENSE file that accompanied this code. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 1472N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1472N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A * This class provides a summary of the glyph measurements for a Font 0N/A * and a set of hints that guide their display. It provides more metrics 44N/A * information for the Font than the java.awt.FontMetrics class. There 0N/A * is also some redundancy with that class. 0N/A * The design metrics for a Font are obtained from Font.getDesignMetrics(). 0N/A * The FontDesignMetrics object returned will be independent of the 0N/A * point size of the Font. 0N/A * Most users are familiar with the idea of using <i>point size</i> to 0N/A * specify the size of glyphs in a font. This point size defines a 0N/A * measurement between the baseline of one line to the baseline of the 0N/A * following line in a single spaced text document. The point size is 0N/A * based on <i>typographic points</i>, approximately 1/72 of an inch. 0N/A * The Java2D API adopts the convention that one point is equivalent 44N/A * to one unit in user coordinates. When using a normalized transform 44N/A * for converting user space coordinates to device space coordinates (see 0N/A * GraphicsConfiguration.getDefaultTransform() and 0N/A * GraphicsConfiguration.getNormalizingTransform()), 72 user space units 0N/A * equal 1 inch in device space. In this case one point is 1/72 of an inch. 0N/A * The FontDesignMetrics class expresses font metrics in terms of arbitrary 0N/A * <i>typographic units</i> (not points) chosen by the font supplier 0N/A * and used in the underlying platform font representations. These units are 0N/A * defined by dividing the em-square into a grid. The em-sqaure is the 0N/A * theoretical square whose dimensions are the full body height of the 0N/A * font. A typographic unit is the smallest measurable unit in the 0N/A * em-square. The number of units-per-em is determined by the font 0N/A * designer. The greater the units-per-em, the greater the precision 0N/A * in metrics. For example, Type 1 fonts divide the em-square into a 0N/A * 1000 x 1000 grid, while TrueType fonts typically use a 2048 x 2048 0N/A * grid. The scale of these units can be obtained by calling 1138N/A * Typographic units are relative -- their absolute size changes as the 0N/A * size of the of the em-square changes. An em-square is 9 points high 0N/A * in a 9-point font. Because typographic units are relative to the 0N/A * em-square, a given location on a glyph will have the same coordinates 0N/A * in typographic units regardless of the point size. 0N/A * Converting typographic units to pixels requires computing pixels-per-em 0N/A * (ppem). This can be computed as: 0N/A ppem = device_resolution * (inches-per-point) * pointSize 0N/A * where device resolution could be measured in pixels/inch and the point 0N/A * size of a font is effectively points/em. Using a normalized transform 0N/A * from user space to device space (see above), results in 1/72 inch/point. 0N/A * In this case, ppem is equal to the point size on a 72 dpi monitor, so 0N/A * that an N point font displays N pixels high. In general, 0N/A pixel_units = typographic_units * (ppem / units_per_em) 0N/A * @see java.awt.Font 0N/A * @see java.awt.GraphicsConfiguration#getDefaultTransform 0N/A * @see java.awt.GraphicsConfiguration#getNormalizingTransform 0N/A // height, ascent, descent, leading are reported to the client 0N/A // as an integer this value is added to the true fp value to 0N/A // obtain a value which is usually going to result in a round up 0N/A // to the next integer except for very marginal cases. 0N/A // These fields are all part of the old serialization representation 0N/A private int[]
cache;
// now unused, still here only for serialization 0N/A // End legacy serialization fields 0N/A private int serVersion =
0;
// If 1 in readObject, these fields are on the input stream: 0N/A private transient float[]
advCache;
// transient since values could change across runtimes 0N/A /* Strongly cache up to 5 most recently requested FontMetrics objects, 0N/A * and softly cache as many as GC allows. In practice this means we 0N/A * should keep references around until memory gets low. 0N/A * We key the cache either by a Font or a combination of the Font and 0N/A * and FRC. A lot of callers use only the font so although there's code 0N/A * duplication, we allow just a font to be a key implying a default FRC. 0N/A * Also we put the references on a queue so that if they do get nulled 0N/A * out we can clear the keys from the table. 0N/A /* It is possible that since this reference object has been 0N/A * enqueued, that a new metrics has been put into the table 0N/A * for the same key value. So we'll test to see if the table maps 0N/A * to THIS reference. If its a new one, we'll leave it alone. 0N/A * It is possible that a new entry comes in after our test, but 0N/A * it is unlikely and if this were a problem we would need to 0N/A * synchronize all 'put' and 'remove' accesses to the cache which 0N/A * I would prefer not to do. /* Synchronize access to this on the class */ /* All accesses to a CHM do not in general need to be synchronized, * as incomplete operations on another thread would just lead to /* When using alternate composites, can't cache based just on * the java.awt.Font. Since this is rarely used and we can still * cache the physical fonts, its not a problem to just return a * new instance in this case. * Note that currently Swing native L&F composites are not handled * by this code as they use the metrics of the physical anyway. /* There are 2 possible keys used to perform lookups in metricsCache. * If the FRC is set to all defaults, we just use the font as the key. * If the FRC is non-default in any way, we construct a hybrid key * that combines the font and FRC. }
else /* use hybrid key */ {
// NB synchronization is not needed here because of updates to // the metrics cache but is needed for the shared key. /* either there was no reference, or it was cleared. Need a new * metrics instance. The key to use in the map is a new * MetricsKey instance when we've determined the FRC is * non-default. Its constructed from local vars so we are * thread-safe - no need to worry about the shared key changing. }
else /* use hybrid key */ {
/* Here's where we keep the recent metrics */ * Constructs a new FontDesignMetrics object for the given Font. * Its private to enable caching - call getMetrics() instead. * @param font a Font object. /* private to enable caching - call getMetrics() instead. */ // 0 is a valid metric so force it to -1 for (
int i =
0; i <
256; i++) {
// when deserialized, members are set to their default values for their type-- // not to the values assigned during initialization before the constructor for (
int i=
0; i <
256; i++) {
// Uses advCache to get character width // It is incorrect to call this method for ch > 255 /* Override of FontMetrics.getFontRenderContext() */ // default metrics for compatibility with legacy code /* TextLayout throws IAE for null, so throw NPE explicitly */ for (
int i=
0; i <
length; i++) {
return (
int) (
0.5 +
width);
/* Explicit test needed to satisfy superclass spec */ return (
int) (
0.5 +
width);
* Gets the advance widths of the first 256 characters in the * <code>Font</code>. The advance is the * distance from the leftmost point to the rightmost point on the * character's baseline. Note that the advance of a * <code>String</code> is not necessarily the sum of the advances * @return an array storing the advance widths of the * characters in the <code>Font</code> * described by this <code>FontMetrics</code> object. // More efficient than base class implementation - reuses existing cache for (
char ch =
0 ;
ch <
256 ;
ch++) {
* Returns the typographic ascent of the font. This is the maximum distance * glyphs in this font extend above the base line (measured in typographic * Returns the typographic descent of the font. This is the maximum distance * glyphs in this font extend below the base line. // nb this ensures the sum of the results of the public methods // for leading, ascent & descent sum to height. // if the calculations in any other methods change this needs // the 0.95 value used here and in the other methods allows some // tiny fraction of leeway before rouding up. A higher value (0.99) // caused some excessive rounding up. // height is calculated as the sum of two separately rounded up values // because typically clients use ascent to determine the y location to // pass to drawString etc and we need to ensure that the height has enough // space below the baseline to fully contain any descender.