1117 lines · 39.6 KB · Java
RawDownload
/* * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved * * The original version of this source code and documentation is * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary * of IBM. These materials are provided under terms of a License * Agreement between Taligent and Sun. This technology is protected * by multiple US and International patents. * * This notice and attribution to Taligent may not be removed. * Taligent is a registered trademark of Taligent, Inc. * */ package java.awt.font; import java.io.InvalidObjectException;import java.text.AttributedCharacterIterator.Attribute;import java.util.Map;import java.util.HashMap; /** * The <code>TextAttribute</code> class defines attribute keys and * attribute values used for text rendering. * <p> * <code>TextAttribute</code> instances are used as attribute keys to * identify attributes in * {@link java.awt.Font Font}, * {@link java.awt.font.TextLayout TextLayout}, * {@link java.text.AttributedCharacterIterator AttributedCharacterIterator}, * and other classes handling text attributes. Other constants defined * in this class can be used as attribute values. * <p> * For each text attribute, the documentation provides: * <UL> *   <LI>the type of its value, *   <LI>the relevant predefined constants, if any *   <LI>the default effect if the attribute is absent *   <LI>the valid values if there are limitations *   <LI>a description of the effect. * </UL> * <p> * <H4>Values</H4> * <UL> *   <LI>The values of attributes must always be immutable. *   <LI>Where value limitations are given, any value outside of that *   set is reserved for future use; the value will be treated as *   the default. *   <LI>The value <code>null</code> is treated the same as the *   default value and results in the default behavior. *   <li>If the value is not of the proper type, the attribute *   will be ignored. *   <li>The identity of the value does not matter, only the actual *   value.  For example, <code>TextAttribute.WEIGHT_BOLD</code> and *   <code>new Float(2.0)</code> *   indicate the same <code>WEIGHT</code>. *   <li>Attribute values of type <code>Number</code> (used for *   <code>WEIGHT</code>, <code>WIDTH</code>, <code>POSTURE</code>, *   <code>SIZE</code>, <code>JUSTIFICATION</code>, and *   <code>TRACKING</code>) can vary along their natural range and are *   not restricted to the predefined constants. *   <code>Number.floatValue()</code> is used to get the actual value *   from the <code>Number</code>. *   <li>The values for <code>WEIGHT</code>, <code>WIDTH</code>, and *   <code>POSTURE</code> are interpolated by the system, which *   can select the 'nearest available' font or use other techniques to *   approximate the user's request. * * </UL> * * <h4>Summary of attributes</h4> * <p> * <font size="-1"> * <table align="center" border="0" cellspacing="0" cellpadding="2" width="%95" *     summary="Key, value type, principal constants, and default value *     behavior of all TextAttributes"> * <tr bgcolor="#ccccff"> * <th valign="TOP" align="CENTER">Key</th> * <th valign="TOP" align="CENTER">Value Type</th> * <th valign="TOP" align="CENTER">Principal Constants</th> * <th valign="TOP" align="CENTER">Default Value</th> * </tr> * <tr> * <td valign="TOP">{@link #FAMILY}</td> * <td valign="TOP">String</td> * <td valign="TOP">See Font {@link java.awt.Font#DIALOG DIALOG},{@link java.awt.Font#DIALOG_INPUT DIALOG_INPUT},<br> {@link java.awt.Font#SERIF SERIF},{@link java.awt.Font#SANS_SERIF SANS_SERIF}, and {@link java.awt.Font#MONOSPACED MONOSPACED}.</td> * <td valign="TOP">"Default" (use platform default)</td> * </tr> * <tr bgcolor="#eeeeff"> * <td valign="TOP">{@link #WEIGHT}</td> * <td valign="TOP">Number</td> * <td valign="TOP">WEIGHT_REGULAR, WEIGHT_BOLD</td> * <td valign="TOP">WEIGHT_REGULAR</td> * </tr> * <tr> * <td valign="TOP">{@link #WIDTH}</td> * <td valign="TOP">Number</td> * <td valign="TOP">WIDTH_CONDENSED, WIDTH_REGULAR,<br>WIDTH_EXTENDED</td> * <td valign="TOP">WIDTH_REGULAR</td> * </tr> * <tr bgcolor="#eeeeff"> * <td valign="TOP">{@link #POSTURE}</td> * <td valign="TOP">Number</td> * <td valign="TOP">POSTURE_REGULAR, POSTURE_OBLIQUE</td> * <td valign="TOP">POSTURE_REGULAR</td> * </tr> * <tr> * <td valign="TOP">{@link #SIZE}</td> * <td valign="TOP">Number</td> * <td valign="TOP">none</td> * <td valign="TOP">12.0</td> * </tr> * <tr bgcolor="#eeeeff"> * <td valign="TOP">{@link #TRANSFORM}</td> * <td valign="TOP">{@link TransformAttribute}</td> * <td valign="TOP">See TransformAttribute {@link TransformAttribute#IDENTITY IDENTITY}</td> * <td valign="TOP">TransformAttribute.IDENTITY</td> * </tr> * <tr> * <td valign="TOP">{@link #SUPERSCRIPT}</td> * <td valign="TOP">Integer</td> * <td valign="TOP">SUPERSCRIPT_SUPER, SUPERSCRIPT_SUB</td> * <td valign="TOP">0 (use the standard glyphs and metrics)</td> * </tr> * <tr bgcolor="#eeeeff"> * <td valign="TOP">{@link #FONT}</td> * <td valign="TOP">{@link java.awt.Font}</td> * <td valign="TOP">none</td> * <td valign="TOP">null (do not override font resolution)</td> * </tr> * <tr> * <td valign="TOP">{@link #CHAR_REPLACEMENT}</td> * <td valign="TOP">{@link GraphicAttribute}</td> * <td valign="TOP">none</td> * <td valign="TOP">null (draw text using font glyphs)</td> * </tr> * <tr bgcolor="#eeeeff"> * <td valign="TOP">{@link #FOREGROUND}</td> * <td valign="TOP">{@link java.awt.Paint}</td> * <td valign="TOP">none</td> * <td valign="TOP">null (use current graphics paint)</td> * </tr> * <tr> * <td valign="TOP">{@link #BACKGROUND}</td> * <td valign="TOP">{@link java.awt.Paint}</td> * <td valign="TOP">none</td> * <td valign="TOP">null (do not render background)</td> * </tr> * <tr bgcolor="#eeeeff"> * <td valign="TOP">{@link #UNDERLINE}</td> * <td valign="TOP">Integer</td> * <td valign="TOP">UNDERLINE_ON</td> * <td valign="TOP">-1 (do not render underline)</td> * </tr> * <tr> * <td valign="TOP">{@link #STRIKETHROUGH}</td> * <td valign="TOP">Boolean</td> * <td valign="TOP">STRIKETHROUGH_ON</td> * <td valign="TOP">false (do not render strikethrough)</td> * </tr> * <tr bgcolor="#eeeeff"> * <td valign="TOP">{@link #RUN_DIRECTION}</td> * <td valign="TOP">Boolean</td> * <td valign="TOP">RUN_DIRECTION_LTR<br>RUN_DIRECTION_RTL</td> * <td valign="TOP">null (use {@link java.text.Bidi} standard default)</td> * </tr> * <tr> * <td valign="TOP">{@link #BIDI_EMBEDDING}</td> * <td valign="TOP">Integer</td> * <td valign="TOP">none</td> * <td valign="TOP">0 (use base line direction)</td> * </tr> * <tr bgcolor="#eeeeff"> * <td valign="TOP">{@link #JUSTIFICATION}</td> * <td valign="TOP">Number</td> * <td valign="TOP">JUSTIFICATION_FULL</td> * <td valign="TOP">JUSTIFICATION_FULL</td> * </tr> * <tr> * <td valign="TOP">{@link #INPUT_METHOD_HIGHLIGHT}</td> * <td valign="TOP">{@link java.awt.im.InputMethodHighlight},<br>{@link java.text.Annotation}</td> * <td valign="TOP">(see class)</td> * <td valign="TOP">null (do not apply input highlighting)</td> * </tr> * <tr bgcolor="#eeeeff"> * <td valign="TOP">{@link #INPUT_METHOD_UNDERLINE}</td> * <td valign="TOP">Integer</td> * <td valign="TOP">UNDERLINE_LOW_ONE_PIXEL,<br>UNDERLINE_LOW_TWO_PIXEL</td> * <td valign="TOP">-1 (do not render underline)</td> * </tr> * <tr> * <td valign="TOP">{@link #SWAP_COLORS}</td> * <td valign="TOP">Boolean</td> * <td valign="TOP">SWAP_COLORS_ON</td> * <td valign="TOP">false (do not swap colors)</td> * </tr> * <tr bgcolor="#eeeeff"> * <td valign="TOP">{@link #NUMERIC_SHAPING}</td> * <td valign="TOP">{@link java.awt.font.NumericShaper}</td> * <td valign="TOP">none</td> * <td valign="TOP">null (do not shape digits)</td> * </tr> * <tr> * <td valign="TOP">{@link #KERNING}</td> * <td valign="TOP">Integer</td> * <td valign="TOP">KERNING_ON</td> * <td valign="TOP">0 (do not request kerning)</td> * </tr> * <tr bgcolor="#eeeeff"> * <td valign="TOP">{@link #LIGATURES}</td> * <td valign="TOP">Integer</td> * <td valign="TOP">LIGATURES_ON</td> * <td valign="TOP">0 (do not form optional ligatures)</td> * </tr> * <tr> * <td valign="TOP">{@link #TRACKING}</td> * <td valign="TOP">Number</td> * <td valign="TOP">TRACKING_LOOSE, TRACKING_TIGHT</td> * <td valign="TOP">0 (do not add tracking)</td> * </tr> * </table> * </font> * * @see java.awt.Font * @see java.awt.font.TextLayout * @see java.text.AttributedCharacterIterator */public final class TextAttribute extends Attribute {     // table of all instances in this class, used by readResolve    private static final Map instanceMap = new HashMap(29);     /**     * Constructs a <code>TextAttribute</code> with the specified name.     * @param name the attribute name to assign to this     * <code>TextAttribute</code>     */    protected TextAttribute(String name) {        super(name);        if (this.getClass() == TextAttribute.class) {            instanceMap.put(name, this);        }    }     /**     * Resolves instances being deserialized to the predefined constants.     */    protected Object readResolve() throws InvalidObjectException {        if (this.getClass() != TextAttribute.class) {            throw new InvalidObjectException(                "subclass didn't correctly implement readResolve");        }         TextAttribute instance = (TextAttribute) instanceMap.get(getName());        if (instance != null) {            return instance;        } else {            throw new InvalidObjectException("unknown attribute name");        }    }     // Serialization compatibility with Java 2 platform v1.2.    // 1.2 will throw an InvalidObjectException if ever asked to    // deserialize INPUT_METHOD_UNDERLINE.    // This shouldn't happen in real life.    static final long serialVersionUID = 7744112784117861702L;     //    // For use with Font.    //     /**     * Attribute key for the font name.  Values are instances of     * <b><code>String</code></b>.  The default value is     * <code>"Default"</code>, which causes the platform default font     * family to be used.     *     * <p> The <code>Font</code> class defines constants for the logical     * font names     * {@link java.awt.Font#DIALOG DIALOG},     * {@link java.awt.Font#DIALOG_INPUT DIALOG_INPUT},     * {@link java.awt.Font#SANS_SERIF SANS_SERIF},     * {@link java.awt.Font#SERIF SERIF}, and     * {@link java.awt.Font#MONOSPACED MONOSPACED}.     *     * <p>This defines the value passed as <code>name</code> to the     * <code>Font</code> constructor.  Both logical and physical     * font names are allowed. If a font with the requested name     * is not found, the default font is used.     *     * <p><em>Note:</em> This attribute is unfortunately misnamed, as     * it specifies the face name and not just the family.  Thus     * values such as "Lucida Sans Bold" will select that face if it     * exists.  Note, though, that if the requested face does not     * exist, the default will be used with <em>regular</em> weight.     * The "Bold" in the name is part of the face name, not a separate     * request that the font's weight be bold.</p>     */    public static final TextAttribute FAMILY =        new TextAttribute("family");     /**     * Attribute key for the weight of a font.  Values are instances     * of <b><code>Number</code></b>.  The default value is     * <code>WEIGHT_REGULAR</code>.     *     * <p>Several constant values are provided, see {@link     * #WEIGHT_EXTRA_LIGHT}, {@link #WEIGHT_LIGHT}, {@link     * #WEIGHT_DEMILIGHT}, {@link #WEIGHT_REGULAR}, {@link     * #WEIGHT_SEMIBOLD}, {@link #WEIGHT_MEDIUM}, {@link     * #WEIGHT_DEMIBOLD}, {@link #WEIGHT_BOLD}, {@link #WEIGHT_HEAVY},     * {@link #WEIGHT_EXTRABOLD}, and {@link #WEIGHT_ULTRABOLD}.  The     * value <code>WEIGHT_BOLD</code> corresponds to the     * style value <code>Font.BOLD</code> as passed to the     * <code>Font</code> constructor.     *     * <p>The value is roughly the ratio of the stem width to that of     * the regular weight.     *     * <p>The system can interpolate the provided value.     */    public static final TextAttribute WEIGHT =        new TextAttribute("weight");     /**     * The lightest predefined weight.     * @see #WEIGHT     */    public static final Float WEIGHT_EXTRA_LIGHT =        Float.valueOf(0.5f);     /**     * The standard light weight.     * @see #WEIGHT     */    public static final Float WEIGHT_LIGHT =        Float.valueOf(0.75f);     /**     * An intermediate weight between <code>WEIGHT_LIGHT</code> and     * <code>WEIGHT_STANDARD</code>.     * @see #WEIGHT     */    public static final Float WEIGHT_DEMILIGHT =        Float.valueOf(0.875f);     /**     * The standard weight. This is the default value for <code>WEIGHT</code>.     * @see #WEIGHT     */    public static final Float WEIGHT_REGULAR =        Float.valueOf(1.0f);     /**     * A moderately heavier weight than <code>WEIGHT_REGULAR</code>.     * @see #WEIGHT     */    public static final Float WEIGHT_SEMIBOLD =        Float.valueOf(1.25f);     /**     * An intermediate weight between <code>WEIGHT_REGULAR</code> and     * <code>WEIGHT_BOLD</code>.     * @see #WEIGHT     */    public static final Float WEIGHT_MEDIUM =        Float.valueOf(1.5f);     /**     * A moderately lighter weight than <code>WEIGHT_BOLD</code>.     * @see #WEIGHT     */    public static final Float WEIGHT_DEMIBOLD =        Float.valueOf(1.75f);     /**     * The standard bold weight.     * @see #WEIGHT     */    public static final Float WEIGHT_BOLD =        Float.valueOf(2.0f);     /**     * A moderately heavier weight than <code>WEIGHT_BOLD</code>.     * @see #WEIGHT     */    public static final Float WEIGHT_HEAVY =        Float.valueOf(2.25f);     /**     * An extra heavy weight.     * @see #WEIGHT     */    public static final Float WEIGHT_EXTRABOLD =        Float.valueOf(2.5f);     /**     * The heaviest predefined weight.     * @see #WEIGHT     */    public static final Float WEIGHT_ULTRABOLD =        Float.valueOf(2.75f);     /**     * Attribute key for the width of a font.  Values are instances of     * <b><code>Number</code></b>.  The default value is     * <code>WIDTH_REGULAR</code>.     *     * <p>Several constant values are provided, see {@link     * #WIDTH_CONDENSED}, {@link #WIDTH_SEMI_CONDENSED}, {@link     * #WIDTH_REGULAR}, {@link #WIDTH_SEMI_EXTENDED}, {@link     * #WIDTH_EXTENDED}.     *     * <p>The value is roughly the ratio of the advance width to that     * of the regular width.     *     * <p>The system can interpolate the provided value.     */    public static final TextAttribute WIDTH =        new TextAttribute("width");     /**     * The most condensed predefined width.     * @see #WIDTH     */    public static final Float WIDTH_CONDENSED =        Float.valueOf(0.75f);     /**     * A moderately condensed width.     * @see #WIDTH     */    public static final Float WIDTH_SEMI_CONDENSED =        Float.valueOf(0.875f);     /**     * The standard width. This is the default value for     * <code>WIDTH</code>.     * @see #WIDTH     */    public static final Float WIDTH_REGULAR =        Float.valueOf(1.0f);     /**     * A moderately extended width.     * @see #WIDTH     */    public static final Float WIDTH_SEMI_EXTENDED =        Float.valueOf(1.25f);     /**     * The most extended predefined width.     * @see #WIDTH     */    public static final Float WIDTH_EXTENDED =        Float.valueOf(1.5f);     /**     * Attribute key for the posture of a font.  Values are instances     * of <b><code>Number</code></b>. The default value is     * <code>POSTURE_REGULAR</code>.     *     * <p>Two constant values are provided, {@link #POSTURE_REGULAR}     * and {@link #POSTURE_OBLIQUE}. The value     * <code>POSTURE_OBLIQUE</code> corresponds to the style value     * <code>Font.ITALIC</code> as passed to the <code>Font</code>     * constructor.     *     * <p>The value is roughly the slope of the stems of the font,     * expressed as the run over the rise.  Positive values lean right.     *

Lines 1–500 of 1117.