1. /*
  2. * @(#)GlyphVector.java 1.19 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. * @author Charlton Innovations, Inc.
  9. */
  10. package java.awt.font;
  11. import java.awt.Graphics2D;
  12. import java.awt.Font;
  13. import java.awt.Polygon; // remind - need a floating point version
  14. import java.awt.geom.Point2D;
  15. import java.awt.geom.Rectangle2D;
  16. import java.awt.geom.AffineTransform;
  17. import java.awt.Shape;
  18. import java.awt.font.GlyphMetrics;
  19. import java.awt.font.GlyphJustificationInfo;
  20. /**
  21. * A <code>GlyphVector</code> object is a collection of glyphs
  22. * containing geometric information for the placement of each glyph
  23. * in a transformed coordinate space which corresponds to the
  24. * device on which the <code>GlyphVector</code> is ultimately
  25. * displayed.
  26. * <p>
  27. * The <code>GlyphVector</code> does not attempt any interpretation of
  28. * the sequence of glyphs it contains. Relationships between adjacent
  29. * glyphs in sequence are solely used to determine the placement of
  30. * the glyphs in the visual coordinate space.
  31. * <p>
  32. * Instances of <code>GlyphVector</code> are created by a {@link Font}.
  33. * <p>
  34. * In a text processing application that can cache intermediate
  35. * representations of text, creation and subsequent caching of a
  36. * <code>GlyphVector</code> for use during rendering is the fastest
  37. * method to present the visual representation of characters to a user.
  38. * <p>
  39. * A <code>GlyphVector</code> is associated with exactly one
  40. * <code>Font</code>, and can provide data useful only in relation to
  41. * this <code>Font</code>. In addition, metrics obtained from a
  42. * <code>GlyphVector</code> are not generally geometrically scaleable
  43. * since the pixelization and spacing are dependent on grid-fitting
  44. * algorithms within a <code>Font</code>. To facilitate accurate
  45. * measurement of a <code>GlyphVector</code> and its component
  46. * glyphs, you must specify a scaling transform, anti-alias mode, and
  47. * fractional metrics mode when creating the <code>GlyphVector</code>.
  48. * These characteristics can be derived from the destination device.
  49. * <p>
  50. * For each glyph in the <code>GlyphVector</code>, you can obtain:
  51. * <ul>
  52. * <li>the position of the glyph
  53. * <li>the transform associated with the glyph
  54. * <li>the metrics of the glyph in the context of the
  55. * <code>GlyphVector</code>. The metrics of the glyph may be
  56. * different under different transforms, application specified
  57. * rendering hints, and the specific instance of the glyph within
  58. * the <code>GlyphVector</code>.
  59. * </ul>
  60. * <p>
  61. * Altering the data used to create the <code>GlyphVector</code> does not
  62. * alter the state of the <code>GlyphVector</code>.
  63. * <p>
  64. * Methods are provided to create new <code>GlyphVector</code>
  65. * objects which are the result of editing operations on the
  66. * <code>GlyphVector</code>, such as glyph insertion and deletion. These
  67. * methods are most appropriate for applications that are forming
  68. * combinations such as ligatures from existing glyphs or are breaking
  69. * such combinations into their component parts for visual presentation.
  70. * <p>
  71. * Methods are provided to create new <code>GlyphVector</code>
  72. * objects that are the result of specifying new positions for the glyphs
  73. * within the <code>GlyphVector</code>. These methods are most
  74. * appropriate for applications that are performing justification
  75. * operations for the presentation of the glyphs.
  76. * <p>
  77. * Methods are provided to return both the visual and logical bounds
  78. * of the entire <code>GlyphVector</code> or of individual glyphs within
  79. * the <code>GlyphVector</code>.
  80. * <p>
  81. * Methods are provided to return a {@link Shape} for the
  82. * <code>GlyphVector</code>, and for individual glyphs within the
  83. * <code>GlyphVector</code>.
  84. * @see Font
  85. * @see GlyphMetrics
  86. * @see TextLayout
  87. * @version 19 Mar 1998
  88. * @author Charlton Innovations, Inc.
  89. */
  90. public abstract class GlyphVector implements Cloneable {
  91. // methods associated with creation-time state
  92. /**
  93. * Returns the <code>Font</code> associated with this
  94. * <code>GlyphVector</code>.
  95. * @returns <code>Font</code> used to create this
  96. * <code>GlyphVector</code>.
  97. * @see Font
  98. */
  99. public abstract Font getFont();
  100. /**
  101. * Returns the {@link FontRenderContext} associated with this
  102. * <code>GlyphVector</code>.
  103. * @return <code>FontRenderContext</code> used to create this
  104. * <code>GlyphVector</code>.
  105. * @see FontRenderContext
  106. * @see Font
  107. */
  108. public abstract FontRenderContext getFontRenderContext();
  109. // methods associated with the GlyphVector as a whole
  110. /**
  111. * Assigns default positions to each glyph in this
  112. * <code>GlyphVector</code>. No shaping, reordering, or contextual
  113. * substitution is performed.
  114. */
  115. public abstract void performDefaultLayout();
  116. /**
  117. * Returns the number of glyphs in this <code>GlyphVector</code>.
  118. * This information is used to create arrays that are to be
  119. * filled with results of other information retrieval
  120. * operations.
  121. * @return number of glyphs in this <code>GlyphVector</code>.
  122. */
  123. public abstract int getNumGlyphs();
  124. /**
  125. * Returns the glyphcode of the specified glyph.
  126. * This return value is meaningless to anything other
  127. * than a <code>Font</code> object and can be used to ask the
  128. * <code>Font</code> object about the existence of ligatures and
  129. * other context sensitive information.
  130. * @param glyphIndex the index into this <code>GlyphVector</code>
  131. * that corresponds to the glyph from which to retrieve the
  132. * glyphcode.
  133. * @return the glyphcode of the glyph corresponding the the specified
  134. * <code>glyphIndex</code>.
  135. * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
  136. * is less than 0 or greater than or equal to the
  137. * number of glyphs in this <code>GlyphVector</code>
  138. */
  139. public abstract int getGlyphCode(int glyphIndex);
  140. /**
  141. * Returns an array of glyphcodes for the specified glyphs.
  142. * The contents of this return value are meaningless to anything other
  143. * than a <code>Font</code> and can be used to ask the
  144. * <code>Font</code> about the existence of ligatures and other
  145. * context sensitive information. This method is used
  146. * for convenience and performance when processing glyphcodes.
  147. * If no array is passed in, an array is created.
  148. * @param beginGlyphIndex the index into this
  149. * <code>GlyphVector</code> at which to start retrieving glyphcodes
  150. * for the corresponding glyphs
  151. * @param numEntries the offset from <code>beginGlyphIndex</code> at
  152. * which to stop retrieving glyphcodes
  153. * @param codeReturn the array that receives the glyphcodes and is
  154. * then returned
  155. * @return an array of glyphcodes for the specified glyphs.
  156. * @throws IllegalArgumentException if <code>numEntries</code> is
  157. * less than 0
  158. * @throws IndexOutOfBoundsException if <code>beginGlyphIndex</code>
  159. * is less than 0
  160. * @throws IndexOutOfBoundsException if the sum of
  161. * <code>beginGlyphIndex</code> and <code>numEntries</code> is
  162. * greater than the number of glyphs in this
  163. * <code>GlyphVector</code>
  164. */
  165. public abstract int[] getGlyphCodes(int beginGlyphIndex, int numEntries,
  166. int[] codeReturn);
  167. // methods associated with the GlyphVector as a whole,
  168. // after layout.
  169. /**
  170. * Returns the logical bounds of this <code>GlyphVector</code>.
  171. * This method is used when positioning this <code>GlyphVector</code>
  172. * in relation to visually adjacent <code>GlyphVector</code> objects.
  173. * @return a {@link Rectangle2D} that is the logical bounds of this
  174. * <code>GlyphVector</code>.
  175. */
  176. public abstract Rectangle2D getLogicalBounds();
  177. /**
  178. * Returns the visual bounds of this <code>GlyphVector</code>
  179. * The visual bounds is the tightest rectangle enclosing all
  180. * non-background pixels in the rendered representation of this
  181. * <code>GlyphVector</code>.
  182. * @return a <code>Rectangle2D</code> that is the tightest bounds
  183. * of this <code>GlyphVector</code>.
  184. */
  185. public abstract Rectangle2D getVisualBounds();
  186. /**
  187. * Returns a <code>Shape</code> whose interior corresponds to the
  188. * visual representation of this <code>GlyphVector</code>.
  189. * @return a <code>Shape</code> that is the outline of this
  190. * <code>GlyphVector</code>.
  191. */
  192. public abstract Shape getOutline();
  193. /**
  194. * Returns a <code>Shape</code> whose interior corresponds to the
  195. * visual representation of this <code>GlyphVector</code>, offset
  196. * to x, y.
  197. * @param x, y the coordinates of the location of the outline
  198. * <code>Shape</code>
  199. * @return a <code>Shape</code> that is the outline of this
  200. * <code>GlyphVector</code>, offset to the specified
  201. * coordinates.
  202. */
  203. public abstract Shape getOutline(float x, float y);
  204. /**
  205. * Returns a <code>Shape</code> whose interior corresponds to the
  206. * visual representation of the specified glyph
  207. * within this <code>GlyphVector</code>.
  208. * @param glyphIndex the index into this <code>GlyphVector</code>
  209. * @return a <code>Shape</code> that is the outline of the glyph
  210. * at the specified <code>glyphIndex</code> of this
  211. * <code>GlyphVector</code>.
  212. * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
  213. * is less than 0 or greater than or equal to the number
  214. * of glyphs in this <code>GlyphVector</code>
  215. */
  216. public abstract Shape getGlyphOutline(int glyphIndex);
  217. /**
  218. * Returns the position of the specified glyph within this
  219. * <code>GlyphVector</code>.
  220. * This position corresponds to the leading edge of the baseline for
  221. * the glyph.
  222. * If <code>glyphIndex</code> equals the number of of glyphs in
  223. * this <code>GlyphVector</code>, this method gets the position after
  224. * the last glyph and this position is used to define the advance of
  225. * the entire <code>GlyphVector</code>.
  226. * @param glyphIndex the index into this <code>GlyphVector</code>
  227. * @return a {@link Point2D} object that is the position of the glyph
  228. * at the specified <code>glyphIndex</code>.
  229. * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
  230. * is less than 0 or greater than the number of glyphs
  231. * in this <code>GlyphVector</code>
  232. */
  233. public abstract Point2D getGlyphPosition(int glyphIndex);
  234. /**
  235. * Sets the position of the specified glyph within this
  236. * <code>GlyphVector</code>.
  237. * This position corresponds to the leading edge of the baseline for
  238. * the glyph.
  239. * If <code>glyphIndex</code> equals the number of of glyphs in
  240. * this <code>GlyphVector</code>, this method sets the position after
  241. * the last glyph and this position is used to define the advance of
  242. * the entire <code>GlyphVector</code>.
  243. * @param glyphIndex the index into this <code>GlyphVector</code>
  244. * @param newPos the <code>Point2D</code> at which to position the
  245. * glyph at the specified <code>glyphIndex</code>
  246. * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
  247. * is less than 0 or greater than the number of glyphs
  248. * in this <code>GlyphVector</code>
  249. */
  250. public abstract void setGlyphPosition(int glyphIndex, Point2D newPos);
  251. /**
  252. * Gets the transform of the specified glyph within this
  253. * <code>GlyphVector</code>. The transform is relative to the
  254. * glyph position. If no special transform has been applied,
  255. * <code>null</code> can be returned. Such a transform would
  256. * be an identity transform.
  257. * @param glyphIndex the index into this <code>GlyphVector</code>
  258. * @return an {@link AffineTransform} that is the transform of
  259. * the glyph at the specified <code>glyphIndex</code>.
  260. * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
  261. * is less than 0 or greater than or equal to the number
  262. * of glyphs in this <code>GlyphVector</code>
  263. */
  264. public abstract AffineTransform getGlyphTransform(int glyphIndex);
  265. /**
  266. * Sets the transform of the specified glyph within this
  267. * <code>GlyphVector</code>. The transform is relative to the glyph
  268. * position. A <code>null</code> argument for <code>newTX</code>
  269. * indicates that no special transform is applied for the specified
  270. * glyph.
  271. * This method can be used to rotate, mirror, translate and scale the
  272. * glyph. Adding a transform can result in signifant performance changes.
  273. * @param glyphIndex the index into this <code>GlyphVector</code>
  274. * @param newTx the specified transform that the transform of the
  275. * glyph at the specified <code>glyphIndex</code> is set to
  276. * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
  277. * is less than 0 or greater than or equal to the number
  278. * of glyphs in this <code>GlyphVector</code>
  279. */
  280. public abstract void setGlyphTransform(int glyphIndex, AffineTransform newTX);
  281. /**
  282. * Returns an array of glyph positions for the specified glyphs.
  283. * The position of each glyph corresponds to the leading edge of the
  284. * baseline for that glyph. This method is used for convenience and
  285. * performance when processing glyph positions.
  286. * If no array is passed in, a new array is created.
  287. * Even numbered array entries beginning with position zero are the X
  288. * coordinates of the glyph numbered beginGlyphIndex + position/2.
  289. * Odd numbered array entries beginning with position one are the Y
  290. * coordinates of the glyph numbered beginGlyphIndex + (position-1)/2.
  291. * If <code>beginGlyphIndex</code> equals the number of of glyphs in
  292. * this <code>GlyphVector</code>, this method gets the position after
  293. * the last glyph and this position is used to define the advance of
  294. * the entire <code>GlyphVector</code>.
  295. * @param beginGlyphIndex the index at which to begin retrieving
  296. * glyph positions
  297. * @param numEntries the offset from <code>beginGlyphIndex</code> at
  298. * which to stop retrieving glyph positions
  299. * @param positionReturn the array that receives the glyph positions
  300. * and is then returned.
  301. * @return an array of glyph positions specified by
  302. * <code>beginGlyphIndex</code> and <code>numEntries</code>.
  303. * @throws IllegalArgumentException if <code>numEntries</code> is
  304. * less than 0
  305. * @throws IndexOutOfBoundsException if <code>beginGlyphIndex</code>
  306. * is less than 0
  307. * @throws IndexOutOfBoundsException if the sum of
  308. * <code>beginGlyphIndex</code> and <code>numEntries</code>
  309. * is greater than the number of glyphs in this
  310. * <code>GlyphVector</code> plus one
  311. */
  312. public abstract float[] getGlyphPositions(int beginGlyphIndex, int numEntries,
  313. float[] positionReturn);
  314. /**
  315. * Returns the logical bounds of the specified glyph within this
  316. * <code>GlyphVector</code>.
  317. * These logical bounds have a total of four edges, with two edges
  318. * parallel to the baseline under the glyph's transform and the other two
  319. * edges are shared with adjacent glyphs if they are present. This
  320. * method is useful for hit-testing of the specified glyph,
  321. * positioning of a caret at the leading or trailing edge of a glyph,
  322. * and for drawing a highlight region around the specified glyph.
  323. * @param glyphIndex the index into this <code>GlyphVector</code>
  324. * that corresponds to the glyph from which to retrieve its logical
  325. * bounds
  326. * @return a <code>Shape</code> that is the logical bounds of the
  327. * glyph at the specified <code>glyphIndex</code>.
  328. * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
  329. * is less than 0 or greater than or equal to the number
  330. * of glyphs in this <code>GlyphVector</code>
  331. * @see #getGlyphVisualBounds
  332. */
  333. public abstract Shape getGlyphLogicalBounds(int glyphIndex);
  334. /**
  335. * Returns the visual bounds of the specified glyph within the
  336. * <code>GlyphVector</code>.
  337. * These visual bounds have a total of four edges, representing the
  338. * tightest polygon enclosing non-background pixels in the rendered
  339. * representation of the glyph whose edges are parallel to the edges
  340. * of the logical bounds. Useful for hit-testing of the specified glyph.
  341. * @param glyphIndex the index into this <code>GlyphVector</code>
  342. * that corresponds to the glyph from which to retrieve its visual
  343. * bounds
  344. * @return a <code>Shape</code> that is the visual bounds of the
  345. * glyph at the specified <code>glyphIndex</code>.
  346. * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
  347. * is less than 0 or greater than or equal to the number
  348. * of glyphs in this <code>GlyphVector</code>
  349. * @see #getGlyphLogicalBounds
  350. */
  351. public abstract Shape getGlyphVisualBounds(int glyphIndex);
  352. /**
  353. * Returns the metrics of the glyph at the specified index into
  354. * this <code>GlyphVector</code>.
  355. * @param glyphIndex the index into this <code>GlyphVector</code>
  356. * that corresponds to the glyph from which to retrieve its metrics
  357. * @return a {@link GlyphMetrics} object that represents the
  358. * metrics of the glyph at the specified <code>glyphIndex</code>
  359. * into this <code>GlyphVector</code>.
  360. * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
  361. * is less than 0 or greater than or equal to the number
  362. * of glyphs in this <code>GlyphVector</code>
  363. */
  364. public abstract GlyphMetrics getGlyphMetrics(int glyphIndex);
  365. /**
  366. * Returns the justification information for the glyph at
  367. * the specified index into this <code>GlyphVector</code>.
  368. * @param glyphIndex the index into this <code>GlyphVector</code>
  369. * that corresponds to the glyph from which to retrieve its
  370. * justification properties
  371. * @return a {@link GlyphJustificationInfo} object that
  372. * represents the justification properties of the glyph at the
  373. * specified <code>glyphIndex</code> into this
  374. * <code>GlyphVector</code>.
  375. * @throws IndexOutOfBoundsException if <code>glyphIndex</code>
  376. * is less than 0 or greater than or equal to the number
  377. * of glyphs in this <code>GlyphVector</code>
  378. */
  379. public abstract GlyphJustificationInfo getGlyphJustificationInfo(int glyphIndex);
  380. // general utility methods
  381. /**
  382. * Tests if the specified <code>GlyphVector</code> exactly
  383. * equals this <code>GlyphVector</code>.
  384. * @param set the specified <code>GlyphVector</code> to test
  385. * @return <code>true</code> if the specified
  386. * <code>GlyphVector</code> equals this <code>GlyphVector</code>
  387. * <code>false</code> otherwise.
  388. */
  389. public abstract boolean equals(GlyphVector set);
  390. }