1 /*
   2  * Copyright 1997-2006 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package sun.font;
  27 
  28 import java.lang.ref.ReferenceQueue;
  29 import java.lang.ref.SoftReference;
  30 
  31 import java.awt.FontMetrics;
  32 import java.awt.Font;
  33 import java.awt.GraphicsEnvironment;
  34 import java.awt.geom.AffineTransform;
  35 import java.awt.geom.NoninvertibleTransformException;
  36 import java.awt.font.FontRenderContext;
  37 import java.awt.font.TextLayout;
  38 
  39 import java.io.IOException;
  40 import java.io.ObjectInputStream;
  41 import java.io.ObjectOutputStream;
  42 
  43 import java.util.concurrent.ConcurrentHashMap;
  44 
  45 import sun.java2d.Disposer;
  46 import sun.java2d.DisposerRecord;
  47 
  48 /*
  49  * This class provides a summary of the glyph measurements  for a Font
  50  * and a set of hints that guide their display.  It provides more metrics
  51  * information for the Font than the java.awt.FontMetrics class. There
  52  * is also some redundancy with that class.
  53  * <p>
  54  * The design metrics for a Font are obtained from Font.getDesignMetrics().
  55  * The FontDesignMetrics object returned will be independent of the
  56  * point size of the Font.
  57  * Most users are familiar with the idea of using <i>point size</i> to
  58  * specify the size of glyphs in a font. This point size defines a
  59  * measurement between the baseline of one line to the baseline of the
  60  * following line in a single spaced text document. The point size is
  61  * based on <i>typographic points</i>, approximately 1/72 of an inch.
  62  * <p>
  63  * The Java2D API adopts the convention that one point is equivalent
  64  * to one unit in user coordinates.  When using a normalized transform
  65  * for converting user space coordinates to device space coordinates (see
  66  * GraphicsConfiguration.getDefaultTransform() and
  67  * GraphicsConfiguration.getNormalizingTransform()), 72 user space units
  68  * equal 1 inch in device space.  In this case one point is 1/72 of an inch.
  69  * <p>
  70  * The FontDesignMetrics class expresses font metrics in terms of arbitrary
  71  * <i>typographic units</i> (not points) chosen by the font supplier
  72  * and used in the underlying platform font representations.  These units are
  73  * defined by dividing the em-square into a grid.  The em-sqaure is the
  74  * theoretical square whose dimensions are the full body height of the
  75  * font.  A typographic unit is the smallest measurable unit in the
  76  * em-square.  The number of units-per-em is determined by the font
  77  * designer.  The greater the units-per-em, the greater the precision
  78  * in metrics.  For example, Type 1 fonts divide the em-square into a
  79  * 1000 x 1000 grid, while TrueType fonts typically use a 2048 x 2048
  80  * grid.  The scale of these units can be obtained by calling
  81  * getUnitsPerEm().
  82  * <p>
  83  * Typographic units are relative -- their absolute size changes as the
  84  * size of the of the em-square changes.  An em-square is 9 points high
  85  * in a 9-point font.  Because typographic units are relative to the
  86  * em-square, a given location on a glyph will have the same coordinates
  87  * in typographic units regardless of the point size.
  88  * <p>
  89  * Converting typographic units to pixels requires computing pixels-per-em
  90  * (ppem).  This can be computed as:
  91  * <pre>
  92          ppem = device_resolution * (inches-per-point) * pointSize
  93  * </pre>
  94  * where device resolution could be measured in pixels/inch and the point
  95  * size of a font is effectively points/em.  Using a normalized transform
  96  * from user space to device space (see above), results in 1/72 inch/point.
  97  * In this case, ppem is equal to the point size on a 72 dpi monitor, so
  98  * that an N point font displays N pixels high.  In general,
  99  * <pre>
 100         pixel_units = typographic_units * (ppem / units_per_em)
 101  * </pre>
 102  * @see java.awt.Font
 103  * @see java.awt.GraphicsConfiguration#getDefaultTransform
 104  * @see java.awt.GraphicsConfiguration#getNormalizingTransform
 105  */
 106 
 107 public final class FontDesignMetrics extends FontMetrics {
 108 
 109     static final long serialVersionUID = 4480069578560887773L;
 110 
 111     private static final float UNKNOWN_WIDTH = -1;
 112     private static final int CURRENT_VERSION = 1;
 113 
 114     // height, ascent, descent, leading are reported to the client
 115     // as an integer this value is added to the true fp value to
 116     // obtain a value which is usually going to result in a round up
 117     // to the next integer except for very marginal cases.
 118     private static float roundingUpValue = 0.95f;
 119 
 120     // These fields are all part of the old serialization representation
 121     private Font  font;
 122     private float ascent;
 123     private float descent;
 124     private float leading;
 125     private float maxAdvance;
 126     private double[] matrix;
 127     private int[] cache; // now unused, still here only for serialization
 128     // End legacy serialization fields
 129 
 130     private int serVersion = 0;  // If 1 in readObject, these fields are on the input stream:
 131     private boolean isAntiAliased;
 132     private boolean usesFractionalMetrics;
 133     private AffineTransform frcTx;
 134 
 135     private transient float[] advCache; // transient since values could change across runtimes
 136     private transient int height = -1;
 137 
 138     private transient FontRenderContext frc;
 139 
 140     private transient double[] devmatrix = null;
 141 
 142     private transient FontStrike fontStrike;
 143 
 144     private static FontRenderContext DEFAULT_FRC = null;
 145 
 146     private static FontRenderContext getDefaultFrc() {
 147 
 148         if (DEFAULT_FRC == null) {
 149             AffineTransform tx;
 150             if (GraphicsEnvironment.isHeadless()) {
 151                 tx = new AffineTransform();
 152             } else {
 153                 tx =  GraphicsEnvironment
 154                     .getLocalGraphicsEnvironment()
 155                     .getDefaultScreenDevice()
 156                     .getDefaultConfiguration()
 157                     .getDefaultTransform();
 158             }
 159             DEFAULT_FRC = new FontRenderContext(tx, false, false);
 160         }
 161         return DEFAULT_FRC;
 162     }
 163 
 164     /* Strongly cache up to 5 most recently requested FontMetrics objects,
 165      * and softly cache as many as GC allows. In practice this means we
 166      * should keep references around until memory gets low.
 167      * We key the cache either by a Font or a combination of the Font and
 168      * and FRC. A lot of callers use only the font so although there's code
 169      * duplication, we allow just a font to be a key implying a default FRC.
 170      * Also we put the references on a queue so that if they do get nulled
 171      * out we can clear the keys from the table.
 172      */
 173     private static class KeyReference extends SoftReference
 174         implements DisposerRecord {
 175 
 176         static ReferenceQueue queue = Disposer.getQueue();
 177 
 178         Object key;
 179 
 180         KeyReference(Object key, Object value) {
 181             super(value, queue);
 182             this.key = key;
 183             Disposer.addReference(this, this);
 184         }
 185 
 186         /* It is possible that since this reference object has been
 187          * enqueued, that a new metrics has been put into the table
 188          * for the same key value. So we'll test to see if the table maps
 189          * to THIS reference. If its a new one, we'll leave it alone.
 190          * It is possible that a new entry comes in after our test, but
 191          * it is unlikely and if this were a problem we would need to
 192          * synchronize all 'put' and 'remove' accesses to the cache which
 193          * I would prefer not to do.
 194          */
 195         public void dispose() {
 196             if (metricsCache.get(key) == this) {
 197                 metricsCache.remove(key);
 198             }
 199         }
 200     }
 201 
 202     private static class MetricsKey {
 203         Font font;
 204         FontRenderContext frc;
 205         int hash;
 206 
 207         MetricsKey() {
 208         }
 209 
 210         MetricsKey(Font font, FontRenderContext frc) {
 211             init(font, frc);
 212         }
 213 
 214         void init(Font font, FontRenderContext frc) {
 215             this.font = font;
 216             this.frc = frc;
 217             this.hash = font.hashCode() + frc.hashCode();
 218         }
 219 
 220         public boolean equals(Object key) {
 221             if (!(key instanceof MetricsKey)) {
 222                 return false;
 223             }
 224             return
 225                 font.equals(((MetricsKey)key).font) &&
 226                 frc.equals(((MetricsKey)key).frc);
 227         }
 228 
 229         public int hashCode() {
 230             return hash;
 231         }
 232 
 233         /* Synchronize access to this on the class */
 234         static final MetricsKey key = new MetricsKey();
 235     }
 236 
 237     /* All accesses to a CHM do not in general need to be synchronized,
 238      * as incomplete operations on another thread would just lead to
 239      * harmless cache misses.
 240      */
 241     private static final ConcurrentHashMap<Object, KeyReference>
 242         metricsCache = new ConcurrentHashMap<Object, KeyReference>();
 243 
 244     private static final int MAXRECENT = 5;
 245     private static final FontDesignMetrics[]
 246         recentMetrics = new FontDesignMetrics[MAXRECENT];
 247     private static int recentIndex = 0;
 248 
 249     public static FontDesignMetrics getMetrics(Font font) {
 250         return getMetrics(font, getDefaultFrc());
 251      }
 252 
 253     public static FontDesignMetrics getMetrics(Font font,
 254                                                FontRenderContext frc) {
 255 
 256 
 257         /* When using alternate composites, can't cache based just on
 258          * the java.awt.Font. Since this is rarely used and we can still
 259          * cache the physical fonts, its not a problem to just return a
 260          * new instance in this case.
 261          * Note that currently Swing native L&F composites are not handled
 262          * by this code as they use the metrics of the physical anyway.
 263          */
 264         if (FontManager.maybeUsingAlternateCompositeFonts() &&
 265             FontManager.getFont2D(font) instanceof CompositeFont) {
 266             return new FontDesignMetrics(font, frc);
 267         }
 268 
 269         FontDesignMetrics m = null;
 270         KeyReference r;
 271 
 272         /* There are 2 possible keys used to perform lookups in metricsCache.
 273          * If the FRC is set to all defaults, we just use the font as the key.
 274          * If the FRC is non-default in any way, we construct a hybrid key
 275          * that combines the font and FRC.
 276          */
 277         boolean usefontkey = frc.equals(getDefaultFrc());
 278 
 279         if (usefontkey) {
 280             r = metricsCache.get(font);
 281         } else /* use hybrid key */ {
 282             // NB synchronization is not needed here because of updates to
 283             // the metrics cache but is needed for the shared key.
 284             synchronized (MetricsKey.class) {
 285                 MetricsKey.key.init(font, frc);
 286                 r = metricsCache.get(MetricsKey.key);
 287             }
 288         }
 289 
 290         if (r != null) {
 291             m = (FontDesignMetrics)r.get();
 292         }
 293 
 294         if (m == null) {
 295             /* either there was no reference, or it was cleared. Need a new
 296              * metrics instance. The key to use in the map is a new
 297              * MetricsKey instance when we've determined the FRC is
 298              * non-default. Its constructed from local vars so we are
 299              * thread-safe - no need to worry about the shared key changing.
 300              */
 301             m = new FontDesignMetrics(font, frc);
 302             if (usefontkey) {
 303                 metricsCache.put(font, new KeyReference(font, m));
 304             } else /* use hybrid key */ {
 305                 MetricsKey newKey = new MetricsKey(font, frc);
 306                 metricsCache.put(newKey, new KeyReference(newKey, m));
 307             }
 308         }
 309 
 310         /* Here's where we keep the recent metrics */
 311         for (int i=0; i<recentMetrics.length; i++) {
 312             if (recentMetrics[i]==m) {
 313                 return m;
 314             }
 315         }
 316 
 317         synchronized (recentMetrics) {
 318             recentMetrics[recentIndex++] = m;
 319             if (recentIndex == MAXRECENT) {
 320                 recentIndex = 0;
 321             }
 322         }
 323         return m;
 324     }
 325 
 326   /*
 327    * Constructs a new FontDesignMetrics object for the given Font.
 328    * Its private to enable caching - call getMetrics() instead.
 329    * @param font a Font object.
 330    */
 331 
 332     private FontDesignMetrics(Font font) {
 333 
 334         this(font, getDefaultFrc());
 335     }
 336 
 337     /* private to enable caching - call getMetrics() instead. */
 338     private FontDesignMetrics(Font font, FontRenderContext frc) {
 339       super(font);
 340       this.font = font;
 341       this.frc = frc;
 342 
 343       this.isAntiAliased = frc.isAntiAliased();
 344       this.usesFractionalMetrics = frc.usesFractionalMetrics();
 345 
 346       frcTx = frc.getTransform();
 347 
 348       matrix = new double[4];
 349       initMatrixAndMetrics();
 350 
 351       initAdvCache();
 352     }
 353 
 354     private void initMatrixAndMetrics() {
 355 
 356         Font2D font2D = FontManager.getFont2D(font);
 357         fontStrike = font2D.getStrike(font, frc);
 358         StrikeMetrics metrics = fontStrike.getFontMetrics();
 359         this.ascent = metrics.getAscent();
 360         this.descent = metrics.getDescent();
 361         this.leading = metrics.getLeading();
 362         this.maxAdvance = metrics.getMaxAdvance();
 363 
 364         devmatrix = new double[4];
 365         frcTx.getMatrix(devmatrix);
 366     }
 367 
 368     private void initAdvCache() {
 369         advCache = new float[256];
 370         // 0 is a valid metric so force it to -1
 371         for (int i = 0; i < 256; i++) {
 372             advCache[i] = UNKNOWN_WIDTH;
 373         }
 374     }
 375 
 376     private void readObject(ObjectInputStream in) throws IOException,
 377                                                   ClassNotFoundException {
 378 
 379         in.defaultReadObject();
 380         if (serVersion != CURRENT_VERSION) {
 381             frc = getDefaultFrc();
 382             isAntiAliased = frc.isAntiAliased();
 383             usesFractionalMetrics = frc.usesFractionalMetrics();
 384             frcTx = frc.getTransform();
 385         }
 386         else {
 387             frc = new FontRenderContext(frcTx, isAntiAliased, usesFractionalMetrics);
 388         }
 389 
 390         // when deserialized, members are set to their default values for their type--
 391         // not to the values assigned during initialization before the constructor
 392         // body!
 393         height = -1;
 394 
 395         cache = null;
 396 
 397         initMatrixAndMetrics();
 398         initAdvCache();
 399     }
 400 
 401     private void writeObject(ObjectOutputStream out) throws IOException {
 402 
 403         cache = new int[256];
 404         for (int i=0; i < 256; i++) {
 405             cache[i] = -1;
 406         }
 407         serVersion = CURRENT_VERSION;
 408 
 409         out.defaultWriteObject();
 410 
 411         cache = null;
 412     }
 413 
 414     private float handleCharWidth(int ch) {
 415         return fontStrike.getCodePointAdvance(ch); // x-component of result only
 416     }
 417 
 418     // Uses advCache to get character width
 419     // It is incorrect to call this method for ch > 255
 420     private float getLatinCharWidth(char ch) {
 421 
 422         float w = advCache[ch];
 423         if (w == UNKNOWN_WIDTH) {
 424             w = handleCharWidth(ch);
 425             advCache[ch] = w;
 426         }
 427         return w;
 428     }
 429 
 430 
 431     /* Override of FontMetrics.getFontRenderContext() */
 432     public FontRenderContext getFontRenderContext() {
 433         return frc;
 434     }
 435 
 436     public int charWidth(char ch) {
 437         // default metrics for compatibility with legacy code
 438         float w;
 439         if (ch < 0x100) {
 440             w = getLatinCharWidth(ch);
 441         }
 442         else {
 443             w = handleCharWidth(ch);
 444         }
 445         return (int)(0.5 + w);
 446     }
 447 
 448     public int charWidth(int ch) {
 449         if (!Character.isValidCodePoint(ch)) {
 450             ch = 0xffff;
 451         }
 452 
 453         float w = handleCharWidth(ch);
 454 
 455         return (int)(0.5 + w);
 456     }
 457 
 458     public int stringWidth(String str) {
 459 
 460         float width = 0;
 461         if (font.hasLayoutAttributes()) {
 462             /* TextLayout throws IAE for null, so throw NPE explicitly */
 463             if (str == null) {
 464                 throw new NullPointerException("str is null");
 465             }
 466             if (str.length() == 0) {
 467                 return 0;
 468             }
 469             width = new TextLayout(str, font, frc).getAdvance();
 470         } else {
 471             int length = str.length();
 472             for (int i=0; i < length; i++) {
 473                 char ch = str.charAt(i);
 474                 if (ch < 0x100) {
 475                     width += getLatinCharWidth(ch);
 476                 } else if (FontManager.isNonSimpleChar(ch)) {
 477                     width = new TextLayout(str, font, frc).getAdvance();
 478                     break;
 479                 } else {
 480                     width += handleCharWidth(ch);
 481                 }
 482             }
 483         }
 484 
 485         return (int) (0.5 + width);
 486     }
 487 
 488     public int charsWidth(char data[], int off, int len) {
 489 
 490         float width = 0;
 491         if (font.hasLayoutAttributes()) {
 492             if (len == 0) {
 493                 return 0;
 494             }
 495             String str = new String(data, off, len);
 496             width = new TextLayout(str, font, frc).getAdvance();
 497         } else {
 498             /* Explicit test needed to satisfy superclass spec */
 499             if (len < 0) {
 500                 throw new IndexOutOfBoundsException("len="+len);
 501             }
 502             int limit = off + len;
 503             for (int i=off; i < limit; i++) {
 504                 char ch = data[i];
 505                 if (ch < 0x100) {
 506                     width += getLatinCharWidth(ch);
 507                 } else if (FontManager.isNonSimpleChar(ch)) {
 508                     String str = new String(data, off, len);
 509                     width = new TextLayout(str, font, frc).getAdvance();
 510                     break;
 511                 } else {
 512                     width += handleCharWidth(ch);
 513                 }
 514             }
 515         }
 516 
 517         return (int) (0.5 + width);
 518     }
 519 
 520     /**
 521      * Gets the advance widths of the first 256 characters in the
 522      * <code>Font</code>.  The advance is the
 523      * distance from the leftmost point to the rightmost point on the
 524      * character's baseline.  Note that the advance of a
 525      * <code>String</code> is not necessarily the sum of the advances
 526      * of its characters.
 527      * @return    an array storing the advance widths of the
 528      *                 characters in the <code>Font</code>
 529      *                 described by this <code>FontMetrics</code> object.
 530      */
 531     // More efficient than base class implementation - reuses existing cache
 532     public int[] getWidths() {
 533         int[] widths = new int[256];
 534         for (char ch = 0 ; ch < 256 ; ch++) {
 535             float w = advCache[ch];
 536             if (w == UNKNOWN_WIDTH) {
 537                 w = advCache[ch] = handleCharWidth(ch);
 538             }
 539             widths[ch] = (int) (0.5 + w);
 540         }
 541         return widths;
 542     }
 543 
 544     public int getMaxAdvance() {
 545         return (int)(0.99f + this.maxAdvance);
 546     }
 547 
 548   /*
 549    * Returns the typographic ascent of the font. This is the maximum distance
 550    * glyphs in this font extend above the base line (measured in typographic
 551    * units).
 552    */
 553     public int getAscent() {
 554         return (int)(roundingUpValue + this.ascent);
 555     }
 556 
 557   /*
 558    * Returns the typographic descent of the font. This is the maximum distance
 559    * glyphs in this font extend below the base line.
 560    */
 561     public int getDescent() {
 562         return (int)(roundingUpValue + this.descent);
 563     }
 564 
 565     public int getLeading() {
 566         // nb this ensures the sum of the results of the public methods
 567         // for leading, ascent & descent sum to height.
 568         // if the calculations in any other methods change this needs
 569         // to be changed too.
 570         // the 0.95 value used here and in the other methods allows some
 571         // tiny fraction of leeway before rouding up. A higher value (0.99)
 572         // caused some excessive rounding up.
 573         return
 574             (int)(roundingUpValue + descent + leading) -
 575             (int)(roundingUpValue + descent);
 576     }
 577 
 578     // height is calculated as the sum of two separately rounded up values
 579     // because typically clients use ascent to determine the y location to
 580     // pass to drawString etc and we need to ensure that the height has enough
 581     // space below the baseline to fully contain any descender.
 582     public int getHeight() {
 583 
 584         if (height < 0) {
 585             height = getAscent() + (int)(roundingUpValue + descent + leading);
 586         }
 587         return height;
 588     }
 589 }