001 /*
002 * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 /*
027 * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
028 * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
029 *
030 * The original version of this source code and documentation is
031 * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
032 * of IBM. These materials are provided under terms of a License
033 * Agreement between Taligent and Sun. This technology is protected
034 * by multiple US and International patents.
035 *
036 * This notice and attribution to Taligent may not be removed.
037 * Taligent is a registered trademark of Taligent, Inc.
038 *
039 */
040
041 package java.awt.font;
042
043 import java.awt.Graphics2D;
044 import java.awt.Font;
045 import java.awt.Shape;
046 import java.awt.geom.AffineTransform;
047 import java.awt.geom.Rectangle2D;
048
049 /**
050 * This class is used with the CHAR_REPLACEMENT attribute.
051 * <p>
052 * The <code>GraphicAttribute</code> class represents a graphic embedded
053 * in text. Clients subclass this class to implement their own char
054 * replacement graphics. Clients wishing to embed shapes and images in
055 * text need not subclass this class. Instead, clients can use the
056 * {@link ShapeGraphicAttribute} and {@link ImageGraphicAttribute}
057 * classes.
058 * <p>
059 * Subclasses must ensure that their objects are immutable once they
060 * are constructed. Mutating a <code>GraphicAttribute</code> that
061 * is used in a {@link TextLayout} results in undefined behavior from the
062 * <code>TextLayout</code>.
063 */
064 public abstract class GraphicAttribute {
065
066 private int fAlignment;
067
068 /**
069 * Aligns top of graphic to top of line.
070 */
071 public static final int TOP_ALIGNMENT = -1;
072
073 /**
074 * Aligns bottom of graphic to bottom of line.
075 */
076 public static final int BOTTOM_ALIGNMENT = -2;
077
078 /**
079 * Aligns origin of graphic to roman baseline of line.
080 */
081 public static final int ROMAN_BASELINE = Font.ROMAN_BASELINE;
082
083 /**
084 * Aligns origin of graphic to center baseline of line.
085 */
086 public static final int CENTER_BASELINE = Font.CENTER_BASELINE;
087
088 /**
089 * Aligns origin of graphic to hanging baseline of line.
090 */
091 public static final int HANGING_BASELINE = Font.HANGING_BASELINE;
092
093 /**
094 * Constructs a <code>GraphicAttribute</code>.
095 * Subclasses use this to define the alignment of the graphic.
096 * @param alignment an int representing one of the
097 * <code>GraphicAttribute</code> alignment fields
098 * @throws IllegalArgumentException if alignment is not one of the
099 * five defined values.
100 */
101 protected GraphicAttribute(int alignment) {
102 if (alignment < BOTTOM_ALIGNMENT
103 || alignment > HANGING_BASELINE) {
104 throw new IllegalArgumentException("bad alignment");
105 }
106 fAlignment = alignment;
107 }
108
109 /**
110 * Returns the ascent of this <code>GraphicAttribute</code>. A
111 * graphic can be rendered above its ascent.
112 * @return the ascent of this <code>GraphicAttribute</code>.
113 * @see #getBounds()
114 */
115 public abstract float getAscent();
116
117 /**
118 * Returns the descent of this <code>GraphicAttribute</code>. A
119 * graphic can be rendered below its descent.
120 * @return the descent of this <code>GraphicAttribute</code>.
121 * @see #getBounds()
122 */
123 public abstract float getDescent();
124
125 /**
126 * Returns the advance of this <code>GraphicAttribute</code>. The
127 * <code>GraphicAttribute</code> object's advance is the distance
128 * from the point at which the graphic is rendered and the point where
129 * the next character or graphic is rendered. A graphic can be
130 * rendered beyond its advance
131 * @return the advance of this <code>GraphicAttribute</code>.
132 * @see #getBounds()
133 */
134 public abstract float getAdvance();
135
136 /**
137 * Returns a {@link Rectangle2D} that encloses all of the
138 * bits drawn by this <code>GraphicAttribute</code> relative to the
139 * rendering position.
140 * A graphic may be rendered beyond its origin, ascent, descent,
141 * or advance; but if it is, this method's implementation must
142 * indicate where the graphic is rendered.
143 * Default bounds is the rectangle (0, -ascent, advance, ascent+descent).
144 * @return a <code>Rectangle2D</code> that encloses all of the bits
145 * rendered by this <code>GraphicAttribute</code>.
146 */
147 public Rectangle2D getBounds() {
148 float ascent = getAscent();
149 return new Rectangle2D.Float(0, -ascent, getAdvance(), ascent
150 + getDescent());
151 }
152
153 /**
154 * Return a {@link java.awt.Shape} that represents the region that
155 * this <code>GraphicAttribute</code> renders. This is used when a
156 * {@link TextLayout} is requested to return the outline of the text.
157 * The (untransformed) shape must not extend outside the rectangular
158 * bounds returned by <code>getBounds</code>.
159 * The default implementation returns the rectangle returned by
160 * {@link #getBounds}, transformed by the provided {@link AffineTransform}
161 * if present.
162 * @param tx an optional {@link AffineTransform} to apply to the
163 * outline of this <code>GraphicAttribute</code>. This can be null.
164 * @return a <code>Shape</code> representing this graphic attribute,
165 * suitable for stroking or filling.
166 * @since 1.6
167 */
168 public Shape getOutline(AffineTransform tx) {
169 Shape b = getBounds();
170 if (tx != null) {
171 b = tx.createTransformedShape(b);
172 }
173 return b;
174 }
175
176 /**
177 * Renders this <code>GraphicAttribute</code> at the specified
178 * location.
179 * @param graphics the {@link Graphics2D} into which to render the
180 * graphic
181 * @param x the user-space X coordinate where the graphic is rendered
182 * @param y the user-space Y coordinate where the graphic is rendered
183 */
184 public abstract void draw(Graphics2D graphics, float x, float y);
185
186 /**
187 * Returns the alignment of this <code>GraphicAttribute</code>.
188 * Alignment can be to a particular baseline, or to the absolute top
189 * or bottom of a line.
190 * @return the alignment of this <code>GraphicAttribute</code>.
191 */
192 public final int getAlignment() {
193
194 return fAlignment;
195 }
196
197 /**
198 * Returns the justification information for this
199 * <code>GraphicAttribute</code>. Subclasses
200 * can override this method to provide different justification
201 * information.
202 * @return a {@link GlyphJustificationInfo} object that contains the
203 * justification information for this <code>GraphicAttribute</code>.
204 */
205 public GlyphJustificationInfo getJustificationInfo() {
206
207 // should we cache this?
208 float advance = getAdvance();
209
210 return new GlyphJustificationInfo(advance, // weight
211 false, // growAbsorb
212 2, // growPriority
213 advance / 3, // growLeftLimit
214 advance / 3, // growRightLimit
215 false, // shrinkAbsorb
216 1, // shrinkPriority
217 0, // shrinkLeftLimit
218 0); // shrinkRightLimit
219 }
220 }
|