Source for java.awt.font.ShapeGraphicAttribute

   1: /* ShapeGraphicAttribute.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.Shape;
  43: import java.awt.geom.Rectangle2D;
  44: 
  45: /**
  46:  * This is an implementation of GraphicAttribute that draws shapes in a TextLayout.
  47:  * 
  48:  * @author Lillian Angel (langel at redhat dot com)
  49:  */
  50: public final class ShapeGraphicAttribute extends GraphicAttribute
  51: {
  52:   /** True if the shape should be filled. */
  53:   public static final boolean FILL = false;
  54:   
  55:   /** True if the shape should be stroked with a 1-pixel wide stroke. */
  56:   public static final boolean STROKE = true;
  57: 
  58:   private Shape shape;
  59:   private boolean stroke;
  60:   private Rectangle2D bounds;
  61:   
  62:   /**
  63:    * Constructor.
  64:    * 
  65:    * @param shape - the Shape to render. The Shape is rendered with its origin.
  66:    * @param alignment - the alignment
  67:    * @param stroke - true if the Shape should be stroked; false if the Shape
  68:    *          should be filled.
  69:    */
  70:   public ShapeGraphicAttribute(Shape shape, int alignment, boolean stroke)
  71:   {
  72:     super(alignment);
  73:     this.shape = shape;
  74:     this.stroke = stroke;
  75:     this.bounds = shape.getBounds2D();
  76:   }
  77: 
  78:   /**
  79:    * Draws the graphic at the given location.
  80:    * 
  81:    * @param graphics - the graphics to use.
  82:    * @param x - the x location to draw at.
  83:    * @param y - the y location to draw at.
  84:    */
  85:   public void draw(Graphics2D graphics, float x, float y)
  86:   {
  87:     graphics.translate(x, y);
  88:     if (stroke == STROKE)
  89:       graphics.draw(shape);
  90:     else
  91:       graphics.fill(shape);
  92:     graphics.translate(- x, - y);
  93:   }
  94: 
  95:   /**
  96:    * Compares this ShapeGraphicAttribute to obj.
  97:    * 
  98:    * @param obj - the object to compare.
  99:    */
 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: }