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