src/java.desktop/share/classes/java/awt/Font.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58632 fbab568169c4
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53793
9bb638b282bc 8216592: Removal of the class sun.awt.AWTSecurityManager
serb
parents: 52248
diff changeset
     2
 * Copyright (c) 1995, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3928
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.font.FontRenderContext;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.font.GlyphVector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.font.LineMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.font.TextAttribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.font.TextLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.geom.Point2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.peer.FontPeer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.lang.ref.SoftReference;
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 11270
diff changeset
    39
import java.nio.file.Files;
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
    40
import java.security.AccessController;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
    41
import java.security.PrivilegedExceptionAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.text.AttributedCharacterIterator.Attribute;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.text.CharacterIterator;
44655
06871a50a4b5 8177461: Wrong references are used in the javadoc in the java.desktop module
serb
parents: 41791
diff changeset
    44
import java.util.EventListener;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.Locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Map;
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 30469
diff changeset
    48
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 30469
diff changeset
    49
import sun.awt.ComponentFactory;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import sun.font.StandardGlyphVector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import sun.font.AttributeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.font.AttributeValues;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import sun.font.CompositeFont;
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
    55
import sun.font.CreatedFontTracker;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import sun.font.Font2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import sun.font.Font2DHandle;
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
    58
import sun.font.FontAccess;
48285
7e8a0c4ee95e 8189809: Large performance regression in Swing text layout
prr
parents: 47216
diff changeset
    59
import sun.font.FontDesignMetrics;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import sun.font.FontManager;
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
    61
import sun.font.FontManagerFactory;
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
    62
import sun.font.FontUtilities;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import sun.font.GlyphLayout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
import sun.font.FontLineMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import sun.font.CoreMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
import static sun.font.EAttribute.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
/**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
    70
 * The {@code Font} class represents fonts, which are used to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * render text in a visible way.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * A font provides the information needed to map sequences of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 * <em>characters</em> to sequences of <em>glyphs</em>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
    74
 * and to render sequences of glyphs on {@code Graphics} and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
    75
 * {@code Component} objects.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *
54397
65030bbf5ac1 8220250: fix headings in java.desktop
psadhukhan
parents: 53793
diff changeset
    77
 * <h2>Characters and Glyphs</h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * A <em>character</em> is a symbol that represents an item such as a letter,
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
    80
 * a digit, or punctuation in an abstract way. For example, {@code 'g'},
21957
97758de70fbd 8028019: AWT Doclint warning/error cleanup
rriggs
parents: 21278
diff changeset
    81
 * LATIN SMALL LETTER G, is a character.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * A <em>glyph</em> is a shape used to render a character or a sequence of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * characters. In simple writing systems, such as Latin, typically one glyph
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * represents one character. In general, however, characters and glyphs do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * have one-to-one correspondence. For example, the character '&aacute;'
21957
97758de70fbd 8028019: AWT Doclint warning/error cleanup
rriggs
parents: 21278
diff changeset
    87
 * LATIN SMALL LETTER A WITH ACUTE, can be represented by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * two glyphs: one for 'a' and one for '&acute;'. On the other hand, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * two-character string "fi" can be represented by a single glyph, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * "fi" ligature. In complex writing systems, such as Arabic or the South
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * and South-East Asian writing systems, the relationship between characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * and glyphs can be more complicated and involve context-dependent selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * of glyphs as well as glyph reordering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * A font encapsulates the collection of glyphs needed to render a selected set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * of characters as well as the tables needed to map sequences of characters to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * corresponding sequences of glyphs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *
54397
65030bbf5ac1 8220250: fix headings in java.desktop
psadhukhan
parents: 53793
diff changeset
    99
 * <h2>Physical and Logical Fonts</h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * The Java Platform distinguishes between two kinds of fonts:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * <em>physical</em> fonts and <em>logical</em> fonts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * <em>Physical</em> fonts are the actual font libraries containing glyph data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * and tables to map from character sequences to glyph sequences, using a font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * technology such as TrueType or PostScript Type 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * All implementations of the Java Platform must support TrueType fonts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * support for other font technologies is implementation dependent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * Physical fonts may use names such as Helvetica, Palatino, HonMincho, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * any number of other font names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * Typically, each physical font supports only a limited set of writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * systems, for example, only Latin characters or only Japanese and Basic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * Latin.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 * The set of available physical fonts varies between configurations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * Applications that require specific fonts can bundle them and instantiate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * them using the {@link #createFont createFont} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * <em>Logical</em> fonts are the five font families defined by the Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * platform which must be supported by any Java runtime environment:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * Serif, SansSerif, Monospaced, Dialog, and DialogInput.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * These logical fonts are not actual font libraries. Instead, the logical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * font names are mapped to physical fonts by the Java runtime environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * The mapping is implementation and usually locale dependent, so the look
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * and the metrics provided by them vary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * Typically, each logical font name maps to several physical fonts in order to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * cover a large range of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Peered AWT components, such as {@link Label Label} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * {@link TextField TextField}, can only use logical fonts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * For a discussion of the relative advantages and disadvantages of using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * physical or logical fonts, see the
32665
f6ce2c519881 8134476: Broken Hyperlink in JDK 8 java.awt.Font javadocs
psadhukhan
parents: 30471
diff changeset
   133
 * <a href="https://docs.oracle.com/javase/tutorial/2d/text/fonts.html#advantages-and-disadvantages">
f6ce2c519881 8134476: Broken Hyperlink in JDK 8 java.awt.Font javadocs
psadhukhan
parents: 30471
diff changeset
   134
 *    Physical and Logical Fonts</a>
f6ce2c519881 8134476: Broken Hyperlink in JDK 8 java.awt.Font javadocs
psadhukhan
parents: 30471
diff changeset
   135
 * in <a href="https://docs.oracle.com/javase/tutorial/index.html">The Java Tutorials</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * document.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *
54397
65030bbf5ac1 8220250: fix headings in java.desktop
psadhukhan
parents: 53793
diff changeset
   138
 * <h2>Font Faces and Names</h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   140
 * A {@code Font}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * can have many faces, such as heavy, medium, oblique, gothic and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * regular. All of these faces have similar typographic design.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * There are three different names that you can get from a
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   145
 * {@code Font} object.  The <em>logical font name</em> is simply the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 * name that was used to construct the font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * The <em>font face name</em>, or just <em>font name</em> for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * short, is the name of a particular font face, like Helvetica Bold. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 * <em>family name</em> is the name of the font family that determines the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * typographic design across several faces, like Helvetica.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   152
 * The {@code Font} class represents an instance of a font face from
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 * a collection of  font faces that are present in the system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * of the host system.  As examples, Arial Bold and Courier Bold Italic
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   155
 * are font faces.  There can be several {@code Font} objects
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
 * associated with a font face, each differing in size, style, transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * and font features.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 * <p>
41791
a0b95c408d42 8166034: [macosx] Non-AA Serif font always displays as regular - no bold
prr
parents: 39866
diff changeset
   159
 * Glyphs may not always be rendered with the requested properties (e.g, font
a0b95c408d42 8166034: [macosx] Non-AA Serif font always displays as regular - no bold
prr
parents: 39866
diff changeset
   160
 * and style) due to platform limitations such as the absence of suitable
a0b95c408d42 8166034: [macosx] Non-AA Serif font always displays as regular - no bold
prr
parents: 39866
diff changeset
   161
 * platform fonts to implement a logical font.
a0b95c408d42 8166034: [macosx] Non-AA Serif font always displays as regular - no bold
prr
parents: 39866
diff changeset
   162
 * <p>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * The {@link GraphicsEnvironment#getAllFonts() getAllFonts} method
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   164
 * of the {@code GraphicsEnvironment} class returns an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * array of all font faces available in the system. These font faces are
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   166
 * returned as {@code Font} objects with a size of 1, identity
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * transform and default font features. These
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   168
 * base fonts can then be used to derive new {@code Font} objects
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * with varying sizes, styles, transforms and font features via the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   170
 * {@code deriveFont} methods in this class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *
54397
65030bbf5ac1 8220250: fix headings in java.desktop
psadhukhan
parents: 53793
diff changeset
   172
 * <h2>Font and TextAttribute</h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   174
 * <p>{@code Font} supports most
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   175
 * {@code TextAttribute}s.  This makes some operations, such as
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 * rendering underlined text, convenient since it is not
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   177
 * necessary to explicitly construct a {@code TextLayout} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * Attributes can be set on a Font by constructing or deriving it
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   179
 * using a {@code Map} of {@code TextAttribute} values.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   181
 * <p>The values of some {@code TextAttributes} are not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * serializable, and therefore attempting to serialize an instance of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   183
 * {@code Font} that has such values will not serialize them.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 * This means a Font deserialized from such a stream will not compare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * equal to the original Font that contained the non-serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 * attributes.  This should very rarely pose a problem
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 * since these attributes are typically used only in special
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * circumstances and are unlikely to be serialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * <ul>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   191
 * <li>{@code FOREGROUND} and {@code BACKGROUND} use
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   192
 * {@code Paint} values. The subclass {@code Color} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   193
 * serializable, while {@code GradientPaint} and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   194
 * {@code TexturePaint} are not.</li>
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   195
 * <li>{@code CHAR_REPLACEMENT} uses
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   196
 * {@code GraphicAttribute} values.  The subclasses
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   197
 * {@code ShapeGraphicAttribute} and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   198
 * {@code ImageGraphicAttribute} are not serializable.</li>
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   199
 * <li>{@code INPUT_METHOD_HIGHLIGHT} uses
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   200
 * {@code InputMethodHighlight} values, which are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * not serializable.  See {@link java.awt.im.InputMethodHighlight}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   204
 * <p>Clients who create custom subclasses of {@code Paint} and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   205
 * {@code GraphicAttribute} can make them serializable and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * avoid this problem.  Clients who use input method highlights can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * convert these to the platform-specific attributes for that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * highlight on the current platform and set them on the Font as
21957
97758de70fbd 8028019: AWT Doclint warning/error cleanup
rriggs
parents: 21278
diff changeset
   209
 * a workaround.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   211
 * <p>The {@code Map}-based constructor and
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   212
 * {@code deriveFont} APIs ignore the FONT attribute, and it is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
 * not retained by the Font; the static {@link #getFont} method should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
 * be used if the FONT attribute might be present.  See {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
 * java.awt.font.TextAttribute#FONT} for more information.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 * <p>Several attributes will cause additional rendering overhead
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   218
 * and potentially invoke layout.  If a {@code Font} has such
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * attributes, the <code>{@link #hasLayoutAttributes()}</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * will return true.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
 * <p>Note: Font rotations can cause text baselines to be rotated.  In
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
 * order to account for this (rare) possibility, font APIs are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * specified to return metrics and take parameters 'in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * baseline-relative coordinates'.  This maps the 'x' coordinate to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * the advance along the baseline, (positive x is forward along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * baseline), and the 'y' coordinate to a distance along the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
 * perpendicular to the baseline at 'x' (positive y is 90 degrees
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * clockwise from the baseline vector).  APIs for which this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * especially important are called out as having 'baseline-relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * coordinates.'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
public class Font implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
{
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   235
    private static class FontAccessImpl extends FontAccess {
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   236
        public Font2D getFont2D(Font font) {
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   237
            return font.getFont2D();
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   238
        }
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   239
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   240
        public void setFont2D(Font font, Font2DHandle handle) {
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   241
            font.font2DHandle = handle;
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   242
        }
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   243
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   244
        public void setCreatedFont(Font font) {
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   245
            font.createdFont = true;
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   246
        }
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   247
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   248
        public boolean isCreatedFont(Font font) {
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   249
            return font.createdFont;
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   250
        }
30469
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 26749
diff changeset
   251
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 26749
diff changeset
   252
        @Override
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 26749
diff changeset
   253
        public FontPeer getFontPeer(final Font font) {
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 26749
diff changeset
   254
            return font.getFontPeer();
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 26749
diff changeset
   255
        }
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   256
    }
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   257
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        /* ensure that the necessary native libraries are loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        Toolkit.loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        initIDs();
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   262
        FontAccess.setFontAccess(new FontAccessImpl());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * This is now only used during serialization.  Typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * it is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @see #getAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     */
