jdk/src/share/classes/java/awt/font/GraphicAttribute.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
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 1998-2006 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
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The original version of this source code and documentation is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * of IBM. These materials are provided under terms of a License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Agreement between Taligent and Sun. This technology is protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
package java.awt.font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.awt.Graphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.awt.Shape;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.awt.geom.AffineTransform;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.awt.geom.Rectangle2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * This class is used with the CHAR_REPLACEMENT attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * The <code>GraphicAttribute</code> class represents a graphic embedded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * in text. Clients subclass this class to implement their own char
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * replacement graphics.  Clients wishing to embed shapes and images in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * text need not subclass this class.  Instead, clients can use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * {@link ShapeGraphicAttribute} and {@link ImageGraphicAttribute}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Subclasses must ensure that their objects are immutable once they
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * are constructed.  Mutating a <code>GraphicAttribute</code> that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * is used in a {@link TextLayout} results in undefined behavior from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <code>TextLayout</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public abstract class GraphicAttribute {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private int fAlignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Aligns top of graphic to top of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public static final int TOP_ALIGNMENT = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Aligns bottom of graphic to bottom of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public static final int BOTTOM_ALIGNMENT = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Aligns origin of graphic to roman baseline of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static final int ROMAN_BASELINE = Font.ROMAN_BASELINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * Aligns origin of graphic to center baseline of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public static final int CENTER_BASELINE = Font.CENTER_BASELINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Aligns origin of graphic to hanging baseline of line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public static final int HANGING_BASELINE = Font.HANGING_BASELINE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Constructs a <code>GraphicAttribute</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Subclasses use this to define the alignment of the graphic.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param alignment an int representing one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <code>GraphicAttribute</code> alignment fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @throws IllegalArgumentException if alignment is not one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * five defined values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    protected GraphicAttribute(int alignment) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (alignment < BOTTOM_ALIGNMENT || alignment > HANGING_BASELINE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
          throw new IllegalArgumentException("bad alignment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        fAlignment = alignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Returns the ascent of this <code>GraphicAttribute</code>.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * graphic can be rendered above its ascent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @return the ascent of this <code>GraphicAttribute</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @see #getBounds()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public abstract float getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * Returns the descent of this <code>GraphicAttribute</code>.  A
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * graphic can be rendered below its descent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @return the descent of this <code>GraphicAttribute</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @see #getBounds()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public abstract float getDescent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Returns the advance of this <code>GraphicAttribute</code>.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <code>GraphicAttribute</code> object's advance is the distance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * from the point at which the graphic is rendered and the point where
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * the next character or graphic is rendered.  A graphic can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * rendered beyond its advance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @return the advance of this <code>GraphicAttribute</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @see #getBounds()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    public abstract float getAdvance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Returns a {@link Rectangle2D} that encloses all of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * bits drawn by this <code>GraphicAttribute</code> relative to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * rendering position.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * A graphic may be rendered beyond its origin, ascent, descent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * or advance;  but if it is, this method's implementation must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * indicate where the graphic is rendered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Default bounds is the rectangle (0, -ascent, advance, ascent+descent).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return a <code>Rectangle2D</code> that encloses all of the bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * rendered by this <code>GraphicAttribute</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public Rectangle2D getBounds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        float ascent = getAscent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return new Rectangle2D.Float(0, -ascent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                                        getAdvance(), ascent+getDescent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Return a {@link java.awt.Shape} that represents the region that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * this <code>GraphicAttribute</code> renders.  This is used when a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * {@link TextLayout} is requested to return the outline of the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * The (untransformed) shape must not extend outside the rectangular
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * bounds returned by <code>getBounds</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * The default implementation returns the rectangle returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * {@link #getBounds}, transformed by the provided {@link AffineTransform}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * if present.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @param tx an optional {@link AffineTransform} to apply to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     *   outline of this <code>GraphicAttribute</code>. This can be null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return a <code>Shape</code> representing this graphic attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *   suitable for stroking or filling.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public Shape getOutline(AffineTransform tx) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Shape b = getBounds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (tx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            b = tx.createTransformedShape(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Renders this <code>GraphicAttribute</code> at the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * location.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param graphics the {@link Graphics2D} into which to render the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * graphic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param x the user-space X coordinate where the graphic is rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * @param y the user-space Y coordinate where the graphic is rendered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    public abstract void draw(Graphics2D graphics, float x, float y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Returns the alignment of this <code>GraphicAttribute</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Alignment can be to a particular baseline, or to the absolute top
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * or bottom of a line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * @return the alignment of this <code>GraphicAttribute</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public final int getAlignment() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return fAlignment;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * Returns the justification information for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * <code>GraphicAttribute</code>.  Subclasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * can override this method to provide different justification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * information.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @return a {@link GlyphJustificationInfo} object that contains the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * justification information for this <code>GraphicAttribute</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    public GlyphJustificationInfo getJustificationInfo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        // should we cache this?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        float advance = getAdvance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return new GlyphJustificationInfo(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                                     advance,   // weight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                     false,     // growAbsorb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                     2,         // growPriority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                     advance/3, // growLeftLimit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                     advance/3, // growRightLimit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                     false,     // shrinkAbsorb
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                     1,         // shrinkPriority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                                     0,         // shrinkLeftLimit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                     0);        // shrinkRightLimit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
}