1. /*
  2. * @(#)TextHitInfo.java 1.30 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.lang.String;
  26. /**
  27. * The <code>TextHitInfo</code> class represents a character position in a
  28. * text model, and a <b>bias</b>, or "side," of the character. Biases are
  29. * either <EM>leading</EM> (the left edge, for a left-to-right character)
  30. * or <EM>trailing</EM> (the right edge, for a left-to-right character).
  31. * Instances of <code>TextHitInfo</code> are used to specify caret and
  32. * insertion positions within text.
  33. * <p>
  34. * For example, consider the text "abc". TextHitInfo.trailing(1)
  35. * corresponds to the right side of the 'b' in the text.
  36. * <p>
  37. * <code>TextHitInfo</code> is used primarily by {@link TextLayout} and
  38. * clients of <code>TextLayout</code>. Clients of <code>TextLayout</code>
  39. * query <code>TextHitInfo</code> instances for an insertion offset, where
  40. * new text is inserted into the text model. The insertion offset is equal
  41. * to the character position in the <code>TextHitInfo</code> if the bias
  42. * is leading, and one character after if the bias is trailing. The
  43. * insertion offset for TextHitInfo.trailing(1) is 2.
  44. * <p>
  45. * Sometimes it is convenient to construct a <code>TextHitInfo</code> with
  46. * the same insertion offset as an existing one, but on the opposite
  47. * character. The <code>getOtherHit</code> method constructs a new
  48. * <code>TextHitInfo</code> with the same insertion offset as an existing
  49. * one, with a hit on the character on the other side of the insertion offset.
  50. * Calling <code>getOtherHit</code> on trailing(1) would return leading(2).
  51. * In general, <code>getOtherHit</code> for trailing(n) returns
  52. * leading(n+1) and <code>getOtherHit</code> for leading(n)
  53. * returns trailing(n-1).
  54. * <p>
  55. * <strong>Example</strong>:<p>
  56. * Converting a graphical point to an insertion point within a text
  57. * model
  58. * <blockquote><pre>
  59. * TextLayout layout = ...;
  60. * Point2D.Float hitPoint = ...;
  61. * TextHitInfo hitInfo = layout.hitTestChar(hitPoint.x, hitPoint.y);
  62. * int insPoint = hitInfo.getInsertionIndex();
  63. * // insPoint is relative to layout; may need to adjust for use
  64. * // in a text model
  65. * </pre></blockquote>
  66. *
  67. * @see TextLayout
  68. */
  69. public final class TextHitInfo {
  70. private int charIndex;
  71. private boolean isLeadingEdge;
  72. /**
  73. * Constructs a new <code>TextHitInfo</code>.
  74. * @param charIndex the index of the character hit
  75. * @param isLeadingEdge <code>true</code> if the leading edge of the
  76. * character was hit
  77. */
  78. private TextHitInfo(int charIndex, boolean isLeadingEdge) {
  79. this.charIndex = charIndex;
  80. this.isLeadingEdge = isLeadingEdge;
  81. }
  82. /**
  83. * Returns the index of the character hit.
  84. * @return the index of the character hit.
  85. */
  86. public int getCharIndex() {
  87. return charIndex;
  88. }
  89. /**
  90. * Returns <code>true</code> if the leading edge of the character was
  91. * hit.
  92. * @return <code>true</code> if the leading edge of the character was
  93. * hit; <code>false</code> otherwise.
  94. */
  95. public boolean isLeadingEdge() {
  96. return isLeadingEdge;
  97. }
  98. /**
  99. * Returns the insertion index. This is the character index if
  100. * the leading edge of the character was hit, and one greater
  101. * than the character index if the trailing edge was hit.
  102. * @return the insertion index.
  103. */
  104. public int getInsertionIndex() {
  105. return isLeadingEdge ? charIndex : charIndex + 1;
  106. }
  107. /**
  108. * Returns the hash code.
  109. * @return the hash code of this <code>TextHitInfo</code>, which is
  110. * also the <code>charIndex</code> of this <code>TextHitInfo</code>.
  111. */
  112. public int hashCode() {
  113. return charIndex;
  114. }
  115. /**
  116. * Returns <code>true</code> if the specified <code>Object</code> is a
  117. * <code>TextHitInfo</code> and equals this <code>TextHitInfo</code>.
  118. * @param obj the <code>Object</code> to test for equality
  119. * @return <code>true</code> if the specified <code>Object</code>
  120. * equals this <code>TextHitInfo</code> <code>false</code> otherwise.
  121. */
  122. public boolean equals(Object obj) {
  123. return (obj instanceof TextHitInfo) && equals((TextHitInfo)obj);
  124. }
  125. /**
  126. * Returns <code>true</code> if the specified <code>TextHitInfo</code>
  127. * has the same <code>charIndex</code> and <code>isLeadingEdge</code>
  128. * as this <code>TextHitInfo</code>. This is not the same as having
  129. * the same insertion offset.
  130. * @param hitInfo a specified <code>TextHitInfo</code>
  131. * @return <code>true</code> if the specified <code>TextHitInfo</code>
  132. * has the same <code>charIndex</code> and <code>isLeadingEdge</code>
  133. * as this <code>TextHitInfo</code>.
  134. */
  135. public boolean equals(TextHitInfo hitInfo) {
  136. return hitInfo != null && charIndex == hitInfo.charIndex &&
  137. isLeadingEdge == hitInfo.isLeadingEdge;
  138. }
  139. /**
  140. * Returns a <code>String</code> representing the hit for debugging
  141. * use only.
  142. * @return a <code>String</code> representing this
  143. * <code>TextHitInfo</code>.
  144. */
  145. public String toString() {
  146. return "TextHitInfo[" + charIndex + (isLeadingEdge ? "L" : "T")+"]";
  147. }
  148. /**
  149. * Creates a <code>TextHitInfo</code> on the leading edge of the
  150. * character at the specified <code>charIndex</code>.
  151. * @param charIndex the index of the character hit
  152. * @return a <code>TextHitInfo</code> on the leading edge of the
  153. * character at the specified <code>charIndex</code>.
  154. */
  155. public static TextHitInfo leading(int charIndex) {
  156. return new TextHitInfo(charIndex, true);
  157. }
  158. /**
  159. * Creates a hit on the trailing edge of the character at
  160. * the specified <code>charIndex</code>.
  161. * @param charIndex the index of the character hit
  162. * @return a <code>TextHitInfo</code> on the trailing edge of the
  163. * character at the specified <code>charIndex</code>.
  164. */
  165. public static TextHitInfo trailing(int charIndex) {
  166. return new TextHitInfo(charIndex, false);
  167. }
  168. /**
  169. * Creates a <code>TextHitInfo</code> at the specified offset,
  170. * associated with the character before the offset.
  171. * @param offset an offset associated with the character before
  172. * the offset
  173. * @return a <code>TextHitInfo</code> at the specified offset.
  174. */
  175. public static TextHitInfo beforeOffset(int offset) {
  176. return new TextHitInfo(offset-1, false);
  177. }
  178. /**
  179. * Creates a <code>TextHitInfo</code> at the specified offset,
  180. * associated with the character after the offset.
  181. * @param offset an offset associated with the character after
  182. * the offset
  183. * @return a <code>TextHitInfo</code> at the specified offset.
  184. */
  185. public static TextHitInfo afterOffset(int offset) {
  186. return new TextHitInfo(offset, true);
  187. }
  188. /**
  189. * Creates a <code>TextHitInfo</code> on the other side of the
  190. * insertion point. This <code>TextHitInfo</code> remains unchanged.
  191. * @return a <code>TextHitInfo</code> on the other side of the
  192. * insertion point.
  193. */
  194. public TextHitInfo getOtherHit() {
  195. if (isLeadingEdge) {
  196. return trailing(charIndex - 1);
  197. } else {
  198. return leading(charIndex + 1);
  199. }
  200. }
  201. /**
  202. * Creates a <code>TextHitInfo</code> whose character index is offset
  203. * by <code>delta</code> from the <code>charIndex</code> of this
  204. * <code>TextHitInfo</code>. This <code>TextHitInfo</code> remains
  205. * unchanged.
  206. * @param delta the value to offset this <code>charIndex</code>
  207. * @return a <code>TextHitInfo</code> whose <code>charIndex</code> is
  208. * offset by <code>delta</code> from the <code>charIndex</code> of
  209. * this <code>TextHitInfo</code>.
  210. */
  211. public TextHitInfo getOffsetHit(int delta) {
  212. return new TextHitInfo(charIndex + delta, isLeadingEdge);
  213. }
  214. }