/* * @(#)FontRenderContext.java 1.30 03/12/19 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ /* * @author Charlton Innovations, Inc. */ package java.awt.font; import java.awt.geom.AffineTransform; /** * The FontRenderContext class is a container for the * information needed to correctly measure text. The measurement of text * can vary because of rules that map outlines to pixels, and rendering * hints provided by an application. *

* One such piece of information is a transform that scales * typographical points to pixels. (A point is defined to be exactly 1/72 * of an inch, which is slightly different than * the traditional mechanical measurement of a point.) A character that * is rendered at 12pt on a 600dpi device might have a different size * than the same character rendered at 12pt on a 72dpi device because of * such factors as rounding to pixel boundaries and hints that the font * designer may have specified. *

* Anti-aliasing and Fractional-metrics specified by an application can also * affect the size of a character because of rounding to pixel * boundaries. *

* Typically, instances of FontRenderContext are * obtained from a {@link java.awt.Graphics2D Graphics2D} object. A * FontRenderContext which is directly constructed will * most likely not represent any actual graphics device, and may lead * to unexpected or incorrect results. *

* @see java.awt.RenderingHints#KEY_TEXT_ANTIALIASING * @see java.awt.RenderingHints#KEY_FRACTIONALMETRICS * @see java.awt.Graphics2D#getFontRenderContext() * @see java.awt.font.LineMetrics */ public class FontRenderContext { private transient AffineTransform tx; private transient boolean bIsAntiAliased; private transient boolean bUsesFractionalMetrics; /** * Constructs a new FontRenderContext * object. * */ protected FontRenderContext() { } /** * Constructs a FontRenderContext object from an * optional {@link AffineTransform} and two boolean * values that determine if the newly constructed object has * anti-aliasing or fractional metrics. * @param tx the transform which is used to scale typographical points * to pixels in this FontRenderContext. If null, an * identity tranform is used. * @param isAntiAliased determines if the newly contructed object has * anti-aliasing * @param usesFractionalMetrics determines if the newly constructed * object uses fractional metrics */ public FontRenderContext(AffineTransform tx, boolean isAntiAliased, boolean usesFractionalMetrics) { if (tx != null && !tx.isIdentity()) { this.tx = new AffineTransform(tx); } this.bIsAntiAliased = isAntiAliased; this.bUsesFractionalMetrics = usesFractionalMetrics; } /** * Gets the transform that is used to scale typographical points * to pixels in this FontRenderContext. * @return the AffineTransform of this * FontRenderContext. * @see AffineTransform */ public AffineTransform getTransform() { return (tx == null) ? new AffineTransform() : new AffineTransform(tx); } /** * Gets the text anti-aliasing mode used in this * FontRenderContext. * @return true, if text is anti-aliased in this * FontRenderContext; false otherwise. * @see java.awt.RenderingHints#KEY_TEXT_ANTIALIASING */ public boolean isAntiAliased() { return this.bIsAntiAliased; } /** * Gets the text fractional metrics mode requested by the application * for use in this FontRenderContext. * @return true, if layout should be performed with * fractional metrics; false otherwise. * in this FontRenderContext. * @see java.awt.RenderingHints#KEY_FRACTIONALMETRICS */ public boolean usesFractionalMetrics() { return this.bUsesFractionalMetrics; } /** * Return true if obj is an instance of FontRenderContext and has the same * transform, antialiasing, and fractional metrics values as this. * @param obj the object to test for equality * @return true if the specified object is equal to * this FontRenderContext; false * otherwise. */ public boolean equals(Object obj) { try { return equals((FontRenderContext)obj); } catch (ClassCastException e) { return false; } } /** * Return true if rhs has the same transform, antialiasing, * and fractional metrics values as this. * @param rhs the FontRenderContext to test for equality * @return true if rhs is equal to * this FontRenderContext; false * otherwise. */ public boolean equals(FontRenderContext rhs) { if (this == rhs) { return true; } if (rhs != null && rhs.bIsAntiAliased == bIsAntiAliased && rhs.bUsesFractionalMetrics == bUsesFractionalMetrics) { return tx == null ? rhs.tx == null : tx.equals(rhs.tx); } return false; } /** * Return a hashcode for this FontRenderContext. */ public int hashCode() { int hash = tx == null ? 0 : tx.hashCode(); if (bIsAntiAliased) { hash ^= 0x1; } if (bUsesFractionalMetrics) { hash ^= 0x2; } return hash; } }