1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
/* * @(#)FontRenderContext.java 1.36 06/04/07 * * Copyright 2006 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.RenderingHints; import static java.awt.RenderingHints.*; import java.awt.geom.AffineTransform; /** * The <code>FontRenderContext</code> 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. * <p> * 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. * <p> * Anti-aliasing and Fractional-metrics specified by an application can also * affect the size of a character because of rounding to pixel * boundaries. * <p> * Typically, instances of <code>FontRenderContext</code> are * obtained from a {@link java.awt.Graphics2D Graphics2D} object. A * <code>FontRenderContext</code> which is directly constructed will * most likely not represent any actual graphics device, and may lead * to unexpected or incorrect results. * <p> * @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 Object aaHintValue; private transient Object fmHintValue; private transient boolean defaulting; /** * Constructs a new <code>FontRenderContext</code> * object. * */ protected FontRenderContext() { aaHintValue = VALUE_TEXT_ANTIALIAS_DEFAULT; fmHintValue = VALUE_FRACTIONALMETRICS_DEFAULT; defaulting = true; } /** * Constructs a <code>FontRenderContext</code> object from an * optional {@link AffineTransform} and two <code>boolean</code> * values that determine if the newly constructed object has * anti-aliasing or fractional metrics. * In each case the boolean values <CODE>true</CODE> and <CODE>false</CODE> * correspond to the rendering hint values <CODE>ON</CODE> and * <CODE>OFF</CODE> respectively. * <p> * 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 <code>FontRenderContext</code>. 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. */ public FontRenderContext(AffineTransform tx, boolean isAntiAliased, boolean usesFractionalMetrics) { 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; } } /** * Constructs a <code>FontRenderContext</code> object from an * optional {@link AffineTransform} and two <code>Object</code> * 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 <code>FontRenderContext</code>. 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 <code>IllegalArgumentException</code>. * {@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 <code>IllegalArgumentException</code> * @throws IllegalArgumentException if the hints are not one of the * legal values. * @since 1.6 */ public FontRenderContext(AffineTransform tx, Object aaHint, Object fmHint){ 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); } } /** * Indicates whether or not this <code>FontRenderContext</code> object * measures text in a transformed render context. * @return <code>true</code> if this <code>FontRenderContext</code> * object has a non-identity AffineTransform attribute. * <code>false</code> otherwise. * @see java.awt.font.FontRenderContext#getTransform * @since 1.6 */ public boolean isTransformed() { if (!defaulting) { return tx != null; } else { return !getTransform().isIdentity(); } } /** * Returns the integer type of the affine transform for this * <code>FontRenderContext</code> as specified by * {@link java.awt.geom.AffineTransform#getType()} * @return the type of the transform. * @see AffineTransform * @since 1.6 */ public int getTransformType() { if (!defaulting) { if (tx == null) { return AffineTransform.TYPE_IDENTITY; } else { return tx.getType(); } } else { return getTransform().getType(); } } /** * Gets the transform that is used to scale typographical points * to pixels in this <code>FontRenderContext</code>. * @return the <code>AffineTransform</code> of this * <code>FontRenderContext</code>. * @see AffineTransform */ public AffineTransform getTransform() { return (tx == null) ? new AffineTransform() : new AffineTransform(tx); } /** * Returns a boolean which indicates whether or not some form of * antialiasing is specified by this <code>FontRenderContext</code>. * Call {@link #getAntiAliasingHint() getAntiAliasingHint()} * for the specific rendering hint value. * @return <code>true</code>, if text is anti-aliased in this * <code>FontRenderContext</code>; <code>false</code> otherwise. * @see java.awt.RenderingHints#KEY_TEXT_ANTIALIASING * @see #FontRenderContext(AffineTransform,boolean,boolean) * @see #FontRenderContext(AffineTransform,Object,Object) */ public boolean isAntiAliased() { return !(aaHintValue == VALUE_TEXT_ANTIALIAS_OFF || aaHintValue == VALUE_TEXT_ANTIALIAS_DEFAULT); } /** * Returns a boolean which whether text fractional metrics mode * is used in this <code>FontRenderContext</code>. * Call {@link #getFractionalMetricsHint() getFractionalMetricsHint()} * to obtain the corresponding rendering hint value. * @return <code>true</code>, if layout should be performed with * fractional metrics; <code>false</code> otherwise. * in this <code>FontRenderContext</code>. * @see java.awt.RenderingHints#KEY_FRACTIONALMETRICS * @see #FontRenderContext(AffineTransform,boolean,boolean) * @see #FontRenderContext(AffineTransform,Object,Object) */ public boolean usesFractionalMetrics() { return !(fmHintValue == VALUE_FRACTIONALMETRICS_OFF || fmHintValue == VALUE_FRACTIONALMETRICS_DEFAULT); } /** * Return the text anti-aliasing rendering mode hint used in this * <code>FontRenderContext</code>. * 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 * <code>FontRenderContext</code>. * @since 1.6 */ public Object getAntiAliasingHint() { if (defaulting) { if (isAntiAliased()) { return VALUE_TEXT_ANTIALIAS_ON; } else { return VALUE_TEXT_ANTIALIAS_OFF; } } return aaHintValue; } /** * Return the text fractional metrics rendering mode hint used in this * <code>FontRenderContext</code>. * 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 * <code>FontRenderContext</code>. * @since 1.6 */ public Object getFractionalMetricsHint() { if (defaulting) { if (usesFractionalMetrics()) { return VALUE_FRACTIONALMETRICS_ON; } else { return VALUE_FRACTIONALMETRICS_OFF; } } return fmHintValue; } /** * 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 <code>true</code> if the specified object is equal to * this <code>FontRenderContext</code>; <code>false</code> * 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 <code>FontRenderContext</code> to test for equality * @return <code>true</code> if <code>rhs</code> is equal to * this <code>FontRenderContext</code>; <code>false</code> * otherwise. * @since 1.4 */ public boolean equals(FontRenderContext rhs) { 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()); } } /** * Return a hashcode for this FontRenderContext. */ public int hashCode() { 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; } }