001 /*
002 * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 /*
027 * @author Charlton Innovations, Inc.
028 */
029
030 package java.awt.font;
031
032 import java.awt.RenderingHints;
033 import static java.awt.RenderingHints.*;
034 import java.awt.geom.AffineTransform;
035
036 /**
037 * The <code>FontRenderContext</code> class is a container for the
038 * information needed to correctly measure text. The measurement of text
039 * can vary because of rules that map outlines to pixels, and rendering
040 * hints provided by an application.
041 * <p>
042 * One such piece of information is a transform that scales
043 * typographical points to pixels. (A point is defined to be exactly 1/72
044 * of an inch, which is slightly different than
045 * the traditional mechanical measurement of a point.) A character that
046 * is rendered at 12pt on a 600dpi device might have a different size
047 * than the same character rendered at 12pt on a 72dpi device because of
048 * such factors as rounding to pixel boundaries and hints that the font
049 * designer may have specified.
050 * <p>
051 * Anti-aliasing and Fractional-metrics specified by an application can also
052 * affect the size of a character because of rounding to pixel
053 * boundaries.
054 * <p>
055 * Typically, instances of <code>FontRenderContext</code> are
056 * obtained from a {@link java.awt.Graphics2D Graphics2D} object. A
057 * <code>FontRenderContext</code> which is directly constructed will
058 * most likely not represent any actual graphics device, and may lead
059 * to unexpected or incorrect results.
060 * <p>
061 * @see java.awt.RenderingHints#KEY_TEXT_ANTIALIASING
062 * @see java.awt.RenderingHints#KEY_FRACTIONALMETRICS
063 * @see java.awt.Graphics2D#getFontRenderContext()
064 * @see java.awt.font.LineMetrics
065 */
066
067 public class FontRenderContext {
068 private transient AffineTransform tx;
069 private transient Object aaHintValue;
070 private transient Object fmHintValue;
071 private transient boolean defaulting;
072
073 /**
074 * Constructs a new <code>FontRenderContext</code>
075 * object.
076 *
077 */
078 protected FontRenderContext() {
079 aaHintValue = VALUE_TEXT_ANTIALIAS_DEFAULT;
080 fmHintValue = VALUE_FRACTIONALMETRICS_DEFAULT;
081 defaulting = true;
082 }
083
084 /**
085 * Constructs a <code>FontRenderContext</code> object from an
086 * optional {@link AffineTransform} and two <code>boolean</code>
087 * values that determine if the newly constructed object has
088 * anti-aliasing or fractional metrics.
089 * In each case the boolean values <CODE>true</CODE> and <CODE>false</CODE>
090 * correspond to the rendering hint values <CODE>ON</CODE> and
091 * <CODE>OFF</CODE> respectively.
092 * <p>
093 * To specify other hint values, use the constructor which
094 * specifies the rendering hint values as parameters :
095 * {@link #FontRenderContext(AffineTransform, Object, Object)}.
096 * @param tx the transform which is used to scale typographical points
097 * to pixels in this <code>FontRenderContext</code>. If null, an
098 * identity transform is used.
099 * @param isAntiAliased determines if the newly constructed object
100 * has anti-aliasing.
101 * @param usesFractionalMetrics determines if the newly constructed
102 * object has fractional metrics.
103 */
104 public FontRenderContext(AffineTransform tx, boolean isAntiAliased,
105 boolean usesFractionalMetrics) {
106 if (tx != null && !tx.isIdentity()) {
107 this .tx = new AffineTransform(tx);
108 }
109 if (isAntiAliased) {
110 aaHintValue = VALUE_TEXT_ANTIALIAS_ON;
111 } else {
112 aaHintValue = VALUE_TEXT_ANTIALIAS_OFF;
113 }
114 if (usesFractionalMetrics) {
115 fmHintValue = VALUE_FRACTIONALMETRICS_ON;
116 } else {
117 fmHintValue = VALUE_FRACTIONALMETRICS_OFF;
118 }
119 }
120
121 /**
122 * Constructs a <code>FontRenderContext</code> object from an
123 * optional {@link AffineTransform} and two <code>Object</code>
124 * values that determine if the newly constructed object has
125 * anti-aliasing or fractional metrics.
126 * @param tx the transform which is used to scale typographical points
127 * to pixels in this <code>FontRenderContext</code>. If null, an
128 * identity tranform is used.
129 * @param aaHint - one of the text antialiasing rendering hint values
130 * defined in {@link java.awt.RenderingHints java.awt.RenderingHints}.
131 * Any other value will throw <code>IllegalArgumentException</code>.
132 * {@link java.awt.RenderingHints#VALUE_TEXT_ANTIALIAS_DEFAULT VALUE_TEXT_ANTIALIAS_DEFAULT}
133 * may be specified, in which case the mode used is implementation
134 * dependent.
135 * @param fmHint - one of the text fractional rendering hint values defined
136 * in {@link java.awt.RenderingHints java.awt.RenderingHints}.
137 * {@link java.awt.RenderingHints#VALUE_FRACTIONALMETRICS_DEFAULT VALUE_FRACTIONALMETRICS_DEFAULT}
138 * may be specified, in which case the mode used is implementation
139 * dependent.
140 * Any other value will throw <code>IllegalArgumentException</code>
141 * @throws IllegalArgumentException if the hints are not one of the
142 * legal values.
143 * @since 1.6
144 */
145 public FontRenderContext(AffineTransform tx, Object aaHint,
146 Object fmHint) {
147 if (tx != null && !tx.isIdentity()) {
148 this .tx = new AffineTransform(tx);
149 }
150 try {
151 if (KEY_TEXT_ANTIALIASING.isCompatibleValue(aaHint)) {
152 aaHintValue = aaHint;
153 } else {
154 throw new IllegalArgumentException("AA hint:" + aaHint);
155 }
156 } catch (Exception e) {
157 throw new IllegalArgumentException("AA hint:" + aaHint);
158 }
159 try {
160 if (KEY_FRACTIONALMETRICS.isCompatibleValue(fmHint)) {
161 fmHintValue = fmHint;
162 } else {
163 throw new IllegalArgumentException("FM hint:" + fmHint);
164 }
165 } catch (Exception e) {
166 throw new IllegalArgumentException("FM hint:" + fmHint);
167 }
168 }
169
170 /**
171 * Indicates whether or not this <code>FontRenderContext</code> object
172 * measures text in a transformed render context.
173 * @return <code>true</code> if this <code>FontRenderContext</code>
174 * object has a non-identity AffineTransform attribute.
175 * <code>false</code> otherwise.
176 * @see java.awt.font.FontRenderContext#getTransform
177 * @since 1.6
178 */
179 public boolean isTransformed() {
180 if (!defaulting) {
181 return tx != null;
182 } else {
183 return !getTransform().isIdentity();
184 }
185 }
186
187 /**
188 * Returns the integer type of the affine transform for this
189 * <code>FontRenderContext</code> as specified by
190 * {@link java.awt.geom.AffineTransform#getType()}
191 * @return the type of the transform.
192 * @see AffineTransform
193 * @since 1.6
194 */
195 public int getTransformType() {
196 if (!defaulting) {
197 if (tx == null) {
198 return AffineTransform.TYPE_IDENTITY;
199 } else {
200 return tx.getType();
201 }
202 } else {
203 return getTransform().getType();
204 }
205 }
206
207 /**
208 * Gets the transform that is used to scale typographical points
209 * to pixels in this <code>FontRenderContext</code>.
210 * @return the <code>AffineTransform</code> of this
211 * <code>FontRenderContext</code>.
212 * @see AffineTransform
213 */
214 public AffineTransform getTransform() {
215 return (tx == null) ? new AffineTransform()
216 : new AffineTransform(tx);
217 }
218
219 /**
220 * Returns a boolean which indicates whether or not some form of
221 * antialiasing is specified by this <code>FontRenderContext</code>.
222 * Call {@link #getAntiAliasingHint() getAntiAliasingHint()}
223 * for the specific rendering hint value.
224 * @return <code>true</code>, if text is anti-aliased in this
225 * <code>FontRenderContext</code>; <code>false</code> otherwise.
226 * @see java.awt.RenderingHints#KEY_TEXT_ANTIALIASING
227 * @see #FontRenderContext(AffineTransform,boolean,boolean)
228 * @see #FontRenderContext(AffineTransform,Object,Object)
229 */
230 public boolean isAntiAliased() {
231 return !(aaHintValue == VALUE_TEXT_ANTIALIAS_OFF || aaHintValue == VALUE_TEXT_ANTIALIAS_DEFAULT);
232 }
233
234 /**
235 * Returns a boolean which whether text fractional metrics mode
236 * is used in this <code>FontRenderContext</code>.
237 * Call {@link #getFractionalMetricsHint() getFractionalMetricsHint()}
238 * to obtain the corresponding rendering hint value.
239 * @return <code>true</code>, if layout should be performed with
240 * fractional metrics; <code>false</code> otherwise.
241 * in this <code>FontRenderContext</code>.
242 * @see java.awt.RenderingHints#KEY_FRACTIONALMETRICS
243 * @see #FontRenderContext(AffineTransform,boolean,boolean)
244 * @see #FontRenderContext(AffineTransform,Object,Object)
245 */
246 public boolean usesFractionalMetrics() {
247 return !(fmHintValue == VALUE_FRACTIONALMETRICS_OFF || fmHintValue == VALUE_FRACTIONALMETRICS_DEFAULT);
248 }
249
250 /**
251 * Return the text anti-aliasing rendering mode hint used in this
252 * <code>FontRenderContext</code>.
253 * This will be one of the text antialiasing rendering hint values
254 * defined in {@link java.awt.RenderingHints java.awt.RenderingHints}.
255 * @return text anti-aliasing rendering mode hint used in this
256 * <code>FontRenderContext</code>.
257 * @since 1.6
258 */
259 public Object getAntiAliasingHint() {
260 if (defaulting) {
261 if (isAntiAliased()) {
262 return VALUE_TEXT_ANTIALIAS_ON;
263 } else {
264 return VALUE_TEXT_ANTIALIAS_OFF;
265 }
266 }
267 return aaHintValue;
268 }
269
270 /**
271 * Return the text fractional metrics rendering mode hint used in this
272 * <code>FontRenderContext</code>.
273 * This will be one of the text fractional metrics rendering hint values
274 * defined in {@link java.awt.RenderingHints java.awt.RenderingHints}.
275 * @return the text fractional metrics rendering mode hint used in this
276 * <code>FontRenderContext</code>.
277 * @since 1.6
278 */
279 public Object getFractionalMetricsHint() {
280 if (defaulting) {
281 if (usesFractionalMetrics()) {
282 return VALUE_FRACTIONALMETRICS_ON;
283 } else {
284 return VALUE_FRACTIONALMETRICS_OFF;
285 }
286 }
287 return fmHintValue;
288 }
289
290 /**
291 * Return true if obj is an instance of FontRenderContext and has the same
292 * transform, antialiasing, and fractional metrics values as this.
293 * @param obj the object to test for equality
294 * @return <code>true</code> if the specified object is equal to
295 * this <code>FontRenderContext</code>; <code>false</code>
296 * otherwise.
297 */
298 public boolean equals(Object obj) {
299 try {
300 return equals((FontRenderContext) obj);
301 } catch (ClassCastException e) {
302 return false;
303 }
304 }
305
306 /**
307 * Return true if rhs has the same transform, antialiasing,
308 * and fractional metrics values as this.
309 * @param rhs the <code>FontRenderContext</code> to test for equality
310 * @return <code>true</code> if <code>rhs</code> is equal to
311 * this <code>FontRenderContext</code>; <code>false</code>
312 * otherwise.
313 * @since 1.4
314 */
315 public boolean equals(FontRenderContext rhs) {
316 if (this == rhs) {
317 return true;
318 }
319 if (rhs == null) {
320 return false;
321 }
322
323 /* if neither instance is a subclass, reference values directly. */
324 if (!rhs.defaulting && !defaulting) {
325 if (rhs.aaHintValue == aaHintValue
326 && rhs.fmHintValue == fmHintValue) {
327
328 return tx == null ? rhs.tx == null : tx.equals(rhs.tx);
329 }
330 return false;
331 } else {
332 return rhs.getAntiAliasingHint() == getAntiAliasingHint()
333 && rhs.getFractionalMetricsHint() == getFractionalMetricsHint()
334 && rhs.getTransform().equals(getTransform());
335 }
336 }
337
338 /**
339 * Return a hashcode for this FontRenderContext.
340 */
341 public int hashCode() {
342 int hash = tx == null ? 0 : tx.hashCode();
343 /* SunHints value objects have identity hashcode, so we can rely on
344 * this to ensure that two equal FRC's have the same hashcode.
345 */
346 if (defaulting) {
347 hash += getAntiAliasingHint().hashCode();
348 hash += getFractionalMetricsHint().hashCode();
349 } else {
350 hash += aaHintValue.hashCode();
351 hash += fmHintValue.hashCode();
352 }
353 return hash;
354 }
355 }
|