< prev index next >

src/java.desktop/share/classes/java/awt/font/GlyphMetrics.java

Print this page




  26 /*
  27  * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
  28  * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
  29  *
  30  * The original version of this source code and documentation is
  31  * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
  32  * of IBM. These materials are provided under terms of a License
  33  * Agreement between Taligent and Sun. This technology is protected
  34  * by multiple US and International patents.
  35  *
  36  * This notice and attribution to Taligent may not be removed.
  37  * Taligent is a registered trademark of Taligent, Inc.
  38  *
  39  */
  40 
  41 package java.awt.font;
  42 
  43 import java.awt.geom.Rectangle2D;
  44 
  45 /**
  46  * The <code>GlyphMetrics</code> class represents information for a
  47  * single glyph.   A glyph is the visual representation of one or more
  48  * characters.  Many different glyphs can be used to represent a single
  49  * character or combination of characters.  <code>GlyphMetrics</code>
  50  * instances are produced by {@link java.awt.Font Font} and are applicable
  51  * to a specific glyph in a particular <code>Font</code>.
  52  * <p>
  53  * Glyphs are either STANDARD, LIGATURE, COMBINING, or COMPONENT.
  54  * <ul>
  55  * <li>STANDARD glyphs are commonly used to represent single characters.
  56  * <li>LIGATURE glyphs are used to represent sequences of characters.
  57  * <li>COMPONENT glyphs in a {@link GlyphVector} do not correspond to a
  58  * particular character in a text model. Instead, COMPONENT glyphs are
  59  * added for typographical reasons, such as Arabic justification.
  60  * <li>COMBINING glyphs embellish STANDARD or LIGATURE glyphs, such
  61  * as accent marks.  Carets do not appear before COMBINING glyphs.
  62  * </ul>
  63  * <p>
  64  * Other metrics available through <code>GlyphMetrics</code> are the
  65  * components of the advance, the visual bounds, and the left and right
  66  * side bearings.
  67  * <p>
  68  * Glyphs for a rotated font, or obtained from a <code>GlyphVector</code>
  69  * which has applied a rotation to the glyph, can have advances that
  70  * contain both X and Y components.  Usually the advance only has one
  71  * component.
  72  * <p>
  73  * The advance of a glyph is the distance from the glyph's origin to the
  74  * origin of the next glyph along the baseline, which is either vertical
  75  * or horizontal.  Note that, in a <code>GlyphVector</code>,
  76  * the distance from a glyph to its following glyph might not be the
  77  * glyph's advance, because of kerning or other positioning adjustments.
  78  * <p>
  79  * The bounds is the smallest rectangle that completely contains the
  80  * outline of the glyph.  The bounds rectangle is relative to the
  81  * glyph's origin.  The left-side bearing is the distance from the glyph
  82  * origin to the left of its bounds rectangle. If the left-side bearing is
  83  * negative, part of the glyph is drawn to the left of its origin.  The
  84  * right-side bearing is the distance from the right side of the bounds
  85  * rectangle to the next glyph origin (the origin plus the advance).  If
  86  * negative, part of the glyph is drawn to the right of the next glyph's
  87  * origin.  Note that the bounds does not necessarily enclose all the pixels
  88  * affected when rendering the glyph, because of rasterization and pixel
  89  * adjustment effects.
  90  * <p>
  91  * Although instances of <code>GlyphMetrics</code> can be directly
  92  * constructed, they are almost always obtained from a
  93  * <code>GlyphVector</code>.  Once constructed, <code>GlyphMetrics</code>
  94  * objects are immutable.
  95  * <p>
  96  * <strong>Example</strong>:<p>
  97  * Querying a <code>Font</code> for glyph information
  98  * <blockquote><pre>
  99  * Font font = ...;
 100  * int glyphIndex = ...;
 101  * GlyphMetrics metrics = GlyphVector.getGlyphMetrics(glyphIndex);
 102  * int isStandard = metrics.isStandard();
 103  * float glyphAdvance = metrics.getAdvance();
 104  * </pre></blockquote>
 105  * @see java.awt.Font
 106  * @see GlyphVector
 107  */
 108 
 109 public final class GlyphMetrics {
 110     /**
 111      * Indicates whether the metrics are for a horizontal or vertical baseline.
 112      */
 113     private boolean horizontal;
 114 
 115     /**
 116      * The x-component of the advance.
 117      */


 154      */
 155     public static final byte COMBINING = 2;
 156 
 157     /**
 158      * Indicates a glyph with no corresponding character in the
 159      * backing store.  The glyph is associated with the character
 160      * represented by the logically preceding non-component glyph.  This
 161      * is used for kashida justification or other visual modifications to
 162      * existing glyphs.  There is no caret position between this glyph
 163      * and the preceding glyph.
 164      */
 165     public static final byte COMPONENT = 3;
 166 
 167     /**
 168      * Indicates a glyph with no visual representation. It can
 169      * be added to the other code values to indicate an invisible glyph.
 170      */
 171     public static final byte WHITESPACE = 4;
 172 
 173     /**
 174      * Constructs a <code>GlyphMetrics</code> object.
 175      * @param advance the advance width of the glyph
 176      * @param bounds the black box bounds of the glyph
 177      * @param glyphType the type of the glyph
 178      */
 179     public GlyphMetrics(float advance, Rectangle2D bounds, byte glyphType) {
 180         this.horizontal = true;
 181         this.advanceX = advance;
 182         this.advanceY = 0;
 183         this.bounds = new Rectangle2D.Float();
 184         this.bounds.setRect(bounds);
 185         this.glyphType = glyphType;
 186     }
 187 
 188     /**
 189      * Constructs a <code>GlyphMetrics</code> object.
 190      * @param horizontal if true, metrics are for a horizontal baseline,
 191      *   otherwise they are for a vertical baseline
 192      * @param advanceX the X-component of the glyph's advance
 193      * @param advanceY the Y-component of the glyph's advance
 194      * @param bounds the visual bounds of the glyph
 195      * @param glyphType the type of the glyph
 196      * @since 1.4
 197      */
 198     public GlyphMetrics(boolean horizontal, float advanceX, float advanceY,
 199                         Rectangle2D bounds, byte glyphType) {
 200 
 201         this.horizontal = horizontal;
 202         this.advanceX = advanceX;
 203         this.advanceY = advanceY;
 204         this.bounds = new Rectangle2D.Float();
 205         this.bounds.setRect(bounds);
 206         this.glyphType = glyphType;
 207     }
 208 
 209     /**


 261      * This is the distance from the right (bottom) of the glyph bounds to
 262      * the advance. If the bounds of the glyph is to the right of (below)
 263      * the advance, the RSB is negative.
 264      * @return the right side bearing of the glyph.
 265      */
 266     public float getRSB() {
 267         return horizontal ?
 268             advanceX - bounds.x - bounds.width :
 269             advanceY - bounds.y - bounds.height;
 270     }
 271 
 272     /**
 273      * Returns the raw glyph type code.
 274      * @return the raw glyph type code.
 275      */
 276     public int getType() {
 277         return glyphType;
 278     }
 279 
 280     /**
 281      * Returns <code>true</code> if this is a standard glyph.
 282      * @return <code>true</code> if this is a standard glyph;
 283      *          <code>false</code> otherwise.
 284      */
 285     public boolean isStandard() {
 286         return (glyphType & 0x3) == STANDARD;
 287     }
 288 
 289     /**
 290      * Returns <code>true</code> if this is a ligature glyph.
 291      * @return <code>true</code> if this is a ligature glyph;
 292      *          <code>false</code> otherwise.
 293      */
 294     public boolean isLigature() {
 295         return (glyphType & 0x3) == LIGATURE;
 296     }
 297 
 298     /**
 299      * Returns <code>true</code> if this is a combining glyph.
 300      * @return <code>true</code> if this is a combining glyph;
 301      *          <code>false</code> otherwise.
 302      */
 303     public boolean isCombining() {
 304         return (glyphType & 0x3) == COMBINING;
 305     }
 306 
 307     /**
 308      * Returns <code>true</code> if this is a component glyph.
 309      * @return <code>true</code> if this is a component glyph;
 310      *          <code>false</code> otherwise.
 311      */
 312     public boolean isComponent() {
 313         return (glyphType & 0x3) == COMPONENT;
 314     }
 315 
 316     /**
 317      * Returns <code>true</code> if this is a whitespace glyph.
 318      * @return <code>true</code> if this is a whitespace glyph;
 319      *          <code>false</code> otherwise.
 320      */
 321     public boolean isWhitespace() {
 322         return (glyphType & 0x4) == WHITESPACE;
 323     }
 324 }


  26 /*
  27  * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
  28  * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
  29  *
  30  * The original version of this source code and documentation is
  31  * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
  32  * of IBM. These materials are provided under terms of a License
  33  * Agreement between Taligent and Sun. This technology is protected
  34  * by multiple US and International patents.
  35  *
  36  * This notice and attribution to Taligent may not be removed.
  37  * Taligent is a registered trademark of Taligent, Inc.
  38  *
  39  */
  40 
  41 package java.awt.font;
  42 
  43 import java.awt.geom.Rectangle2D;
  44 
  45 /**
  46  * The {@code GlyphMetrics} class represents information for a
  47  * single glyph.   A glyph is the visual representation of one or more
  48  * characters.  Many different glyphs can be used to represent a single
  49  * character or combination of characters.  {@code GlyphMetrics}
  50  * instances are produced by {@link java.awt.Font Font} and are applicable
  51  * to a specific glyph in a particular {@code Font}.
  52  * <p>
  53  * Glyphs are either STANDARD, LIGATURE, COMBINING, or COMPONENT.
  54  * <ul>
  55  * <li>STANDARD glyphs are commonly used to represent single characters.
  56  * <li>LIGATURE glyphs are used to represent sequences of characters.
  57  * <li>COMPONENT glyphs in a {@link GlyphVector} do not correspond to a
  58  * particular character in a text model. Instead, COMPONENT glyphs are
  59  * added for typographical reasons, such as Arabic justification.
  60  * <li>COMBINING glyphs embellish STANDARD or LIGATURE glyphs, such
  61  * as accent marks.  Carets do not appear before COMBINING glyphs.
  62  * </ul>
  63  * <p>
  64  * Other metrics available through {@code GlyphMetrics} are the
  65  * components of the advance, the visual bounds, and the left and right
  66  * side bearings.
  67  * <p>
  68  * Glyphs for a rotated font, or obtained from a {@code GlyphVector}
  69  * which has applied a rotation to the glyph, can have advances that
  70  * contain both X and Y components.  Usually the advance only has one
  71  * component.
  72  * <p>
  73  * The advance of a glyph is the distance from the glyph's origin to the
  74  * origin of the next glyph along the baseline, which is either vertical
  75  * or horizontal.  Note that, in a {@code GlyphVector},
  76  * the distance from a glyph to its following glyph might not be the
  77  * glyph's advance, because of kerning or other positioning adjustments.
  78  * <p>
  79  * The bounds is the smallest rectangle that completely contains the
  80  * outline of the glyph.  The bounds rectangle is relative to the
  81  * glyph's origin.  The left-side bearing is the distance from the glyph
  82  * origin to the left of its bounds rectangle. If the left-side bearing is
  83  * negative, part of the glyph is drawn to the left of its origin.  The
  84  * right-side bearing is the distance from the right side of the bounds
  85  * rectangle to the next glyph origin (the origin plus the advance).  If
  86  * negative, part of the glyph is drawn to the right of the next glyph's
  87  * origin.  Note that the bounds does not necessarily enclose all the pixels
  88  * affected when rendering the glyph, because of rasterization and pixel
  89  * adjustment effects.
  90  * <p>
  91  * Although instances of {@code GlyphMetrics} can be directly
  92  * constructed, they are almost always obtained from a
  93  * {@code GlyphVector}.  Once constructed, {@code GlyphMetrics}
  94  * objects are immutable.
  95  * <p>
  96  * <strong>Example</strong>:<p>
  97  * Querying a {@code Font} for glyph information
  98  * <blockquote><pre>
  99  * Font font = ...;
 100  * int glyphIndex = ...;
 101  * GlyphMetrics metrics = GlyphVector.getGlyphMetrics(glyphIndex);
 102  * int isStandard = metrics.isStandard();
 103  * float glyphAdvance = metrics.getAdvance();
 104  * </pre></blockquote>
 105  * @see java.awt.Font
 106  * @see GlyphVector
 107  */
 108 
 109 public final class GlyphMetrics {
 110     /**
 111      * Indicates whether the metrics are for a horizontal or vertical baseline.
 112      */
 113     private boolean horizontal;
 114 
 115     /**
 116      * The x-component of the advance.
 117      */


 154      */
 155     public static final byte COMBINING = 2;
 156 
 157     /**
 158      * Indicates a glyph with no corresponding character in the
 159      * backing store.  The glyph is associated with the character
 160      * represented by the logically preceding non-component glyph.  This
 161      * is used for kashida justification or other visual modifications to
 162      * existing glyphs.  There is no caret position between this glyph
 163      * and the preceding glyph.
 164      */
 165     public static final byte COMPONENT = 3;
 166 
 167     /**
 168      * Indicates a glyph with no visual representation. It can
 169      * be added to the other code values to indicate an invisible glyph.
 170      */
 171     public static final byte WHITESPACE = 4;
 172 
 173     /**
 174      * Constructs a {@code GlyphMetrics} object.
 175      * @param advance the advance width of the glyph
 176      * @param bounds the black box bounds of the glyph
 177      * @param glyphType the type of the glyph
 178      */
 179     public GlyphMetrics(float advance, Rectangle2D bounds, byte glyphType) {
 180         this.horizontal = true;
 181         this.advanceX = advance;
 182         this.advanceY = 0;
 183         this.bounds = new Rectangle2D.Float();
 184         this.bounds.setRect(bounds);
 185         this.glyphType = glyphType;
 186     }
 187 
 188     /**
 189      * Constructs a {@code GlyphMetrics} object.
 190      * @param horizontal if true, metrics are for a horizontal baseline,
 191      *   otherwise they are for a vertical baseline
 192      * @param advanceX the X-component of the glyph's advance
 193      * @param advanceY the Y-component of the glyph's advance
 194      * @param bounds the visual bounds of the glyph
 195      * @param glyphType the type of the glyph
 196      * @since 1.4
 197      */
 198     public GlyphMetrics(boolean horizontal, float advanceX, float advanceY,
 199                         Rectangle2D bounds, byte glyphType) {
 200 
 201         this.horizontal = horizontal;
 202         this.advanceX = advanceX;
 203         this.advanceY = advanceY;
 204         this.bounds = new Rectangle2D.Float();
 205         this.bounds.setRect(bounds);
 206         this.glyphType = glyphType;
 207     }
 208 
 209     /**


 261      * This is the distance from the right (bottom) of the glyph bounds to
 262      * the advance. If the bounds of the glyph is to the right of (below)
 263      * the advance, the RSB is negative.
 264      * @return the right side bearing of the glyph.
 265      */
 266     public float getRSB() {
 267         return horizontal ?
 268             advanceX - bounds.x - bounds.width :
 269             advanceY - bounds.y - bounds.height;
 270     }
 271 
 272     /**
 273      * Returns the raw glyph type code.
 274      * @return the raw glyph type code.
 275      */
 276     public int getType() {
 277         return glyphType;
 278     }
 279 
 280     /**
 281      * Returns {@code true} if this is a standard glyph.
 282      * @return {@code true} if this is a standard glyph;
 283      *          {@code false} otherwise.
 284      */
 285     public boolean isStandard() {
 286         return (glyphType & 0x3) == STANDARD;
 287     }
 288 
 289     /**
 290      * Returns {@code true} if this is a ligature glyph.
 291      * @return {@code true} if this is a ligature glyph;
 292      *          {@code false} otherwise.
 293      */
 294     public boolean isLigature() {
 295         return (glyphType & 0x3) == LIGATURE;
 296     }
 297 
 298     /**
 299      * Returns {@code true} if this is a combining glyph.
 300      * @return {@code true} if this is a combining glyph;
 301      *          {@code false} otherwise.
 302      */
 303     public boolean isCombining() {
 304         return (glyphType & 0x3) == COMBINING;
 305     }
 306 
 307     /**
 308      * Returns {@code true} if this is a component glyph.
 309      * @return {@code true} if this is a component glyph;
 310      *          {@code false} otherwise.
 311      */
 312     public boolean isComponent() {
 313         return (glyphType & 0x3) == COMPONENT;
 314     }
 315 
 316     /**
 317      * Returns {@code true} if this is a whitespace glyph.
 318      * @return {@code true} if this is a whitespace glyph;
 319      *          {@code false} otherwise.
 320      */
 321     public boolean isWhitespace() {
 322         return (glyphType & 0x4) == WHITESPACE;
 323     }
 324 }
< prev index next >