001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Ilya S. Okomin
019: * @version $Revision$
020: */package java.awt.font;
021:
022: import java.awt.BasicStroke;
023: import java.awt.Graphics2D;
024: import java.awt.Shape;
025: import java.awt.Stroke;
026: import java.awt.geom.AffineTransform;
027: import java.awt.geom.Rectangle2D;
028:
029: import org.apache.harmony.misc.HashCode;
030:
031: public final class ShapeGraphicAttribute extends GraphicAttribute {
032:
033: // shape to render
034: private Shape fShape;
035:
036: // flag, if the shape should be stroked (true) or filled (false)
037: private boolean fStroke;
038:
039: // bounds of the shape
040: private Rectangle2D fBounds;
041:
042: // X coordinate of the origin point
043: private float fOriginX;
044:
045: // Y coordinate of the origin point
046: private float fOriginY;
047:
048: // width of the shape
049: private float fShapeWidth;
050:
051: // height of the shape
052: private float fShapeHeight;
053:
054: public static final boolean STROKE = true;
055:
056: public static final boolean FILL = false;
057:
058: public ShapeGraphicAttribute(Shape shape, int alignment,
059: boolean stroke) {
060: super (alignment);
061:
062: this .fShape = shape;
063: this .fStroke = stroke;
064:
065: this .fBounds = fShape.getBounds2D();
066:
067: this .fOriginX = (float) fBounds.getMinX();
068: this .fOriginY = (float) fBounds.getMinY();
069:
070: this .fShapeWidth = (float) fBounds.getWidth();
071: this .fShapeHeight = (float) fBounds.getHeight();
072: }
073:
074: @Override
075: public int hashCode() {
076: HashCode hash = new HashCode();
077:
078: hash.append(fShape.hashCode());
079: hash.append(getAlignment());
080: return hash.hashCode();
081: }
082:
083: public boolean equals(ShapeGraphicAttribute sga) {
084: if (sga == null) {
085: return false;
086: }
087:
088: if (sga == this ) {
089: return true;
090: }
091:
092: return (fStroke == sga.fStroke
093: && getAlignment() == sga.getAlignment() && fShape
094: .equals(sga.fShape));
095:
096: }
097:
098: @Override
099: public boolean equals(Object obj) {
100: try {
101: return equals((ShapeGraphicAttribute) obj);
102: } catch (ClassCastException e) {
103: return false;
104: }
105: }
106:
107: @Override
108: public void draw(Graphics2D g2, float x, float y) {
109: AffineTransform at = AffineTransform.getTranslateInstance(x, y);
110: if (fStroke == STROKE) {
111: Stroke oldStroke = g2.getStroke();
112: g2.setStroke(new BasicStroke());
113: g2.draw(at.createTransformedShape(fShape));
114: g2.setStroke(oldStroke);
115: } else {
116: g2.fill(at.createTransformedShape(fShape));
117: }
118:
119: }
120:
121: @Override
122: public float getAdvance() {
123: return Math.max(0, fShapeWidth + fOriginX);
124: }
125:
126: @Override
127: public float getAscent() {
128: return Math.max(0, -fOriginY);
129: }
130:
131: @Override
132: public Rectangle2D getBounds() {
133: return (Rectangle2D) fBounds.clone();
134: }
135:
136: @Override
137: public float getDescent() {
138: return Math.max(0, fShapeHeight + fOriginY);
139: }
140:
141: }
|