1. /*
  2. * @(#)GraphicAttribute.java 1.17 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /*
  8. * (C) Copyright Taligent, Inc. 1996 - 1997, All Rights Reserved
  9. * (C) Copyright IBM Corp. 1996 - 1998, All Rights Reserved
  10. *
  11. * The original version of this source code and documentation is
  12. * copyrighted and owned by Taligent, Inc., a wholly-owned subsidiary
  13. * of IBM. These materials are provided under terms of a License
  14. * Agreement between Taligent and Sun. This technology is protected
  15. * by multiple US and International patents.
  16. *
  17. * This notice and attribution to Taligent may not be removed.
  18. * Taligent is a registered trademark of Taligent, Inc.
  19. *
  20. */
  21. package java.awt.font;
  22. import java.awt.geom.Rectangle2D;
  23. import java.awt.Graphics2D;
  24. import java.awt.Font;
  25. /**
  26. * This class is used with the CHAR_REPLACEMENT attribute.
  27. * <p>
  28. * The <code>GraphicAttribute</code> class represents a graphic embedded
  29. * in text. Clients subclass this class to implement their own char
  30. * replacement graphics. Clients wishing to embed shapes and images in
  31. * text need not subclass this class. Instead, clients can use the
  32. * {@link ShapeGraphicAttribute} and {@link ImageGraphicAttribute}
  33. * classes.
  34. * <p>
  35. * Subclasses must ensure that their objects are immutable once they
  36. * are constructed. Mutating a <code>GraphicAttribute</code> that
  37. * is used in a {@link TextLayout} results in undefined behavior from the
  38. * <code>TextLayout</code>.
  39. */
  40. public abstract class GraphicAttribute {
  41. private int fAlignment;
  42. /**
  43. * Aligns top of graphic to top of line.
  44. */
  45. public static final int TOP_ALIGNMENT = -1;
  46. /**
  47. * Aligns bottom of graphic to bottom of line.
  48. */
  49. public static final int BOTTOM_ALIGNMENT = -2;
  50. /**
  51. * Aligns origin of graphic to roman baseline of line.
  52. */
  53. public static final int ROMAN_BASELINE = Font.ROMAN_BASELINE;
  54. /**
  55. * Aligns origin of graphic to center baseline of line.
  56. */
  57. public static final int CENTER_BASELINE = Font.CENTER_BASELINE;
  58. /**
  59. * Aligns origin of graphic to hanging baseline of line.
  60. */
  61. public static final int HANGING_BASELINE = Font.HANGING_BASELINE;
  62. /**
  63. * Constructs a <code>GraphicAttribute</code>.
  64. * Subclasses use this to define the alignment of the graphic.
  65. * @param alignment an int representing one of the
  66. * <code>GraphicAttribute</code> alignment fields
  67. */
  68. protected GraphicAttribute(int alignment) {
  69. if (alignment < BOTTOM_ALIGNMENT || alignment > HANGING_BASELINE) {
  70. throw new IllegalArgumentException("bad alignment");
  71. }
  72. fAlignment = alignment;
  73. }
  74. /**
  75. * Returns the ascent of this <code>GraphicAttribute</code>. A
  76. * graphic can be rendered above its ascent.
  77. * @return the ascent of this <code>GraphicAttribute</code>.
  78. * @see #getBounds()
  79. */
  80. public abstract float getAscent();
  81. /**
  82. * Returns the descent of this <code>GraphicAttribute</code>. A
  83. * graphic can be rendered below its descent.
  84. * @return the descent of this <code>GraphicAttribute</code>.
  85. * @see #getBounds()
  86. */
  87. public abstract float getDescent();
  88. /**
  89. * Returns the advance of this <code>GraphicAttribute</code>. The
  90. * <code>GraphicAttribute</code> object's advance is the distance
  91. * from the point at which the graphic is rendered and the point where
  92. * the next character or graphic is rendered. A graphic can be
  93. * rendered beyond its advance
  94. * @return the advance of this <code>GraphicAttribute</code>.
  95. * @see #getBounds()
  96. */
  97. public abstract float getAdvance();
  98. /**
  99. * Returns a {@link Rectangle2D} that encloses all of the
  100. * bits drawn by this <code>GraphicAttribute</code> relative to the
  101. * rendering position.
  102. * A graphic may be rendered beyond its origin, ascent, descent,
  103. * or advance; but if it is, this method's implementation must
  104. * indicate where the graphic is rendered.
  105. * Default bounds is the rectangle (0, -ascent, advance, ascent+descent).
  106. * @return a <code>Rectangle2D</code> that encloses all of the bits
  107. * rendered by this <code>GraphicAttribute</code>.
  108. */
  109. public Rectangle2D getBounds() {
  110. float ascent = getAscent();
  111. return new Rectangle2D.Float(0, -ascent,
  112. getAdvance(), ascent+getDescent());
  113. }
  114. /**
  115. * Renders this <code>GraphicAttribute</code> at the specified
  116. * location.
  117. * @param graphics the {@link Graphics2D} into which to render the
  118. * graphic
  119. * @param x, y the user-space coordinates where
  120. * the graphic is rendered
  121. */
  122. public abstract void draw(Graphics2D graphics, float x, float y);
  123. /**
  124. * Returns the alignment of this <code>GraphicAttribute</code>.
  125. * Alignment can be to a particular baseline, or to the absolute top
  126. * or bottom of a line.
  127. * @return the alignment of this <code>GraphicAttribute</code>.
  128. */
  129. public final int getAlignment() {
  130. return fAlignment;
  131. }
  132. /**
  133. * Returns the justification information for this
  134. * <code>GraphicAttribute</code>. Subclasses
  135. * can override this method to provide different justification
  136. * information.
  137. * @return a {@link GlyphJustificationInfo} object that contains the
  138. * justification information for this <code>GraphicAttribute</code>.
  139. */
  140. public GlyphJustificationInfo getJustificationInfo() {
  141. // should we cache this?
  142. float advance = getAdvance();
  143. return new GlyphJustificationInfo(
  144. advance, // weight
  145. false, // growAbsorb
  146. 2, // growPriority
  147. advance3, // growLeftLimit
  148. advance3, // growRightLimit
  149. false, // shrinkAbsorb
  150. 1, // shrinkPriority
  151. 0, // shrinkLeftLimit
  152. 0); // shrinkRightLimit
  153. }
  154. }