001 /* ImageGraphicAttribute.java
002 Copyright (C) 2003 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package java.awt.font;
040
041 import java.awt.Graphics2D;
042 import java.awt.Image;
043 import java.awt.geom.Rectangle2D;
044
045 /**
046 * This is an implementation of GraphicAttribute which draws images in a
047 * TextLayout.
048 *
049 * @author Lillian Angel
050 * @author Michael Koch
051 */
052 public final class ImageGraphicAttribute
053 extends GraphicAttribute
054 {
055 private Image image;
056 private float originX;
057 private float originY;
058
059 /**
060 * Constucts an instance from the specified Image. The origin is at (0, 0).
061 *
062 * @param image - image to construct from.
063 * @param alignment - the alignment
064 */
065 public ImageGraphicAttribute(Image image, int alignment)
066 {
067 this(image, alignment, 0, 0);
068 }
069
070 /**
071 * Constucts an instance from the specified Image. The origin is at (originX,
072 * originY).
073 *
074 * @param image - image to construct from
075 * @param alignment - the alignment
076 * @param originX - x point of origin
077 * @param originY - y point of origin
078 */
079 public ImageGraphicAttribute(Image image, int alignment, float originX,
080 float originY)
081 {
082 super(alignment);
083 this.image = image;
084 this.originX = originX;
085 this.originY = originY;
086 }
087
088 /**
089 * Draws the image at the specified location, relative to the
090 * origin.
091 *
092 * @param g - the graphics to use to render the image
093 * @param x - the x location
094 * @param y - the y location
095 */
096 public void draw(Graphics2D g, float x, float y)
097 {
098 g.drawImage(image, (int) (x - originX), (int) (y - originY), null);
099 }
100
101 /**
102 * Compares this to the specified Object
103 *
104 * @param obj - the object to compare
105 * @return true if the obj and this are equivalent
106 */
107 public boolean equals(Object obj)
108 {
109 if (! (obj instanceof ImageGraphicAttribute))
110 return false;
111
112 return equals((ImageGraphicAttribute) obj);
113 }
114
115 /**
116 * Compares this to the ImageGraphicAttribute given, by
117 * comparing all fields and values.
118 *
119 * @param rhs - the ImageGraphicAttribute to compare
120 * @return true if the object given is equivalent to this
121 */
122 public boolean equals(ImageGraphicAttribute rhs)
123 {
124 return ((this == rhs) || ((this.getAscent() == rhs.getAscent())
125 && (this.getAdvance() == rhs.getAdvance())
126 && (this.getAlignment() == rhs.getAlignment())
127 && (this.getBounds().equals(rhs.getBounds()))
128 && (this.getDescent() == rhs.getDescent())
129 && (this.hashCode() == rhs.hashCode())
130 && (this.image.equals(rhs.image))
131 && (this.originX == rhs.originX)
132 && (this.originY == rhs.originY)));
133 }
134
135 /**
136 * Returns distance from the origin to the right edge of the image of this.
137 *
138 * @return the advance
139 */
140 public float getAdvance()
141 {
142 return Math.max(0, image.getWidth(null) - originX);
143 }
144
145 /**
146 * Returns the the distance from the top of the image to the origin of this.
147 *
148 * @return the ascent.
149 */
150 public float getAscent()
151 {
152 return Math.max(0, originY);
153 }
154
155 /**
156 * Gets the bounds of the object rendered, relative to the position.
157 *
158 * @return the bounds of the object rendered, relative to the position.
159 */
160 public Rectangle2D getBounds()
161 {
162 // This is equivalent to what Sun's JDK returns.
163 // I am not entirely sure why the origin is negative.
164 return new Rectangle2D.Float(- originX, - originY, image.getWidth(null),
165 image.getHeight(null));
166 }
167
168 /**
169 * Returns the distance from the origin to the bottom of the image.
170 *
171 * @return the descent
172 */
173 public float getDescent()
174 {
175 return Math.max(0, image.getHeight(null) - originY);
176 }
177
178 /**
179 * Gets the hash code for this image.
180 *
181 * @return the hash code
182 */
183 public int hashCode()
184 {
185 return image.hashCode();
186 }
187 }