FileDocCategorySizeDatePackage
FontRenderContext.javaAPI DocJava SE 6 API12429Tue Jun 10 00:25:24 BST 2008java.awt.font

FontRenderContext

public class FontRenderContext extends Object
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

Fields Summary
private transient AffineTransform
tx
private transient Object
aaHintValue
private transient Object
fmHintValue
private transient boolean
defaulting
Constructors Summary
protected FontRenderContext()
Constructs a new FontRenderContext object.

	aaHintValue = VALUE_TEXT_ANTIALIAS_DEFAULT;
	fmHintValue = VALUE_FRACTIONALMETRICS_DEFAULT;
        defaulting = true;
    
public FontRenderContext(AffineTransform tx, boolean isAntiAliased, boolean usesFractionalMetrics)
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. In each case the boolean values true and false correspond to the rendering hint values ON and OFF respectively.

To specify other hint values, use the constructor which specifies the rendering hint values as parameters : {@link #FontRenderContext(AffineTransform, Object, Object)}.

param
tx the transform which is used to scale typographical points to pixels in this FontRenderContext. If null, an identity transform is used.
param
isAntiAliased determines if the newly constructed object has anti-aliasing.
param
usesFractionalMetrics determines if the newly constructed object has fractional metrics.

        if (tx != null && !tx.isIdentity()) {
            this.tx = new AffineTransform(tx);
        }
	if (isAntiAliased) {
	    aaHintValue = VALUE_TEXT_ANTIALIAS_ON;
	} else {
	    aaHintValue = VALUE_TEXT_ANTIALIAS_OFF;
	}
	if (usesFractionalMetrics) {
	    fmHintValue = VALUE_FRACTIONALMETRICS_ON;
	} else {
	    fmHintValue = VALUE_FRACTIONALMETRICS_OFF;
	}
    
public FontRenderContext(AffineTransform tx, Object aaHint, Object fmHint)
Constructs a FontRenderContext object from an optional {@link AffineTransform} and two Object 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
aaHint - one of the text antialiasing rendering hint values defined in {@link java.awt.RenderingHints java.awt.RenderingHints}. Any other value will throw IllegalArgumentException. {@link java.awt.RenderingHints#VALUE_TEXT_ANTIALIAS_DEFAULT VALUE_TEXT_ANTIALIAS_DEFAULT} may be specified, in which case the mode used is implementation dependent.
param
fmHint - one of the text fractional rendering hint values defined in {@link java.awt.RenderingHints java.awt.RenderingHints}. {@link java.awt.RenderingHints#VALUE_FRACTIONALMETRICS_DEFAULT VALUE_FRACTIONALMETRICS_DEFAULT} may be specified, in which case the mode used is implementation dependent. Any other value will throw IllegalArgumentException
throws
IllegalArgumentException if the hints are not one of the legal values.
since
1.6

        if (tx != null && !tx.isIdentity()) {
            this.tx = new AffineTransform(tx);
        }
	try {
	    if (KEY_TEXT_ANTIALIASING.isCompatibleValue(aaHint)) {
		aaHintValue = aaHint;
	    } else {
		throw new IllegalArgumentException("AA hint:" + aaHint);
	    }
	} catch (Exception e) {
	    throw new IllegalArgumentException("AA hint:" +aaHint);
	}	
	try {
	    if (KEY_FRACTIONALMETRICS.isCompatibleValue(fmHint)) {
		fmHintValue = fmHint;
	    } else {
		throw new IllegalArgumentException("FM hint:" + fmHint);
	    }
	} catch (Exception e) {
	    throw new IllegalArgumentException("FM hint:" +fmHint);
	}
    
Methods Summary
public booleanequals(java.lang.Object obj)
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.

	try {
	    return equals((FontRenderContext)obj);
	}
	catch (ClassCastException e) {
	    return false;
	}
    
public booleanequals(java.awt.font.FontRenderContext rhs)
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.
since
1.4

	if (this == rhs) {
	    return true;
	}
	if (rhs == null) {
	    return false;
	}

	/* if neither instance is a subclass, reference values directly. */
	if (!rhs.defaulting && !defaulting) {
	    if (rhs.aaHintValue == aaHintValue &&
		rhs.fmHintValue == fmHintValue) {

		return tx == null ? rhs.tx == null : tx.equals(rhs.tx);
	    }
	    return false;
	} else {
	    return
		rhs.getAntiAliasingHint() == getAntiAliasingHint() &&
		rhs.getFractionalMetricsHint() == getFractionalMetricsHint() &&
		rhs.getTransform().equals(getTransform());
	}
    
public java.lang.ObjectgetAntiAliasingHint()
Return the text anti-aliasing rendering mode hint used in this FontRenderContext. This will be one of the text antialiasing rendering hint values defined in {@link java.awt.RenderingHints java.awt.RenderingHints}.

return
text anti-aliasing rendering mode hint used in this FontRenderContext.
since
1.6

        if (defaulting) {
            if (isAntiAliased()) {
                 return VALUE_TEXT_ANTIALIAS_ON;
	    } else {
		return VALUE_TEXT_ANTIALIAS_OFF;
	    }
        }
	return aaHintValue;
    
public java.lang.ObjectgetFractionalMetricsHint()
Return the text fractional metrics rendering mode hint used in this FontRenderContext. This will be one of the text fractional metrics rendering hint values defined in {@link java.awt.RenderingHints java.awt.RenderingHints}.

return
the text fractional metrics rendering mode hint used in this FontRenderContext.
since
1.6

        if (defaulting) {
            if (usesFractionalMetrics()) {
                 return VALUE_FRACTIONALMETRICS_ON;
	    } else {
		return VALUE_FRACTIONALMETRICS_OFF;
	    }
        }
	return fmHintValue;
    
public java.awt.geom.AffineTransformgetTransform()
Gets the transform that is used to scale typographical points to pixels in this FontRenderContext.

return
the AffineTransform of this FontRenderContext.
see
AffineTransform

        return (tx == null) ? new AffineTransform() : new AffineTransform(tx);
    
public intgetTransformType()
Returns the integer type of the affine transform for this FontRenderContext as specified by {@link java.awt.geom.AffineTransform#getType()}

return
the type of the transform.
see
AffineTransform
since
1.6

        if (!defaulting) {
            if (tx == null) {
                return AffineTransform.TYPE_IDENTITY;
            } else {
                return tx.getType(); 
            }
        } else {
            return getTransform().getType();
        }
    
public inthashCode()
Return a hashcode for this FontRenderContext.

	int hash = tx == null ? 0 : tx.hashCode();
	/* SunHints value objects have identity hashcode, so we can rely on
	 * this to ensure that two equal FRC's have the same hashcode.
	 */
	if (defaulting) {
	    hash += getAntiAliasingHint().hashCode();
	    hash += getFractionalMetricsHint().hashCode();
	} else {
	    hash += aaHintValue.hashCode();
	    hash += fmHintValue.hashCode();
	}
	return hash;
    
public booleanisAntiAliased()
Returns a boolean which indicates whether or not some form of antialiasing is specified by this FontRenderContext. Call {@link #getAntiAliasingHint() getAntiAliasingHint()} for the specific rendering hint value.

return
true, if text is anti-aliased in this FontRenderContext; false otherwise.
see
java.awt.RenderingHints#KEY_TEXT_ANTIALIASING
see
#FontRenderContext(AffineTransform,boolean,boolean)
see
#FontRenderContext(AffineTransform,Object,Object)

        return !(aaHintValue == VALUE_TEXT_ANTIALIAS_OFF ||
		 aaHintValue == VALUE_TEXT_ANTIALIAS_DEFAULT);
    
public booleanisTransformed()
Indicates whether or not this FontRenderContext object measures text in a transformed render context.

return
true if this FontRenderContext object has a non-identity AffineTransform attribute. false otherwise.
see
java.awt.font.FontRenderContext#getTransform
since
1.6

        if (!defaulting) {
            return tx != null;
        } else {
            return !getTransform().isIdentity();
        }
    
public booleanusesFractionalMetrics()
Returns a boolean which whether text fractional metrics mode is used in this FontRenderContext. Call {@link #getFractionalMetricsHint() getFractionalMetricsHint()} to obtain the corresponding rendering hint value.

return
true, if layout should be performed with fractional metrics; false otherwise. in this FontRenderContext.
see
java.awt.RenderingHints#KEY_FRACTIONALMETRICS
see
#FontRenderContext(AffineTransform,boolean,boolean)
see
#FontRenderContext(AffineTransform,Object,Object)

        return !(fmHintValue == VALUE_FRACTIONALMETRICS_OFF ||
		 fmHintValue == VALUE_FRACTIONALMETRICS_DEFAULT);