1. /*
  2. * @(#)GraphicAttribute.java 1.12 01/11/29
  3. *
  4. * Copyright 2002 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. */
  66. protected GraphicAttribute(int alignment) {
  67. if (alignment < BOTTOM_ALIGNMENT || alignment > HANGING_BASELINE) {
  68. throw new IllegalArgumentException("bad alignment");
  69. }
  70. fAlignment = alignment;
  71. }
  72. /**
  73. * Returns the ascent of this <code>GraphicAttribute</code>. A
  74. * graphic can be rendered above its ascent.
  75. * @return the ascent of this <code>GraphicAttribute</code>.
  76. * @see #getBounds()
  77. */
  78. public abstract float getAscent();
  79. /**
  80. * Returns the descent of this <code>GraphicAttribute</code>. A
  81. * graphic can be rendered below its descent.
  82. * @return the descent of this <code>GraphicAttribute</code>.
  83. * @see #getBounds()
  84. */
  85. public abstract float getDescent();
  86. /**
  87. * Returns the advance of this <code>GraphicAttribute</code>. The
  88. * <code>GraphicAttribute</code> object's advance is the distance
  89. * from the point at which the graphic is rendered and the point where
  90. * the next character or graphic is rendered. A graphic can be
  91. * rendered beyond its advance
  92. * @return the advance of this <code>GraphicAttribute</code>.
  93. * @see #getBounds()
  94. */
  95. public abstract float getAdvance();
  96. /**
  97. * Returns a {@link Rectangle2D} that encloses all of the
  98. * bits drawn by this <code>GraphicAttribute</code> relative to the
  99. * rendering position.
  100. * A graphic may be rendered beyond its origin, ascent, descent,
  101. * or advance; but if it is, this method's implementation must
  102. * indicate where the graphic is rendered.
  103. * Default bounds is the rectangle (0, -ascent, advance, ascent+descent).
  104. * @return a <code>Rectangle2D</code> that encloses all of the bits
  105. * rendered by this <code>GraphicAttribute</code>.
  106. */
  107. public Rectangle2D getBounds() {
  108. float ascent = getAscent();
  109. return new Rectangle2D.Float(0, -ascent,
  110. getAdvance(), ascent+getDescent());
  111. }
  112. /**
  113. * Renders this <code>GraphicAttribute</code> at the specified
  114. * location.
  115. * @param graphics the {@link Graphics2D} into which to render the
  116. * graphic
  117. * @param x, y the user-space coordinates where
  118. * the graphic is rendered
  119. */
  120. public abstract void draw(Graphics2D graphics, float x, float y);
  121. /**
  122. * Returns the alignment of this <code>GraphicAttribute</code>.
  123. * Alignment can be to a particular baseline, or to the absolute top
  124. * or bottom of a line.
  125. * @return the alignment of this <code>GraphicAttribute</code>.
  126. */
  127. public final int getAlignment() {
  128. return fAlignment;
  129. }
  130. /**
  131. * Returns the justification information for this
  132. * <code>GraphicAttribute</code>. Subclasses
  133. * can override this method to provide different justification
  134. * information.
  135. * @return a {@link GlyphJustificationInfo} object that contains the
  136. * justification information for this <code>GraphicAttribute</code>.
  137. */
  138. public GlyphJustificationInfo getJustificationInfo() {
  139. // should we cache this?
  140. float advance = getAdvance();
  141. return new GlyphJustificationInfo(
  142. advance, // weight
  143. false, // growAbsorb
  144. 2, // growPriority
  145. advance3, // growLeftLimit
  146. advance3, // growRightLimit
  147. false, // shrinkAbsorb
  148. 1, // shrinkPriority
  149. 0, // shrinkLeftLimit
  150. 0); // shrinkRightLimit
  151. }
  152. }