KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > font > FontRenderContext


1 /*
2  * @(#)FontRenderContext.java 1.30 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 /*
9  * @author Charlton Innovations, Inc.
10  */

11
12 package java.awt.font;
13
14 import java.awt.geom.AffineTransform JavaDoc;
15
16 /**
17 * The <code>FontRenderContext</code> class is a container for the
18 * information needed to correctly measure text. The measurement of text
19 * can vary because of rules that map outlines to pixels, and rendering
20 * hints provided by an application.
21 * <p>
22 * One such piece of information is a transform that scales
23 * typographical points to pixels. (A point is defined to be exactly 1/72
24 * of an inch, which is slightly different than
25 * the traditional mechanical measurement of a point.) A character that
26 * is rendered at 12pt on a 600dpi device might have a different size
27 * than the same character rendered at 12pt on a 72dpi device because of
28 * such factors as rounding to pixel boundaries and hints that the font
29 * designer may have specified.
30 * <p>
31 * Anti-aliasing and Fractional-metrics specified by an application can also
32 * affect the size of a character because of rounding to pixel
33 * boundaries.
34 * <p>
35 * Typically, instances of <code>FontRenderContext</code> are
36 * obtained from a {@link java.awt.Graphics2D Graphics2D} object. A
37 * <code>FontRenderContext</code> which is directly constructed will
38 * most likely not represent any actual graphics device, and may lead
39 * to unexpected or incorrect results.
40 * <p>
41 * @see java.awt.RenderingHints#KEY_TEXT_ANTIALIASING
42 * @see java.awt.RenderingHints#KEY_FRACTIONALMETRICS
43 * @see java.awt.Graphics2D#getFontRenderContext()
44 * @see java.awt.font.LineMetrics
45 */

46
47 public class FontRenderContext {
48     private transient AffineTransform JavaDoc tx;
49     private transient boolean bIsAntiAliased;
50     private transient boolean bUsesFractionalMetrics;
51
52     /**
53      * Constructs a new <code>FontRenderContext</code>
54      * object.
55      *
56      */

57     protected FontRenderContext() {
58     }
59
60     /**
61      * Constructs a <code>FontRenderContext</code> object from an
62      * optional {@link AffineTransform} and two <code>boolean</code>
63      * values that determine if the newly constructed object has
64      * anti-aliasing or fractional metrics.
65      * @param tx the transform which is used to scale typographical points
66      * to pixels in this <code>FontRenderContext</code>. If null, an
67      * identity tranform is used.
68      * @param isAntiAliased determines if the newly contructed object has
69      * anti-aliasing
70      * @param usesFractionalMetrics determines if the newly constructed
71      * object uses fractional metrics
72      */

73     public FontRenderContext(AffineTransform JavaDoc tx,
74                             boolean isAntiAliased,
75                             boolean usesFractionalMetrics) {
76         if (tx != null && !tx.isIdentity()) {
77             this.tx = new AffineTransform JavaDoc(tx);
78         }
79         this.bIsAntiAliased = isAntiAliased;
80         this.bUsesFractionalMetrics = usesFractionalMetrics;
81     }
82
83
84     /**
85     * Gets the transform that is used to scale typographical points
86     * to pixels in this <code>FontRenderContext</code>.
87     * @return the <code>AffineTransform</code> of this
88     * <code>FontRenderContext</code>.
89     * @see AffineTransform
90     */

91     public AffineTransform JavaDoc getTransform() {
92         return (tx == null) ? new AffineTransform JavaDoc() : new AffineTransform JavaDoc(tx);
93     }
94
95     /**
96     * Gets the text anti-aliasing mode used in this
97     * <code>FontRenderContext</code>.
98     * @return <code>true</code>, if text is anti-aliased in this
99     * <code>FontRenderContext</code>; <code>false</code> otherwise.
100     * @see java.awt.RenderingHints#KEY_TEXT_ANTIALIASING
101     */

102     public boolean isAntiAliased() {
103         return this.bIsAntiAliased;
104     }
105
106     /**
107     * Gets the text fractional metrics mode requested by the application
108     * for use in this <code>FontRenderContext</code>.
109     * @return <code>true</code>, if layout should be performed with
110     * fractional metrics; <code>false</code> otherwise.
111     * in this <code>FontRenderContext</code>.
112     * @see java.awt.RenderingHints#KEY_FRACTIONALMETRICS
113     */

114     public boolean usesFractionalMetrics() {
115         return this.bUsesFractionalMetrics;
116     }
117
118     /**
119      * Return true if obj is an instance of FontRenderContext and has the same
120      * transform, antialiasing, and fractional metrics values as this.
121      * @param obj the object to test for equality
122      * @return <code>true</code> if the specified object is equal to
123      * this <code>FontRenderContext</code>; <code>false</code>
124      * otherwise.
125      */

126     public boolean equals(Object JavaDoc obj) {
127     try {
128         return equals((FontRenderContext JavaDoc)obj);
129     }
130     catch (ClassCastException JavaDoc e) {
131         return false;
132     }
133     }
134
135     /**
136      * Return true if rhs has the same transform, antialiasing,
137      * and fractional metrics values as this.
138      * @param rhs the <code>FontRenderContext</code> to test for equality
139      * @return <code>true</code> if <code>rhs</code> is equal to
140      * this <code>FontRenderContext</code>; <code>false</code>
141      * otherwise.
142      */

143     public boolean equals(FontRenderContext JavaDoc rhs) {
144     if (this == rhs) {
145         return true;
146     }
147     if (rhs != null &&
148         rhs.bIsAntiAliased == bIsAntiAliased &&
149         rhs.bUsesFractionalMetrics == bUsesFractionalMetrics) {
150         
151         return tx == null ? rhs.tx == null : tx.equals(rhs.tx);
152     }
153     return false;
154     }
155
156     /**
157      * Return a hashcode for this FontRenderContext.
158      */

159     public int hashCode() {
160     int hash = tx == null ? 0 : tx.hashCode();
161     if (bIsAntiAliased) {
162         hash ^= 0x1;
163     }
164     if (bUsesFractionalMetrics) {
165         hash ^= 0x2;
166     }
167     return hash;
168     }
169 }
170
Popular Tags