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 */
21package java.awt.font;
22
23import java.awt.geom.AffineTransform;
24
25/**
26 * The FontRenderContext class contains the information about text measurement.
27 * Anti-aliasing and fractional-metrics modes are defined by an application and
28 * affect the size of a character.
29 *
30 * @since Android 1.0
31 */
32public class FontRenderContext {
33
34    // Affine transform of this mode
35    /**
36     * The transform.
37     */
38    private AffineTransform transform;
39
40    // Is the anti-aliased mode used
41    /**
42     * The anti aliased.
43     */
44    private boolean fAntiAliased;
45
46    // Is the fractional metrics used
47    /**
48     * The fractional metrics.
49     */
50    private boolean fFractionalMetrics;
51
52
53    /**
54     * Instantiates a new FontRenderContext object with the specified
55     * AffineTransform, anti-aliasing and fractional metrics flags.
56     *
57     * @param trans
58     *            the AffineTransform.
59     * @param antiAliased
60     *            the anti-aliasing flag.
61     * @param usesFractionalMetrics
62     *            the fractional metrics flag.
63     */
64    public FontRenderContext(AffineTransform trans, boolean antiAliased,
65            boolean usesFractionalMetrics) {
66        if (trans != null){
67            transform = new AffineTransform(trans);
68        }
69        fAntiAliased = antiAliased;
70        fFractionalMetrics = usesFractionalMetrics;
71    }
72
73    /**
74     * Instantiates a new FontRenderContext object.
75     */
76    protected FontRenderContext() {
77    }
78
79    /**
80     * Compares the specified Object with current FontRenderContext object.
81     *
82     * @param obj
83     *            the Object to be compared.
84     * @return true, if the specified Object is equal to current
85     *         FontRenderContext object.
86     */
87    @Override
88    public boolean equals(Object obj) {
89        if (obj == this) {
90            return true;
91        }
92
93        if (obj != null) {
94            try {
95                return equals((FontRenderContext) obj);
96            } catch (ClassCastException e) {
97                return false;
98            }
99        }
100        return false;
101
102    }
103
104    /**
105     * Gets the transform which is used for scaling typographical points to
106     * pixels in this FontRenderContext.
107     *
108     * @return the AffineTransform which is used for scaling typographical
109     *         points to pixels in this FontRenderContext.
110     */
111    public AffineTransform getTransform() {
112        if (transform != null){
113            return new AffineTransform(transform);
114        }
115        return new AffineTransform();
116    }
117
118    /**
119     * Compares the specified FontRenderContext object with current
120     * FontRenderContext.
121     *
122     * @param frc
123     *            the FontRenderContext object to be compared.
124     * @return true, if the specified FontRenderContext object is equal to
125     *         current FontRenderContext.
126     */
127    public boolean equals(FontRenderContext frc) {
128        if (this == frc){
129            return true;
130        }
131
132        if (frc == null){
133            return false;
134        }
135
136        if (!frc.getTransform().equals(this.getTransform()) &&
137            !frc.isAntiAliased() == this.fAntiAliased &&
138            !frc.usesFractionalMetrics() == this.fFractionalMetrics){
139            return false;
140        }
141        return true;
142    }
143
144    /**
145     * Returns true if the text fractional metrics are used in this
146     * FontRenderContext.
147     *
148     * @return true, if the text fractional metrics are used in this
149     *         FontRenderContext, false otherwise.
150     */
151    public boolean usesFractionalMetrics() {
152        return this.fFractionalMetrics;
153    }
154
155    /**
156     * Returns true if anti-aliasing is used in this FontRenderContext.
157     *
158     * @return true, if is anti-aliasing is used in this FontRenderContext,
159     *         false otherwise.
160     */
161    public boolean isAntiAliased() {
162        return this.fAntiAliased;
163    }
164
165    /**
166     * Returns hash code of the FontRenderContext object.
167     *
168     * @return the hash code of the FontRenderContext object.
169     */
170    @Override
171    public int hashCode() {
172        return this.getTransform().hashCode() ^
173                new Boolean(this.fFractionalMetrics).hashCode() ^
174                new Boolean(this.fAntiAliased).hashCode();
175    }
176
177}
178
179