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