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