11270
d7b0b63bd082 7117334: Warnings cleanup day: reduce number of javac warnings in the java.awt package
bagiras
parents: 9035
diff changeset
   272
    private Hashtable<Object, Object> fRequestedAttributes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Constants to be used for logical font family names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * A String constant for the canonical family name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * logical font "Dialog". It is useful in Font construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * to provide compile-time verification of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    public static final String DIALOG = "Dialog";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * A String constant for the canonical family name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * logical font "DialogInput". It is useful in Font construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * to provide compile-time verification of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public static final String DIALOG_INPUT = "DialogInput";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * A String constant for the canonical family name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * logical font "SansSerif". It is useful in Font construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * to provide compile-time verification of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    public static final String SANS_SERIF = "SansSerif";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * A String constant for the canonical family name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * logical font "Serif". It is useful in Font construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * to provide compile-time verification of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    public static final String SERIF = "Serif";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * A String constant for the canonical family name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * logical font "Monospaced". It is useful in Font construction
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * to provide compile-time verification of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    public static final String MONOSPACED = "Monospaced";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Constants to be used for styles. Can be combined to mix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * The plain style constant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    public static final int PLAIN       = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * The bold style constant.  This can be combined with the other style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * constants (except PLAIN) for mixed styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    public static final int BOLD        = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * The italicized style constant.  This can be combined with the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * style constants (except PLAIN) for mixed styles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public static final int ITALIC      = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * The baseline used in most Roman scripts when laying out text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public static final int ROMAN_BASELINE = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * The baseline used in ideographic scripts like Chinese, Japanese,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * and Korean when laying out text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    public static final int CENTER_BASELINE = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
   352
     * The baseline used in Devanagari and similar scripts when laying
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * out text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public static final int HANGING_BASELINE = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * Identify a font resource of type TRUETYPE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Used to specify a TrueType font resource to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * {@link #createFont} method.
5578
c25ebb5bc2d6 6954424: Support OpenType/CFF fonts in JDK 7
prr
parents: 3928
diff changeset
   361
     * The TrueType format was extended to become the OpenType
c25ebb5bc2d6 6954424: Support OpenType/CFF fonts in JDK 7
prr
parents: 3928
diff changeset
   362
     * format, which adds support for fonts with Postscript outlines,
c25ebb5bc2d6 6954424: Support OpenType/CFF fonts in JDK 7
prr
parents: 3928
diff changeset
   363
     * this tag therefore references these fonts, as well as those
c25ebb5bc2d6 6954424: Support OpenType/CFF fonts in JDK 7
prr
parents: 3928
diff changeset
   364
     * with TrueType outlines.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    public static final int TRUETYPE_FONT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Identify a font resource of type TYPE1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Used to specify a Type1 font resource to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * {@link #createFont} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public static final int TYPE1_FONT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   379
     * The logical name of this {@code Font}, as passed to the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * constructor.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   381
     * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @see #getName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    protected String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   389
     * The style of this {@code Font}, as passed to the constructor.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * This style can be PLAIN, BOLD, ITALIC, or BOLD+ITALIC.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   391
     * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @see #getStyle()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    protected int style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   399
     * The point size of this {@code Font}, rounded to integer.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   400
     * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @see #getSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    protected int size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   408
     * The point size of this {@code Font} in {@code float}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * @see #getSize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * @see #getSize2D()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    protected float pointSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * The platform specific font information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    private transient FontPeer peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    private transient long pData;       // native JDK1.1 font pointer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    private transient Font2DHandle font2DHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    private transient AttributeValues values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    private transient boolean hasLayoutAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * If the origin of a Font is a created font then this attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * must be set on all derived fonts too.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    private transient boolean createdFont = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * This is true if the font transform is not identity.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * is used to avoid unnecessary instantiation of an AffineTransform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    private transient boolean nonIdentityTx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * A cached value used when a transform is required for internal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * use.  This must not be exposed to callers since AffineTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * is mutable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    private static final AffineTransform identityTx = new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * JDK 1.1 serialVersionUID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    private static final long serialVersionUID = -4206021311591459213L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    /**
30469
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 26749
diff changeset
   451
     * Gets the peer of this {@code Font}.
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 26749
diff changeset
   452
     *
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 26749
diff changeset
   453
     * @return the peer of the {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
30469
bac0a7ff7e1e 8074028: Remove API references to java.awt.peer
serb
parents: 26749
diff changeset
   455
    private FontPeer getFontPeer() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if(peer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            Toolkit tk = Toolkit.getDefaultToolkit();
30471
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 30469
diff changeset
   458
            if (tk instanceof ComponentFactory) {
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 30469
diff changeset
   459
                peer = ((ComponentFactory) tk).getFontPeer(name, style);
c1568a2416a8 8074757: Remove java.awt.Toolkit methods which return peer types
serb
parents: 30469
diff changeset
   460
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return peer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * Return the AttributeValues object associated with this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * font.  Most of the time, the internal object is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * If required, it will be created from the 'standard'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * state on the font.  Only non-default values will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * set in the AttributeValues object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * <p>Since the AttributeValues object is mutable, and it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * is cached in the font, care must be taken to ensure that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * it is not mutated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    private AttributeValues getAttributeValues() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        if (values == null) {
3017
217bf37bb0ea 6853617: race condition in java.awt.Font.getAttributes() (private method)
prr
parents: 2624
diff changeset
   478
            AttributeValues valuesTmp = new AttributeValues();
217bf37bb0ea 6853617: race condition in java.awt.Font.getAttributes() (private method)
prr
parents: 2624
diff changeset
   479
            valuesTmp.setFamily(name);
217bf37bb0ea 6853617: race condition in java.awt.Font.getAttributes() (private method)
prr
parents: 2624
diff changeset
   480
            valuesTmp.setSize(pointSize); // expects the float value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            if ((style & BOLD) != 0) {
3017
217bf37bb0ea 6853617: race condition in java.awt.Font.getAttributes() (private method)
prr
parents: 2624
diff changeset
   483
                valuesTmp.setWeight(2); // WEIGHT_BOLD
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            if ((style & ITALIC) != 0) {
3017
217bf37bb0ea 6853617: race condition in java.awt.Font.getAttributes() (private method)
prr
parents: 2624
diff changeset
   487
                valuesTmp.setPosture(.2f); // POSTURE_OBLIQUE
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
3017
217bf37bb0ea 6853617: race condition in java.awt.Font.getAttributes() (private method)
prr
parents: 2624
diff changeset
   489
            valuesTmp.defineAll(PRIMARY_MASK); // for streaming compatibility
217bf37bb0ea 6853617: race condition in java.awt.Font.getAttributes() (private method)
prr
parents: 2624
diff changeset
   490
            values = valuesTmp;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    private Font2D getFont2D() {
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   497
        FontManager fm = FontManagerFactory.getInstance();
53793
9bb638b282bc 8216592: Removal of the class sun.awt.AWTSecurityManager
serb
parents: 52248
diff changeset
   498
        if (font2DHandle == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            font2DHandle =
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   500
                fm.findFont2D(name, style,
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   501
                              FontManager.LOGICAL_FALLBACK).handle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        /* Do not cache the de-referenced font2D. It must be explicitly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
         * de-referenced to pick up a valid font in the event that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
         * original one is marked invalid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        return font2DHandle.font2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   511
     * Creates a new {@code Font} from the specified name, style and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * point size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * The font name can be a font face name or a font family name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * It is used together with the style to find an appropriate font face.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * When a font family name is specified, the style argument is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * select the most appropriate face from the family. When a font face
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * name is specified, the face's style and the style argument are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * merged to locate the best matching font from the same family.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * For example if face name "Arial Bold" is specified with style
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   521
     * {@code Font.ITALIC}, the font system looks for a face in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * "Arial" family that is bold and italic, and may associate the font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * instance with the physical font face "Arial Bold Italic".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * The style argument is merged with the specified face's style, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * added or subtracted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * This means, specifying a bold face and a bold style does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * double-embolden the font, and specifying a bold face and a plain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * style does not lighten the font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * If no face for the requested style can be found, the font system
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * may apply algorithmic styling to achieve the desired style.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   532
     * For example, if {@code ITALIC} is requested, but no italic
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * face is available, glyphs from the plain face may be algorithmically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * obliqued (slanted).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * Font name lookup is case insensitive, using the case folding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * rules of the US locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   539
     * If the {@code name} parameter represents something other than a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     * logical font, i.e. is interpreted as a physical font face or family, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * this cannot be mapped by the implementation to a physical font or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * compatible alternative, then the font system will map the Font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * instance to "Dialog", such that for example, the family as reported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * by {@link #getFamily() getFamily} will be "Dialog".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @param name the font name.  This can be a font face name or a font
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * family name, and may represent either a logical font or a physical
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * font found in this {@code GraphicsEnvironment}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * The family names for logical fonts are: Dialog, DialogInput,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * Monospaced, Serif, or SansSerif. Pre-defined String constants exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * for all of these names, for example, {@code DIALOG}. If {@code name} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * {@code null}, the <em>logical font name</em> of the new
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * {@code Font} as returned by {@code getName()} is set to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * the name "Default".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * @param style the style constant for the {@code Font}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * The style argument is an integer bitmask that may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * be {@code PLAIN}, or a bitwise union of {@code BOLD} and/or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * {@code ITALIC} (for example, {@code ITALIC} or {@code BOLD|ITALIC}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * If the style argument does not conform to one of the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * integer bitmasks then the style is set to {@code PLAIN}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * @param size the point size of the {@code Font}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @see GraphicsEnvironment#getAllFonts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @see GraphicsEnvironment#getAvailableFontFamilyNames
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   564
     * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    public Font(String name, int style, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        this.name = (name != null) ? name : "Default";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        this.style = (style & ~0x03) == 0 ? style : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        this.size = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        this.pointSize = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    private Font(String name, int style, float sizePts) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        this.name = (name != null) ? name : "Default";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        this.style = (style & ~0x03) == 0 ? style : 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        this.size = (int)(sizePts + 0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        this.pointSize = sizePts;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    /* This constructor is used by deriveFont when attributes is null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    private Font(String name, int style, float sizePts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                 boolean created, Font2DHandle handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        this(name, style, sizePts);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        this.createdFont = created;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        /* Fonts created from a stream will use the same font2D instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
         * as the parent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
         * One exception is that if the derived font is requested to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
         * in a different style, then also check if its a CompositeFont
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
         * and if so build a new CompositeFont from components of that style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
         * CompositeFonts can only be marked as "created" if they are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
         * to add fall backs to a physical font. And non-composites are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
         * always from "Font.createFont()" and shouldn't get this treatment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        if (created) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (handle.font2D instanceof CompositeFont &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                handle.font2D.getStyle() != style) {
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   597
                FontManager fm = FontManagerFactory.getInstance();
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   598
                this.font2DHandle = fm.getNewComposite(null, style, handle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                this.font2DHandle = handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /* used to implement Font.createFont */
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   606
    private Font(File fontFile, int fontFormat,
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   607
                 boolean isCopy, CreatedFontTracker tracker)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        throws FontFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
        this.createdFont = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        /* Font2D instances created by this method track their font file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
         * so that when the Font2D is GC'd it can also remove the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
         */
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   613
        FontManager fm = FontManagerFactory.getInstance();
36899
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   614
        Font2D[] fonts =
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   615
            fm.createFont2D(fontFile, fontFormat, false, isCopy, tracker);
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   616
        this.font2DHandle = fonts[0].handle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        this.name = this.font2DHandle.font2D.getFontName(Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        this.style = Font.PLAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        this.size = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        this.pointSize = 1f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    /* This constructor is used when one font is derived from another.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * Fonts created from a stream will use the same font2D instance as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * parent. They can be distinguished because the "created" argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * will be "true". Since there is no way to recreate these fonts they
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * need to have the handle to the underlying font2D passed in.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * "created" is also true when a special composite is referenced by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * handle for essentially the same reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * But when deriving a font in these cases two particular attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * need special attention: family/face and style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * The "composites" in these cases need to be recreated with optimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * fonts for the new values of family and style.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * For fonts created with createFont() these are treated differently.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * JDK can often synthesise a different style (bold from plain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * for example). For fonts created with "createFont" this is a reasonable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * solution but its also possible (although rare) to derive a font with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * different family attribute. In this case JDK needs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * to break the tie with the original Font2D and find a new Font.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * The oldName and oldStyle are supplied so they can be compared with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * what the Font2D and the values. To speed things along :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * oldName == null will be interpreted as the name is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * oldStyle = -1 will be interpreted as the style is unchanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * In these cases there is no need to interrogate "values".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    private Font(AttributeValues values, String oldName, int oldStyle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                 boolean created, Font2DHandle handle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        this.createdFont = created;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        if (created) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            this.font2DHandle = handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            String newName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            if (oldName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                newName = values.getFamily();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
                if (oldName.equals(newName)) newName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            int newStyle = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            if (oldStyle == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                newStyle = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
                if (values.getWeight() >= 2f)   newStyle  = BOLD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                if (values.getPosture() >= .2f) newStyle |= ITALIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                if (oldStyle == newStyle)       newStyle  = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            if (handle.font2D instanceof CompositeFont) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                if (newStyle != -1 || newName != null) {
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   668
                    FontManager fm = FontManagerFactory.getInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                    this.font2DHandle =
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
   670
                        fm.getNewComposite(newName, newStyle, handle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            } else if (newName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                this.createdFont = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                this.font2DHandle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        initFromValues(values);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   681
     * Creates a new {@code Font} with the specified attributes.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * Only keys defined in {@link java.awt.font.TextAttribute TextAttribute}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * are recognized.  In addition the FONT attribute is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *  not recognized by this constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * (see {@link #getAvailableAttributes}). Only attributes that have
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   686
     * values of valid types will affect the new {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   688
     * If {@code attributes} is {@code null}, a new
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   689
     * {@code Font} is initialized with default values.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @see java.awt.font.TextAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * @param attributes the attributes to assign to the new
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   692
     *          {@code Font}, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    public Font(Map<? extends Attribute, ?> attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        initFromValues(AttributeValues.fromMap(attributes, RECOGNIZED_MASK));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   699
     * Creates a new {@code Font} from the specified {@code font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * This constructor is intended for use by subclasses.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   701
     * @param font from which to create this {@code Font}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   702
     * @throws NullPointerException if {@code font} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    protected Font(Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (font.values != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            initFromValues(font.getAttributeValues().clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            this.name = font.name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            this.style = font.style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            this.size = font.size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            this.pointSize = font.pointSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        this.font2DHandle = font.font2DHandle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        this.createdFont = font.createdFont;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Font recognizes all attributes except FONT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    private static final int RECOGNIZED_MASK = AttributeValues.MASK_ALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        & ~AttributeValues.getMask(EFONT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * These attributes are considered primary by the FONT attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    private static final int PRIMARY_MASK =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        AttributeValues.getMask(EFAMILY, EWEIGHT, EWIDTH, EPOSTURE, ESIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                                ETRANSFORM, ESUPERSCRIPT, ETRACKING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * These attributes are considered secondary by the FONT attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    private static final int SECONDARY_MASK =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        RECOGNIZED_MASK & ~PRIMARY_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * These attributes are handled by layout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    private static final int LAYOUT_MASK =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        AttributeValues.getMask(ECHAR_REPLACEMENT, EFOREGROUND, EBACKGROUND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                                EUNDERLINE, ESTRIKETHROUGH, ERUN_DIRECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                                EBIDI_EMBEDDING, EJUSTIFICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                                EINPUT_METHOD_HIGHLIGHT, EINPUT_METHOD_UNDERLINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                                ESWAP_COLORS, ENUMERIC_SHAPING, EKERNING,
534
be8500bc69b2 6684056: SUPERSCRIPT TextAttribute on font needs to trigger layout.
prr
parents: 2
diff changeset
   746
                                ELIGATURES, ETRACKING, ESUPERSCRIPT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    private static final int EXTRA_MASK =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            AttributeValues.getMask(ETRANSFORM, ESUPERSCRIPT, EWIDTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Initialize the standard Font fields from the values object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    private void initFromValues(AttributeValues values) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        this.values = values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        values.defineAll(PRIMARY_MASK); // for 1.5 streaming compatibility
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        this.name = values.getFamily();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        this.pointSize = values.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        this.size = (int)(values.getSize() + 0.5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        if (values.getWeight() >= 2f) this.style |= BOLD; // not == 2f
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if (values.getPosture() >= .2f) this.style |= ITALIC; // not  == .2f
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        this.nonIdentityTx = values.anyNonDefault(EXTRA_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        this.hasLayoutAttributes =  values.anyNonDefault(LAYOUT_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    /**
37562
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   769
     * Returns true if any part of the specified text is from a
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   770
     * complex script for which the implementation will need to invoke
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   771
     * layout processing in order to render correctly when using
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   772
     * {@link Graphics#drawString(String,int,int) drawString(String,int,int)}
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   773
     * and other text rendering methods. Measurement of the text
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   774
     * may similarly need the same extra processing.
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   775
     * The {@code start} and {@code end} indices are provided so that
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   776
     * the application can request only a subset of the text be considered.
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   777
     * The last char index examined is at {@code "end-1"},
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   778
     * i.e a request to examine the entire array would be
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   779
     * <pre>
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   780
     * {@code Font.textRequiresLayout(chars, 0, chars.length);}
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   781
     * </pre>
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   782
     * An application may find this information helpful in
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   783
     * performance sensitive code.
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   784
     * <p>
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   785
     * Note that even if this method returns {@code false}, layout processing
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   786
     * may still be invoked when used with any {@code Font}
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   787
     * for which {@link #hasLayoutAttributes()} returns {@code true},
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   788
     * so that method will need to be consulted for the specific font,
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   789
     * in order to obtain an answer which accounts for such font attributes.
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   790
     *
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   791
     * @param chars the text.
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   792
     * @param start the index of the first char to examine.
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   793
     * @param end the ending index, exclusive.
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   794
     * @return {@code true} if the specified text will need special layout.
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   795
     * @throws NullPointerException if {@code chars} is null.
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   796
     * @throws ArrayIndexOutOfBoundsException if {@code start} is negative or
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   797
     * {@code end} is greater than the length of the {@code chars} array.
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   798
     * @since 9
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   799
     */
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   800
    public static boolean textRequiresLayout(char[] chars,
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   801
                                             int start, int end) {
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   802
        if (chars == null) {
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   803
           throw new NullPointerException("null char array");
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   804
        }
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   805
        if (start < 0 || end > chars.length) {
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   806
            throw new ArrayIndexOutOfBoundsException("start < 0 or end > len");
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   807
        }
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   808
        return FontUtilities.isComplexScript(chars, start, end);
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   809
    }
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   810
5b36d70170d6 8146324: Add sun.font.FontUtilities.isComplexCharCode or related method
prr
parents: 36899
diff changeset
   811
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   812
     * Returns a {@code Font} appropriate to the attributes.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   813
     * If {@code attributes} contains a {@code FONT} attribute
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   814
     * with a valid {@code Font} as its value, it will be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * merged with any remaining attributes.  See
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * {@link java.awt.font.TextAttribute#FONT} for more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     * information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * @param attributes the attributes to assign to the new
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   820
     *          {@code Font}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   821
     * @return a new {@code Font} created with the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *          attributes
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
   823
     * @throws NullPointerException if {@code attributes} is null.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @see java.awt.font.TextAttribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    public static Font getFont(Map<? extends Attribute, ?> attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        // optimize for two cases:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        // 1) FONT attribute, and nothing else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        // 2) attributes, but no FONT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        // avoid turning the attributemap into a regular map for no reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        if (attributes instanceof AttributeMap &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
            ((AttributeMap)attributes).getValues() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            AttributeValues values = ((AttributeMap)attributes).getValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            if (values.isNonDefault(EFONT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                Font font = values.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                if (!values.anyDefined(SECONDARY_MASK)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    return font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                // merge
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                values = font.getAttributeValues().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                values.merge(attributes, SECONDARY_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                return new Font(values, font.name, font.style,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                                font.createdFont, font.font2DHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            return new Font(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        Font font = (Font)attributes.get(TextAttribute.FONT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        if (font != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            if (attributes.size() > 1) { // oh well, check for anything else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                AttributeValues values = font.getAttributeValues().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                values.merge(attributes, SECONDARY_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                return new Font(values, font.name, font.style,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                                font.createdFont, font.font2DHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            return font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
        return new Font(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    /**
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   866
     * Used with the byte count tracker for fonts created from streams.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   867
     * If a thread can create temp files anyway, no point in counting
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   868
     * font bytes.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   869
     */
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   870
    private static boolean hasTempPermission() {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   871
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   872
        if (System.getSecurityManager() == null) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   873
            return true;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   874
        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   875
        File f = null;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   876
        boolean hasPerm = false;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   877
        try {
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 11270
diff changeset
   878
            f = Files.createTempFile("+~JT", ".tmp").toFile();
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   879
            f.delete();
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   880
            f = null;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   881
            hasPerm = true;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   882
        } catch (Throwable t) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   883
            /* inc. any kind of SecurityException */
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   884
        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   885
        return hasPerm;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   886
    }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
   887
36899
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   888
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   889
    /**
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   890
     * Returns a new array of {@code Font} decoded from the specified stream.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   891
     * The returned {@code Font[]} will have at least one element.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   892
     * <p>
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   893
     * The explicit purpose of this variation on the
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   894
     * {@code createFont(int, InputStream)} method is to support font
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   895
     * sources which represent a TrueType/OpenType font collection and
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   896
     * be able to return all individual fonts in that collection.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   897
     * Consequently this method will throw {@code FontFormatException}
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   898
     * if the data source does not contain at least one TrueType/OpenType
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   899
     * font. The same exception will also be thrown if any of the fonts in
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   900
     * the collection does not contain the required font tables.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   901
     * <p>
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   902
     * The condition "at least one", allows for the stream to represent
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   903
     * a single OpenType/TrueType font. That is, it does not have to be
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   904
     * a collection.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   905
     * Each {@code Font} element of the returned array is
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   906
     * created with a point size of 1 and style {@link #PLAIN PLAIN}.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   907
     * This base font can then be used with the {@code deriveFont}
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   908
     * methods in this class to derive new {@code Font} objects with
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   909
     * varying sizes, styles, transforms and font features.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   910
     * <p>This method does not close the {@link InputStream}.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   911
     * <p>
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   912
     * To make each {@code Font} available to Font constructors it
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   913
     * must be registered in the {@code GraphicsEnvironment} by calling
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   914
     * {@link GraphicsEnvironment#registerFont(Font) registerFont(Font)}.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   915
     * @param fontStream an {@code InputStream} object representing the
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   916
     * input data for the font or font collection.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   917
     * @return a new {@code Font[]}.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   918
     * @throws FontFormatException if the {@code fontStream} data does
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   919
     *     not contain the required font tables for any of the elements of
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   920
     *     the collection, or if it contains no fonts at all.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   921
     * @throws IOException if the {@code fontStream} cannot be completely read.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   922
     * @see GraphicsEnvironment#registerFont(Font)
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   923
     * @since 9
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   924
     */
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   925
    public static Font[] createFonts(InputStream fontStream)
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   926
        throws FontFormatException, IOException {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   927
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   928
        final int fontFormat = Font.TRUETYPE_FONT;
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   929
        if (hasTempPermission()) {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   930
            return createFont0(fontFormat, fontStream, true, null);
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   931
        }
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   932
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   933
        // Otherwise, be extra conscious of pending temp file creation and
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   934
        // resourcefully handle the temp file resources, among other things.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   935
        CreatedFontTracker tracker = CreatedFontTracker.getTracker();
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   936
        boolean acquired = false;
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   937
        try {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   938
            acquired = tracker.acquirePermit();
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   939
            if (!acquired) {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   940
                throw new IOException("Timed out waiting for resources.");
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   941
            }
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   942
            return createFont0(fontFormat, fontStream, true, tracker);
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   943
        } catch (InterruptedException e) {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   944
            throw new IOException("Problem reading font data.");
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   945
        } finally {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   946
            if (acquired) {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   947
                tracker.releasePermit();
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   948
            }
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   949
        }
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   950
    }
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   951
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   952
    /* used to implement Font.createFont */
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   953
    private Font(Font2D font2D) {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   954
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   955
        this.createdFont = true;
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   956
        this.font2DHandle = font2D.handle;
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   957
        this.name = font2D.getFontName(Locale.getDefault());
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   958
        this.style = Font.PLAIN;
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   959
        this.size = 1;
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   960
        this.pointSize = 1f;
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   961
    }
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   962
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   963
    /**
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   964
     * Returns a new array of {@code Font} decoded from the specified file.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   965
     * The returned {@code Font[]} will have at least one element.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   966
     * <p>
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   967
     * The explicit purpose of this variation on the
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   968
     * {@code createFont(int, File)} method is to support font
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   969
     * sources which represent a TrueType/OpenType font collection and
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   970
     * be able to return all individual fonts in that collection.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   971
     * Consequently this method will throw {@code FontFormatException}
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   972
     * if the data source does not contain at least one TrueType/OpenType
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   973
     * font. The same exception will also be thrown if any of the fonts in
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   974
     * the collection does not contain the required font tables.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   975
     * <p>
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   976
     * The condition "at least one", allows for the stream to represent
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   977
     * a single OpenType/TrueType font. That is, it does not have to be
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   978
     * a collection.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   979
     * Each {@code Font} element of the returned array is
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   980
     * created with a point size of 1 and style {@link #PLAIN PLAIN}.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   981
     * This base font can then be used with the {@code deriveFont}
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   982
     * methods in this class to derive new {@code Font} objects with
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   983
     * varying sizes, styles, transforms and font features.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   984
     * <p>
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   985
     * To make each {@code Font} available to Font constructors it
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   986
     * must be registered in the {@code GraphicsEnvironment} by calling
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   987
     * {@link GraphicsEnvironment#registerFont(Font) registerFont(Font)}.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   988
     * @param fontFile a {@code File} object containing the
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   989
     * input data for the font or font collection.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   990
     * @return a new {@code Font[]}.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   991
     * @throws FontFormatException if the {@code File} does
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   992
     *     not contain the required font tables for any of the elements of
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   993
     *     the collection, or if it contains no fonts at all.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   994
     * @throws IOException if the {@code fontFile} cannot be read.
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   995
     * @see GraphicsEnvironment#registerFont(Font)
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   996
     * @since 9
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   997
     */
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   998
    public static Font[] createFonts(File fontFile)
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
   999
            throws FontFormatException, IOException
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1000
    {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1001
        int fontFormat = Font.TRUETYPE_FONT;
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1002
        fontFile = checkFontFile(fontFormat, fontFile);
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1003
        FontManager fm = FontManagerFactory.getInstance();
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1004
        Font2D[] font2DArr =
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1005
            fm.createFont2D(fontFile, fontFormat, true, false, null);
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1006
        int num = font2DArr.length;
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1007
        Font[] fonts = new Font[num];
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1008
        for (int i = 0; i < num; i++) {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1009
           fonts[i] = new Font(font2DArr[i]);
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1010
        }
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1011
        return fonts;
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1012
    }
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1013
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1014
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1015
     * Returns a new {@code Font} using the specified font type
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1016
     * and input data.  The new {@code Font} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     * created with a point size of 1 and style {@link #PLAIN PLAIN}.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1018
     * This base font can then be used with the {@code deriveFont}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1019
     * methods in this class to derive new {@code Font} objects with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * varying sizes, styles, transforms and font features.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * method does not close the {@link InputStream}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1023
     * To make the {@code Font} available to Font constructors the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1024
     * returned {@code Font} must be registered in the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1025
     * {@code GraphicsEnvironment} by calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     * {@link GraphicsEnvironment#registerFont(Font) registerFont(Font)}.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1027
     * @param fontFormat the type of the {@code Font}, which is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
     * {@link #TRUETYPE_FONT TRUETYPE_FONT} if a TrueType resource is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
     * or {@link #TYPE1_FONT TYPE1_FONT} if a Type 1 resource is specified.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1030
     * @param fontStream an {@code InputStream} object representing the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * input data for the font.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1032
     * @return a new {@code Font} created with the specified font type.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1033
     * @throws IllegalArgumentException if {@code fontFormat} is not
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1034
     *     {@code TRUETYPE_FONT} or {@code TYPE1_FONT}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1035
     * @throws FontFormatException if the {@code fontStream} data does
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     *     not contain the required font tables for the specified format.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1037
     * @throws IOException if the {@code fontStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *     cannot be completely read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * @see GraphicsEnvironment#registerFont(Font)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    public static Font createFont(int fontFormat, InputStream fontStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        throws java.awt.FontFormatException, java.io.IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
18214
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1045
        if (hasTempPermission()) {
36899
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1046
            return createFont0(fontFormat, fontStream, false, null)[0];
18214
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1047
        }
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1048
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1049
        // Otherwise, be extra conscious of pending temp file creation and
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1050
        // resourcefully handle the temp file resources, among other things.
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1051
        CreatedFontTracker tracker = CreatedFontTracker.getTracker();
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1052
        boolean acquired = false;
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1053
        try {
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1054
            acquired = tracker.acquirePermit();
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1055
            if (!acquired) {
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1056
                throw new IOException("Timed out waiting for resources.");
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1057
            }
36899
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1058
            return createFont0(fontFormat, fontStream, false, tracker)[0];
18214
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1059
        } catch (InterruptedException e) {
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1060
            throw new IOException("Problem reading font data.");
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1061
        } finally {
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1062
            if (acquired) {
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1063
                tracker.releasePermit();
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1064
            }
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1065
        }
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1066
    }
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1067
36899
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1068
    private static Font[] createFont0(int fontFormat, InputStream fontStream,
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1069
                                      boolean allFonts,
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1070
                                      CreatedFontTracker tracker)
18214
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1071
        throws java.awt.FontFormatException, java.io.IOException {
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1072
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        if (fontFormat != Font.TRUETYPE_FONT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
            fontFormat != Font.TYPE1_FONT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
            throw new IllegalArgumentException ("font format not recognized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        }
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1077
        boolean copiedFontData = false;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1078
        try {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1079
            final File tFile = AccessController.doPrivileged(
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1080
                new PrivilegedExceptionAction<File>() {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1081
                    public File run() throws IOException {
13041
8477cb6992be 7143606: File.createTempFile should be improved for temporary files created by the platform.
robm
parents: 11270
diff changeset
  1082
                        return Files.createTempFile("+~JF", ".tmp").toFile();
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1083
                    }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1084
                }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1085
            );
18214
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1086
            if (tracker != null) {
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1087
                tracker.add(tFile);
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1088
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1090
            int totalSize = 0;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1091
            try {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1092
                final OutputStream outStream =
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1093
                    AccessController.doPrivileged(
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1094
                        new PrivilegedExceptionAction<OutputStream>() {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1095
                            public OutputStream run() throws IOException {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1096
                                return new FileOutputStream(tFile);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1097
                            }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1098
                        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1099
                    );
18214
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1100
                if (tracker != null) {
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1101
                    tracker.set(tFile, outStream);
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1102
                }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1103
                try {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1104
                    byte[] buf = new byte[8192];
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1105
                    for (;;) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1106
                        int bytesRead = fontStream.read(buf);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1107
                        if (bytesRead < 0) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1108
                            break;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1109
                        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1110
                        if (tracker != null) {
11270
d7b0b63bd082 7117334: Warnings cleanup day: reduce number of javac warnings in the java.awt package
bagiras
parents: 9035
diff changeset
  1111
                            if (totalSize+bytesRead > CreatedFontTracker.MAX_FILE_SIZE) {
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1112
                                throw new IOException("File too big.");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1113
                            }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1114
                            if (totalSize+tracker.getNumBytes() >
11270
d7b0b63bd082 7117334: Warnings cleanup day: reduce number of javac warnings in the java.awt package
bagiras
parents: 9035
diff changeset
  1115
                                CreatedFontTracker.MAX_TOTAL_BYTES)
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1116
                              {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1117
                                throw new IOException("Total files too big.");
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1118
                            }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1119
                            totalSize += bytesRead;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1120
                            tracker.addBytes(bytesRead);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1121
                        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1122
                        outStream.write(buf, 0, bytesRead);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1123
                    }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1124
                    /* don't close the input stream */
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1125
                } finally {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1126
                    outStream.close();
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1127
                }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1128
                /* After all references to a Font2D are dropped, the file
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1129
                 * will be removed. To support long-lived AppContexts,
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1130
                 * we need to then decrement the byte count by the size
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1131
                 * of the file.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1132
                 * If the data isn't a valid font, the implementation will
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1133
                 * delete the tmp file and decrement the byte count
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1134
                 * in the tracker object before returning from the
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1135
                 * constructor, so we can set 'copiedFontData' to true here
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1136
                 * without waiting for the results of that constructor.
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1137
                 */
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1138
                copiedFontData = true;
36899
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1139
                FontManager fm = FontManagerFactory.getInstance();
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1140
                 Font2D[] font2DArr =
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1141
                    fm.createFont2D(tFile, fontFormat, allFonts, true, tracker);
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1142
                int num = font2DArr.length;
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1143
                Font[] fonts = new Font[num];
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1144
                for (int i = 0; i < num; i++) {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1145
                   fonts[i] = new Font(font2DArr[i]);
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1146
                }
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1147
                return fonts;
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1148
            } finally {
18214
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1149
                if (tracker != null) {
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1150
                    tracker.remove(tFile);
e9eff0f1f1df 8001038: Resourcefully handle resources
jgodinez
parents: 13041
diff changeset
  1151
                }
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1152
                if (!copiedFontData) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1153
                    if (tracker != null) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1154
                        tracker.subBytes(totalSize);
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1155
                    }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1156
                    AccessController.doPrivileged(
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1157
                        new PrivilegedExceptionAction<Void>() {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1158
                            public Void run() {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1159
                                tFile.delete();
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1160
                                return null;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1161
                            }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1162
                        }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1163
                    );
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1164
                }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1165
            }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1166
        } catch (Throwable t) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1167
            if (t instanceof FontFormatException) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1168
                throw (FontFormatException)t;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1169
            }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1170
            if (t instanceof IOException) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1171
                throw (IOException)t;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1172
            }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1173
            Throwable cause = t.getCause();
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1174
            if (cause instanceof FontFormatException) {
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1175
                throw (FontFormatException)cause;
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1176
            }
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1177
            throw new IOException("Problem reading font data.");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1182
     * Returns a new {@code Font} using the specified font type
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1183
     * and the specified font file.  The new {@code Font} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     * created with a point size of 1 and style {@link #PLAIN PLAIN}.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1185
     * This base font can then be used with the {@code deriveFont}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1186
     * methods in this class to derive new {@code Font} objects with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
     * varying sizes, styles, transforms and font features.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1188
     * @param fontFormat the type of the {@code Font}, which is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
     * {@link #TRUETYPE_FONT TRUETYPE_FONT} if a TrueType resource is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * specified or {@link #TYPE1_FONT TYPE1_FONT} if a Type 1 resource is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     * specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * So long as the returned font, or its derived fonts are referenced
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1193
     * the implementation may continue to access {@code fontFile}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     * to retrieve font data. Thus the results are undefined if the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     * is changed, or becomes inaccessible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
     * <p>
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1197
     * To make the {@code Font} available to Font constructors the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1198
     * returned {@code Font} must be registered in the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1199
     * {@code GraphicsEnvironment} by calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * {@link GraphicsEnvironment#registerFont(Font) registerFont(Font)}.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1201
     * @param fontFile a {@code File} object representing the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     * input data for the font.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1203
     * @return a new {@code Font} created with the specified font type.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1204
     * @throws IllegalArgumentException if {@code fontFormat} is not
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1205
     *     {@code TRUETYPE_FONT} or {@code TYPE1_FONT}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1206
     * @throws NullPointerException if {@code fontFile} is null.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1207
     * @throws IOException if the {@code fontFile} cannot be read.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1208
     * @throws FontFormatException if {@code fontFile} does
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
     *     not contain the required font tables for the specified format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
     * @throws SecurityException if the executing code does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
     * permission to read from the file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
     * @see GraphicsEnvironment#registerFont(Font)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
    public static Font createFont(int fontFormat, File fontFile)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
        throws java.awt.FontFormatException, java.io.IOException {
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1217
36899
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1218
        fontFile = checkFontFile(fontFormat, fontFile);
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1219
        return new Font(fontFile, fontFormat, false, null);
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1220
    }
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1221
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1222
    private static File checkFontFile(int fontFormat, File fontFile)
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1223
        throws FontFormatException, IOException {
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1224
2606
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1225
        fontFile = new File(fontFile.getPath());
09ad5edb5330 6632886: Font.createFont can be persuaded to leak temporary files
prr
parents: 547
diff changeset
  1226
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        if (fontFormat != Font.TRUETYPE_FONT &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            fontFormat != Font.TYPE1_FONT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            throw new IllegalArgumentException ("font format not recognized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
            FilePermission filePermission =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                new FilePermission(fontFile.getPath(), "read");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
            sm.checkPermission(filePermission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        if (!fontFile.canRead()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            throw new IOException("Can't read " + fontFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        }
36899
e766024a0873 8055463: Need public API allowing full access to font collections in Font.createFont()
prr
parents: 35667
diff changeset
  1240
        return fontFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
     * Returns a copy of the transform associated with this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1245
     * {@code Font}.  This transform is not necessarily the one
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     * used to construct the font.  If the font has algorithmic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * superscripting or width adjustment, this will be incorporated
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1248
     * into the returned {@code AffineTransform}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * Typically, fonts will not be transformed.  Clients generally
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     * should call {@link #isTransformed} first, and only call this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1252
     * method if {@code isTransformed} returns true.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * @return an {@link AffineTransform} object representing the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1255
     *          transform attribute of this {@code Font} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    public AffineTransform getTransform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        /* The most common case is the identity transform.  Most callers
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
         * should call isTransformed() first, to decide if they need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
         * get the transform, but some may not.  Here we check to see
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
         * if we have a nonidentity transform, and only do the work to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
         * fetch and/or compute it if so, otherwise we return a new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
         * identity transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
         * Note that the transform is _not_ necessarily the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
         * the transform passed in as an Attribute in a Map, as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
         * transform returned will also reflect the effects of WIDTH and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
         * SUPERSCRIPT attributes.  Clients who want the actual transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
         * need to call getRequestedAttributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        if (nonIdentityTx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            AttributeValues values = getAttributeValues();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
            AffineTransform at = values.isNonDefault(ETRANSFORM)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
                ? new AffineTransform(values.getTransform())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
                : new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            if (values.getSuperscript() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                // can't get ascent and descent here, recursive call to this fn,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
                // so use pointsize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
                // let users combine super- and sub-scripting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
                int superscript = values.getSuperscript();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                double trans = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
                int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                boolean up = superscript > 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
                int sign = up ? -1 : 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                int ss = up ? superscript : -superscript;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
                while ((ss & 7) > n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
                    int newn = ss & 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
                    trans += sign * (ssinfo[newn] - ssinfo[n]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                    ss >>= 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
                    sign = -sign;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                    n = newn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                trans *= pointSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                double scale = Math.pow(2./3., n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                at.preConcatenate(AffineTransform.getTranslateInstance(0, trans));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                at.scale(scale, scale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
                // note on placement and italics
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                // We preconcatenate the transform because we don't want to translate along
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
                // the italic angle, but purely perpendicular to the baseline.  While this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                // looks ok for superscripts, it can lead subscripts to stack on each other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
                // and bring the following text too close.  The way we deal with potential
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
                // collisions that can occur in the case of italics is by adjusting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                // horizontal spacing of the adjacent glyphvectors.  Examine the italic
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                // angle of both vectors, if one is non-zero, compute the minimum ascent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
                // and descent, and then the x position at each for each vector along its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
                // italic angle starting from its (offset) baseline.  Compute the difference
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
                // between the x positions and use the maximum difference to adjust the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                // position of the right gv.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
            if (values.isNonDefault(EWIDTH)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
                at.scale(values.getWidth(), 1f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
            return at;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
        return new AffineTransform();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
    // x = r^0 + r^1 + r^2... r^n
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
    // rx = r^1 + r^2 + r^3... r^(n+1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
    // x - rx = r^0 - r^(n+1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
    // x (1 - r) = r^0 - r^(n+1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
    // x = (r^0 - r^(n+1)) / (1 - r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
    // x = (1 - r^(n+1)) / (1 - r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
    // scale ratio is 2/3
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    // trans = 1/2 of ascent * x
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
    // assume ascent is 3/4 of point size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    private static final float[] ssinfo = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        0.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        0.375f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        0.625f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
        0.7916667f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
        0.9027778f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
        0.9768519f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
        1.0262346f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
        1.0591564f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1351
     * Returns the family name of this {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * <p>The family name of a font is font specific. Two fonts such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * Helvetica Italic and Helvetica Bold have the same family name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
     * <i>Helvetica</i>, whereas their font face names are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
     * <i>Helvetica Bold</i> and <i>Helvetica Italic</i>. The list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
     * available family names may be obtained by using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * {@link GraphicsEnvironment#getAvailableFontFamilyNames()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1360
     * <p>Use {@code getName} to get the logical name of the font.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1361
     * Use {@code getFontName} to get the font face name of the font.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1362
     * @return a {@code String} that is the family name of this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1363
     *          {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * @see #getName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * @see #getFontName
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
  1367
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
    public String getFamily() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        return getFamily_NoClientCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
    // NOTE: This method is called by privileged threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
    //       We implement this functionality in a package-private
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
    //       method to insure that it cannot be overridden by client
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
    //       subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
    //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    final String getFamily_NoClientCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
        return getFamily(Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1382
     * Returns the family name of this {@code Font}, localized for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * the specified locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * <p>The family name of a font is font specific. Two fonts such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     * Helvetica Italic and Helvetica Bold have the same family name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * <i>Helvetica</i>, whereas their font face names are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * <i>Helvetica Bold</i> and <i>Helvetica Italic</i>. The list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * available family names may be obtained by using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * {@link GraphicsEnvironment#getAvailableFontFamilyNames()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1392
     * <p>Use {@code getFontName} to get the font face name of the font.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * @param l locale for which to get the family name
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1394
     * @return a {@code String} representing the family name of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     *          font, localized for the specified locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * @see #getFontName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * @see java.util.Locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
    public String getFamily(Locale l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
            throw new NullPointerException("null locale doesn't mean default");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
        return getFont2D().getFamilyName(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1408
     * Returns the postscript name of this {@code Font}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1409
     * Use {@code getFamily} to get the family name of the font.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1410
     * Use {@code getFontName} to get the font face name of the font.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1411
     * @return a {@code String} representing the postscript name of
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1412
     *          this {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
    public String getPSName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        return getFont2D().getPostscriptName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1420
     * Returns the logical name of this {@code Font}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1421
     * Use {@code getFamily} to get the family name of the font.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1422
     * Use {@code getFontName} to get the font face name of the font.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1423
     * @return a {@code String} representing the logical name of
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1424
     *          this {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
     * @see #getFamily
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     * @see #getFontName
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
  1427
     * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    public String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1434
     * Returns the font face name of this {@code Font}.  For example,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * Helvetica Bold could be returned as a font face name.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1436
     * Use {@code getFamily} to get the family name of the font.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1437
     * Use {@code getName} to get the logical name of the font.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1438
     * @return a {@code String} representing the font face name of
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1439
     *          this {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * @see #getFamily
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
     * @see #getName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
    public String getFontName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
      return getFontName(Locale.getDefault());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1449
     * Returns the font face name of the {@code Font}, localized
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
     * for the specified locale. For example, Helvetica Fett could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
     * returned as the font face name.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1452
     * Use {@code getFamily} to get the family name of the font.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * @param l a locale for which to get the font face name
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1454
     * @return a {@code String} representing the font face name,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     *          localized for the specified locale.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * @see #getFamily
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * @see java.util.Locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    public String getFontName(Locale l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
        if (l == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
            throw new NullPointerException("null locale doesn't mean default");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        return getFont2D().getFontName(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1467
     * Returns the style of this {@code Font}.  The style can be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
     * PLAIN, BOLD, ITALIC, or BOLD+ITALIC.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1469
     * @return the style of this {@code Font}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * @see #isPlain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
     * @see #isBold
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     * @see #isItalic
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
  1473
     * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
    public int getStyle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        return style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1480
     * Returns the point size of this {@code Font}, rounded to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     * an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     * Most users are familiar with the idea of using <i>point size</i> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * specify the size of glyphs in a font. This point size defines a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     * measurement between the baseline of one line to the baseline of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * following line in a single spaced text document. The point size is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     * based on <i>typographic points</i>, approximately 1/72 of an inch.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
     * The Java(tm)2D API adopts the convention that one point is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
     * equivalent to one unit in user coordinates.  When using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
     * normalized transform for converting user space coordinates to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
     * device space coordinates 72 user
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
     * space units equal 1 inch in device space.  In this case one point
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
     * is 1/72 of an inch.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1494
     * @return the point size of this {@code Font} in 1/72 of an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
     *          inch units.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
     * @see #getSize2D
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * @see GraphicsConfiguration#getDefaultTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
     * @see GraphicsConfiguration#getNormalizingTransform
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
  1499
     * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    public int getSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
        return size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1506
     * Returns the point size of this {@code Font} in
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1507
     * {@code float} value.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1508
     * @return the point size of this {@code Font} as a
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1509
     * {@code float} value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
     * @see #getSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
    public float getSize2D() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        return pointSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1518
     * Indicates whether or not this {@code Font} object's style is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
     * PLAIN.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1520
     * @return    {@code true} if this {@code Font} has a
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20455
diff changeset
  1521
     *            PLAIN style;
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1522
     *            {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
     * @see       java.awt.Font#getStyle
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
  1524
     * @since     1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
    public boolean isPlain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
        return style == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1531
     * Indicates whether or not this {@code Font} object's style is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
     * BOLD.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1533
     * @return    {@code true} if this {@code Font} object's
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
     *            style is BOLD;
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1535
     *            {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
     * @see       java.awt.Font#getStyle
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
  1537
     * @since     1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
    public boolean isBold() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
        return (style & BOLD) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1544
     * Indicates whether or not this {@code Font} object's style is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
     * ITALIC.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1546
     * @return    {@code true} if this {@code Font} object's
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
     *            style is ITALIC;
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1548
     *            {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
     * @see       java.awt.Font#getStyle
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
  1550
     * @since     1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    public boolean isItalic() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
        return (style & ITALIC) != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1557
     * Indicates whether or not this {@code Font} object has a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
     * transform that affects its size in addition to the Size
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
     * attribute.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1560
     * @return  {@code true} if this {@code Font} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
     *          has a non-identity AffineTransform attribute.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1562
     *          {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
     * @see     java.awt.Font#getTransform
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
     * @since   1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    public boolean isTransformed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
        return nonIdentityTx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     * Return true if this Font contains attributes that require extra
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * layout processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     * @return true if the font has layout attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    public boolean hasLayoutAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
        return hasLayoutAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1581
     * Returns a {@code Font} object from the system properties list.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1582
     * {@code nm} is treated as the name of a system property to be
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1583
     * obtained.  The {@code String} value of this property is then
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1584
     * interpreted as a {@code Font} object according to the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1585
     * specification of {@code Font.decode(String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * If the specified property is not found, or the executing code does
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * not have permission to read the property, null is returned instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * @param nm the property name
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1590
     * @return a {@code Font} object that the property name
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     *          describes, or null if no such property exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * @throws NullPointerException if nm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * @see #decode(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    public static Font getFont(String nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
        return getFont(nm, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1601
     * Returns the {@code Font} that the {@code str}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * argument describes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * To ensure that this method returns the desired Font,
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1604
     * format the {@code str} parameter in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * one of these ways
21957
97758de70fbd 8028019: AWT Doclint warning/error cleanup
rriggs
parents: 21278
diff changeset
  1606
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * <li><em>fontname-style-pointsize</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * <li><em>fontname-pointsize</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * <li><em>fontname-style</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     * <li><em>fontname</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * <li><em>fontname style pointsize</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     * <li><em>fontname pointsize</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
     * <li><em>fontname style</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
     * <li><em>fontname</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
     * in which <i>style</i> is one of the four
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
     * case-insensitive strings:
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1619
     * {@code "PLAIN"}, {@code "BOLD"}, {@code "BOLDITALIC"}, or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1620
     * {@code "ITALIC"}, and pointsize is a positive decimal integer
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * representation of the point size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     * For example, if you want a font that is Arial, bold, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * a point size of 18, you would call this method with:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * "Arial-BOLD-18".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     * This is equivalent to calling the Font constructor :
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1626
     * {@code new Font("Arial", Font.BOLD, 18);}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * and the values are interpreted as specified by that constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * A valid trailing decimal field is always interpreted as the pointsize.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * Therefore a fontname containing a trailing decimal value should not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * be used in the fontname only form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
     * If a style name field is not one of the valid style strings, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * interpreted as part of the font name, and the default style is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     * Only one of ' ' or '-' may be used to separate fields in the input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     * The identified separator is the one closest to the end of the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * which separates a valid pointsize, or a valid style name from
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     * the rest of the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
     * Null (empty) pointsize and style fields are treated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
     * as valid fields with the default value for that field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
     *<p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
     * Some font names may include the separator characters ' ' or '-'.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1644
     * If {@code str} is not formed with 3 components, e.g. such that
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1645
     * {@code style} or {@code pointsize} fields are not present in
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1646
     * {@code str}, and {@code fontname} also contains a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
     * character determined to be the separator character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
     * then these characters where they appear as intended to be part of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1649
     * {@code fontname} may instead be interpreted as separators
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
     * so the font name may not be properly recognised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
     * The default size is 12 and the default style is PLAIN.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1654
     * If {@code str} does not specify a valid size, the returned
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1655
     * {@code Font} has a size of 12.  If {@code str} does not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * specify a valid style, the returned Font has a style of PLAIN.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * If you do not specify a valid font name in
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1658
     * the {@code str} argument, this method will return
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     * a font with the family name "Dialog".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
     * To determine what font family names are available on
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * your system, use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     * {@link GraphicsEnvironment#getAvailableFontFamilyNames()} method.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1663
     * If {@code str} is {@code null}, a new {@code Font}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     * is returned with the family name "Dialog", a size of 12 and a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * PLAIN style.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1666
     * @param str the name of the font, or {@code null}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1667
     * @return the {@code Font} object that {@code str}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1668
     *          describes, or a new default {@code Font} if
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1669
     *          {@code str} is {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
     * @see #getFamily
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
  1671
     * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
    public static Font decode(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        String fontName = str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        String styleName = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
        int fontSize = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        int fontStyle = Font.PLAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
        if (str == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
            return new Font(DIALOG, fontStyle, fontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
        int lastHyphen = str.lastIndexOf('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
        int lastSpace = str.lastIndexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        char sepChar = (lastHyphen > lastSpace) ? '-' : ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
        int sizeIndex = str.lastIndexOf(sepChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        int styleIndex = str.lastIndexOf(sepChar, sizeIndex-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
        int strlen = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
        if (sizeIndex > 0 && sizeIndex+1 < strlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
                fontSize =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
                    Integer.valueOf(str.substring(sizeIndex+1)).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
                if (fontSize <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
                    fontSize = 12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
                /* It wasn't a valid size, if we didn't also find the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                 * start of the style string perhaps this is the style */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
                styleIndex = sizeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
                sizeIndex = strlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
                if (str.charAt(sizeIndex-1) == sepChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
                    sizeIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        if (styleIndex >= 0 && styleIndex+1 < strlen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            styleName = str.substring(styleIndex+1, sizeIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            styleName = styleName.toLowerCase(Locale.ENGLISH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            if (styleName.equals("bolditalic")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
                fontStyle = Font.BOLD | Font.ITALIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
            } else if (styleName.equals("italic")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
                fontStyle = Font.ITALIC;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
            } else if (styleName.equals("bold")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
                fontStyle = Font.BOLD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
            } else if (styleName.equals("plain")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
                fontStyle = Font.PLAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
                /* this string isn't any of the expected styles, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
                 * assume its part of the font name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
                styleIndex = sizeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
                if (str.charAt(styleIndex-1) == sepChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
                    styleIndex--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            fontName = str.substring(0, styleIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
            int fontEnd = strlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            if (styleIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                fontEnd = styleIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
            } else if (sizeIndex > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                fontEnd = sizeIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
            if (fontEnd > 0 && str.charAt(fontEnd-1) == sepChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
                fontEnd--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
            fontName = str.substring(0, fontEnd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
        return new Font(fontName, fontStyle, fontSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1747
     * Gets the specified {@code Font} from the system properties
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1748
     * list.  As in the {@code getProperty} method of
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1749
     * {@code System}, the first
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * argument is treated as the name of a system property to be
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1751
     * obtained.  The {@code String} value of this property is then
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1752
     * interpreted as a {@code Font} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
     * The property value should be one of the forms accepted by
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1755
     * {@code Font.decode(String)}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     * If the specified property is not found, or the executing code does not
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1757
     * have permission to read the property, the {@code font}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     * argument is returned instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
     * @param nm the case-insensitive property name
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1760
     * @param font a default {@code Font} to return if property
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1761
     *          {@code nm} is not defined
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1762
     * @return    the {@code Font} value of the property.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
     * @throws NullPointerException if nm is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
     * @see #decode(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    public static Font getFont(String nm, Font font) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
        String str = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
            str =System.getProperty(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
        } catch(SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        if (str == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            return font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
        return decode ( str );
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
    transient int hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1780
     * Returns a hashcode for this {@code Font}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1781
     * @return     a hashcode value for this {@code Font}.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
  1782
     * @since      1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
        if (hash == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
            hash = name.hashCode() ^ style ^ size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
            /* It is possible many fonts differ only in transform.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
             * So include the transform in the hash calculation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
             * nonIdentityTx is set whenever there is a transform in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
             * 'values'. The tests for null are required because it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
             * also be set for other reasons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            if (nonIdentityTx &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
                values != null && values.getTransform() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
                hash ^= values.getTransform().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        return hash;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1802
     * Compares this {@code Font} object to the specified
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1803
     * {@code Object}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1804
     * @param obj the {@code Object} to compare
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1805
     * @return {@code true} if the objects are the same
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1806
     *          or if the argument is a {@code Font} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
     *          describing the same font as this object;
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1808
     *          {@code false} otherwise.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
  1809
     * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
        if (obj == this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
39866
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1816
        if (obj instanceof Font) {
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1817
            Font font = (Font)obj;
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1818
            if (size == font.size &&
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1819
                style == font.style &&
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1820
                nonIdentityTx == font.nonIdentityTx &&
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1821
                hasLayoutAttributes == font.hasLayoutAttributes &&
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1822
                pointSize == font.pointSize &&
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1823
                name.equals(font.name)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
39866
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1825
                /* 'values' is usually initialized lazily, except when
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1826
                 * the font is constructed from a Map, or derived using
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1827
                 * a Map or other values. So if only one font has
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1828
                 * the field initialized we need to initialize it in
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1829
                 * the other instance and compare.
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1830
                 */
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1831
                if (values == null) {
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1832
                    if (font.values == null) {
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1833
                        return true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
                    } else {
39866
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1835
                        return getAttributeValues().equals(font.values);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
                    }
39866
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1837
                } else {
ad2a71a4f495 8160721: Avoid deoptimizations in Font.equals.
goetz
parents: 37562
diff changeset
  1838
                    return values.equals(font.getAttributeValues());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1846
     * Converts this {@code Font} object to a {@code String}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
     * representation.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1848
     * @return     a {@code String} representation of this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1849
     *          {@code Font} object.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
  1850
     * @since      1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
    // NOTE: This method may be called by privileged threads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
    //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
        String  strStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        if (isBold()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
            strStyle = isItalic() ? "bolditalic" : "bold";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
            strStyle = isItalic() ? "italic" : "plain";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        return getClass().getName() + "[family=" + getFamily() + ",name=" + name + ",style=" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
            strStyle + ",size=" + size + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    } // toString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1868
    /** Serialization support.  A {@code readObject}
26749
b6598aa90114 8055326: Fix typos in client-related packages
serb
parents: 25859
diff changeset
  1869
     *  method is necessary because the constructor creates
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
     *  the font's peer, and we can't serialize the peer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
     *  Similarly the computed font "family" may be different
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1872
     *  at {@code readObject} time than at
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1873
     *  {@code writeObject} time.  An integer version is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
     *  written so that future versions of this class will be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
     *  able to recognize serialized output from this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1878
     * The {@code Font} Serializable Data Form.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
    private int fontSerializedDataVersion = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
     * Writes default serializable fields to a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1887
     * @param s the {@code ObjectOutputStream} to write
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
     * @see AWTEventMulticaster#save(ObjectOutputStream, String, EventListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
     * @see #readObject(java.io.ObjectInputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
    private void writeObject(java.io.ObjectOutputStream s)
51116
7e34f3da2293 8203263: Remove unnecessary throws clauses from serialization-related methods
darcy
parents: 48285
diff changeset
  1892
      throws java.io.IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
        if (values != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
          synchronized(values) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
            // transient
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
            fRequestedAttributes = values.toSerializableHashtable();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
            s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
            fRequestedAttributes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
          s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1907
     * Reads the {@code ObjectInputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     * Unrecognized keys or values will be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1910
     * @param s the {@code ObjectInputStream} to read
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * @see #writeObject(java.io.ObjectOutputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    private void readObject(java.io.ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
      throws java.lang.ClassNotFoundException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
             java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        if (pointSize == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
            pointSize = (float)size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        // Handle fRequestedAttributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
        // in 1.5, we always streamed out the font values plus
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        // TRANSFORM, SUPERSCRIPT, and WIDTH, regardless of whether the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        // values were default or not.  In 1.6 we only stream out
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
        // defined values.  So, 1.6 streams in from a 1.5 stream,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        // it check each of these values and 'undefines' it if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
        // value is the default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        if (fRequestedAttributes != null) {
58632
fbab568169c4 8224915: Better serial attributes
prr
parents: 55366
diff changeset
  1932
            try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            values = getAttributeValues(); // init
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
            AttributeValues extras =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
                AttributeValues.fromSerializableHashtable(fRequestedAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
            if (!AttributeValues.is16Hashtable(fRequestedAttributes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
                extras.unsetDefault(); // if legacy stream, undefine these
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
            values = getAttributeValues().merge(extras);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
            this.nonIdentityTx = values.anyNonDefault(EXTRA_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
            this.hasLayoutAttributes =  values.anyNonDefault(LAYOUT_MASK);
58632
fbab568169c4 8224915: Better serial attributes
prr
parents: 55366
diff changeset
  1942
            } catch (Throwable t) {
fbab568169c4 8224915: Better serial attributes
prr
parents: 55366
diff changeset
  1943
                throw new IOException(t);
fbab568169c4 8224915: Better serial attributes
prr
parents: 55366
diff changeset
  1944
            } finally {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
            fRequestedAttributes = null; // don't need it any more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
    }
58632
fbab568169c4 8224915: Better serial attributes
prr
parents: 55366
diff changeset
  1948
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1951
     * Returns the number of glyphs in this {@code Font}. Glyph codes
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1952
     * for this {@code Font} range from 0 to
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1953
     * {@code getNumGlyphs()} - 1.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1954
     * @return the number of glyphs in this {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
    public int getNumGlyphs() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        return  getFont2D().getNumGlyphs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1962
     * Returns the glyphCode which is used when this {@code Font}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     * does not have a glyph for a specified unicode code point.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1964
     * @return the glyphCode of this {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
    public int getMissingGlyphCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
        return getFont2D().getMissingGlyphCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * Returns the baseline appropriate for displaying this character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * Large fonts can support different writing systems, and each system can
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     * use a different baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * The character argument determines the writing system to use. Clients
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     * should not assume all characters use the same baseline.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
     * @param c a character used to identify the writing system
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
     * @return the baseline appropriate for the specified character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
     * @see LineMetrics#getBaselineOffsets
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
     * @see #ROMAN_BASELINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
     * @see #CENTER_BASELINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
     * @see #HANGING_BASELINE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
    public byte getBaselineFor(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
        return getFont2D().getBaselineFor(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
     * Returns a map of font attributes available in this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1993
     * {@code Font}.  Attributes include things like ligatures and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
     * glyph substitution.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  1995
     * @return the attributes map of this {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
    public Map<TextAttribute,?> getAttributes(){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
        return new AttributeMap(getAttributeValues());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
     * Returns the keys of all the attributes supported by this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2003
     * {@code Font}.  These attributes can be used to derive other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
     * fonts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
     * @return an array containing the keys of all the attributes
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2006
     *          supported by this {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
    public Attribute[] getAvailableAttributes() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
        // FONT is not supported by Font
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
52248
2e330da7cbf4 8211300: Convert C-style array declarations in JDK client code
tvaleev
parents: 51116
diff changeset
  2012
        Attribute[] attributes = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
            TextAttribute.FAMILY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
            TextAttribute.WEIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
            TextAttribute.WIDTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
            TextAttribute.POSTURE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
            TextAttribute.SIZE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
            TextAttribute.TRANSFORM,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
            TextAttribute.SUPERSCRIPT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
            TextAttribute.CHAR_REPLACEMENT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
            TextAttribute.FOREGROUND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
            TextAttribute.BACKGROUND,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
            TextAttribute.UNDERLINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
            TextAttribute.STRIKETHROUGH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
            TextAttribute.RUN_DIRECTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
            TextAttribute.BIDI_EMBEDDING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
            TextAttribute.JUSTIFICATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
            TextAttribute.INPUT_METHOD_HIGHLIGHT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            TextAttribute.INPUT_METHOD_UNDERLINE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            TextAttribute.SWAP_COLORS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            TextAttribute.NUMERIC_SHAPING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            TextAttribute.KERNING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            TextAttribute.LIGATURES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            TextAttribute.TRACKING,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
        return attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2041
     * Creates a new {@code Font} object by replicating this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2042
     * {@code Font} object and applying a new style and size.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2043
     * @param style the style for the new {@code Font}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2044
     * @param size the size for the new {@code Font}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2045
     * @return a new {@code Font} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
    public Font deriveFont(int style, float size){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
        if (values == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
            return new Font(name, style, size, createdFont, font2DHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
        AttributeValues newValues = getAttributeValues().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
        int oldStyle = (this.style != style) ? this.style : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        applyStyle(style, newValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        newValues.setSize(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
        return new Font(newValues, null, oldStyle, createdFont, font2DHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2060
     * Creates a new {@code Font} object by replicating this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2061
     * {@code Font} object and applying a new style and transform.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2062
     * @param style the style for the new {@code Font}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2063
     * @param trans the {@code AffineTransform} associated with the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2064
     * new {@code Font}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2065
     * @return a new {@code Font} object.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2066
     * @throws IllegalArgumentException if {@code trans} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2067
     *         {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    public Font deriveFont(int style, AffineTransform trans){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
        AttributeValues newValues = getAttributeValues().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
        int oldStyle = (this.style != style) ? this.style : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
        applyStyle(style, newValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        applyTransform(trans, newValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        return new Font(newValues, null, oldStyle, createdFont, font2DHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2079
     * Creates a new {@code Font} object by replicating the current
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2080
     * {@code Font} object and applying a new size to it.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2081
     * @param size the size for the new {@code Font}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2082
     * @return a new {@code Font} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
    public Font deriveFont(float size){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        if (values == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
            return new Font(name, style, size, createdFont, font2DHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
        AttributeValues newValues = getAttributeValues().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        newValues.setSize(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        return new Font(newValues, null, -1, createdFont, font2DHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2095
     * Creates a new {@code Font} object by replicating the current
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2096
     * {@code Font} object and applying a new transform to it.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2097
     * @param trans the {@code AffineTransform} associated with the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2098
     * new {@code Font}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2099
     * @return a new {@code Font} object.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2100
     * @throws IllegalArgumentException if {@code trans} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2101
     *         {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
    public Font deriveFont(AffineTransform trans){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        AttributeValues newValues = getAttributeValues().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        applyTransform(trans, newValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        return new Font(newValues, null, -1, createdFont, font2DHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2111
     * Creates a new {@code Font} object by replicating the current
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2112
     * {@code Font} object and applying a new style to it.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2113
     * @param style the style for the new {@code Font}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2114
     * @return a new {@code Font} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
    public Font deriveFont(int style){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        if (values == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
           return new Font(name, style, size, createdFont, font2DHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
        AttributeValues newValues = getAttributeValues().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        int oldStyle = (this.style != style) ? this.style : -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        applyStyle(style, newValues);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
        return new Font(newValues, null, oldStyle, createdFont, font2DHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2128
     * Creates a new {@code Font} object by replicating the current
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2129
     * {@code Font} object and applying a new set of font attributes
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
     * to it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
     * @param attributes a map of attributes enabled for the new
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2133
     * {@code Font}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2134
     * @return a new {@code Font} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
    public Font deriveFont(Map<? extends Attribute, ?> attributes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
        if (attributes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
        AttributeValues newValues = getAttributeValues().clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
        newValues.merge(attributes, RECOGNIZED_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
        return new Font(newValues, name, style, createdFont, font2DHandle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2148
     * Checks if this {@code Font} has a glyph for the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
     * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
     *
55366
de1d2a535c08 8225368: broken links in java.desktop files
psadhukhan
parents: 54397
diff changeset
  2151
     * <p> <b>Note:</b> This method cannot handle
de1d2a535c08 8225368: broken links in java.desktop files
psadhukhan
parents: 54397
diff changeset
  2152
     * <a href="../../../java.base/java/lang/Character.html#supplementary">
de1d2a535c08 8225368: broken links in java.desktop files
psadhukhan
parents: 54397
diff changeset
  2153
     * supplementary characters</a>.
de1d2a535c08 8225368: broken links in java.desktop files
psadhukhan
parents: 54397
diff changeset
  2154
     * To support all Unicode characters, including
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
     * supplementary characters, use the {@link #canDisplay(int)}
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2156
     * method or {@code canDisplayUpTo} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
     * @param c the character for which a glyph is needed
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2159
     * @return {@code true} if this {@code Font} has a glyph for this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2160
     *          character; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
    public boolean canDisplay(char c){
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
        return getFont2D().canDisplay(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2168
     * Checks if this {@code Font} has a glyph for the specified
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
     * character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
     * @param codePoint the character (Unicode code point) for which a glyph
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
     *        is needed.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2173
     * @return {@code true} if this {@code Font} has a glyph for the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2174
     *          character; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     * @throws IllegalArgumentException if the code point is not a valid Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     *          code point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * @see Character#isValidCodePoint(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
    public boolean canDisplay(int codePoint) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
        if (!Character.isValidCodePoint(codePoint)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2182
            throw new IllegalArgumentException("invalid code point: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2183
                                               Integer.toHexString(codePoint));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2184
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        return getFont2D().canDisplay(codePoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2189
     * Indicates whether or not this {@code Font} can display a
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2190
     * specified {@code String}.  For strings with Unicode encoding,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
     * it is important to know if a particular font can display the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2192
     * string. This method returns an offset into the {@code String}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2193
     * {@code str} which is the first character this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2194
     * {@code Font} cannot display without using the missing glyph
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2195
     * code. If the {@code Font} can display all characters, -1 is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
     * returned.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2197
     * @param str a {@code String} object
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2198
     * @return an offset into {@code str} that points
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2199
     *          to the first character in {@code str} that this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2200
     *          {@code Font} cannot display; or {@code -1} if
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2201
     *          this {@code Font} can display all characters in
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2202
     *          {@code str}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
    public int canDisplayUpTo(String str) {
8520
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2206
        Font2D font2d = getFont2D();
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2207
        int len = str.length();
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2208
        for (int i = 0; i < len; i++) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2209
            char c = str.charAt(i);
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2210
            if (font2d.canDisplay(c)) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2211
                continue;
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2212
            }
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2213
            if (!Character.isHighSurrogate(c)) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2214
                return i;
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2215
            }
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2216
            if (!font2d.canDisplay(str.codePointAt(i))) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2217
                return i;
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2218
            }
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2219
            i++;
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2220
        }
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2221
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2225
     * Indicates whether or not this {@code Font} can display
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2226
     * the characters in the specified {@code text}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2227
     * starting at {@code start} and ending at
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2228
     * {@code limit}.  This method is a convenience overload.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2229
     * @param text the specified array of {@code char} values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
     * @param start the specified starting offset (in
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2231
     *              {@code char}s) into the specified array of
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2232
     *              {@code char} values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2233
     * @param limit the specified ending offset (in
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2234
     *              {@code char}s) into the specified array of
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2235
     *              {@code char} values
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2236
     * @return an offset into {@code text} that points
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2237
     *          to the first character in {@code text} that this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2238
     *          {@code Font} cannot display; or {@code -1} if
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2239
     *          this {@code Font} can display all characters in
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2240
     *          {@code text}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2242
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2243
    public int canDisplayUpTo(char[] text, int start, int limit) {
8520
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2244
        Font2D font2d = getFont2D();
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2245
        for (int i = start; i < limit; i++) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2246
            char c = text[i];
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2247
            if (font2d.canDisplay(c)) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2248
                continue;
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2249
            }
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2250
            if (!Character.isHighSurrogate(c)) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2251
                return i;
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2252
            }
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2253
            if (!font2d.canDisplay(Character.codePointAt(text, i, limit))) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2254
                return i;
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2255
            }
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2256
            i++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
        }
8520
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2258
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2262
     * Indicates whether or not this {@code Font} can display the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2263
     * text specified by the {@code iter} starting at
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2264
     * {@code start} and ending at {@code limit}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
     * @param iter  a {@link CharacterIterator} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
     * @param start the specified starting offset into the specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2268
     *              {@code CharacterIterator}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     * @param limit the specified ending offset into the specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2270
     *              {@code CharacterIterator}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2271
     * @return an offset into {@code iter} that points
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2272
     *          to the first character in {@code iter} that this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2273
     *          {@code Font} cannot display; or {@code -1} if
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2274
     *          this {@code Font} can display all characters in
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2275
     *          {@code iter}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
    public int canDisplayUpTo(CharacterIterator iter, int start, int limit) {
8520
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2279
        Font2D font2d = getFont2D();
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2280
        char c = iter.setIndex(start);
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2281
        for (int i = start; i < limit; i++, c = iter.next()) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2282
            if (font2d.canDisplay(c)) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2283
                continue;
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2284
            }
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2285
            if (!Character.isHighSurrogate(c)) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2286
                return i;
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2287
            }
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2288
            char c2 = iter.next();
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2289
            // c2 could be CharacterIterator.DONE which is not a low surrogate.
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2290
            if (!Character.isLowSurrogate(c2)) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2291
                return i;
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2292
            }
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2293
            if (!font2d.canDisplay(Character.toCodePoint(c, c2))) {
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2294
                return i;
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2295
            }
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2296
            i++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2297
        }
8520
a9e798f494cb 6623219: Font.canDisplayUpTo does not work with supplementary characters
okutsu
parents: 7668
diff changeset
  2298
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2302
     * Returns the italic angle of this {@code Font}.  The italic angle
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
     * is the inverse slope of the caret which best matches the posture of this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2304
     * {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
     * @see TextAttribute#POSTURE
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2306
     * @return the angle of the ITALIC style of this {@code Font}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
    public float getItalicAngle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
        return getItalicAngle(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
    /* The FRC hints don't affect the value of the italic angle but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
     * we need to pass them in to look up a strike.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
     * If we can pass in ones already being used it can prevent an extra
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     * strike from being allocated. Note that since italic angle is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
     * a property of the font, the font transform is needed not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
     * device transform. Finally, this is private but the only caller of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     * in the JDK - and the only likely caller - is in this same class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    private float getItalicAngle(FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
        Object aa, fm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        if (frc == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
            aa = RenderingHints.VALUE_TEXT_ANTIALIAS_OFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            fm = RenderingHints.VALUE_FRACTIONALMETRICS_OFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
            aa = frc.getAntiAliasingHint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
            fm = frc.getFractionalMetricsHint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
        }
547
ff9562f275ab 6694480: Two small inefficiencies in getting font strikes for transformed fonts
prr
parents: 534
diff changeset
  2329
        return getFont2D().getItalicAngle(this, identityTx, aa, fm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2330
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2333
     * Checks whether or not this {@code Font} has uniform
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2334
     * line metrics.  A logical {@code Font} might be a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
     * composite font, which means that it is composed of different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
     * physical fonts to cover different code ranges.  Each of these
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2337
     * fonts might have different {@code LineMetrics}.  If the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2338
     * logical {@code Font} is a single
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
     * font then the metrics would be uniform.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2340
     * @return {@code true} if this {@code Font} has
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2341
     * uniform line metrics; {@code false} otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
    public boolean hasUniformLineMetrics() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
        return false;   // REMIND always safe, but prevents caller optimize
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
11270
d7b0b63bd082 7117334: Warnings cleanup day: reduce number of javac warnings in the java.awt package
bagiras
parents: 9035
diff changeset
  2347
    private transient SoftReference<FontLineMetrics> flmref;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
    private FontLineMetrics defaultLineMetrics(FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2349
        FontLineMetrics flm = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
        if (flmref == null
11270
d7b0b63bd082 7117334: Warnings cleanup day: reduce number of javac warnings in the java.awt package
bagiras
parents: 9035
diff changeset
  2351
            || (flm = flmref.get()) == null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
            || !flm.frc.equals(frc)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
            /* The device transform in the frc is not used in obtaining line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
             * metrics, although it probably should be: REMIND find why not?
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
             * The font transform is used but its applied in getFontMetrics, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
             * just pass identity here
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            float [] metrics = new float[8];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
            getFont2D().getFontMetrics(this, identityTx,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
                                       frc.getAntiAliasingHint(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
                                       frc.getFractionalMetricsHint(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
                                       metrics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
            float ascent  = metrics[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
            float descent = metrics[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
            float leading = metrics[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
            float ssOffset = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            if (values != null && values.getSuperscript() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                ssOffset = (float)getTransform().getTranslateY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
                ascent -= ssOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2371
                descent += ssOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
            float height = ascent + descent + leading;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
            int baselineIndex = 0; // need real index, assumes roman for everything
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2376
            // need real baselines eventually
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2377
            float[] baselineOffsets = { 0, (descent/2f - ascent) / 2f, -ascent };
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2379
            float strikethroughOffset = metrics[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2380
            float strikethroughThickness = metrics[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2381
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2382
            float underlineOffset = metrics[6];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
            float underlineThickness = metrics[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
            float italicAngle = getItalicAngle(frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
            if (isTransformed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                AffineTransform ctx = values.getCharTransform(); // extract rotation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                if (ctx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                    Point2D.Float pt = new Point2D.Float();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                    pt.setLocation(0, strikethroughOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                    ctx.deltaTransform(pt, pt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                    strikethroughOffset = pt.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                    pt.setLocation(0, strikethroughThickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                    ctx.deltaTransform(pt, pt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                    strikethroughThickness = pt.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                    pt.setLocation(0, underlineOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                    ctx.deltaTransform(pt, pt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                    underlineOffset = pt.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
                    pt.setLocation(0, underlineThickness);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
                    ctx.deltaTransform(pt, pt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
                    underlineThickness = pt.y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2404
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2405
            strikethroughOffset += ssOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
            underlineOffset += ssOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2407
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2408
            CoreMetrics cm = new CoreMetrics(ascent, descent, leading, height,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                                             baselineIndex, baselineOffsets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
                                             strikethroughOffset, strikethroughThickness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
                                             underlineOffset, underlineThickness,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
                                             ssOffset, italicAngle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2413
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            flm = new FontLineMetrics(0, cm, frc);
11270
d7b0b63bd082 7117334: Warnings cleanup day: reduce number of javac warnings in the java.awt package
bagiras
parents: 9035
diff changeset
  2415
            flmref = new SoftReference<FontLineMetrics>(flm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
        return (FontLineMetrics)flm.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * Returns a {@link LineMetrics} object created with the specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2423
     * {@code String} and {@link FontRenderContext}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2424
     * @param str the specified {@code String}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2425
     * @param frc the specified {@code FontRenderContext}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2426
     * @return a {@code LineMetrics} object created with the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2427
     * specified {@code String} and {@link FontRenderContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
    public LineMetrics getLineMetrics( String str, FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
        FontLineMetrics flm = defaultLineMetrics(frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
        flm.numchars = str.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
        return flm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2436
     * Returns a {@code LineMetrics} object created with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * specified arguments.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2438
     * @param str the specified {@code String}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2439
     * @param beginIndex the initial offset of {@code str}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2440
     * @param limit the end offset of {@code str}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2441
     * @param frc the specified {@code FontRenderContext}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2442
     * @return a {@code LineMetrics} object created with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
     * specified arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
    public LineMetrics getLineMetrics( String str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
                                    int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
                                    FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
        FontLineMetrics flm = defaultLineMetrics(frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        int numChars = limit - beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        flm.numchars = (numChars < 0)? 0: numChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
        return flm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2455
     * Returns a {@code LineMetrics} object created with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     * specified arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     * @param chars an array of characters
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2458
     * @param beginIndex the initial offset of {@code chars}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2459
     * @param limit the end offset of {@code chars}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2460
     * @param frc the specified {@code FontRenderContext}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2461
     * @return a {@code LineMetrics} object created with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     * specified arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
    public LineMetrics getLineMetrics(char [] chars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
                                    int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
                                    FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
        FontLineMetrics flm = defaultLineMetrics(frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
        int numChars = limit - beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
        flm.numchars = (numChars < 0)? 0: numChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
        return flm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2474
     * Returns a {@code LineMetrics} object created with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     * specified arguments.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2476
     * @param ci the specified {@code CharacterIterator}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2477
     * @param beginIndex the initial offset in {@code ci}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2478
     * @param limit the end offset of {@code ci}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2479
     * @param frc the specified {@code FontRenderContext}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2480
     * @return a {@code LineMetrics} object created with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     * specified arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
    public LineMetrics getLineMetrics(CharacterIterator ci,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
                                    int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
                                    FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
        FontLineMetrics flm = defaultLineMetrics(frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
        int numChars = limit - beginIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
        flm.numchars = (numChars < 0)? 0: numChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
        return flm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2493
     * Returns the logical bounds of the specified {@code String} in
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2494
     * the specified {@code FontRenderContext}.  The logical bounds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     * contains the origin, ascent, advance, and height, which includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
     * the leading.  The logical bounds does not always enclose all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     * text.  For example, in some languages and in some fonts, accent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     * marks can be positioned above the ascent or below the descent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
     * To obtain a visual bounding box, which encloses all the text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
     * use the {@link TextLayout#getBounds() getBounds} method of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2501
     * {@code TextLayout}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
     * <p>Note: The returned bounds is in baseline-relative coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     * (see {@link java.awt.Font class notes}).
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2504
     * @param str the specified {@code String}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2505
     * @param frc the specified {@code FontRenderContext}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
     * @return a {@link Rectangle2D} that is the bounding box of the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2507
     * specified {@code String} in the specified
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2508
     * {@code FontRenderContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
     * @see FontRenderContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     * @see Font#createGlyphVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
    public Rectangle2D getStringBounds( String str, FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
        char[] array = str.toCharArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
        return getStringBounds(array, 0, array.length, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
   /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2519
     * Returns the logical bounds of the specified {@code String} in
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2520
     * the specified {@code FontRenderContext}.  The logical bounds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
     * contains the origin, ascent, advance, and height, which includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2522
     * the leading.  The logical bounds does not always enclose all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2523
     * text.  For example, in some languages and in some fonts, accent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2524
     * marks can be positioned above the ascent or below the descent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     * To obtain a visual bounding box, which encloses all the text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2526
     * use the {@link TextLayout#getBounds() getBounds} method of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2527
     * {@code TextLayout}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2528
     * <p>Note: The returned bounds is in baseline-relative coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
     * (see {@link java.awt.Font class notes}).
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2530
     * @param str the specified {@code String}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2531
     * @param beginIndex the initial offset of {@code str}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2532
     * @param limit the end offset of {@code str}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2533
     * @param frc the specified {@code FontRenderContext}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2534
     * @return a {@code Rectangle2D} that is the bounding box of the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2535
     * specified {@code String} in the specified
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2536
     * {@code FontRenderContext}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2537
     * @throws IndexOutOfBoundsException if {@code beginIndex} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2538
     *         less than zero, or {@code limit} is greater than the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2539
     *         length of {@code str}, or {@code beginIndex}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2540
     *         is greater than {@code limit}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
     * @see FontRenderContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2542
     * @see Font#createGlyphVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2543
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
    public Rectangle2D getStringBounds( String str,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
                                    int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2547
                                        FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
        String substr = str.substring(beginIndex, limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
        return getStringBounds(substr, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2550
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
     * Returns the logical bounds of the specified array of characters
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2554
     * in the specified {@code FontRenderContext}.  The logical
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
     * bounds contains the origin, ascent, advance, and height, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2556
     * includes the leading.  The logical bounds does not always enclose
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
     * all the text.  For example, in some languages and in some fonts,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
     * accent marks can be positioned above the ascent or below the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
     * descent.  To obtain a visual bounding box, which encloses all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
     * text, use the {@link TextLayout#getBounds() getBounds} method of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2561
     * {@code TextLayout}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
     * <p>Note: The returned bounds is in baseline-relative coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
     * (see {@link java.awt.Font class notes}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
     * @param chars an array of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
     * @param beginIndex the initial offset in the array of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
     * characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
     * @param limit the end offset in the array of characters
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2568
     * @param frc the specified {@code FontRenderContext}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2569
     * @return a {@code Rectangle2D} that is the bounding box of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
     * specified array of characters in the specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2571
     * {@code FontRenderContext}.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2572
     * @throws IndexOutOfBoundsException if {@code beginIndex} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2573
     *         less than zero, or {@code limit} is greater than the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2574
     *         length of {@code chars}, or {@code beginIndex}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2575
     *         is greater than {@code limit}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
     * @see FontRenderContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
     * @see Font#createGlyphVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
    public Rectangle2D getStringBounds(char [] chars,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
                                    int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
                                       FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        if (beginIndex < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2584
            throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2586
        if (limit > chars.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
            throw new IndexOutOfBoundsException("limit: " + limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2589
        if (beginIndex > limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
            throw new IndexOutOfBoundsException("range length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
                                                (limit - beginIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
        // this code should be in textlayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
        // quick check for simple text, assume GV ok to use if simple
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
        boolean simple = values == null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2598
            (values.getKerning() == 0 && values.getLigatures() == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2599
              values.getBaselineTransform() == null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2600
        if (simple) {
3928
be186a33df9b 6795908: Refactor FontManager
rkennke
parents: 3017
diff changeset
  2601
            simple = ! FontUtilities.isComplexText(chars, beginIndex, limit);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2602
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2603
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2604
        if (simple) {
48285
7e8a0c4ee95e 8189809: Large performance regression in Swing text layout
prr
parents: 47216
diff changeset
  2605
            FontDesignMetrics metrics = FontDesignMetrics.getMetrics(this, frc);
7e8a0c4ee95e 8189809: Large performance regression in Swing text layout
prr
parents: 47216
diff changeset
  2606
            return metrics.getSimpleBounds(chars, beginIndex, limit-beginIndex);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
            // need char array constructor on textlayout
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
            String str = new String(chars, beginIndex, limit - beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            TextLayout tl = new TextLayout(str, this, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
            return new Rectangle2D.Float(0, -tl.getAscent(), tl.getAdvance(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
                                         tl.getAscent() + tl.getDescent() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
                                         tl.getLeading());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
     * Returns the logical bounds of the characters indexed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
     * specified {@link CharacterIterator} in the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2620
     * specified {@code FontRenderContext}.  The logical bounds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
     * contains the origin, ascent, advance, and height, which includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2622
     * the leading.  The logical bounds does not always enclose all the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
     * text.  For example, in some languages and in some fonts, accent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2624
     * marks can be positioned above the ascent or below the descent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
     * To obtain a visual bounding box, which encloses all the text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
     * use the {@link TextLayout#getBounds() getBounds} method of
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2627
     * {@code TextLayout}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
     * <p>Note: The returned bounds is in baseline-relative coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
     * (see {@link java.awt.Font class notes}).
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2630
     * @param ci the specified {@code CharacterIterator}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2631
     * @param beginIndex the initial offset in {@code ci}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2632
     * @param limit the end offset in {@code ci}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2633
     * @param frc the specified {@code FontRenderContext}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2634
     * @return a {@code Rectangle2D} that is the bounding box of the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2635
     * characters indexed in the specified {@code CharacterIterator}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2636
     * in the specified {@code FontRenderContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2637
     * @see FontRenderContext
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2638
     * @see Font#createGlyphVector
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2639
     * @since 1.2
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2640
     * @throws IndexOutOfBoundsException if {@code beginIndex} is
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2641
     *         less than the start index of {@code ci}, or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2642
     *         {@code limit} is greater than the end index of
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2643
     *         {@code ci}, or {@code beginIndex} is greater
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2644
     *         than {@code limit}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
    public Rectangle2D getStringBounds(CharacterIterator ci,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
                                    int beginIndex, int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
                                       FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
        int start = ci.getBeginIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
        int end = ci.getEndIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
        if (beginIndex < start) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
            throw new IndexOutOfBoundsException("beginIndex: " + beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2655
        if (limit > end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
            throw new IndexOutOfBoundsException("limit: " + limit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2657
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
        if (beginIndex > limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
            throw new IndexOutOfBoundsException("range length: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2660
                                                (limit - beginIndex));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2662
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
        char[]  arr = new char[limit - beginIndex];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
        ci.setIndex(beginIndex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
        for(int idx = 0; idx < arr.length; idx++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
            arr[idx] = ci.current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2668
            ci.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2670
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
        return getStringBounds(arr,0,arr.length,frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2673
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
     * Returns the bounds for the character with the maximum
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2676
     * bounds as defined in the specified {@code FontRenderContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
     * <p>Note: The returned bounds is in baseline-relative coordinates
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
     * (see {@link java.awt.Font class notes}).
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2679
     * @param frc the specified {@code FontRenderContext}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2680
     * @return a {@code Rectangle2D} that is the bounding box
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
     * for the character with the maximum bounds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2683
    public Rectangle2D getMaxCharBounds(FontRenderContext frc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
        float [] metrics = new float[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
        getFont2D().getFontMetrics(this, frc, metrics);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
        return new Rectangle2D.Float(0, -metrics[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
                                metrics[3],
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
                                metrics[0] + metrics[1] + metrics[2]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
     * Creates a {@link java.awt.font.GlyphVector GlyphVector} by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
     * mapping characters to glyphs one-to-one based on the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2696
     * Unicode cmap in this {@code Font}.  This method does no other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2697
     * processing besides the mapping of glyphs to characters.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2698
     * means that this method is not useful for some scripts, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
     * as Arabic, Hebrew, Thai, and Indic, that require reordering,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2700
     * shaping, or ligature substitution.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2701
     * @param frc the specified {@code FontRenderContext}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2702
     * @param str the specified {@code String}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2703
     * @return a new {@code GlyphVector} created with the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2704
     * specified {@code String} and the specified
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2705
     * {@code FontRenderContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
    public GlyphVector createGlyphVector(FontRenderContext frc, String str)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
        return (GlyphVector)new StandardGlyphVector(this, str, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2711
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2713
     * Creates a {@link java.awt.font.GlyphVector GlyphVector} by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
     * mapping characters to glyphs one-to-one based on the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2715
     * Unicode cmap in this {@code Font}.  This method does no other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2716
     * processing besides the mapping of glyphs to characters.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2717
     * means that this method is not useful for some scripts, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2718
     * as Arabic, Hebrew, Thai, and Indic, that require reordering,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2719
     * shaping, or ligature substitution.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2720
     * @param frc the specified {@code FontRenderContext}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2721
     * @param chars the specified array of characters
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2722
     * @return a new {@code GlyphVector} created with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2723
     * specified array of characters and the specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2724
     * {@code FontRenderContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2725
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2726
    public GlyphVector createGlyphVector(FontRenderContext frc, char[] chars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2727
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2728
        return (GlyphVector)new StandardGlyphVector(this, chars, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2731
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2732
     * Creates a {@link java.awt.font.GlyphVector GlyphVector} by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2733
     * mapping the specified characters to glyphs one-to-one based on the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2734
     * Unicode cmap in this {@code Font}.  This method does no other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2735
     * processing besides the mapping of glyphs to characters.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2736
     * means that this method is not useful for some scripts, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2737
     * as Arabic, Hebrew, Thai, and Indic, that require reordering,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2738
     * shaping, or ligature substitution.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2739
     * @param frc the specified {@code FontRenderContext}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2740
     * @param ci the specified {@code CharacterIterator}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2741
     * @return a new {@code GlyphVector} created with the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2742
     * specified {@code CharacterIterator} and the specified
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2743
     * {@code FontRenderContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2745
    public GlyphVector createGlyphVector(   FontRenderContext frc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2746
                                            CharacterIterator ci)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2747
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2748
        return (GlyphVector)new StandardGlyphVector(this, ci, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2749
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2751
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2752
     * Creates a {@link java.awt.font.GlyphVector GlyphVector} by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2753
     * mapping characters to glyphs one-to-one based on the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2754
     * Unicode cmap in this {@code Font}.  This method does no other
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2755
     * processing besides the mapping of glyphs to characters.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2756
     * means that this method is not useful for some scripts, such
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2757
     * as Arabic, Hebrew, Thai, and Indic, that require reordering,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2758
     * shaping, or ligature substitution.
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2759
     * @param frc the specified {@code FontRenderContext}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2760
     * @param glyphCodes the specified integer array
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2761
     * @return a new {@code GlyphVector} created with the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2762
     * specified integer array and the specified
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2763
     * {@code FontRenderContext}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2764
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2765
    public GlyphVector createGlyphVector(   FontRenderContext frc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2766
                                            int [] glyphCodes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2767
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2768
        return (GlyphVector)new StandardGlyphVector(this, glyphCodes, frc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2771
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2772
     * Returns a new {@code GlyphVector} object, performing full
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2773
     * layout of the text if possible.  Full layout is required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2774
     * complex text, such as Arabic or Hindi.  Support for different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2775
     * scripts depends on the font and implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2776
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2777
     * Layout requires bidi analysis, as performed by
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2778
     * {@code Bidi}, and should only be performed on text that
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2779
     * has a uniform direction.  The direction is indicated in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2780
     * flags parameter,by using LAYOUT_RIGHT_TO_LEFT to indicate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2781
     * right-to-left (Arabic and Hebrew) run direction, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2782
     * LAYOUT_LEFT_TO_RIGHT to indicate a left-to-right (English)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2783
     * run direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2784
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2785
     * In addition, some operations, such as Arabic shaping, require
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2786
     * context, so that the characters at the start and limit can have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2787
     * the proper shapes.  Sometimes the data in the buffer outside
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2788
     * the provided range does not have valid data.  The values
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2789
     * LAYOUT_NO_START_CONTEXT and LAYOUT_NO_LIMIT_CONTEXT can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2790
     * added to the flags parameter to indicate that the text before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2791
     * start, or after limit, respectively, should not be examined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2792
     * for context.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2793
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2794
     * All other values for the flags parameter are reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2795
     *
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2796
     * @param frc the specified {@code FontRenderContext}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2797
     * @param text the text to layout
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2798
     * @param start the start of the text to use for the {@code GlyphVector}
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2799
     * @param limit the limit of the text to use for the {@code GlyphVector}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2800
     * @param flags control flags as described above
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 32665
diff changeset
  2801
     * @return a new {@code GlyphVector} representing the text between
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2802
     * start and limit, with glyphs chosen and positioned so as to best represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2803
     * the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2804
     * @throws ArrayIndexOutOfBoundsException if start or limit is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2805
     * out of bounds
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2806
     * @see java.text.Bidi
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2807
     * @see #LAYOUT_LEFT_TO_RIGHT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2808
     * @see #LAYOUT_RIGHT_TO_LEFT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2809
     * @see #LAYOUT_NO_START_CONTEXT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2810
     * @see #LAYOUT_NO_LIMIT_CONTEXT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2811
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2812
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2813
    public GlyphVector layoutGlyphVector(FontRenderContext frc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2814
                                         char[] text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2815
                                         int start,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2816
                                         int limit,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2817
                                         int flags) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2818
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2819
        GlyphLayout gl = GlyphLayout.get(null); // !!! no custom layout engines
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2820
        StandardGlyphVector gv = gl.layout(this, frc, text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2821
                                           start, limit-start, flags, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2822
        GlyphLayout.done(gl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2823
        return gv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2824
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2826
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2827
     * A flag to layoutGlyphVector indicating that text is left-to-right as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2828
     * determined by Bidi analysis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2830
    public static final int LAYOUT_LEFT_TO_RIGHT = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2831
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2833
     * A flag to layoutGlyphVector indicating that text is right-to-left as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2834
     * determined by Bidi analysis.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2835
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2836
    public static final int LAYOUT_RIGHT_TO_LEFT = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2837
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2839
     * A flag to layoutGlyphVector indicating that text in the char array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2840
     * before the indicated start should not be examined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
    public static final int LAYOUT_NO_START_CONTEXT = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2844
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
     * A flag to layoutGlyphVector indicating that text in the char array
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2846
     * after the indicated limit should not be examined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2847
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
    public static final int LAYOUT_NO_LIMIT_CONTEXT = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2849
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
    private static void applyTransform(AffineTransform trans, AttributeValues values) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
        if (trans == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
            throw new IllegalArgumentException("transform must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
        values.setTransform(trans);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2856
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2858
    private static void applyStyle(int style, AttributeValues values) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
        // WEIGHT_BOLD, WEIGHT_REGULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2860
        values.setWeight((style & BOLD) != 0 ? 2f : 1f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
        // POSTURE_OBLIQUE, POSTURE_REGULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
        values.setPosture((style & ITALIC) != 0 ? .2f : 0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
     * Initialize JNI field and method IDs
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2867
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2868
    private static native void initIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2869
}