1. /*
  2. * @(#)GlyphMetrics.java 1.32 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. /**
  24. * The <code>GlyphMetrics</code> class represents infomation for a
  25. * single glyph. A glyph is the visual representation of one or more
  26. * characters. Many different glyphs can be used to represent a single
  27. * character or combination of characters. <code>GlyphMetrics</code>
  28. * instances are produced by {@link java.awt.Font Font} and are applicable
  29. * to a specific glyph in a particular <code>Font</code>.
  30. * <p>
  31. * Glyphs are either STANDARD, LIGATURE, COMBINING, or COMPONENT.
  32. * <ul>
  33. * <li>STANDARD glyphs are commonly used to represent single characters.
  34. * <li>LIGATURE glyphs are used to represent sequences of characters.
  35. * <li>COMPONENT glyphs in a {@link GlyphVector} do not correspond to a
  36. * particular character in a text model. Instead, COMPONENT glyphs are
  37. * added for typographical reasons, such as Arabic justification.
  38. * <li>COMBINING glyphs embellish STANDARD or LIGATURE glyphs, such
  39. * as accent marks. Carets do not appear before COMBINING glyphs.
  40. * </ul>
  41. * <p>
  42. * Other metrics available through <code>GlyphMetrics</code> are the
  43. * advance, bounds, and left and right side bearings.
  44. * <p>
  45. * The advance of a glyph is the distance from the glyph's origin to the
  46. * origin of the next glyph. Note that, in a <code>GlyphVector</code>,
  47. * the distance from a glyph to its following glyph might not be the
  48. * glyph's advance.
  49. * <p>
  50. * The bounds is the smallest rectangle that completely contains the
  51. * visible portion of the glyph. The bounds rectangle is relative to the
  52. * glyph's origin. The left-side bearing is the distance from the glyph
  53. * origin to the left of its bounds rectangle. If the left-side bearing is
  54. * negative, part of the glyph is drawn to the left of its origin. The
  55. * right-side bearing is the distance from the right side of the bounds
  56. * rectangle to the next glyph origin (the origin plus the advance). If
  57. * negative, part of the glyph is drawn to the right of the next glyph's
  58. * origin.
  59. * <p>
  60. * Although instances of <code>GlyphMetrics</code> can be directly
  61. * constructed, they are almost always obtained from a
  62. * <code>GlyphVector</code>. Once constructed, <code>GlyphMetrics</code>
  63. * objects are immutable.
  64. * <p>
  65. * <strong>Example</strong>:<p>
  66. * Querying a <code>Font</code> for glyph information
  67. * <blockquote><pre>
  68. * Font font = ...;
  69. * int glyphIndex = ...;
  70. * GlyphMetrics metrics = GlyphVector.getGlyphMetrics(glyphIndex);
  71. * int isStandard = metrics.isStandard();
  72. * float glyphAdvance = metrics.getAdvance();
  73. * </blockquote></pre>
  74. * @see java.awt.Font
  75. * @see GlyphVector
  76. */
  77. public final class GlyphMetrics {
  78. /**
  79. * The advance (horizontal or vertical) of the associated glyph.
  80. */
  81. // please please don't change the name of this field! Some JNI code uses it
  82. private float advance;
  83. /**
  84. * The bounds of the associated glyph.
  85. */
  86. // please please don't change the name of this field! Some JNI code uses it
  87. private Rectangle2D.Float bounds;
  88. /**
  89. * Additional information about the glyph encoded as a byte.
  90. */
  91. // please please don't change the name of this field! Some JNI code uses it
  92. private byte glyphType;
  93. /**
  94. * Indicates a glyph that represents a single standard
  95. * character.
  96. */
  97. public static final byte STANDARD = 0;
  98. /**
  99. * Indicates a glyph that represents multiple characters
  100. * as a ligature, for example 'fi' or 'ffi'. It is followed by
  101. * filler glyphs for the remaining characters. Filler and combining
  102. * glyphs can be intermixed to control positioning of accent marks
  103. * on the logically preceeding ligature.
  104. */
  105. public static final byte LIGATURE = 1;
  106. /**
  107. * Indicates a glyph that represents a combining character,
  108. * such as an umlaut. There is no caret position between this glyph
  109. * and the preceeding glyph.
  110. */
  111. public static final byte COMBINING = 2;
  112. /**
  113. * Indicates a glyph with no corresponding character in the
  114. * backing store. The glyph is associated with the character
  115. * represented by the logicaly preceeding non-component glyph. This
  116. * is used for kashida justification or other visual modifications to
  117. * existing glyphs. There is no caret position between this glyph
  118. * and the preceeding glyph.
  119. */
  120. public static final byte COMPONENT = 3;
  121. /**
  122. * Indicates a glyph with no visual representation. It can
  123. * be added to the other code values to indicate an invisible glyph.
  124. */
  125. public static final byte WHITESPACE = 4;
  126. /**
  127. * Constructs a <code>GlyphMetrics</code> object.
  128. * @param advance the advance width or height of the glyph
  129. * @param bounds the black box bounds of the glyph
  130. * @param glyphType the type of the glyph
  131. */
  132. public GlyphMetrics(float advance, Rectangle2D bounds, byte glyphType) {
  133. this.advance = advance;
  134. this.bounds = new Rectangle2D.Float();
  135. this.bounds.setRect(bounds);
  136. this.glyphType = glyphType;
  137. }
  138. /**
  139. * Returns the advance width or height of the glyph.
  140. * @return the advance of the glyph.
  141. */
  142. public float getAdvance() {
  143. return advance;
  144. }
  145. /**
  146. * Returns the black box bounds of the glyph.
  147. * @return a {@link Rectangle2D} that is the bounds of the glyph.
  148. */
  149. public Rectangle2D getBounds2D() {
  150. return new Rectangle2D.Float(bounds.x, bounds.y, bounds.width, bounds.height);
  151. }
  152. /**
  153. * Returns the left (top) side bearing of the glyph.
  154. * <p>
  155. * This is the distance from 0, 0 to the left (top) of the glyph
  156. * bounds. If the bounds of the glyph is to the left of (above) the
  157. * origin, the LSB is negative.
  158. * @return the left side bearing of the glyph.
  159. */
  160. public float getLSB() {
  161. return bounds.x;
  162. }
  163. /**
  164. * Returns the right (bottom) side bearing of the glyph.
  165. * <p>
  166. * This is the distance from the right (bottom) of the glyph bounds to
  167. * the advance. If the bounds of the glyph is to the right of (below)
  168. * the advance, the RSB is negative.
  169. * @return the right side bearing of the glyph.
  170. */
  171. public float getRSB() {
  172. return advance - bounds.x - bounds.width;
  173. }
  174. /**
  175. * Returns the raw glyph type code.
  176. * @return the raw glyph type code.
  177. */
  178. public int getType() {
  179. return glyphType;
  180. }
  181. /**
  182. * Returns <code>true</code> if this is a standard glyph.
  183. * @return <code>true</code> if this is a standard glyph;
  184. * <code>false</code> otherwise.
  185. */
  186. public boolean isStandard() {
  187. return (glyphType & 0x3) == STANDARD;
  188. }
  189. /**
  190. * Returns <code>true</code> if this is a ligature glyph.
  191. * @return <code>true</code> if this is a ligature glyph;
  192. * <code>false</code> otherwise.
  193. */
  194. public boolean isLigature() {
  195. return (glyphType & 0x3) == LIGATURE;
  196. }
  197. /**
  198. * Returns <code>true</code> if this is a combining glyph.
  199. * @return <code>true</code> if this is a combining glyph;
  200. * <code>false</code> otherwise.
  201. */
  202. public boolean isCombining() {
  203. return (glyphType & 0x3) == COMBINING;
  204. }
  205. /**
  206. * Returns <code>true</code> if this is a component glyph.
  207. * @return <code>true</code> if this is a component glyph;
  208. * <code>false</code> otherwise.
  209. */
  210. public boolean isComponent() {
  211. return (glyphType & 0x3) == COMPONENT;
  212. }
  213. /**
  214. * Returns <code>true</code> if this is a whitespace glyph.
  215. * @return <code>true</code> if this is a whitespace glyph;
  216. * <code>false</code> otherwise.
  217. */
  218. public boolean isWhitespace() {
  219. return (glyphType & 0x4) == WHITESPACE;
  220. }
  221. }