1 /* ImageGraphicAttribute.java
2    Copyright (C) 2003 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10 
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING.  If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20 
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library.  Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
25 
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module.  An independent module is a module which is not derived from
33 or based on this library.  If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so.  If you do not wish to do so, delete this
36 exception statement from your version. */
37 
38 
39 package java.awt.font;
40 
41 import java.awt.Graphics2D;
42 import java.awt.Image;
43 import java.awt.geom.Rectangle2D;
44 
45 /**
46  * This is an implementation of GraphicAttribute which draws images in a
47  * TextLayout.
48  *
49  * @author Lillian Angel
50  * @author Michael Koch
51  */
52 public final class ImageGraphicAttribute
53     extends GraphicAttribute
54 {
55   private Image image;
56   private float originX;
57   private float originY;
58 
59   /**
60    * Constucts an instance from the specified Image. The origin is at (0, 0).
61    *
62    * @param image - image to construct from.
63    * @param alignment - the alignment
64    */
ImageGraphicAttribute(Image image, int alignment)65   public ImageGraphicAttribute(Image image, int alignment)
66   {
67     this(image, alignment, 0, 0);
68   }
69 
70   /**
71    * Constucts an instance from the specified Image. The origin is at (originX,
72    * originY).
73    *
74    * @param image - image to construct from
75    * @param alignment - the alignment
76    * @param originX - x point of origin
77    * @param originY - y point of origin
78    */
ImageGraphicAttribute(Image image, int alignment, float originX, float originY)79   public ImageGraphicAttribute(Image image, int alignment, float originX,
80                                float originY)
81   {
82     super(alignment);
83     this.image = image;
84     this.originX = originX;
85     this.originY = originY;
86   }
87 
88   /**
89    * Draws the image at the specified location, relative to the
90    * origin.
91    *
92    * @param g - the graphics to use to render the image
93    * @param x - the x location
94    * @param y - the y location
95    */
draw(Graphics2D g, float x, float y)96   public void draw(Graphics2D g, float x, float y)
97   {
98     g.drawImage(image, (int) (x - originX), (int) (y - originY), null);
99   }
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    */
equals(Object obj)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    */
equals(ImageGraphicAttribute rhs)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    */
getAdvance()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    */
getAscent()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    */
getBounds()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    */
getDescent()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    */
hashCode()183   public int hashCode()
184   {
185     return image.hashCode();
186   }
187 }
188