1 /*
2 * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.font;
27
28 import java.lang.ref.ReferenceQueue;
29 import java.lang.ref.SoftReference;
30
31 import java.awt.FontMetrics;
32 import java.awt.Font;
33 import java.awt.GraphicsEnvironment;
34 import java.awt.geom.AffineTransform;
35 import java.awt.geom.NoninvertibleTransformException;
36 import java.awt.geom.Rectangle2D;
37 import java.awt.font.FontRenderContext;
38 import java.awt.font.TextLayout;
39
40 import java.io.IOException;
41 import java.io.ObjectInputStream;
42 import java.io.ObjectOutputStream;
43
44 import java.util.concurrent.ConcurrentHashMap;
45
46 import sun.java2d.Disposer;
47 import sun.java2d.DisposerRecord;
48
49 /*
50 * This class provides a summary of the glyph measurements for a Font
51 * and a set of hints that guide their display. It provides more metrics
52 * information for the Font than the java.awt.FontMetrics class. There
53 * is also some redundancy with that class.
54 * <p>
55 * The design metrics for a Font are obtained from Font.getDesignMetrics().
56 * The FontDesignMetrics object returned will be independent of the
57 * point size of the Font.
58 * Most users are familiar with the idea of using <i>point size</i> to
59 * specify the size of glyphs in a font. This point size defines a
60 * measurement between the baseline of one line to the baseline of the
61 * following line in a single spaced text document. The point size is
62 * based on <i>typographic points</i>, approximately 1/72 of an inch.
63 * <p>
246 private static final FontDesignMetrics[]
247 recentMetrics = new FontDesignMetrics[MAXRECENT];
248 private static int recentIndex = 0;
249
250 public static FontDesignMetrics getMetrics(Font font) {
251 return getMetrics(font, getDefaultFrc());
252 }
253
254 public static FontDesignMetrics getMetrics(Font font,
255 FontRenderContext frc) {
256
257
258 /* When using alternate composites, can't cache based just on
259 * the java.awt.Font. Since this is rarely used and we can still
260 * cache the physical fonts, its not a problem to just return a
261 * new instance in this case.
262 * Note that currently Swing native L&F composites are not handled
263 * by this code as they use the metrics of the physical anyway.
264 */
265 SunFontManager fm = SunFontManager.getInstance();
266 if (fm.maybeUsingAlternateCompositeFonts() &&
267 FontUtilities.getFont2D(font) instanceof CompositeFont) {
268 return new FontDesignMetrics(font, frc);
269 }
270
271 FontDesignMetrics m = null;
272 KeyReference r;
273
274 /* There are 2 possible keys used to perform lookups in metricsCache.
275 * If the FRC is set to all defaults, we just use the font as the key.
276 * If the FRC is non-default in any way, we construct a hybrid key
277 * that combines the font and FRC.
278 */
279 boolean usefontkey = frc.equals(getDefaultFrc());
280
281 if (usefontkey) {
282 r = metricsCache.get(font);
283 } else /* use hybrid key */ {
284 // NB synchronization is not needed here because of updates to
285 // the metrics cache but is needed for the shared key.
286 synchronized (MetricsKey.class) {
|
1 /*
2 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.font;
27
28 import java.awt.Font;
29 import java.awt.FontMetrics;
30 import java.awt.GraphicsEnvironment;
31 import java.awt.font.FontRenderContext;
32 import java.awt.font.TextLayout;
33 import java.awt.geom.AffineTransform;
34 import java.awt.geom.Rectangle2D;
35 import java.io.IOException;
36 import java.io.ObjectInputStream;
37 import java.io.ObjectOutputStream;
38 import java.lang.ref.ReferenceQueue;
39 import java.lang.ref.SoftReference;
40 import java.util.concurrent.ConcurrentHashMap;
41
42 import sun.java2d.Disposer;
43 import sun.java2d.DisposerRecord;
44
45 /*
46 * This class provides a summary of the glyph measurements for a Font
47 * and a set of hints that guide their display. It provides more metrics
48 * information for the Font than the java.awt.FontMetrics class. There
49 * is also some redundancy with that class.
50 * <p>
51 * The design metrics for a Font are obtained from Font.getDesignMetrics().
52 * The FontDesignMetrics object returned will be independent of the
53 * point size of the Font.
54 * Most users are familiar with the idea of using <i>point size</i> to
55 * specify the size of glyphs in a font. This point size defines a
56 * measurement between the baseline of one line to the baseline of the
57 * following line in a single spaced text document. The point size is
58 * based on <i>typographic points</i>, approximately 1/72 of an inch.
59 * <p>
242 private static final FontDesignMetrics[]
243 recentMetrics = new FontDesignMetrics[MAXRECENT];
244 private static int recentIndex = 0;
245
246 public static FontDesignMetrics getMetrics(Font font) {
247 return getMetrics(font, getDefaultFrc());
248 }
249
250 public static FontDesignMetrics getMetrics(Font font,
251 FontRenderContext frc) {
252
253
254 /* When using alternate composites, can't cache based just on
255 * the java.awt.Font. Since this is rarely used and we can still
256 * cache the physical fonts, its not a problem to just return a
257 * new instance in this case.
258 * Note that currently Swing native L&F composites are not handled
259 * by this code as they use the metrics of the physical anyway.
260 */
261 SunFontManager fm = SunFontManager.getInstance();
262 if (fm.usingAlternateCompositeFonts() &&
263 FontUtilities.getFont2D(font) instanceof CompositeFont) {
264 return new FontDesignMetrics(font, frc);
265 }
266
267 FontDesignMetrics m = null;
268 KeyReference r;
269
270 /* There are 2 possible keys used to perform lookups in metricsCache.
271 * If the FRC is set to all defaults, we just use the font as the key.
272 * If the FRC is non-default in any way, we construct a hybrid key
273 * that combines the font and FRC.
274 */
275 boolean usefontkey = frc.equals(getDefaultFrc());
276
277 if (usefontkey) {
278 r = metricsCache.get(font);
279 } else /* use hybrid key */ {
280 // NB synchronization is not needed here because of updates to
281 // the metrics cache but is needed for the shared key.
282 synchronized (MetricsKey.class) {
|