001 /* ShapeGraphicAttribute.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.Shape;
043 import java.awt.geom.Rectangle2D;
044
045 /**
046 * This is an implementation of GraphicAttribute that draws shapes in a TextLayout.
047 *
048 * @author Lillian Angel (langel at redhat dot com)
049 */
050 public final class ShapeGraphicAttribute extends GraphicAttribute
051 {
052 /** True if the shape should be filled. */
053 public static final boolean FILL = false;
054
055 /** True if the shape should be stroked with a 1-pixel wide stroke. */
056 public static final boolean STROKE = true;
057
058 private Shape shape;
059 private boolean stroke;
060 private Rectangle2D bounds;
061
062 /**
063 * Constructor.
064 *
065 * @param shape - the Shape to render. The Shape is rendered with its origin.
066 * @param alignment - the alignment
067 * @param stroke - true if the Shape should be stroked; false if the Shape
068 * should be filled.
069 */
070 public ShapeGraphicAttribute(Shape shape, int alignment, boolean stroke)
071 {
072 super(alignment);
073 this.shape = shape;
074 this.stroke = stroke;
075 this.bounds = shape.getBounds2D();
076 }
077
078 /**
079 * Draws the graphic at the given location.
080 *
081 * @param graphics - the graphics to use.
082 * @param x - the x location to draw at.
083 * @param y - the y location to draw at.
084 */
085 public void draw(Graphics2D graphics, float x, float y)
086 {
087 graphics.translate(x, y);
088 if (stroke == STROKE)
089 graphics.draw(shape);
090 else
091 graphics.fill(shape);
092 graphics.translate(- x, - y);
093 }
094
095 /**
096 * Compares this ShapeGraphicAttribute to obj.
097 *
098 * @param obj - the object to compare.
099 */
100 public boolean equals(Object obj)
101 {
102 if (! (obj instanceof ShapeGraphicAttribute))
103 return false;
104
105 return equals((ShapeGraphicAttribute) obj);
106 }
107
108 /**
109 * Compares this ShapeGraphicAttribute to rhs.
110 *
111 * @param rhs - the ShapeGraphicAttribute to compare.
112 */
113 public boolean equals(ShapeGraphicAttribute rhs)
114 {
115 return (this == rhs || (this.shape.equals(rhs.shape)
116 && getAlignment() == rhs.getAlignment()
117 && stroke == rhs.stroke
118 && getAdvance() == rhs.getAdvance()
119 && getAscent() == rhs.getAscent()
120 && getBounds().equals(rhs.getBounds())
121 && getDescent() == rhs.getDescent()
122 && hashCode() == rhs.hashCode()));
123 }
124
125 /**
126 * Gets the distance from the origin of its Shape to the right side of the
127 * bounds of its Shape.
128 *
129 * @return the advance
130 */
131 public float getAdvance()
132 {
133 return Math.max(0, (float) bounds.getMaxX());
134 }
135
136 /**
137 * Gets the positive distance from the origin of its Shape to the top of
138 * bounds.
139 *
140 * @return the ascent
141 */
142 public float getAscent()
143 {
144 return Math.max(0, -(float) bounds.getMinY());
145 }
146
147 /**
148 * Gets the distance from the origin of its Shape to the bottom of the bounds.
149 *
150 * @return the descent
151 */
152 public float getDescent()
153 {
154 return Math.max(0, (float) bounds.getMaxY());
155 }
156
157 /**
158 * Returns a Rectangle2D that encloses all of the bits drawn by this shape.
159 *
160 * @return the bounds of the shape.
161 */
162 public Rectangle2D getBounds()
163 {
164 Rectangle2D.Float bounds = new Rectangle2D.Float();
165 bounds.setRect(this.bounds);
166
167 if (stroke == STROKE)
168 {
169 bounds.width++;
170 bounds.height++;
171 }
172
173 return bounds;
174 }
175
176 /**
177 * Gets the hash code.
178 *
179 * @return the hash code.
180 */
181 public int hashCode()
182 {
183 return shape.hashCode();
184 }
185 }