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 / GraphicAttribute.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  4.6 KB  |  134 lines

  1. /*
  2.  * @(#)GraphicAttribute.java    1.5 98/03/18
  3.  *
  4.  * Copyright 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. import java.awt.Graphics2D;
  34. import java.awt.Font;
  35.  
  36. /**
  37.  * This is the base class for the attribute which indicates a
  38.  * graphic embedded in text.  Clients can subclass this class
  39.  * to implement their own graphics.  Clients wishing to embed
  40.  * Shapes and Images in text need not subclass this class;
  41.  * they can use the ShapeGraphicAttribute
  42.  * and ImageGraphicAttribute classes instead.
  43.  *
  44.  * Subclasses should ensure that their subclasses are immutable
  45.  * (that is, that the class does not change once it has been
  46.  * constructed).  Mutating a GraphicAttribute which is used in
  47.  * a TextLayout results in undefined behavior from the TextLayout.
  48.  */
  49.  
  50. public abstract class GraphicAttribute {
  51.  
  52.     public static final int TOP_ALIGNMENT = -1;
  53.     public static final int BOTTOM_ALIGNMENT = -2;
  54.     public static final int ROMAN_BASELINE = Font.ROMAN_BASELINE;
  55.     public static final int CENTER_BASELINE = Font.CENTER_BASELINE;
  56.     public static final int HANGING_BASELINE = Font.HANGING_BASELINE;
  57.  
  58.     private /*final*/ int fAlignment;
  59.  
  60.     protected GraphicAttribute(int alignment) {
  61.  
  62.         fAlignment = alignment;
  63.     }
  64.  
  65.     /**
  66.      * Return the ascent of this graphic.  A graphic can draw above its ascent
  67.      * (see getBounds).
  68.      */
  69.     public abstract float getAscent();
  70.  
  71.     /**
  72.      * Return the descent of this graphic.  A graphic can draw below its descent
  73.      * (see getBounds).
  74.      */
  75.     public abstract float getDescent();
  76.  
  77.     /**
  78.      * Return the advance of this graphic.  A graphic can draw beyond its advance
  79.      * (see getBounds).
  80.      */
  81.     public abstract float getAdvance();
  82.  
  83.     /**
  84.      * The rectangle returned from this method should enclose all of the
  85.      * bits drawn by this graphic (relative to the drawing position, of
  86.      * course).  A graphic may draw beyond its ascent, descent, or advance;
  87.      * but if it does, this method's implementation should indicate where
  88.      * the graphic draws.
  89.      * Default bounds is the rectangle (0, -ascent, advance, ascent+descent).
  90.      */
  91.     // is this default worth it?
  92.     public Rectangle2D getBounds() {
  93.         float ascent = getAscent();
  94.         return new Rectangle2D.Float(0, -ascent,
  95.                                         getAdvance(), ascent+getDescent());
  96.     }
  97.  
  98.     /**
  99.      * Draw the graphic at the given location.
  100.      */
  101.     public abstract void draw(Graphics2D graphics, float x, float y);
  102.  
  103.     /**
  104.      * Return the alignment of this graphic.  Alignment can be to a particular
  105.      * baseline, or to the absolute top or bottom of a TextLayout.
  106.      */
  107.     // Should we let subclasses override this?
  108.     public final int getAlignment() {
  109.  
  110.         return fAlignment;
  111.     }
  112.  
  113.     /**
  114.      * Return the justification information for this graphic.  Subclasses
  115.      * can override to provide different justification information.
  116.      */
  117.     public GlyphJustificationInfo getJustificationInfo() {
  118.  
  119.         // should we cache this?
  120.         float advance = getAdvance();
  121.  
  122.         return new GlyphJustificationInfo(
  123.                                      advance,   // weight
  124.                                      false,     // growAbsorb
  125.                                      2,         // growPriority
  126.                                      advance/3, // growLeftLimit
  127.                                      advance/3, // growRightLimit
  128.                                      false,     // shrinkAbsorb
  129.                                      1,         // shrinkPriority
  130.                                      0,         // shrinkLeftLimit
  131.                                      0);        // shrinkRightLimit
  132.     }
  133. }
  134.