KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > font > GraphicAttribute


1 /*
2  * @(#)GraphicAttribute.java 1.18 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /*
9  * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
10  * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
11  *
12  * The original version of this source code and documentation is
13  * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
14  * of IBM. These materials are provided under terms of a License
15  * Agreement between Taligent and Sun. This technology is protected
16  * by multiple US and International patents.
17  *
18  * This notice and attribution to Taligent may not be removed.
19  * Taligent is a registered trademark of Taligent, Inc.
20  *
21  */

22
23 package java.awt.font;
24
25 import java.awt.geom.Rectangle2D JavaDoc;
26 import java.awt.Graphics2D JavaDoc;
27 import java.awt.Font JavaDoc;
28
29 /**
30  * This class is used with the CHAR_REPLACEMENT attribute.
31  * <p>
32  * The <code>GraphicAttribute</code> class represents a graphic embedded
33  * in text. Clients subclass this class to implement their own char
34  * replacement graphics. Clients wishing to embed shapes and images in
35  * text need not subclass this class. Instead, clients can use the
36  * {@link ShapeGraphicAttribute} and {@link ImageGraphicAttribute}
37  * classes.
38  * <p>
39  * Subclasses must ensure that their objects are immutable once they
40  * are constructed. Mutating a <code>GraphicAttribute</code> that
41  * is used in a {@link TextLayout} results in undefined behavior from the
42  * <code>TextLayout</code>.
43  */

44 public abstract class GraphicAttribute {
45
46     private int fAlignment;
47
48     /**
49      * Aligns top of graphic to top of line.
50      */

51     public static final int TOP_ALIGNMENT = -1;
52
53     /**
54      * Aligns bottom of graphic to bottom of line.
55      */

56     public static final int BOTTOM_ALIGNMENT = -2;
57
58     /**
59      * Aligns origin of graphic to roman baseline of line.
60      */

61     public static final int ROMAN_BASELINE = Font.ROMAN_BASELINE;
62
63     /**
64      * Aligns origin of graphic to center baseline of line.
65      */

66     public static final int CENTER_BASELINE = Font.CENTER_BASELINE;
67
68     /**
69      * Aligns origin of graphic to hanging baseline of line.
70      */

71     public static final int HANGING_BASELINE = Font.HANGING_BASELINE;
72
73     /**
74      * Constructs a <code>GraphicAttribute</code>.
75      * Subclasses use this to define the alignment of the graphic.
76      * @param alignment an int representing one of the
77      * <code>GraphicAttribute</code> alignment fields
78      */

79     protected GraphicAttribute(int alignment) {
80         if (alignment < BOTTOM_ALIGNMENT || alignment > HANGING_BASELINE) {
81       throw new IllegalArgumentException JavaDoc("bad alignment");
82         }
83         fAlignment = alignment;
84     }
85
86     /**
87      * Returns the ascent of this <code>GraphicAttribute</code>. A
88      * graphic can be rendered above its ascent.
89      * @return the ascent of this <code>GraphicAttribute</code>.
90      * @see #getBounds()
91      */

92     public abstract float getAscent();
93
94     /**
95      * Returns the descent of this <code>GraphicAttribute</code>. A
96      * graphic can be rendered below its descent.
97      * @return the descent of this <code>GraphicAttribute</code>.
98      * @see #getBounds()
99      */

100     public abstract float getDescent();
101
102     /**
103      * Returns the advance of this <code>GraphicAttribute</code>. The
104      * <code>GraphicAttribute</code> object's advance is the distance
105      * from the point at which the graphic is rendered and the point where
106      * the next character or graphic is rendered. A graphic can be
107      * rendered beyond its advance
108      * @return the advance of this <code>GraphicAttribute</code>.
109      * @see #getBounds()
110      */

111     public abstract float getAdvance();
112
113     /**
114      * Returns a {@link Rectangle2D} that encloses all of the
115      * bits drawn by this <code>GraphicAttribute</code> relative to the
116      * rendering position.
117      * A graphic may be rendered beyond its origin, ascent, descent,
118      * or advance; but if it is, this method's implementation must
119      * indicate where the graphic is rendered.
120      * Default bounds is the rectangle (0, -ascent, advance, ascent+descent).
121      * @return a <code>Rectangle2D</code> that encloses all of the bits
122      * rendered by this <code>GraphicAttribute</code>.
123      */

124     public Rectangle2D JavaDoc getBounds() {
125         float ascent = getAscent();
126         return new Rectangle2D.Float JavaDoc(0, -ascent,
127                                         getAdvance(), ascent+getDescent());
128     }
129
130     /**
131      * Renders this <code>GraphicAttribute</code> at the specified
132      * location.
133      * @param graphics the {@link Graphics2D} into which to render the
134      * graphic
135      * @param x,&nbsp;y the user-space coordinates where
136      * the graphic is rendered
137      */

138     public abstract void draw(Graphics2D JavaDoc graphics, float x, float y);
139
140     /**
141      * Returns the alignment of this <code>GraphicAttribute</code>.
142      * Alignment can be to a particular baseline, or to the absolute top
143      * or bottom of a line.
144      * @return the alignment of this <code>GraphicAttribute</code>.
145      */

146     public final int getAlignment() {
147
148         return fAlignment;
149     }
150
151     /**
152      * Returns the justification information for this
153      * <code>GraphicAttribute</code>. Subclasses
154      * can override this method to provide different justification
155      * information.
156      * @return a {@link GlyphJustificationInfo} object that contains the
157      * justification information for this <code>GraphicAttribute</code>.
158      */

159     public GlyphJustificationInfo JavaDoc getJustificationInfo() {
160
161         // should we cache this?
162
float advance = getAdvance();
163
164         return new GlyphJustificationInfo JavaDoc(
165                                      advance, // weight
166
false, // growAbsorb
167
2, // growPriority
168
advance/3, // growLeftLimit
169
advance/3, // growRightLimit
170
false, // shrinkAbsorb
171
1, // shrinkPriority
172
0, // shrinkLeftLimit
173
0); // shrinkRightLimit
174
}
175 }
176
Popular Tags