home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-03-20 | 9.0 KB | 279 lines |
- /*
- * @(#)GlyphMetrics.java 1.23 98/03/18
- *
- * Copyright 1997, 1998 by Sun Microsystems, Inc.,
- * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
- * All rights reserved.
- *
- * This software is the confidential and proprietary information
- * of Sun Microsystems, Inc. ("Confidential Information"). You
- * shall not disclose such Confidential Information and shall use
- * it only in accordance with the terms of the license agreement
- * you entered into with Sun.
- */
-
- /*
- * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
- * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
- *
- * The original version of this source code and documentation is
- * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
- * of IBM. These materials are provided under terms of a License
- * Agreement between Taligent and Sun. This technology is protected
- * by multiple US and International patents.
- *
- * This notice and attribution to Taligent may not be removed.
- * Taligent is a registered trademark of Taligent, Inc.
- *
- */
-
- package java.awt.font;
-
- import java.awt.geom.Rectangle2D;
-
- /**
- * GlyphMetrics represents infomation for a single glyph. GlyphMetrics
- * instances are produced by Font and are applicable to a specific glyph in a
- * particular Font.
- * <p>
- * Glyphs are either STANDARD, LIGATURE, COMBINING, COMPONENT, or FILLER.
- * They also may be marked as being either WHITESPACE or HANGING_PUNCTUATION.
- * STANDARD glyphs are commonly used to represent single characters.
- * LIGATURE glyphs are always the first glyph in a sequence of glyphs that
- * combine to represent a set of "combining characters," such as the 'fi' ligature
- * found in some fonts. A LIGATURE glyph in a GlyphSet is always followed by one
- * or more FILLER glyphs. The number of LIGATURE and FILLER glyphs is always
- * the same as the number of characters represented by the ligature. COMPONENT
- * glyphs in a GlyphSet do not correspond to a particular character in a text model.
- * Instead, COMPONENT glyphs are added for typographical reasons (such as Arabic
- * justification). COMBINING glyphs embellish STANDARD or LIGATURE glyphs, such
- * as accent marks. Carets do not appear before COMBINING glyphs.
- * <p>
- * Other metrics available through GlyphMetrics are the advance, bounds, and left
- * and right side bearings. The advance of a glyph is the distance from the glyph's
- * origin to the origin of the next glyph. Note that, in a glyphset, the distance
- * from a glyph to its following glyph may not be the glyph's advance. The bounds
- * is the smallest rectangle which completely contains the visible portion of the
- * glyph. The bounds rectangle is relative to the glyph's origin. The left-side
- * bearing is the distance from the glyph origin to the left of its bounds rectangle.
- * If the left-side bearing is negative, part of the glyph is drawn to the left of its
- * origin. The right-side bearing is the distance from the right side of the bounds
- * rectangle to the next glyph origin (the origin plus the advance). If negative, part
- * of the glyph is drawn to the right of the next glyph's origin.
- * <p>
- * Although instances of GlyphMetrics may be directly constructed, they are almost
- * always obtained from a Font. Once constructed, GlyphMetrics are immutable.
- * <p>
- * <strong>Example</strong>:<p>
- * Querying a Font for glyph information
- * <blockquote><pre>
- * Font font = ...;
- * int glyphCode = ...;
- * GlyphMetrics metrics = font.getGlyphMetrics(glyphCode);
- * int isStandard = metrics.isStandard();
- * float glyphAdvance = metrics.getAdvance();
- * </blockquote></pre>
- * @see Font
- * @see GlyphSet
- */
-
- public final class GlyphMetrics {
- /**
- * The advance (horizontal or vertical) of the associated glyph.
- */
- private float advance;
-
- /**
- * The bounds of the associated glyph.
- */
- private Rectangle2D bounds;
-
- /**
- * The left-side-bearing of the associated glyph.
- */
- private float lsb;
-
- /**
- * The right-side-bearing of the associated glyph.
- */
- private float rsb;
-
- /**
- * Additional information about the glyph encoded as a byte.
- */
- private byte glyphType;
-
- /**
- * This code indicates a glyph that represents a single standard
- * character.
- */
- public static final byte STANDARD = 0;
-
- /**
- * This code indicates a glyph that represents multiple characters
- * as a ligature, for example 'fi' or 'ffi'. It is followed by
- * filler glyphs for the remaining characters. Filler and combining
- * glyphs can be intermixed to control positioning of accent marks
- * on the logically preceeding ligature.
- */
- public static final byte LIGATURE = 1;
-
- /**
- * This code indicates a glyph that represents a combining character,
- * such as an umlaut. There is no caret position between this glyph
- * and the preceeding glyph.
- */
- public static final byte COMBINING = 2;
-
- /**
- * This code indicates a glyph with no corresponding character in the
- * backing store. The glyph is associated with the character
- * represented by the logicaly preceeding non-component glyph. This
- * is used for kashida justification or other visual modifications to
- * existing glyphs. There is no caret position between this glyph
- * and the preceeding glyph.
- */
- public static final byte COMPONENT = 3;
-
- /**
- * This code indicates a glyph used as a filler for a ligature. It
- * has a width that is used to control caret positions within the
- * ligature when carets are allowed in ligatures. It replaces the
- * succeeding logical characters that were used to create the
- * ligature.
- */
- public static final byte FILLER = 4;
-
- /**
- * This value is added to the base code to indicate hanging
- * punctuation. This information is used in justification.
- */
- public static final byte HANGING_PUNCTUATION = 8;
-
- /**
- * This value is added to the base code to indicate whitespace.
- * This information is used in justification.
- */
- public static final byte WHITESPACE = 16;
-
- /**
- * Construct a GlyphMetrics.
- *
- * @param advance the advance width (height) of the glyph
- * @param bounds the black box bounds of the glyph.
- * @param glyphType the type of the glyph
- */
- public GlyphMetrics(float advance, Rectangle2D bounds, byte glyphType) {
- this.advance = advance;
- this.bounds = new Rectangle2D.Float();
- this.bounds.setRect(bounds);
- this.glyphType = glyphType;
-
- boolean isVertical = false;
- if (isVertical) {
- this.lsb = (float)bounds.getY();
- this.rsb = advance - (this.lsb + (float)bounds.getHeight());
- } else {
- this.lsb = (float)bounds.getX();
- this.rsb = advance - (this.lsb + (float)bounds.getWidth());
- }
- }
-
- /**
- * Return the advance width (height) of the glyph.
- */
- public float getAdvance() {
- return advance;
- }
-
- /**
- * Return the black box bounds of the glyph.
- *
- * The returned bounds must not be modified by the caller.
- */
- public Rectangle2D getBounds2D() {
- Rectangle2D b = new Rectangle2D.Float();
- b.setRect(bounds);
- return b;
- }
-
- /**
- * Return the left (top) side bearing of the glyph.
- *
- * This is the distance from 0, 0 to the left (top) of the glyph bounds.
- * If the bounds of the glyph is to the left of (above) the origin, the
- * LSB is negative.
- */
- public float getLSB() {
- return lsb;
- }
-
- /**
- * Return the right (bottom) side bearing of the glyph.
- *
- * This is the distance from the right (bottom) of the glyph bounds to
- * the advance. If the bounds of the glyph is to the right of (below)
- * the advance, the RSB is negative.
- */
- public float getRSB() {
- return rsb;
- }
-
- /**
- * Return the raw glyph type code.
- */
- public byte getType() {
- return glyphType;
- }
-
- /**
- * Return true if this is a standard glyph.
- */
- public boolean isStandard() {
- return (glyphType & 0x7) == STANDARD;
- }
-
- /**
- * Return true if this is a ligature glyph.
- */
- public boolean isLigature() {
- return (glyphType & 0x7) == LIGATURE;
- }
-
- /**
- * Return true if this is a combining glyph.
- */
- public boolean isCombining() {
- return (glyphType & 0x7) == COMBINING;
- }
-
- /**
- * Return true if this is a component glyph.
- */
- public boolean isComponent() {
- return (glyphType & 0x7) == COMPONENT;
- }
-
- /**
- * Return true if this is a component glyph.
- */
- public boolean isFiller() {
- return (glyphType & 0x7) == FILLER;
- }
-
- /**
- * Return true if this is a whitespace glyph.
- */
- public boolean isWhitespace() {
- return (glyphType & WHITESPACE) != 0;
- }
-
- /**
- * Return true if this is a hanging punctuation glyph.
- */
- public boolean isHangingPunctuation() {
- return (glyphType & HANGING_PUNCTUATION) != 0;
- }
- }
-
-