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