home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / awt / font / GlyphMetrics.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  9.0 KB  |  279 lines

  1. /*
  2.  * @(#)GlyphMetrics.java    1.23 98/03/18
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. /*
  16.  * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
  17.  * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
  18.  *
  19.  * The original version of this source code and documentation is
  20.  * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
  21.  * of IBM. These materials are provided under terms of a License
  22.  * Agreement between Taligent and Sun. This technology is protected
  23.  * by multiple US and International patents.
  24.  *
  25.  * This notice and attribution to Taligent may not be removed.
  26.  * Taligent is a registered trademark of Taligent, Inc.
  27.  *
  28.  */
  29.  
  30. package java.awt.font;
  31.  
  32. import java.awt.geom.Rectangle2D;
  33.  
  34. /**
  35.  * GlyphMetrics represents infomation for a single glyph.  GlyphMetrics
  36.  * instances are produced by Font and are applicable to a specific glyph in a
  37.  * particular Font.
  38.  * <p>
  39.  * Glyphs are either STANDARD, LIGATURE, COMBINING, COMPONENT, or FILLER.
  40.  * They also may be marked as being either WHITESPACE or HANGING_PUNCTUATION.
  41.  * STANDARD glyphs are commonly used to represent single characters.
  42.  * LIGATURE glyphs are always the first glyph in a sequence of glyphs that
  43.  * combine to represent a set of "combining characters," such as the 'fi' ligature
  44.  * found in some fonts.  A LIGATURE glyph in a GlyphSet is always followed by one
  45.  * or more FILLER glyphs.  The number of LIGATURE and FILLER glyphs is always
  46.  * the same as the number of characters represented by the ligature.  COMPONENT
  47.  * glyphs in a GlyphSet do not correspond to a particular character in a text model.
  48.  * Instead, COMPONENT glyphs are added for typographical reasons (such as Arabic
  49.  * justification).  COMBINING glyphs embellish STANDARD or LIGATURE glyphs, such
  50.  * as accent marks.  Carets do not appear before COMBINING glyphs.
  51.  * <p>
  52.  * Other metrics available through GlyphMetrics are the advance, bounds, and left
  53.  * and right side bearings.  The advance of a glyph is the distance from the glyph's
  54.  * origin to the origin of the next glyph.  Note that, in a glyphset, the distance
  55.  * from a glyph to its following glyph may not be the glyph's advance.  The bounds
  56.  * is the smallest rectangle which completely contains the visible portion of the
  57.  * glyph.  The bounds rectangle is relative to the glyph's origin.  The left-side
  58.  * bearing is the distance from the glyph origin to the left of its bounds rectangle.
  59.  * If the left-side bearing is negative, part of the glyph is drawn to the left of its
  60.  * origin.  The right-side bearing is the distance from the right side of the bounds
  61.  * rectangle to the next glyph origin (the origin plus the advance).  If negative, part
  62.  * of the glyph is drawn to the right of the next glyph's origin.
  63.  * <p>
  64.  * Although instances of GlyphMetrics may be directly constructed, they are almost
  65.  * always obtained from a Font.  Once constructed, GlyphMetrics are immutable.
  66.  * <p>
  67.  * <strong>Example</strong>:<p>
  68.  * Querying a Font for glyph information
  69.  * <blockquote><pre>
  70.  * Font font = ...;
  71.  * int glyphCode = ...;
  72.  * GlyphMetrics metrics = font.getGlyphMetrics(glyphCode);
  73.  * int isStandard = metrics.isStandard();
  74.  * float glyphAdvance = metrics.getAdvance();
  75.  * </blockquote></pre>
  76.  * @see Font
  77.  * @see GlyphSet
  78.  */
  79.  
  80. public final class GlyphMetrics {
  81.     /**
  82.      * The advance (horizontal or vertical) of the associated glyph.
  83.      */
  84.     private float advance;
  85.  
  86.     /**
  87.      * The bounds of the associated glyph.
  88.      */
  89.     private Rectangle2D bounds;
  90.  
  91.     /**
  92.      * The left-side-bearing of the associated glyph.
  93.      */
  94.     private float lsb;
  95.  
  96.     /**
  97.      * The right-side-bearing of the associated glyph.
  98.      */
  99.     private float rsb;
  100.  
  101.     /**
  102.      * Additional information about the glyph encoded as a byte.
  103.      */
  104.     private byte glyphType;
  105.  
  106.     /**
  107.      * This code indicates a glyph that represents a single standard
  108.      * character.
  109.      */
  110.     public static final byte STANDARD = 0;
  111.  
  112.     /**
  113.      * This code indicates a glyph that represents multiple characters
  114.      * as a ligature, for example 'fi' or 'ffi'.  It is followed by
  115.      * filler glyphs for the remaining characters. Filler and combining
  116.      * glyphs can be intermixed to control positioning of accent marks
  117.      * on the logically preceeding ligature.
  118.      */
  119.     public static final byte LIGATURE = 1;
  120.  
  121.     /**
  122.      * This code indicates a glyph that represents a combining character,
  123.      * such as an umlaut.  There is no caret position between this glyph
  124.      * and the preceeding glyph.
  125.      */
  126.     public static final byte COMBINING = 2;
  127.  
  128.     /**
  129.      * This code indicates a glyph with no corresponding character in the
  130.      * backing store.  The glyph is associated with the character
  131.      * represented by the logicaly preceeding non-component glyph.  This
  132.      * is used for kashida justification or other visual modifications to
  133.      * existing glyphs.  There is no caret position between this glyph
  134.      * and the preceeding glyph.
  135.      */
  136.     public static final byte COMPONENT = 3;
  137.  
  138.     /**
  139.      * This code indicates a glyph used as a filler for a ligature.  It
  140.      * has a width that is used to control caret positions within the
  141.      * ligature when carets are allowed in ligatures.  It replaces the
  142.      * succeeding logical characters that were used to create the
  143.      * ligature.
  144.      */
  145.     public static final byte FILLER = 4;
  146.  
  147.     /**
  148.      * This value is added to the base code to indicate hanging
  149.      * punctuation.  This information is used in justification.
  150.      */
  151.     public static final byte HANGING_PUNCTUATION = 8;
  152.  
  153.     /**
  154.      * This value is added to the base code to indicate whitespace.
  155.      * This information is used in justification.
  156.      */
  157.     public static final byte WHITESPACE = 16;
  158.  
  159.     /**
  160.      * Construct a GlyphMetrics.
  161.      *
  162.      * @param advance the advance width (height) of the glyph
  163.      * @param bounds the black box bounds of the glyph.
  164.      * @param glyphType the type of the glyph
  165.      */
  166.     public GlyphMetrics(float advance, Rectangle2D bounds, byte glyphType) {
  167.         this.advance = advance;
  168.         this.bounds = new Rectangle2D.Float();
  169.         this.bounds.setRect(bounds);
  170.         this.glyphType = glyphType;
  171.  
  172.         boolean isVertical = false;
  173.         if (isVertical) {
  174.         this.lsb = (float)bounds.getY();
  175.         this.rsb = advance - (this.lsb + (float)bounds.getHeight());
  176.     } else {
  177.         this.lsb = (float)bounds.getX();
  178.         this.rsb = advance - (this.lsb + (float)bounds.getWidth());
  179.         }
  180.     }
  181.     
  182.     /**
  183.      * Return the advance width (height) of the glyph.
  184.      */
  185.     public float getAdvance() {
  186.         return advance;
  187.     }
  188.     
  189.     /**
  190.      * Return the black box bounds of the glyph.
  191.      *
  192.      * The returned bounds must not be modified by the caller.
  193.      */
  194.     public Rectangle2D getBounds2D() {
  195.         Rectangle2D b = new Rectangle2D.Float();
  196.         b.setRect(bounds);
  197.         return b;
  198.     }
  199.  
  200.     /**
  201.      * Return the left (top) side bearing of the glyph.
  202.      *
  203.      * This is the distance from 0, 0 to the left (top) of the glyph bounds.
  204.      * If the bounds of the glyph is to the left of (above) the origin, the
  205.      * LSB is negative.
  206.      */
  207.     public float getLSB() {
  208.         return lsb;
  209.     }
  210.     
  211.     /**
  212.      * Return the right (bottom) side bearing of the glyph.
  213.      *
  214.      * This is the distance from the right (bottom) of the glyph bounds to
  215.      * the advance. If the bounds of the glyph is to the right of (below)
  216.      * the advance, the RSB is negative.
  217.      */
  218.     public float getRSB() {
  219.         return rsb;
  220.     }
  221.  
  222.     /**
  223.      * Return the raw glyph type code.
  224.      */
  225.     public byte getType() {
  226.         return glyphType;
  227.     }
  228.  
  229.     /**
  230.      * Return true if this is a standard glyph.
  231.      */
  232.     public boolean isStandard() {
  233.         return (glyphType & 0x7) == STANDARD;
  234.     }
  235.  
  236.     /**
  237.      * Return true if this is a ligature glyph.
  238.      */
  239.     public boolean isLigature() {
  240.         return (glyphType & 0x7) == LIGATURE;
  241.     }
  242.  
  243.     /**
  244.      * Return true if this is a combining glyph.
  245.      */
  246.     public boolean isCombining() {
  247.         return (glyphType & 0x7) == COMBINING;
  248.     }
  249.  
  250.     /**
  251.      * Return true if this is a component glyph.
  252.      */
  253.     public boolean isComponent() {
  254.         return (glyphType & 0x7) == COMPONENT;
  255.     }
  256.  
  257.     /**
  258.      * Return true if this is a component glyph.
  259.      */
  260.     public boolean isFiller() {
  261.         return (glyphType & 0x7) == FILLER;
  262.     }
  263.     
  264.     /**
  265.      * Return true if this is a whitespace glyph.
  266.      */
  267.     public boolean isWhitespace() {
  268.         return (glyphType & WHITESPACE) != 0;
  269.     }
  270.     
  271.     /**
  272.      * Return true if this is a hanging punctuation glyph.
  273.      */
  274.     public boolean isHangingPunctuation() {
  275.         return (glyphType & HANGING_PUNCTUATION) != 0;
  276.     }
  277. }
  278.  
  279.