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. * @author Charlton Innovations, Inc.
  9. */
  10. package java.awt.font;
  11. import java.awt.geom.AffineTransform;
  12. /**
  13. * The <code>FontRenderContext</code> class is a container for the
  14. * information needed to correctly measure text. The measurement of text
  15. * can vary because of rules that map outlines to pixels, and rendering
  16. * hints provided by an application.
  17. * <p>
  18. * One such piece of information is a transform that scales
  19. * typographical points to pixels. (A point is defined to be exactly 1/72
  20. * of an inch, which is slightly different than
  21. * the traditional mechanical measurement of a point.) A character that
  22. * is rendered at 12pt on a 600dpi device might have a different size
  23. * than the same character rendered at 12pt on a 72dpi device because of
  24. * such factors as rounding to pixel boundaries and hints that the font
  25. * designer may have specified.
  26. * <p>
  27. * Anti-aliasing and Fractional-metrics specified by an application can also
  28. * affect the size of a character because of rounding to pixel
  29. * boundaries.
  30. * <p>
  31. * Typically, instances of <code>FontRenderContext</code> are
  32. * obtained from a {@link java.awt.Graphics2D Graphics2D} object. A
  33. * <code>FontRenderContext</code> which is directly constructed will
  34. * most likely not represent any actual graphics device, and may lead
  35. * to unexpected or incorrect results.
  36. * <p>
  37. * @see java.awt.RenderingHints#KEY_TEXT_ANTIALIASING
  38. * @see java.awt.RenderingHints#KEY_FRACTIONALMETRICS
  39. * @see java.awt.Graphics2D#getFontRenderContext()
  40. * @see java.awt.font.LineMetrics
  41. */
  42. public class FontRenderContext {
  43. private transient AffineTransform tx;
  44. private transient boolean bIsAntiAliased;
  45. private transient boolean bUsesFractionalMetrics;
  46. /**
  47. * Constructs a new <code>FontRenderContext</code>
  48. * object.
  49. *
  50. */
  51. protected FontRenderContext() {
  52. }
  53. /**
  54. * Constructs a <code>FontRenderContext</code> object from an
  55. * optional {@link AffineTransform} and two <code>boolean</code>
  56. * values that determine if the newly constructed object has
  57. * anti-aliasing or fractional metrics.
  58. * @param tx the transform which is used to scale typographical points
  59. * to pixels in this <code>FontRenderContext</code>. If null, an
  60. * identity tranform is used.
  61. * @param isAntiAliased determines if the newly contructed object has
  62. * anti-aliasing
  63. * @param usesFractionalMetrics determines if the newly constructed
  64. * object uses fractional metrics
  65. */
  66. public FontRenderContext(AffineTransform tx,
  67. boolean isAntiAliased,
  68. boolean usesFractionalMetrics) {
  69. if (tx != null && !tx.isIdentity()) {
  70. this.tx = new AffineTransform(tx);
  71. }
  72. this.bIsAntiAliased = isAntiAliased;
  73. this.bUsesFractionalMetrics = usesFractionalMetrics;
  74. }
  75. /**
  76. * Gets the transform that is used to scale typographical points
  77. * to pixels in this <code>FontRenderContext</code>.
  78. * @return the <code>AffineTransform</code> of this
  79. * <code>FontRenderContext</code>.
  80. * @see AffineTransform
  81. */
  82. public AffineTransform getTransform() {
  83. return (tx == null) ? new AffineTransform() : new AffineTransform(tx);
  84. }
  85. /**
  86. * Gets the text anti-aliasing mode used in this
  87. * <code>FontRenderContext</code>.
  88. * @return <code>true</code>, if text is anti-aliased in this
  89. * <code>FontRenderContext</code> <code>false</code> otherwise.
  90. * @see java.awt.RenderingHints#KEY_TEXT_ANTIALIASING
  91. */
  92. public boolean isAntiAliased() {
  93. return this.bIsAntiAliased;
  94. }
  95. /**
  96. * Gets the text fractional metrics mode requested by the application
  97. * for use in this <code>FontRenderContext</code>.
  98. * @return <code>true</code>, if layout should be performed with
  99. * fractional metrics; <code>false</code> otherwise.
  100. * in this <code>FontRenderContext</code>.
  101. * @see java.awt.RenderingHints#KEY_FRACTIONALMETRICS
  102. */
  103. public boolean usesFractionalMetrics() {
  104. return this.bUsesFractionalMetrics;
  105. }
  106. /**
  107. * Return true if obj is an instance of FontRenderContext and has the same
  108. * transform, antialiasing, and fractional metrics values as this.
  109. * @param obj the object to test for equality
  110. * @return <code>true</code> if the specified object is equal to
  111. * this <code>FontRenderContext</code> <code>false</code>
  112. * otherwise.
  113. */
  114. public boolean equals(Object obj) {
  115. try {
  116. return equals((FontRenderContext)obj);
  117. }
  118. catch (ClassCastException e) {
  119. return false;
  120. }
  121. }
  122. /**
  123. * Return true if rhs has the same transform, antialiasing,
  124. * and fractional metrics values as this.
  125. * @param rhs the <code>FontRenderContext</code> to test for equality
  126. * @return <code>true</code> if <code>rhs</code> is equal to
  127. * this <code>FontRenderContext</code> <code>false</code>
  128. * otherwise.
  129. */
  130. public boolean equals(FontRenderContext rhs) {
  131. if (this == rhs) {
  132. return true;
  133. }
  134. if (rhs != null &&
  135. rhs.bIsAntiAliased == bIsAntiAliased &&
  136. rhs.bUsesFractionalMetrics == bUsesFractionalMetrics) {
  137. return tx == null ? rhs.tx == null : tx.equals(rhs.tx);
  138. }
  139. return false;
  140. }
  141. /**
  142. * Return a hashcode for this FontRenderContext.
  143. */
  144. public int hashCode() {
  145. int hash = tx == null ? 0 : tx.hashCode();
  146. if (bIsAntiAliased) {
  147. hash ^= 0x1;
  148. }
  149. if (bUsesFractionalMetrics) {
  150. hash ^= 0x2;
  151. }
  152. return hash;
  153. }
  154. }