1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Ilya S. Okomin
19 * @version $Revision$
20 */
21
22package java.awt.font;
23
24import java.awt.Graphics2D;
25import java.awt.Image;
26import java.awt.geom.Rectangle2D;
27
28import org.apache.harmony.misc.HashCode;
29
30/**
31 * The ImageGraphicAttribute class provides an opportunity to insert images to a
32 * text.
33 *
34 * @since Android 1.0
35 */
36public final class ImageGraphicAttribute extends GraphicAttribute {
37
38    // Image object rendered by this ImageGraphicAttribute
39    /**
40     * The image.
41     */
42    private Image fImage;
43
44    // X coordinate of the origin point
45    /**
46     * The origin x.
47     */
48    private float fOriginX;
49
50    // Y coordinate of the origin point
51    /**
52     * The origin y.
53     */
54    private float fOriginY;
55
56    // the width of the image object
57    /**
58     * The img width.
59     */
60    private float fImgWidth;
61
62    // the height of the image object
63    /**
64     * The img height.
65     */
66    private float fImgHeight;
67
68    /**
69     * Instantiates a new ImageGraphicAttribute with the specified image,
70     * alignment and origins.
71     *
72     * @param image
73     *            the Image to be rendered by ImageGraphicAttribute.
74     * @param alignment
75     *            the alignment of the ImageGraphicAttribute.
76     * @param originX
77     *            the origin X coordinate in the image of ImageGraphicAttribute.
78     * @param originY
79     *            the origin Y coordinate in the image of ImageGraphicAttribute.
80     */
81    public ImageGraphicAttribute(Image image, int alignment, float originX, float originY) {
82        super(alignment);
83
84        this.fImage = image;
85        this.fOriginX = originX;
86        this.fOriginY = originY;
87
88        this.fImgWidth = fImage.getWidth(null);
89        this.fImgHeight = fImage.getHeight(null);
90
91    }
92
93    /**
94     * Instantiates a new ImageGraphicAttribute with the specified image and
95     * alignment.
96     *
97     * @param image
98     *            the Image to be rendered by ImageGraphicAttribute.
99     * @param alignment
100     *            the alignment of the ImageGraphicAttribute.
101     */
102    public ImageGraphicAttribute(Image image, int alignment) {
103        this(image, alignment, 0, 0);
104    }
105
106    /**
107     * Returns a hash code of this ImageGraphicAttribute object.
108     *
109     * @return the hash code of this ImageGraphicAttribute object.
110     */
111    @Override
112    public int hashCode() {
113        HashCode hash = new HashCode();
114
115        hash.append(fImage.hashCode());
116        hash.append(getAlignment());
117        return hash.hashCode();
118    }
119
120    /**
121     * Compares the specified ImageGraphicAttribute object with this
122     * ImageGraphicAttribute object.
123     *
124     * @param iga
125     *            the ImageGraphicAttribute object to be compared.
126     * @return true, if the specified ImageGraphicAttribute object is equal to
127     *         this ImageGraphicAttribute object, false otherwise.
128     */
129    public boolean equals(ImageGraphicAttribute iga) {
130        if (iga == null) {
131            return false;
132        }
133
134        if (iga == this) {
135            return true;
136        }
137
138        return (fOriginX == iga.fOriginX && fOriginY == iga.fOriginY
139                && getAlignment() == iga.getAlignment() && fImage.equals(iga.fImage));
140    }
141
142    /**
143     * Compares the specified Object with this ImageGraphicAttribute object.
144     *
145     * @param obj
146     *            the Object to be compared.
147     * @return true, if the specified Object is equal to this
148     *         ImageGraphicAttribute object, false otherwise.
149     */
150    @Override
151    public boolean equals(Object obj) {
152        try {
153            return equals((ImageGraphicAttribute)obj);
154        } catch (ClassCastException e) {
155            return false;
156        }
157
158    }
159
160    @Override
161    public void draw(Graphics2D g2, float x, float y) {
162        g2.drawImage(fImage, (int)(x - fOriginX), (int)(y - fOriginY), null);
163    }
164
165    @Override
166    public float getAdvance() {
167        return Math.max(0, fImgWidth - fOriginX);
168    }
169
170    @Override
171    public float getAscent() {
172        return Math.max(0, fOriginY);
173    }
174
175    @Override
176    public Rectangle2D getBounds() {
177        return new Rectangle2D.Float(-fOriginX, -fOriginY, fImgWidth, fImgHeight);
178    }
179
180    @Override
181    public float getDescent() {
182        return Math.max(0, fImgHeight - fOriginY);
183    }
184
185}
186